Get wires working again

master
Louis Pearson 2022-01-20 15:43:42 -07:00
parent 313c058ae3
commit d73f8d16bb
2 changed files with 11 additions and 9 deletions

View File

@ -131,18 +131,19 @@ fn cell2index(c: Cell) ?usize {
const CellState = struct { enabled: bool = false, tile: u8 };
const MAXCELLS = 400;
const MAXBRIDGES = 10;
const CellMap = [MAXCELLS]CellState;
const BridgeState = struct { cells: [2]Cell, id: usize, enabled: bool };
offset: Cell,
cells: CellMap,
bridges: std.BoundedArray(BridgeState, 10),
bridges: std.BoundedArray(BridgeState, MAXBRIDGES),
pub fn init() @This() {
var this = @This(){
.offset = Cell{ 0, 0 },
.cells = undefined,
.bridges = std.BoundedArray(BridgeState, 10).init(0) catch unreachable,
.bridges = std.BoundedArray(BridgeState, MAXBRIDGES).init(0) catch unreachable,
};
return this;
}
@ -178,8 +179,8 @@ pub fn bridge(this: *@This(), cells: [2]Cell, bridgeID: usize) void {
}
}
pub fn enabledBridges(this: @This()) std.BoundedArray(usize, 10) {
var items = std.BoundedArray(usize, 10).init(0) catch unreachable;
pub fn enabledBridges(this: @This()) std.BoundedArray(usize, MAXBRIDGES) {
var items = std.BoundedArray(usize, MAXBRIDGES).init(0) catch unreachable;
for (this.bridges.constSlice()) |b| {
if (b.enabled) items.append(b.id) catch unreachable;
}

View File

@ -115,7 +115,7 @@ const ParticleSystem = struct {
pub fn draw(this: @This()) void {
for (this.particles.constSlice()) |*part| {
w4.DRAW_COLORS.* = 0x0001;
w4.DRAW_COLORS.* = 0x0002;
w4.oval(util.vec2fToVec2(part.pos.pos), Vec2{ 2, 2 });
}
}
@ -230,10 +230,9 @@ var indicator: ?struct { pos: Vec2, t: enum { wire, plug, lever }, active: bool
var time: usize = 0;
export fn update() void {
w4.DRAW_COLORS.* = 0x0004;
w4.rect(.{ 0, 0 }, .{ 160, 160 });
{
// Doing this before clearing the screen since the stack
// reaches the screen during this block
circuit.clear();
const q = World.Query.require(&.{.wire});
var wireIter = world.iter(q);
@ -251,7 +250,7 @@ export fn update() void {
var wireComponents = world.components.items(.wire);
var enabledWires = circuit.enabledBridges();
for (enabledWires.slice()) |wireID| {
var wire = wireComponents[wireID].?;
var wire = &wireComponents[wireID].?;
wire.enabled = true;
if (time % 60 == 0) {
if (!wire.begin().pinned) particles.createNRandom(wire.begin().pos, 8);
@ -259,6 +258,8 @@ export fn update() void {
}
}
}
w4.DRAW_COLORS.* = 0x0004;
w4.rect(.{ 0, 0 }, .{ 160, 160 });
world.process(1, &.{.pos}, velocityProcess);
world.process(1, &.{ .pos, .physics }, physicsProcess);