0463-Bomb-实现进入下一关
环境
- Time 2024-03-23
- Zig 0.12.0-dev.3152+90c1a2c41
- WSL-Ubuntu 22.04.3 LTS
- raylib 5.0
前言
说明
参考资料:
- 《游戏开发:世嘉新人培训教材》
- 图片素材:https://www.ituring.com.cn/book/1742
目标
如果地图上所有的砖块都已经清除,则进入下一关。
同时,由于多线程的情况下,内存释放了,线程还在访问,所以把 AI 的多线程控制转为单线程。
hasClear
检查是否清除。
pub fn hasClear(self: Map) bool {
for (self.world.data) |value| {
if (value.contains(.brick)) return false;
} else return true;
}
ai.zig
去掉了多线程的控制。
const std = @import("std");
const engine = @import("../engine.zig");
const World = @import("world.zig").World;
const Player = @import("player.zig").Player;
const enemySpeed = 500;
pub fn control(world: World) void {
for (world.players) |*enemy| {
if (enemy.type == .enemy and enemy.alive)
controlEnemy(world, enemy);
}
}
fn controlEnemy(world: World, enemy: *Player) void {
const direction = enemy.direction orelse return;
if (direction == .north) {
var e = enemy.*;
e.y -|= enemySpeed;
if (world.isCollisionY(e, e.getCell().x, e.getCell().y -| 1)) {
enemy.direction = @enumFromInt(engine.random(4));
} else {
enemy.y -|= enemySpeed;
}
}
if (direction == .south) {
var e = enemy.*;
e.y -|= enemySpeed;
if (world.isCollisionY(e, e.getCell().x, e.getCell().y + 1)) {
enemy.direction = @enumFromInt(engine.random(4));
} else {
enemy.y += enemySpeed;
}
}
if (direction == .west) {
var e = enemy.*;
e.x -|= enemySpeed;
if (world.isCollisionY(e, e.getCell().x -| 1, e.getCell().y)) {
enemy.direction = @enumFromInt(engine.random(4));
} else {
enemy.x -|= enemySpeed;
}
}
if (direction == .east) {
var e = enemy.*;
e.x += enemySpeed;
if (world.isCollisionY(e, e.getCell().x + 1, e.getCell().y)) {
enemy.direction = @enumFromInt(engine.random(4));
} else {
enemy.x += enemySpeed;
}
}
}
效果

总结
增加了进入下一关的支持。

浙公网安备 33010602011771号