Splitting road implementation preparation
This commit is contained in:
@@ -3,7 +3,9 @@ const rl = @import("raylib");
|
||||
|
||||
const c = @import("common/constants.zig");
|
||||
const st = @import("common/structures.zig");
|
||||
const ut = @import("common/utils.zig");
|
||||
const Road = @import("infrastructure/road.zig").Road;
|
||||
const Node = @import("infrastructure/node.zig").Node;
|
||||
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
|
||||
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
|
||||
|
||||
@@ -143,11 +145,25 @@ pub const Simulator = struct {
|
||||
};
|
||||
|
||||
if (self.node_man.temp_node) |temp| {
|
||||
// Prevents the road from being attached to 2 identical nodes (0 length road)
|
||||
if (temp.id == cur_node.id) return;
|
||||
|
||||
// TODO replace with road splitting
|
||||
self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| {
|
||||
std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err});
|
||||
};
|
||||
|
||||
const intersections = self.getIntersectingRoads(self.allocator, temp, cur_node) catch |err| {
|
||||
std.debug.panic("Intersection selection failure: {}\n", .{err});
|
||||
};
|
||||
// DEBUG TODO REMOVE
|
||||
std.debug.print("Displaying intersection position and the intersected road:\n", .{});
|
||||
for (0..intersections.len) |i| {
|
||||
const int = intersections[i];
|
||||
std.debug.print("Road ID={d} Pos: ({d}, {d})\n", .{int.road.id, int.pos.x, int.pos.y});
|
||||
}
|
||||
|
||||
|
||||
self.node_man.temp_node = if (self.auto_continue) cur_node else null;
|
||||
return;
|
||||
}
|
||||
@@ -196,4 +212,46 @@ pub const Simulator = struct {
|
||||
|
||||
self.highlighted_entity = null;
|
||||
}
|
||||
|
||||
fn getIntersectingRoads(self: *const Simulator, allocator: std.mem.Allocator, start: *const Node, end: *const Node) ![]st.IntersectionData {
|
||||
var intersections: std.ArrayList(st.IntersectionData) = .empty;
|
||||
var collision_point: rl.Vector2 = undefined;
|
||||
|
||||
outer: for (self.road_man.roads.items) |road| {
|
||||
if (!rl.checkCollisionLines(start.pos, end.pos, road.nodes[0].pos, road.nodes[1].pos, &collision_point))
|
||||
continue;
|
||||
|
||||
const intersection = st.IntersectionData {
|
||||
.road = road,
|
||||
.pos = collision_point,
|
||||
};
|
||||
|
||||
// We put a 0 here, just to satisfy the constructor function
|
||||
const node: Node = .init(0, intersection.pos);
|
||||
// If the newly acquired intersection node is within the snapping radius of already existing nodes,
|
||||
// we don't add it to the list
|
||||
for (intersections.items) |inter_collision| {
|
||||
if (node.withinSnapRadius(inter_collision.pos)) continue :outer;
|
||||
}
|
||||
|
||||
// If there is an existing node that covers our position within its snapping radius,
|
||||
// then such position will not be saved as intersection
|
||||
if (self.node_man.getNodeIfExists(node.pos) != null) continue :outer;
|
||||
|
||||
try intersections.append(allocator, intersection);
|
||||
}
|
||||
|
||||
const sorted_intersection = try intersections.toOwnedSlice(allocator);
|
||||
std.sort.block(st.IntersectionData, sorted_intersection, start, ut.compareIntersections);
|
||||
|
||||
return sorted_intersection;
|
||||
}
|
||||
|
||||
fn splitRoadsByIntersections(self: *Simulator, intersections: st.IntersectionData, start: *Node, end: *Node) void {
|
||||
_ = intersections; // autofix
|
||||
_ = end; // autofix
|
||||
_ = start; // autofix
|
||||
_ = self; // autofix
|
||||
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user