add box pushing

This commit is contained in:
Rokas Puzonas 2026-09-20 21:16:57 +03:00
parent 0ac0cd0c37
commit f7e6c25a82

View File

@ -136,12 +136,17 @@ const World = struct {
return null; return null;
} }
fn moveEntity(self: *World, id: Entity.Id, x: i32, y: i32) bool { fn getEntityAt(self: *World, x: i32, y: i32) Entity.Id {
const tile = self.getTile(x, y) orelse return .nil;
return tile.entity;
}
fn setEntityPosition(self: *World, id: Entity.Id, x: i32, y: i32) bool {
const e = self.entities.get(id) orelse return false; const e = self.entities.get(id) orelse return false;
const destination_tile = self.getTile(x, y) orelse return false; const destination_tile = self.getTile(x, y) orelse return false;
if (self.entities.exists(destination_tile.entity)) { if (self.entities.exists(destination_tile.entity)) {
// Cna't overwrite entity // Cant't overwrite entity
return false; return false;
} }
destination_tile.entity = id; destination_tile.entity = id;
@ -156,16 +161,25 @@ const World = struct {
return true; return true;
} }
fn movePlayer(self: *World, id: Entity.Id, dir_x: i32, dir_y: i32) bool { fn moveEntity(self: *World, id: Entity.Id, dx: i32, dy: i32) bool {
const e = self.entities.get(id) orelse return false; const e = self.entities.get(id) orelse return false;
const next_x = e.pos_x+dir_x; const next_x = e.pos_x+dx;
const next_y = e.pos_y+dir_y; const next_y = e.pos_y+dy;
if (!self.moveEntity(id, next_x, next_y)) {
return false; const destination_tile = self.getTile(next_x, next_y) orelse return false;
if (e.type == .player or e.type == .box) {
if (self.entities.get(destination_tile.entity)) |destination_entity| {
if (destination_entity.type == .box) {
if (!self.moveEntity(destination_tile.entity, dx, dy)) {
return false;
}
}
}
} }
return true; return self.setEntityPosition(id, next_x, next_y);
} }
}; };
@ -289,7 +303,7 @@ pub fn init(self: *App, plt: Platform.Init) !?u8 {
if (tilemap.getTile(walls, tilesets, x, y)) |tiled_tile| { if (tilemap.getTile(walls, tilesets, x, y)) |tiled_tile| {
const pos = tiled_tile.getPosition(); const pos = tiled_tile.getPosition();
const wall_id = try self.world.spawnWall(tilesheet.get(pos.x, pos.y)); const wall_id = try self.world.spawnWall(tilesheet.get(pos.x, pos.y));
_ = self.world.moveEntity(wall_id, @intCast(ox), @intCast(oy)); _ = self.world.setEntityPosition(wall_id, @intCast(ox), @intCast(oy));
} }
} }
} }
@ -310,14 +324,14 @@ pub fn init(self: *App, plt: Platform.Init) !?u8 {
if (tilemap.getTile(boxes, tilesets, x, y)) |tiled_tile| { if (tilemap.getTile(boxes, tilesets, x, y)) |tiled_tile| {
const pos = tiled_tile.getPosition(); const pos = tiled_tile.getPosition();
const box_id = try self.world.spawnBox(tilesheet.get(pos.x, pos.y)); const box_id = try self.world.spawnBox(tilesheet.get(pos.x, pos.y));
_ = self.world.moveEntity(box_id, @intCast(ox), @intCast(oy)); _ = self.world.setEntityPosition(box_id, @intCast(ox), @intCast(oy));
} }
} }
} }
} }
const player_id = try self.world.spawnPlayer(); const player_id = try self.world.spawnPlayer();
_ = self.world.moveEntity(player_id, @intFromFloat(player_pos.x), @intFromFloat(player_pos.y)); _ = self.world.setEntityPosition(player_id, @intFromFloat(player_pos.x), @intFromFloat(player_pos.y));
return null; return null;
} }
@ -368,7 +382,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
while (entity_iter.next()) |entity_id| { while (entity_iter.next()) |entity_id| {
const entity = self.world.entities.getAssumeExists(entity_id); const entity = self.world.entities.getAssumeExists(entity_id);
if (entity.type == .player) { if (entity.type == .player) {
if ((dir_x != 0 or dir_y != 0) and self.world.movePlayer(entity_id, dir_x, dir_y)) { if ((dir_x != 0 or dir_y != 0) and self.world.moveEntity(entity_id, dir_x, dir_y)) {
var new_direction = entity.player_direction; var new_direction = entity.player_direction;
if (dir_x < 0) { if (dir_x < 0) {
new_direction = .left; new_direction = .left;