From afd7aa50c4e6a3ca7365e17cff8f9cd4c49ca02c Mon Sep 17 00:00:00 2001 From: Marto Date: Fri, 1 May 2026 16:44:52 +0200 Subject: [PATCH] Added comments and slightly refactored code for deletion of temp node --- src/common/constants.zig | 3 +++ src/infrastructure/node_manager.zig | 14 +++++++++----- src/simulator.zig | 2 ++ 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/common/constants.zig b/src/common/constants.zig index 4c6cb77..4a57651 100644 --- a/src/common/constants.zig +++ b/src/common/constants.zig @@ -25,6 +25,9 @@ pub const ROAD_COLOUR = clr.black; /// Colour of the road that is highlighted pub const ROAD_HIGHLIGHTED_COLOUR = clr.green; +/// Regular text size pub const TEXT_SIZE = 50; +/// Text size that is used for displaying entity IDs pub const ENTITY_DATA_TEXT_SIZE = TEXT_SIZE / 2; +/// Text colour in which entity IDs are displayed if toggled pub const ENTITY_DATA_TEXT_COLOUR = clr.orange; \ No newline at end of file diff --git a/src/infrastructure/node_manager.zig b/src/infrastructure/node_manager.zig index f010d51..a2ea6c2 100644 --- a/src/infrastructure/node_manager.zig +++ b/src/infrastructure/node_manager.zig @@ -118,16 +118,20 @@ pub const NodeManager = struct { return null; } + /// Essentially what it does is it sets temp node pointer to null and + /// if the node it pointed at had no road references (essentially it was a new node for road building), + /// it deletes the node from the node list as well pub fn deleteTempNode(self: *NodeManager, allocator: std.mem.Allocator) void { if (self.temp_node == null) return; const node = self.temp_node.?; - if (node.roads.items.len == 0) - self.deleteNode(allocator, node) catch |err| { - std.debug.panic("Failed to delete the temporary node: {}\n", .{err}); - }; - self.temp_node = null; + + if (node.roads.items.len != 0) return; + self.deleteNode(allocator, node) catch |err| { + std.debug.panic("Failed to delete the temporary node: {}\n", .{err}); + }; + } }; diff --git a/src/simulator.zig b/src/simulator.zig index ccc3cb5..9644f5d 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -219,6 +219,7 @@ pub const Simulator = struct { self.highlighted_entity = null; } + /// Returns array of IntersectionData struct, containing pointers to roads that got intersected and exact position fn getIntersectingRoads(self: *const Simulator, allocator: std.mem.Allocator, start: *const Node, end: *const Node) ![]st.IntersectionData { var intersections: std.ArrayList(st.IntersectionData) = .empty; var collision_point: rl.Vector2 = undefined; @@ -286,6 +287,7 @@ pub const Simulator = struct { return sorted_intersection; } + /// Takes the data about intersections and adds new nodes there alongside with linking existing roads to them fn splitRoadsByIntersections(self: *Simulator, intersections: []st.IntersectionData, start: *Node, end: *Node) void { if (intersections.len == 0) { self.road_man.addRoad(self.allocator, start, end) catch |err| {