diff --git a/src/common/constants.zig b/src/common/constants.zig index adb435b..80b8f07 100644 --- a/src/common/constants.zig +++ b/src/common/constants.zig @@ -16,10 +16,11 @@ pub const NODE_COLOUR = clr.brown; pub const NODE_TEMP_COLOUR = clr.orange; /// The colour of the node being at the cursor pub const NODE_CURSOR_COLOUR = clr.blue; +pub const NODE_RELATED_COLOUR = clr.purple; /// Road (line) size pub const ROAD_SIZE = 20; /// Regular road colour pub const ROAD_COLOUR = clr.black; /// Colour of the road that is highlighted -pub const ROAD_HIGHLIGHTED_COLOUR = clr.green; \ No newline at end of file +pub const ROAD_HIGHLIGHTED_COLOUR = clr.green; diff --git a/src/infrastructure/node.zig b/src/infrastructure/node.zig index 7c01393..1946d5d 100644 --- a/src/infrastructure/node.zig +++ b/src/infrastructure/node.zig @@ -3,6 +3,7 @@ const rl = @import("raylib"); const c = @import("../common/constants.zig"); const e = @import("../errors.zig"); +const st = @import("../common/structures.zig"); const Road = @import("road.zig").Road; pub const Node = struct { diff --git a/src/infrastructure/node_manager.zig b/src/infrastructure/node_manager.zig index 20fa784..0764ad9 100644 --- a/src/infrastructure/node_manager.zig +++ b/src/infrastructure/node_manager.zig @@ -26,6 +26,7 @@ pub const NodeManager = struct { /// Deinitialises every node (pointer) within the list and then deinits the list containing the nodes itself pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) !void { for (self.nodes.items) |node| { + node.roads.clearRetainingCapacity(); try node.deinit(allocator); } self.nodes.deinit(allocator); diff --git a/src/infrastructure/road.zig b/src/infrastructure/road.zig index c31d632..af19395 100644 --- a/src/infrastructure/road.zig +++ b/src/infrastructure/road.zig @@ -1,6 +1,7 @@ const rl = @import("raylib"); const c = @import("../common/constants.zig"); +const st = @import("../common/structures.zig"); const Node = @import("node.zig").Node; pub const Road = struct { diff --git a/src/simulator.zig b/src/simulator.zig index f00e852..8d0192b 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -24,6 +24,8 @@ pub const Simulator = struct { /// /// For example, if I hover over a node it will highlight all roads that are connected to it; /// Same goes for hovering over a road or in the future, a car (might show destination and path to it) + /// + /// Note: It only works outside of the delete mode show_connections: bool, highlighted_entity: ?st.Entity, @@ -51,7 +53,6 @@ pub const Simulator = struct { rl.clearBackground(c.BACKGROUND_COLOR); var highlighted_road: ?*Road = null; - if (self.delete_mode) { if (self.highlighted_entity) |entity| { if (entity == .road) highlighted_road = entity.road; @@ -60,6 +61,33 @@ pub const Simulator = struct { self.road_man.draw(highlighted_road); self.node_man.draw(pos); + + self.drawRelatedSelectedEntities(); + } + + fn drawRelatedSelectedEntities(self: *const Simulator) void { + if (!self.show_connections or self.highlighted_entity == null) return; + const h_entity = self.highlighted_entity.?; + + switch (h_entity) { + .node => { + const node = h_entity.node; + + for (node.roads.items) |road| { + road.draw(true); + } + + node.draw(c.NODE_RELATED_COLOUR); + }, + .road => { + const road = h_entity.road; + + road.draw(true); + + road.nodes[0].draw(c.NODE_RELATED_COLOUR); + road.nodes[1].draw(c.NODE_RELATED_COLOUR); + }, + } } /// Update tick @@ -77,7 +105,7 @@ pub const Simulator = struct { fn handleKeyboardInput(self: *Simulator) void { self.auto_continue = rl.isKeyDown(.left_control); self.delete_mode = rl.isKeyDown(.left_shift); - self.show_connections = rl.isKeyDown(.left_alt); + self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode; if (rl.isKeyReleased(.c)) self.clear() catch |err| { std.debug.panic("Failed to clear the entities: {}\n", .{err}); @@ -102,6 +130,7 @@ pub const Simulator = struct { /// User initiated road building functionality fn new_road(self: *Simulator, pos: rl.Vector2) void { + if (self.show_connections) return; const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| { std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{ pos.x, pos.y, err @@ -143,6 +172,7 @@ pub const Simulator = struct { /// Clearing node and road lists without deinitialising them (only the children) fn clear(self: *Simulator) !void { + self.highlighted_entity = null; self.road_man.clear(self.allocator); try self.node_man.clear(self.allocator); }