Comments to describe each functionality in-depth

This commit is contained in:
2026-04-29 16:04:32 +02:00
parent db16bafd6c
commit 9defec8448
3 changed files with 39 additions and 0 deletions

View File

@@ -7,10 +7,14 @@ const Node = @import("node.zig").Node;
const Road = @import("road.zig").Road;
pub const NodeManager = struct {
/// Tracks which ID the next added node will get
next_id: usize,
/// Tracks all the node (pointers)
nodes: std.ArrayList(*Node),
/// Tracks the temporary node, being part of the newly built road
temp_node: ?*Node,
/// Constructor (for convenience)
pub fn init() NodeManager {
return .{
.next_id = 0,
@@ -19,6 +23,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| {
try node.deinit(allocator);
@@ -26,6 +31,7 @@ pub const NodeManager = struct {
self.nodes.deinit(allocator);
}
/// Regular draw function
pub fn draw(self: *const NodeManager, pos: Vector2) void {
for (self.nodes.items) |node| {
node.draw(null);
@@ -43,6 +49,8 @@ pub const NodeManager = struct {
}
}
/// Checks if there is a node pointer within the snap radius of pos coordinate;
/// otherwise creates a new node and returns its pointer
pub fn getSelectedNode(self: *NodeManager, allocator: std.mem.Allocator, pos: Vector2) !*Node {
for (self.nodes.items) |node| {
if (node.posWithinRadius(pos)) return node;
@@ -57,6 +65,7 @@ pub const NodeManager = struct {
return self.nodes.items[self.nodes.items.len - 1];
}
/// Gets next id, resets only on clear()
fn getNextID(self: *NodeManager) usize {
const id = self.next_id;
self.next_id += 1;
@@ -64,6 +73,7 @@ pub const NodeManager = struct {
return id;
}
/// Clears all existing nodes connected, not deinitialisation
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void {
self.temp_node = null;
for (self.nodes.items) |node| {
@@ -73,6 +83,7 @@ pub const NodeManager = struct {
self.next_id = 0;
}
/// Deletes node, returns error if node still has road references
pub fn deleteNode(self: *NodeManager, allocator: std.mem.Allocator, node_to_delete: *Node) !void {
if (node_to_delete.roads.items.len != 0) return e.Entity.HasReferences;