Initial commit

This commit is contained in:
2026-04-07 10:45:02 +02:00
commit 9d217b427e
11 changed files with 501 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
const std = @import("std");
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,
roads: std.ArrayList(*Road),
pub fn init(new_id: usize, new_pos: s.Vector2) Node {
return .{
.id = new_id,
.pos = new_pos,
.roads = .empty,
};
}
pub fn deinit(self: *Node, allocator: std.mem.Allocator) void {
self.roads.deinit(allocator);
}
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road: *Road) !void {
if (self.roadInList(road)) return;
try self.roads.append(allocator, road);
}
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;
}
return false;
}
};

View File

@@ -0,0 +1,81 @@
const std = @import("std");
const rl = @import("raylib");
const c = @import("../constants.zig");
const Node = @import("node.zig").Node;
pub const NodeManager = struct {
temp_node: ?*Node,
nodes: std.ArrayList(Node),
pub fn init(allocator: std.mem.Allocator) !NodeManager {
return .{
.temp_node = null,
.nodes = try .initCapacity(allocator, c.ALLOC_SIZE),
};
}
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, display_details: bool) void {
for (self.nodes.items) |node| {
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
}
const pos = rl.getMousePosition();
if (self.temp_node) |node| {
if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS, .pink);
rl.drawLineEx(node.pos, pos, c.ROAD_SIZE, .black);
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
rl.drawCircleV(pos, c.NODE_RADIUS, .blue);
}
}
pub fn add(self: *NodeManager, pos: rl.Vector2) ?*Node {
const pos_node = self.getSelectedNode(pos);
if (self.temp_node) |_| {
return pos_node;
}
self.temp_node = pos_node;
return null;
}
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))
continue;
return node;
}
// We couldn't find the existing node and as such must create a new one
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});
};
return self.getLastRef().?;
}
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];
}
};

View File

@@ -0,0 +1,13 @@
const Node = @import("node.zig").Node;
pub const Road = struct {
id: usize,
nodes: [2]*Node,
pub fn init(new_id: usize, start_node: *Node, end_node: *Node) Road {
return .{
.id = new_id,
.nodes = .{start_node, end_node},
};
}
};

View File

@@ -0,0 +1,52 @@
const std = @import("std");
const rl = @import("raylib");
const c = @import("../constants.zig");
const Road = @import("road.zig").Road;
const Node = @import("node.zig").Node;
pub const RoadManager = struct {
roads: std.ArrayList(Road),
pub fn init(allocator: std.mem.Allocator) !RoadManager {
return .{
.roads = try .initCapacity(allocator, c.ALLOC_SIZE),
};
}
pub fn deinit(self: *RoadManager, allocator: std.mem.Allocator) void {
self.roads.deinit(allocator);
}
pub fn draw(self: *const RoadManager) void {
for (self.roads.items) |road| {
rl.drawLineEx(road.nodes[0].*.pos, road.nodes[1].*.pos, c.ROAD_SIZE, .black);
}
}
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);
self.roads.appendBounded(road) catch |err| {
std.debug.print("This is because preallocated size got exceeded, TODO fix:\n{}\n", .{err});
return error.OutOfMemoryBounded;
};
const road_ref = self.getLastRef().?;
try start_node.referenceRoad(allocator, road_ref);
try end_node.referenceRoad(allocator, road_ref);
}
fn getLastRef(self: *const RoadManager) ?*Road {
const rlen = self.roads.items.len;
return if (rlen == 0) null else &self.roads.items[rlen - 1];
}
fn getNewID(self: *const RoadManager) usize {
const last_ref = self.getLastRef();
return if (last_ref) |ref| ref.*.id + 1 else 0;
}
};