Implemented that each road, node in managers will be stored as pointers (allocated in heap)

This commit is contained in:
2026-04-29 10:46:32 +02:00
parent 37e2f36ae9
commit fc67fe3d7e
5 changed files with 26 additions and 13 deletions

View File

@@ -7,7 +7,7 @@ const Road = @import("road.zig").Road;
pub const NodeManager = struct {
next_id: usize,
nodes: std.ArrayList(Node),
nodes: std.ArrayList(*Node),
temp_node: ?*Node,
pub fn init() NodeManager {
@@ -19,8 +19,9 @@ pub const NodeManager = struct {
}
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
for (self.nodes.items) |*node| {
for (self.nodes.items) |node| {
node.deinit(allocator);
allocator.destroy(node);
}
self.nodes.deinit(allocator);
}
@@ -43,15 +44,17 @@ pub const NodeManager = struct {
}
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
for (self.nodes.items) |*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);
const node_ptr = try allocator.create(Node);
node_ptr.* = node;
try self.nodes.append(allocator, node_ptr);
return &self.nodes.items[self.nodes.items.len - 1];
return self.nodes.items[self.nodes.items.len - 1];
}
fn getNextID(self: *NodeManager) usize {
@@ -63,8 +66,9 @@ pub const NodeManager = struct {
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
self.temp_node = null;
for (self.nodes.items) |*node| {
for (self.nodes.items) |node| {
node.deinit(allocator);
allocator.destroy(node);
}
self.nodes.clearRetainingCapacity();
self.next_id = 0;