Added left and right click events, fixed display details option/functionality, added function that safely removes the node and prevented addition of new roads if they are bounded by the same nodes (distance/length check)

This commit is contained in:
2026-04-07 12:54:39 +02:00
parent 92adc698f1
commit e46ce0fc97
2 changed files with 35 additions and 7 deletions

View File

@@ -22,13 +22,13 @@ pub const NodeManager = struct {
pub fn draw(self: *const NodeManager, display_details: bool) void {
for (self.nodes.items) |node| {
if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS, .pink);
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
}
const pos = rl.getMousePosition();
if (self.temp_node) |node| {
if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS, .pink);
rl.drawLineEx(node.pos, pos, c.ROAD_SIZE, .black);
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
@@ -41,10 +41,7 @@ pub const NodeManager = struct {
pub fn add(self: *NodeManager, pos: rl.Vector2) ?*Node {
const pos_node = self.getSelectedNode(pos);
if (self.temp_node) |_| {
// TODO add road length check before creating the second node / returning
return pos_node;
}
if (self.temp_node != null) return pos_node;
self.temp_node = pos_node;
return null;
@@ -83,9 +80,23 @@ pub const NodeManager = struct {
return if (last_ref) |ref| ref.*.id + 1 else 0;
}
/// Iterates through all nodes and runs the deinit procedure on each of them
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
for (self.nodes.items) |*node| {
node.deinit(allocator);
}
}
/// Removes the node from the list with all appropriate checks
pub fn removeNode(self: *NodeManager, node_to_remove: *Node) void {
// In case the node has references to the existing roads we can not remove it
if (node_to_remove.roads.items.len > 0) return;
for (self.nodes.items, 0..) |*node, i| {
if (node.id != node_to_remove.id) continue;
_ = self.nodes.swapRemove(i);
return;
}
}
};