Fixed things, added new features and added new bugs

This commit is contained in:
2026-04-08 01:11:42 +02:00
parent e46ce0fc97
commit 175b338ee4
8 changed files with 173 additions and 38 deletions

View File

@@ -22,14 +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);
if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS, .pink);
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
}
const pos = rl.getMousePosition();
if (self.temp_node) |node| {
rl.drawLineEx(node.pos, pos, c.ROAD_SIZE, .black);
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
@@ -47,16 +46,22 @@ pub const NodeManager = struct {
return null;
}
/// Returns the pointer to the node that is in the snapping node radius of the pos
/// or creates a new node and returns pointer to it
fn getSelectedNode(self: *NodeManager, pos: rl.Vector2) *Node {
pub fn getNodeWithinRadius(self: *const NodeManager, pos: rl.Vector2) ?*Node {
for (self.nodes.items) |*node| {
if (!rl.checkCollisionPointCircle(pos, node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS))
if (!node.withinSnappingRadius(pos))
continue;
return node;
}
return null;
}
/// Returns the pointer to the node that is in the snapping node radius of the pos
/// or creates a new node and returns pointer to it
fn getSelectedNode(self: *NodeManager, pos: rl.Vector2) *Node {
if (self.getNodeWithinRadius(pos)) |node| return node;
// We couldn't find the existing node and as such must create a new one
const node: Node = .init(self.getNewID(), pos);
@@ -75,9 +80,7 @@ pub const NodeManager = struct {
/// generates finds the last element in the list and returns id + 1 or 0 if there are no elements in the list
fn getNewID(self: *const NodeManager) usize {
const last_ref = self.getLastRef();
return if (last_ref) |ref| ref.*.id + 1 else 0;
return if (self.getLastRef()) |ref| ref.*.id + 1 else 0;
}
/// Iterates through all nodes and runs the deinit procedure on each of them
@@ -88,8 +91,9 @@ pub const NodeManager = struct {
}
/// Removes the node from the list with all appropriate checks
pub fn removeNode(self: *NodeManager, node_to_remove: *Node) void {
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
// This also means we don't have to deinit it, since it has no elements
if (node_to_remove.roads.items.len > 0) return;
for (self.nodes.items, 0..) |*node, i| {
@@ -98,5 +102,7 @@ pub const NodeManager = struct {
_ = self.nodes.swapRemove(i);
return;
}
return error.NodeNotExist;
}
};