Implementation of adding new roads, bug incoming, TODO entity viewer, something akin to imgui

This commit is contained in:
2026-04-16 01:03:24 +02:00
parent 8ea690f252
commit 513b77752e
2 changed files with 32 additions and 7 deletions

View File

@@ -35,7 +35,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 {
pub fn add(self: *RoadManager, allocator: std.mem.Allocator, start_node: *Node, end_node: *Node) !*Road {
const road: Road = .init(self.getNewID(), start_node, end_node);
self.roads.appendBounded(road) catch |err| {
@@ -47,6 +47,8 @@ pub const RoadManager = struct {
try start_node.referenceRoad(allocator, road_ref);
try end_node.referenceRoad(allocator, road_ref);
return road_ref;
}
/// Returns the reference to the last element in the list; returns null if there are no elements in the list

View File

@@ -156,7 +156,6 @@ pub const Simulator = struct {
c.HEIGHT - 2 * c.TEXT_SIZE,
c.TEXT_SIZE, .black);
// Displays the mode the simulation is currently in
rl.drawText(@tagName(self.mode), 10, c.HEIGHT - c.TEXT_SIZE, c.TEXT_SIZE, .black);
@@ -210,10 +209,14 @@ pub const Simulator = struct {
}
fn splitRoadsByPoints(self: *Simulator, intersections: []const st.IntersectionData, start: *const Node, end: *const Node) void {
_ = end; // autofix
_ = start; // autofix
// todo plan it out
if (intersections.len == 0) return;
const first_intersection_road = intersections[0];
const first_intersection_node = self.node_man.getSelectedNode(first_intersection_road.point);
const first_road = self.road_man.add(self.allocator, start, first_intersection_node) catch |err| {
std.debug.panic("Failed to create the first split road: {}\n", .{err});
};
first_intersection_node.referenceRoad(self.allocator, first_road);
// We have done that every intersection creates new node and reconnects existing connections
// TODO
@@ -233,11 +236,31 @@ pub const Simulator = struct {
// As is the end node's road reference
road_old_node.unreferenceRoad(intersection.road);
self.road_man.add(self.allocator, new_node, road_old_node) catch |err| {
std.debug.panic("Failed to create the new split road: {}\n", .{err});
_ = self.road_man.add(self.allocator, new_node, road_old_node) catch |err| {
std.debug.panic("Failed to create the last split road: {}\n", .{err});
};
if (i == intersections.len - 1) continue;
const connecting_road = self.road_man.add(
self.allocator,
self.node_man.getSelectedNode(intersection.point),
self.node_man.getSelectedNode(intersections[i+1].point)) catch |err| {
std.debug.panic("Failed to create the connecting road: {}\n", .{err});
};
// todo
// okay so here we connect the road to current and next node
// and then this should hopefully be it
// 20 new bugs incoming
}
const last_intersection_road = intersections[intersections.len - 1];
const last_intersection_node = self.node_man.getSelectedNode(last_intersection_road.point);
const road = self.road_man.add(self.allocator, last_intersection_node, end) catch |err| {
std.debug.panic("Failed to add the road: {}\n", .{err});
};
last_intersection_node.referenceRoad(self.allocator, road);
}
pub fn update(self: *Simulator) void {