const std = @import("std"); const Vector2 = @import("raylib").Vector2; const c = @import("../constants.zig"); const Node = @import("node.zig").Node; const Road = @import("road.zig").Road; pub const NodeManager = struct { next_id: usize, nodes: std.ArrayList(Node), temp_node: ?*Node, pub fn init() NodeManager { return .{ .next_id = 0, .nodes = .empty, .temp_node = null, }; } pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void { for (self.nodes.items) |*node| { node.deinit(allocator); } self.nodes.deinit(allocator); } pub fn draw(self: *const NodeManager, pos: Vector2) void { for (self.nodes.items) |node| { node.draw(null); } if (self.temp_node) |node| { // Temporary node that points at the cursor var cur_node = Node.init(0, pos); // Temporary road that is to be drawn as one in the making const road: Road = .init(0, node, &cur_node); road.draw(); node.*.draw(c.NODE_TEMP_COLOUR); cur_node.draw(c.NODE_CURSOR_COLOUR); } } pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node { for (self.nodes.items) |*node| { if (node.posWithinRadius(pos)) return node; } // No node is within that position, so we must create a new one const node: Node = .init(self.getNextID(), pos); try self.nodes.append(allocator, node); return &self.nodes.items[self.nodes.items.len - 1]; } fn getNextID(self: *NodeManager) usize { const id = self.next_id; self.next_id += 1; return id; } pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void { self.temp_node = null; for (self.nodes.items) |*node| { node.deinit(allocator); } self.nodes.clearRetainingCapacity(); self.next_id = 0; } };