diff --git a/src/game.zig b/src/game.zig index 3d3618b..3ea7fcb 100644 --- a/src/game.zig +++ b/src/game.zig @@ -418,6 +418,8 @@ pub fn update(time: usize) !State { try remove.append(i); music.playCollect(score); shouldSave = true; + const coord = world.Coordinate.fromVec2(util.world2cell(coin.pos.pos)); + db.collectCoin(coord); } } while (remove.popOrNull()) |i| { @@ -661,7 +663,7 @@ fn manipulationProcess(pos: *Pos, control: *Control) !void { const new_switch = circuit.toggle(cell); if (new_switch) |tile| { const T = world.Tiles; - const new_state: u8 = switch(tile) { + const new_state: u8 = switch (tile) { T.SwitchTeeWestOn, T.SwitchTeeEastOn, T.SwitchVerticalOn => 1, else => 0, }; diff --git a/src/world.zig b/src/world.zig index 692ce00..076ebf4 100644 --- a/src/world.zig +++ b/src/world.zig @@ -216,6 +216,10 @@ pub const Coordinate = struct { } }; } + pub fn fromVec2(vec: @Vector(2, i32)) Coordinate { + return .{ .val = .{ @intCast(i16, vec[0]), @intCast(i16, vec[1]) } }; + } + pub fn toLevelTopLeft(coord: Coordinate) Coordinate { const worldc = coord.toWorld(); return .{ .val = .{ @@ -431,6 +435,7 @@ pub const EntityKind = enum(u8) { WireEndAnchor, Door, Trapdoor, + Collected, }; pub const Entity = struct { @@ -714,6 +719,18 @@ pub const Database = struct { } // Entity functions + pub fn getEntityID(database: *Database, coord: Coordinate) ?usize { + for (database.entities) |entity, i| { + if (!entity.coord.eq(coord)) continue; + return i; + } + return null; + } + + pub fn collectCoin(db: *Database, coord: Coordinate) void { + const coin = db.getEntityID(coord) orelse return; + db.entities[coin].kind = .Collected; + } pub fn getWire(database: *Database, level: Level, num: usize) ?[2]Entity { const nw = Coord.fromWorld(level.world_x, level.world_y);