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

@@ -1,16 +1,16 @@
const std = @import("std");
const rl = @import("raylib");
const s = @import("../structures.zig");
const c = @import("../constants.zig");
const Road = @import("road.zig").Road;
pub const Node = struct {
id: usize,
pos: s.Vector2,
pos: rl.Vector2,
roads: std.ArrayList(*Road),
pub fn init(new_id: usize, new_pos: s.Vector2) Node {
pub fn init(new_id: usize, new_pos: rl.Vector2) Node {
return .{
.id = new_id,
.pos = new_pos,
@@ -22,12 +22,14 @@ pub const Node = struct {
self.roads.deinit(allocator);
}
/// References the passed road with the node
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road: *Road) !void {
if (self.roadInList(road)) return;
try self.roads.append(allocator, road);
}
/// Returns bool whether the road passed is part of the roads list
fn roadInList(self: *const Node, road_to_add: *const Road) bool {
for (self.roads.items) |road| {
if (road.id == road_to_add.id) return true;

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);
}
}
};

View File

@@ -24,6 +24,7 @@ pub const RoadManager = struct {
}
}
/// Creates a new road with nodes; Then it references the said road to the nodes themselves
pub fn add(self: *RoadManager, allocator: std.mem.Allocator, start_node: *Node, end_node: *Node) !void {
const road: Road = .init(self.getNewID(), start_node, end_node);
@@ -38,12 +39,14 @@ pub const RoadManager = struct {
try end_node.referenceRoad(allocator, road_ref);
}
/// Returns the reference to the last element in the list; returns null if there are no elements in the list
fn getLastRef(self: *const RoadManager) ?*Road {
const rlen = self.roads.items.len;
return if (rlen == 0) null else &self.roads.items[rlen - 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 RoadManager) usize {
const last_ref = self.getLastRef();