Added comments and additional keybind functionality (clearing all entities and drawing UI [debug] data)

This commit is contained in:
2026-04-07 11:24:11 +02:00
parent 9d217b427e
commit 92adc698f1
7 changed files with 74 additions and 13 deletions

View File

@@ -16,9 +16,7 @@ pub const NodeManager = struct {
}
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
for (self.nodes.items) |*node| {
node.deinit(allocator);
}
self.clear(allocator);
self.nodes.deinit(allocator);
}
@@ -39,10 +37,12 @@ pub const NodeManager = struct {
}
}
/// Adds a new node to the list, and if the temp_node already has a value it returns the pointer to the 2nd one
pub fn add(self: *NodeManager, pos: rl.Vector2) ?*Node {
const pos_node = self.getSelectedNode(pos);
if (self.temp_node) |_| {
// TODO add road length check before creating the second node / returning
return pos_node;
}
@@ -50,6 +50,8 @@ 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 {
for (self.nodes.items) |*node| {
if (!rl.checkCollisionPointCircle(pos, node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS))
@@ -62,20 +64,28 @@ pub const NodeManager = struct {
const node: Node = .init(self.getNewID(), pos);
self.nodes.appendBounded(node) catch |err| {
std.debug.panic("This is because preallocated size got exceeded, TODO fix:\n{}\n", .{err});
std.debug.panic("This is because preallocated size got exceeded, TODO fix: {}\n", .{err});
};
return self.getLastRef().?;
}
/// Returns the reference to the last element in the list; returns null if there are no elements in the list
fn getLastRef(self: *const NodeManager) ?*Node {
const nlen = self.nodes.items.len;
return if (nlen == 0) null else &self.nodes.items[nlen - 1];
}
/// 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;
}
fn getLastRef(self: *const NodeManager) ?*Node {
const nlen = self.nodes.items.len;
return if (nlen == 0) null else &self.nodes.items[nlen - 1];
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
for (self.nodes.items) |*node| {
node.deinit(allocator);
}
}
};