Basic drawing

This commit is contained in:
2026-04-28 22:11:35 +02:00
parent edfc54b927
commit ccbf7344b5
6 changed files with 121 additions and 3 deletions

View File

@@ -1,21 +1,43 @@
const std = @import("std");
const rl = @import("raylib");
const c = @import("../constants.zig");
const Road = @import("road.zig").Road;
pub const Node = struct {
id: usize,
pos: rl.Vector2,
roads: std.ArrayList(*Road),
pub fn init(new_id: usize, new_pos: rl.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 draw(self: *const Node, direct_colour: ?rl.Color) void {
const colour = if (direct_colour) |clr| clr else c.NODE_COLOUR;
rl.drawCircleV(self.pos, c.NODE_RADIUS, colour);
}
pub fn posWithinRadius(self: *const Node, pos: rl.Vector2) bool {
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_SNAP_RADIUS);
}
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road_to_add: *Road) !void {
// Note the road_to_add pointer must be one from the roads list as otherwise the pointer is dangling one
for (self.roads.items) |road| {
if (road.*.id == road_to_add.*.id) return error.RoadAlreadyReferenced;
}
try self.roads.append(allocator, road_to_add);
}
};