Persist collection of coins to memory

master
Louis Pearson 2022-08-08 18:22:56 -06:00
parent 240587344d
commit 1404d6067e
2 changed files with 20 additions and 1 deletions

View File

@ -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,
};

View File

@ -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);