Fixed things, added new features and added new bugs

This commit is contained in:
2026-04-08 01:11:42 +02:00
parent e46ce0fc97
commit 175b338ee4
8 changed files with 173 additions and 38 deletions

View File

@@ -24,17 +24,33 @@ pub const Node = struct {
/// References the passed road with the node
pub fn referenceRoad(self: *Node, allocator: std.mem.Allocator, road: *Road) !void {
if (self.roadInList(road)) return;
if (self.roadInList(road) != null) return;
try self.roads.append(allocator, road);
}
/// Tries to unrference the road passed into from the node (self)
///
/// Returns error if the road is not within the node
pub fn unreferenceRoad(self: *Node, road: *const Road) !void {
if (self.roadInList(road)) |i| _ = self.roads.swapRemove(i);
return error.InvalidNode;
}
/// 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;
fn roadInList(self: *const Node, road_to_check: *const Road) ?usize {
for (self.roads.items, 0..) |road, i| {
std.debug.print("Road id = {d}\n", .{road.id});
std.debug.print("Road to check id = {d}\n", .{road_to_check.id});
// TODO fix why doesn't 0 == 0 return i
if (road.id == road_to_check.id) return i;
}
return false;
return null;
}
pub fn withinSnappingRadius(self: *const Node, pos: rl.Vector2) bool {
return rl.checkCollisionPointCircle(pos, self.pos, c.NODE_SNAP_RADIUS);
}
};

View File

@@ -22,14 +22,13 @@ pub const NodeManager = struct {
pub fn draw(self: *const NodeManager, display_details: bool) void {
for (self.nodes.items) |node| {
if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS, .pink);
if (display_details) rl.drawCircleV(node.pos, c.NODE_SNAP_RADIUS, .pink);
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
}
const pos = rl.getMousePosition();
if (self.temp_node) |node| {
rl.drawLineEx(node.pos, pos, c.ROAD_SIZE, .black);
rl.drawCircleV(node.pos, c.NODE_RADIUS, .brown);
@@ -47,16 +46,22 @@ 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 {
pub fn getNodeWithinRadius(self: *const NodeManager, pos: rl.Vector2) ?*Node {
for (self.nodes.items) |*node| {
if (!rl.checkCollisionPointCircle(pos, node.pos, c.NODE_SNAP_RADIUS * c.NODE_RADIUS))
if (!node.withinSnappingRadius(pos))
continue;
return node;
}
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 {
if (self.getNodeWithinRadius(pos)) |node| return node;
// We couldn't find the existing node and as such must create a new one
const node: Node = .init(self.getNewID(), pos);
@@ -75,9 +80,7 @@ pub const NodeManager = struct {
/// 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;
return if (self.getLastRef()) |ref| ref.*.id + 1 else 0;
}
/// Iterates through all nodes and runs the deinit procedure on each of them
@@ -88,8 +91,9 @@ pub const NodeManager = struct {
}
/// Removes the node from the list with all appropriate checks
pub fn removeNode(self: *NodeManager, node_to_remove: *Node) void {
pub fn removeNode(self: *NodeManager, node_to_remove: *Node) !void {
// In case the node has references to the existing roads we can not remove it
// This also means we don't have to deinit it, since it has no elements
if (node_to_remove.roads.items.len > 0) return;
for (self.nodes.items, 0..) |*node, i| {
@@ -98,5 +102,7 @@ pub const NodeManager = struct {
_ = self.nodes.swapRemove(i);
return;
}
return error.NodeNotExist;
}
};

View File

@@ -10,4 +10,9 @@ pub const Road = struct {
.nodes = .{start_node, end_node},
};
}
pub fn unreferenceNodes(self: *Road) !void {
try self.nodes[0].unreferenceRoad(self);
try self.nodes[1].unreferenceRoad(self);
}
};

View File

@@ -2,15 +2,18 @@ const std = @import("std");
const rl = @import("raylib");
const c = @import("../constants.zig");
const st = @import("../structures.zig");
const Road = @import("road.zig").Road;
const Node = @import("node.zig").Node;
pub const RoadManager = struct {
roads: std.ArrayList(Road),
highlighted_road: ?*Road,
pub fn init(allocator: std.mem.Allocator) !RoadManager {
return .{
.roads = try .initCapacity(allocator, c.ALLOC_SIZE),
.highlighted_road = null,
};
}
@@ -18,9 +21,18 @@ pub const RoadManager = struct {
self.roads.deinit(allocator);
}
pub fn draw(self: *const RoadManager) void {
pub fn draw(self: *RoadManager, display_highlighted_road: bool) void {
self.highlighted_road = self.getSelectedRoad(rl.getMousePosition());
for (self.roads.items) |road| {
rl.drawLineEx(road.nodes[0].*.pos, road.nodes[1].*.pos, c.ROAD_SIZE, .black);
var colour: rl.Color = .black;
if (self.highlighted_road) |hr| {
if (display_highlighted_road and road.id == hr.id) {
colour = .green;
}
}
rl.drawLineEx(road.nodes[0].*.pos, road.nodes[1].*.pos, c.ROAD_SIZE, colour);
}
}
@@ -42,14 +54,38 @@ pub const RoadManager = struct {
/// 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
/// 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();
return if (self.getLastRef()) |ref| ref.*.id + 1 else 0;
}
return if (last_ref) |ref| ref.*.id + 1 else 0;
/// Removes the road which reference is passed in; returns error if the road is invalid
pub fn remove(self: *RoadManager, road_to_remove: *Road) !void {
try road_to_remove.unreferenceNodes();
for (self.roads.items, 0..) |*road, i| {
if (road.id == road_to_remove.id) {
_ = self.roads.swapRemove(i);
return;
}
}
return error.RoadNotInList;
}
/// Returns the pointer to the road that pos is pointing at or null if there is no road in the vicinity
pub fn getSelectedRoad(self: *const RoadManager, pos: rl.Vector2) ?*Road {
for (self.roads.items) |*road| {
if (!rl.checkCollisionPointLine(
pos, road.nodes[0].pos, road.nodes[1].pos, c.ROAD_SIZE / 2))
continue;
return road;
}
return null;
}
};