Allow interaction with plug centered on player

master
Louis Pearson 2022-02-02 00:06:36 -07:00
parent ae34313849
commit 56746b0041
1 changed files with 19 additions and 8 deletions

View File

@ -399,6 +399,21 @@ fn getNearestCircuitInteraction(pos: Vec2f) ?Interaction {
return null; return null;
} }
fn getNearestPlugInteraction(pos: Vec2f, wireID: usize, which: usize) ?Interaction {
const cell = util.world2cell(pos);
if (circuit.get_cell(cell)) |tile| {
if (Circuit.is_plug(tile)) {
const active = circuit.isEnabled(cell);
return Interaction{
.details = .{ .plug = .{ .wireID = wireID, .which = which } },
.pos = cell * Map.tile_size + Vec2{ 4, 4 },
.active = active,
};
}
}
return null;
}
fn getNearestWireInteraction(pos: Vec2f, range: f32) ?Interaction { fn getNearestWireInteraction(pos: Vec2f, range: f32) ?Interaction {
var newIndicator: ?Interaction = null; var newIndicator: ?Interaction = null;
var minDistance: f32 = range; var minDistance: f32 = range;
@ -451,7 +466,6 @@ fn manipulationProcess(pos: *Pos, control: *Control) void {
indicator = i; indicator = i;
} }
} else if (control.grabbing) |details| { } else if (control.grabbing) |details| {
const cell = util.world2cell(offsetPos);
var wire = &wires.slice()[details.id]; var wire = &wires.slice()[details.id];
var nodes = wire.nodes.slice(); var nodes = wire.nodes.slice();
@ -465,13 +479,10 @@ fn manipulationProcess(pos: *Pos, control: *Control) void {
nodes[details.which].pos = pos.pos + Vec2f{ 0, -4 }; nodes[details.which].pos = pos.pos + Vec2f{ 0, -4 };
} }
if (Circuit.is_plug(circuit.get_cell(cell) orelse 0)) { if (getNearestPlugInteraction(offsetPos, details.id, details.which)) |i| {
const active = circuit.isEnabled(cell); indicator = i;
indicator = .{ } else if (getNearestPlugInteraction(centeredPos, details.id, details.which)) |i| {
.details = .{ .plug = .{ .wireID = details.id, .which = details.which } }, indicator = i;
.pos = cell * Map.tile_size + Vec2{ 4, 4 },
.active = active,
};
} else if (input.btnp(.one, .two)) { } else if (input.btnp(.one, .two)) {
nodes[details.which].pinned = false; nodes[details.which].pinned = false;
control.grabbing = null; control.grabbing = null;