Begin adding global circuit sim

master
Louis Pearson 2022-08-07 16:37:37 -06:00
parent 020e6f3615
commit 76d50ce508
2 changed files with 46 additions and 3 deletions

View File

@ -653,6 +653,19 @@ fn updateCircuit() !void {
const cellEnd = util.world2cell(nodes[nodes.len - 1].pos); const cellEnd = util.world2cell(nodes[nodes.len - 1].pos);
circuit.bridge(.{ cellBegin, cellEnd }, wireID); 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 // Simulate circuit
@ -681,6 +694,8 @@ fn updateCircuit() !void {
for (enabledDoors.items) |door| { for (enabledDoors.items) |door| {
try map.set_cell(door, world.Tiles.Empty); try map.set_cell(door, world.Tiles.Empty);
} }
} }
fn wirePhysicsProcess(dt: f32, wire: *Wire) !void { fn wirePhysicsProcess(dt: f32, wire: *Wire) !void {

View File

@ -167,6 +167,10 @@ pub const Coordinate = struct {
return .{ .val = .{ coord.val[0] + val[0], coord.val[1] + val[1] } }; 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 { pub fn eq(coord: Coordinate, other: Coordinate) bool {
return coord.val[0] == other.val[0] and coord.val[1] == other.val[1]; 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 }; 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 { pub fn toLevelTopLeft(coord: Coordinate) Coordinate {
const worldc = coord.toWorld(); const worldc = coord.toWorld();
return .{ .val = .{ return .{ .val = .{
@ -533,7 +544,6 @@ pub const Database = struct {
level_info: []LevelHeader, level_info: []LevelHeader,
circuit_info: []CircuitNode, circuit_info: []CircuitNode,
level_data_begin: usize, level_data_begin: usize,
// circuit_data_begin: usize,
const world_data = @embedFile(@import("world_data").path); const world_data = @embedFile(@import("world_data").path);
@ -604,6 +614,25 @@ pub const Database = struct {
} }
return null; 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 // All levels in the game. If two rooms are next to each other, they
@ -710,8 +739,7 @@ pub const NodeKind = union(NodeEnum) {
} }; } };
}, },
.Plug => { .Plug => {
const plug = const plug = try reader.readInt(NodeID, .Little);
try reader.readInt(NodeID, .Little);
if (plug == std.math.maxInt(NodeID)) { if (plug == std.math.maxInt(NodeID)) {
kind = .{ .Plug = null }; kind = .{ .Plug = null };
} else { } else {