Added comments and additional keybind functionality (clearing all entities and drawing UI [debug] data)

This commit is contained in:
2026-04-07 11:24:11 +02:00
parent 9d217b427e
commit 92adc698f1
7 changed files with 74 additions and 13 deletions

View File

@@ -1,14 +1,19 @@
const std = @import("std");
const rl = @import("raylib");
const c = @import("constants.zig");
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
pub const Simulator = struct {
allocator: std.mem.Allocator,
/// Contains data and functions about nodes
node_man: NodeManager,
/// Contains data and functions about roads
road_man: RoadManager,
/// Tracks whether to display node snapping radius
display_details: bool,
/// Tracks whether to automatically start build a new road after the creation of previous one is finished
auto_continue: bool,
pub fn init(allocator: std.mem.Allocator) !Simulator {
@@ -26,33 +31,63 @@ pub const Simulator = struct {
self.road_man.deinit(self.allocator);
}
/// Input handling function that is exposed to the public
pub fn handleInput(self: *Simulator) void {
self.handleKeyboardInput();
self.handleMouseInput();
}
/// Every keyboard event is checked here
fn handleKeyboardInput(self: *Simulator) void {
self.display_details = rl.isKeyDown(.left_alt);
self.auto_continue = rl.isKeyDown(.left_control);
if (rl.isKeyReleased(.c)) {
self.node_man.temp_node = null;
self.road_man.roads.clearRetainingCapacity();
self.node_man.clear(self.allocator);
self.node_man.nodes.clearRetainingCapacity();
}
}
/// every mouse event is checked here
fn handleMouseInput(self: *Simulator) void {
const pos = rl.getMousePosition();
if (!rl.isMouseButtonReleased(.left)) return;
if (self.node_man.add(pos)) |node| {
self.road_man.add(self.allocator, self.node_man.temp_node.?, node) catch |err| {
std.debug.panic("Error while attempting to add a road:\n{}\n", .{err});
std.debug.panic("Error while attempting to add a road: {}\n", .{err});
};
self.node_man.temp_node = if (self.auto_continue) node else null;
}
}
/// The main drawing function that is exposed upwards
pub fn draw(self: *const Simulator) void {
self.road_man.draw();
self.node_man.draw(self.display_details);
self.drawUI();
rl.clearBackground(.light_gray);
}
fn drawUI(self: *const Simulator) void {
var buf: [1024]u8 = undefined;
const entities_list =
std.fmt.bufPrintZ(
&buf,
"Nodes: {d}\nRoads: {d}",
.{self.node_man.nodes.items.len, self.road_man.roads.items.len}) catch |err| {
std.debug.panic("Insufficient space for displaying entire entity list: {}\n", .{err});
};
rl.drawText(
entities_list,
c.WIDTH - 13 * @as(i32, @intCast(entities_list.len)),
c.HEIGHT - 2 * c.TEXT_SIZE,
c.TEXT_SIZE, .black);
}
};