diff --git a/src/main.zig b/src/main.zig index a05b4a4..df84c86 100644 --- a/src/main.zig +++ b/src/main.zig @@ -172,6 +172,7 @@ var camera = Vec2{ 0, 0 }; const Coin = struct { pos: Pos, sprite: Sprite, anim: Anim, area: AABB }; var coins = std.BoundedArray(Coin, 10).init(0) catch unreachable; var score: u8 = 0; +var solids_mutable = assets.solid; const anim_store = struct { const stand = Anim.frame(8); @@ -203,7 +204,7 @@ export fn start() void { particles = ParticleSystem.init(); circuit = Circuit.init(&assets.conduit, assets.conduit_size); - map = Map.init(&assets.solid, assets.solid_size); + map = Map.init(&solids_mutable, assets.solid_size); camera = @divTrunc(assets.spawn, @splat(2, @as(i32, 20))) * @splat(2, @as(i32, 20)); @@ -489,7 +490,7 @@ fn updateCircuit() void { const cellEnd = util.world2cell(end.pos); if (circuit.isEnabled(cellBegin) or circuit.isEnabled(cellEnd)) wire.enabled = true; } - map.reset(); + map.reset(&assets.solid); const enabledDoors = circuit.enabledDoors(); for (enabledDoors.constSlice()) |door| { map.set_cell(door, 0); diff --git a/src/map.zig b/src/map.zig index deca3b6..f66ffdf 100644 --- a/src/map.zig +++ b/src/map.zig @@ -19,33 +19,31 @@ const tilemap_width = 16; const tilemap_height = 16; const tilemap_stride = 128; -const UpdateMap = util.Map(Cell, u8, MAXCELLS); - -tiles: []const u8, -tile_updates: UpdateMap, +tiles: []u8, map_size: Vec2, -pub fn init(map: []const u8, map_size: Vec2) @This() { +pub fn init(map: []u8, map_size: Vec2) @This() { var this = @This(){ .tiles = map, - .tile_updates = UpdateMap.init(), .map_size = map_size, }; return this; } pub fn set_cell(this: *@This(), cell: Cell, tile: u8) void { - this.tile_updates.set(cell, tile); + const x = cell[0]; + const y = cell[1]; + if (x < 0 or x > this.map_size[0] or y < 0 or y > this.map_size[1]) unreachable; + const i = x + y * this.map_size[0]; + this.tiles[@intCast(usize, i)] = tile; } -pub fn reset(this: *@This()) void { - var updates = this.tile_updates.values.slice(); - for (this.tile_updates.keys.slice()) |key, i| { - updates[i] = this.get_cell_raw(key).?; - } +pub fn reset(this: *@This(), initialState: []const u8) void { + std.debug.assert(initialState.len == this.tiles.len); + std.mem.copy(u8, this.tiles, initialState); } -fn get_cell_raw(this: @This(), cell: Cell) ?u8 { +pub fn get_cell(this: @This(), cell: Cell) ?u8 { const x = cell[0]; const y = cell[1]; if (x < 0 or x > this.map_size[0] or y < 0 or y > this.map_size[1]) return null; @@ -53,11 +51,6 @@ fn get_cell_raw(this: @This(), cell: Cell) ?u8 { return this.tiles[@intCast(u32, i)]; } -pub fn get_cell(this: @This(), cell: Cell) ?u8 { - if (this.tile_updates.get_const(cell)) |tile| return tile; - return this.get_cell_raw(cell); -} - pub fn draw(this: @This(), offset: Vec2) void { w4.DRAW_COLORS.* = 0x0210; var y: usize = 0;