Save switch state

master
Louis Pearson 2022-08-08 23:29:16 -06:00
parent 797c21a80b
commit 68bdf13c89
3 changed files with 92 additions and 24 deletions

View File

@ -7,6 +7,24 @@ const T = world.Tiles;
const Vec2 = util.Vec2; const Vec2 = util.Vec2;
const Cell = util.Cell; const Cell = util.Cell;
pub fn switchIsOn(tile: u8) bool {
return switch (tile) {
T.SwitchTeeWestOn,
T.SwitchTeeEastOn,
T.SwitchVerticalOn,
T.SwitchHorizontalOn,
=> true,
T.SwitchTeeWestOff,
T.SwitchTeeEastOff,
T.SwitchVerticalOff,
T.SwitchHorizontalOff,
=> false,
else => false,
};
}
pub fn toggle_switch(tile: u8) u8 { pub fn toggle_switch(tile: u8) u8 {
return switch (tile) { return switch (tile) {
// Tee west // Tee west
@ -230,6 +248,17 @@ pub fn isEnabled(this: @This(), cell: Cell) bool {
return this.levels[i] >= 1; return this.levels[i] >= 1;
} }
pub fn switchOn(this: *@This(), cell: Cell) void {
if (this.get_cell(cell)) |tile| {
if (T.is_switch(tile)) {
if (switchIsOn(tile)) return;
const toggled = toggle_switch(tile);
this.set_cell(cell, toggled);
return;
}
}
}
pub fn toggle(this: *@This(), c: Cell) ?u8 { pub fn toggle(this: *@This(), c: Cell) ?u8 {
const cell = c; const cell = c;
if (this.get_cell(cell)) |tile| { if (this.get_cell(cell)) |tile| {

View File

@ -275,6 +275,19 @@ fn loadLevel(lvl: usize) !void {
} }
} }
{
var i: usize = 0;
while (level.getSwitch(i)) |_switch| : (i += 1) {
const globalc = levelc.addC(_switch);
var e = false;
if (db.getSwitchState(globalc)) |state| {
e = true;
if (state != 0) circuit.switchOn(.{ _switch.val[0], _switch.val[1] });
}
w4.tracef("---- Switch %d: (%d, %d) <%d>", i, globalc.val[0], globalc.val[1], @boolToInt(e));
}
}
try coins.resize(0); try coins.resize(0);
// if (!try Disk.load()) { // if (!try Disk.load()) {
var i: usize = 0; var i: usize = 0;
@ -425,8 +438,8 @@ pub fn update(time: usize) !State {
if (player.pos.pos[0] > 160 - 4) try moveLevel(.R); if (player.pos.pos[0] > 160 - 4) try moveLevel(.R);
if (player.pos.pos[0] < 4) try moveLevel(.L); if (player.pos.pos[0] < 4) try moveLevel(.L);
if (player.pos.pos[1] > 160 - 4) try moveLevel(.D); if (player.pos.pos[1] > 160) try moveLevel(.D);
if (player.pos.pos[1] < 4) try moveLevel(.U); if (player.pos.pos[1] < 8) try moveLevel(.U);
try kinematicProcess(1, &player.pos, &player.kinematic); try kinematicProcess(1, &player.pos, &player.kinematic);
controlAnimProcess(1, &player.sprite, &player.controlAnim, &player.control); controlAnimProcess(1, &player.sprite, &player.controlAnim, &player.control);
@ -771,24 +784,24 @@ fn updateCircuit() !void {
try db.updateCircuit(frame_alloc); try db.updateCircuit(frame_alloc);
// for (db.circuit_info) |node, n| { for (db.circuit_info) |node, n| {
// const e = @boolToInt(node.energized); const e = @boolToInt(node.energized);
// switch (node.kind) { switch (node.kind) {
// .Conduit => |Conduit| w4.tracef("[%d]: Conduit [%d, %d] <%d>", n, Conduit[0], Conduit[1], e), .Conduit => |Conduit| w4.tracef("[%d]: Conduit [%d, %d] <%d>", n, Conduit[0], Conduit[1], e),
// .And => |And| w4.tracef("[%d]: And [%d, %d] <%d>", n, And[0], And[1], e), .And => |And| w4.tracef("[%d]: And [%d, %d] <%d>", n, And[0], And[1], e),
// .Xor => |Xor| w4.tracef("[%d]: Xor [%d, %d] <%d>", n, Xor[0], Xor[1], e), .Xor => |Xor| w4.tracef("[%d]: Xor [%d, %d] <%d>", n, Xor[0], Xor[1], e),
// .Source => w4.tracef("[%d]: Source", n), .Source => w4.tracef("[%d]: Source", n),
// .Socket => |Socket| { .Socket => |Socket| {
// const socket = Socket orelse std.math.maxInt(world.NodeID); const socket = Socket orelse std.math.maxInt(world.NodeID);
// w4.tracef("[%d]: Socket [%d] <%d>", n, socket, e); w4.tracef("[%d]: Socket [%d] <%d>", n, socket, e);
// }, },
// .Plug => |Plug| w4.tracef("[%d]: Plug [%d] <%d>", n, Plug, e), .Plug => |Plug| w4.tracef("[%d]: Plug [%d] <%d>", n, Plug, e),
// .Switch => |Switch| w4.tracef("[%d]: Switch %d [%d] <%d>", n, Switch.state, Switch.source, e), .Switch => |Switch| w4.tracef("[%d]: Switch %d [%d] <%d>", n, Switch.state, Switch.source, e),
// .SwitchOutlet => |Switch| w4.tracef("[%d]: SwitchOutlet %d [%d] <%d>", n, Switch.which, Switch.source, e), .SwitchOutlet => |Switch| w4.tracef("[%d]: SwitchOutlet %d [%d] <%d>", n, Switch.which, Switch.source, e),
// .Join => |Join| w4.tracef("[%d]: Join [%d] <%d>", n, Join, e), .Join => |Join| w4.tracef("[%d]: Join [%d] <%d>", n, Join, e),
// .Outlet => |Outlet| w4.tracef("[%d]: Outlet [%d] <%d>", n, Outlet, e), .Outlet => |Outlet| w4.tracef("[%d]: Outlet [%d] <%d>", n, Outlet, e),
// } }
// } }
} }
fn wirePhysicsProcess(dt: f32, wire: *Wire) !void { fn wirePhysicsProcess(dt: f32, wire: *Wire) !void {

View File

@ -337,6 +337,27 @@ pub const Level = struct {
} }
return null; return null;
} }
pub fn getSwitch(level: Level, which: usize) ?Coordinate {
const tiles = level.tiles orelse return null;
var switchCount: usize = 0;
for (tiles) |tile, i| {
switch (tile) {
.flags => |flag| {
if (flag.circuit == .Switch_Off or flag.circuit == .Switch_On) {
if (switchCount == which) {
const x = @intCast(i16, @mod(i, 20));
const y = @intCast(i16, @divFloor(i, 20));
return Coord.init(.{ x, y });
}
switchCount += 1;
}
},
else => continue,
}
}
return null;
}
}; };
// AutoTile algorithm datatypes // AutoTile algorithm datatypes
@ -760,6 +781,11 @@ pub const Database = struct {
} }
} }
pub fn getSwitchState(db: *Database, coord: Coord) ?u8 {
const _switch = db.getNodeID(coord) orelse return null;
return db.circuit_info[_switch].kind.Switch.state;
}
pub fn setSwitch(db: *Database, coord: Coord, new_state: u8) void { pub fn setSwitch(db: *Database, coord: Coord, new_state: u8) void {
const _switch = db.getNodeID(coord) orelse return; const _switch = db.getNodeID(coord) orelse return;
db.circuit_info[_switch].kind.Switch.state = new_state; db.circuit_info[_switch].kind.Switch.state = new_state;