From 76d50ce508fcda793fe1f6e7761f5e771d061707 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Sun, 7 Aug 2022 16:37:37 -0600 Subject: [PATCH] Begin adding global circuit sim --- src/game.zig | 15 +++++++++++++++ src/world.zig | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/game.zig b/src/game.zig index 22758b9..2c6a4f2 100644 --- a/src/game.zig +++ b/src/game.zig @@ -653,6 +653,19 @@ fn updateCircuit() !void { const cellEnd = util.world2cell(nodes[nodes.len - 1].pos); circuit.bridge(.{ cellBegin, cellEnd }, wireID); + + const Coord = world.Coordinate; + const topleft = Coord.fromWorld( level.world_x, level.world_y ); + const p1 = Coord.init(.{ + @intCast(i16, cellBegin[0]), + @intCast(i16, cellBegin[1]), + }).addC(topleft); + const p2 = Coord.init(.{ + @intCast(i16, cellEnd[0]), + @intCast(i16, cellEnd[1]), + }).addC(topleft); + + db.connectPlugs(p1, p2); } // Simulate circuit @@ -681,6 +694,8 @@ fn updateCircuit() !void { for (enabledDoors.items) |door| { try map.set_cell(door, world.Tiles.Empty); } + + } fn wirePhysicsProcess(dt: f32, wire: *Wire) !void { diff --git a/src/world.zig b/src/world.zig index 6b07b0c..09dc7a5 100644 --- a/src/world.zig +++ b/src/world.zig @@ -167,6 +167,10 @@ pub const Coordinate = struct { return .{ .val = .{ coord.val[0] + val[0], coord.val[1] + val[1] } }; } + pub fn addC(coord: Coordinate, other: Coordinate) Coordinate { + return .{ .val = .{ coord.val[0] + other.val[0], coord.val[1] + other.val[1] } }; + } + pub fn eq(coord: Coordinate, other: Coordinate) bool { return coord.val[0] == other.val[0] and coord.val[1] == other.val[1]; } @@ -177,6 +181,13 @@ pub const Coordinate = struct { return .{ world_x, world_y }; } + pub fn fromWorld(x: i8, y: i8) Coordinate { + return .{ .val = .{ + @intCast(i16, x) * 20, + @intCast(i16, y) * 20, + } }; + } + pub fn toLevelTopLeft(coord: Coordinate) Coordinate { const worldc = coord.toWorld(); return .{ .val = .{ @@ -533,7 +544,6 @@ pub const Database = struct { level_info: []LevelHeader, circuit_info: []CircuitNode, level_data_begin: usize, - // circuit_data_begin: usize, const world_data = @embedFile(@import("world_data").path); @@ -604,6 +614,25 @@ pub const Database = struct { } return null; } + + pub fn connectPlugs(db: *Database, p1: Coord, p2: Coord) void { + var opt1: ?usize = null; + var opt2: ?usize = null; + for (db.circuit_info) |node, i| { + if (!p1.eq(node.coord) or !p2.eq(node.coord)) continue; + if (p1.eq(node.coord)) opt1 = i else opt2 = i; + } + var w1 = db.circuit_info[opt1 orelse return]; + var w2 = db.circuit_info[opt2 orelse return]; + + if (w1.energized and !w2.energized) { + w2.energized = true; + w2.kind.Plug = @intCast(NodeID, opt1.?); + } else if (w2.energized and !w1.energized) { + w1.energized = true; + w1.kind.Plug = @intCast(NodeID, opt2.?); + } + } }; // All levels in the game. If two rooms are next to each other, they @@ -710,8 +739,7 @@ pub const NodeKind = union(NodeEnum) { } }; }, .Plug => { - const plug = - try reader.readInt(NodeID, .Little); + const plug = try reader.readInt(NodeID, .Little); if (plug == std.math.maxInt(NodeID)) { kind = .{ .Plug = null }; } else {