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

@@ -2,9 +2,13 @@ const std = @import("std");
const rl = @import("raylib");
const c = @import("constants.zig");
const st = @import("structures.zig");
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
const Node = @import("infrastructure/node.zig").Node;
pub const Simulator = struct {
allocator: std.mem.Allocator,
/// Contains data and functions about nodes
@@ -15,6 +19,9 @@ pub const Simulator = struct {
display_details: bool,
/// Tracks whether to automatically start build a new road after the creation of previous one is finished
auto_continue: bool,
/// Displays intersection data for easier implementation and debugging
debug_intersection_data: std.ArrayList(st.IntersectionData),
mode: st.Mode,
pub fn init(allocator: std.mem.Allocator) !Simulator {
return .{
@@ -23,12 +30,15 @@ pub const Simulator = struct {
.road_man = try .init(allocator),
.display_details = false,
.auto_continue = false,
.debug_intersection_data = .empty,
.mode = .VISUAL,
};
}
pub fn deinit(self: *Simulator) void {
self.node_man.deinit(self.allocator);
self.road_man.deinit(self.allocator);
self.debug_intersection_data.deinit(self.allocator);
}
/// Input handling function that is exposed to the public
@@ -42,50 +52,72 @@ pub const Simulator = struct {
self.display_details = rl.isKeyDown(.left_alt);
self.auto_continue = rl.isKeyDown(.left_control);
if (rl.isKeyReleased(.c)) {
if (rl.isKeyReleased(.c) and self.mode == .DELETE) {
self.debug_intersection_data.clearRetainingCapacity();
self.node_man.temp_node = null;
self.road_man.roads.clearRetainingCapacity();
self.node_man.clear(self.allocator);
self.node_man.nodes.clearRetainingCapacity();
}
self.mode = switch (rl.getKeyPressed()) {
.v => .VISUAL,
.d => .DELETE,
.b => .BUILD,
else => self.mode,
};
}
/// every mouse event is checked here
fn handleMouseInput(self: *Simulator) void {
const pos = rl.getMousePosition();
if (rl.isMouseButtonReleased(.left)) {
if (self.mode == .BUILD and rl.isMouseButtonReleased(.left)) {
self.leftClickEvent(pos);
}
else if (rl.isMouseButtonReleased(.right)) self.rightClickEvent();
else if (self.mode == .BUILD and rl.isMouseButtonReleased(.right)) {
if (self.node_man.temp_node) |node| {
// todo
self.node_man.removeNode(node) catch {};
}
}
else if (self.mode == .DELETE and rl.isMouseButtonReleased(.left) and self.road_man.highlighted_road != null) {
// todo
self.road_man.remove(self.road_man.highlighted_road.?) catch |err| {
std.debug.panic("Failed to remove the road: {}\n", .{err});
};
}
}
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
if (self.node_man.add(pos)) |node| {
if (self.node_man.temp_node.?.id == node.id) return;
const temp = self.node_man.temp_node.?;
if (temp.id == node.id) return;
self.road_man.add(self.allocator, self.node_man.temp_node.?, node) catch |err| {
self.road_man.add(self.allocator, temp, node) catch |err| {
std.debug.panic("Error while attempting to add a road: {}\n", .{err});
};
// get intersections made
self.getIntersectingRoads(temp, node) catch |err| {
std.debug.panic("Failed to save intersection data: {}\n", .{err});
};
self.node_man.temp_node = if (self.auto_continue) node else null;
}
}
fn rightClickEvent(self: *Simulator) void {
if (self.node_man.temp_node) |node| {
self.node_man.removeNode(node);
self.node_man.temp_node = null;
}
}
/// The main drawing function that is exposed upwards
pub fn draw(self: *const Simulator) void {
self.road_man.draw();
pub fn draw(self: *Simulator) void {
self.road_man.draw(self.mode == .DELETE);
self.node_man.draw(self.display_details);
self.drawUI();
// Display intersection 'nodes'
for (self.debug_intersection_data.items) |intersection| {
rl.drawCircleV(intersection.point, c.NODE_RADIUS / 2, .red);
}
rl.clearBackground(.light_gray);
}
@@ -93,6 +125,7 @@ pub const Simulator = struct {
fn drawUI(self: *const Simulator) void {
var buf: [1024]u8 = undefined;
// Displays how many nodes and roads are currently in the lists
const entities_list =
std.fmt.bufPrintZ(
&buf,
@@ -106,5 +139,31 @@ pub const Simulator = struct {
c.WIDTH - 13 * @as(i32, @intCast(entities_list.len)),
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);
}
/// Gets list of pointers of all roads that 'collide' with the road bounded by the nodes we pass into it
fn getIntersectingRoads(self: *Simulator, start: *const Node, end: *const Node) !void {
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,
.point = collision_point,
};
const node: Node = .init(0, intersection.point);
// here we need to check if the points captured already are already within the reach
for (self.debug_intersection_data.items) |collision| {
if (node.withinSnappingRadius(collision.point))
continue :outer;
}
if (self.node_man.getNodeWithinRadius(intersection.point) == null) try self.debug_intersection_data.append(self.allocator, intersection);
}
}
};