Initial commit
This commit is contained in:
58
src/simulator.zig
Normal file
58
src/simulator.zig
Normal file
@@ -0,0 +1,58 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
|
||||
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
|
||||
|
||||
pub const Simulator = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
node_man: NodeManager,
|
||||
road_man: RoadManager,
|
||||
display_details: bool,
|
||||
auto_continue: bool,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) !Simulator {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.node_man = try .init(allocator),
|
||||
.road_man = try .init(allocator),
|
||||
.display_details = false,
|
||||
.auto_continue = false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Simulator) void {
|
||||
self.node_man.deinit(self.allocator);
|
||||
self.road_man.deinit(self.allocator);
|
||||
}
|
||||
|
||||
pub fn handleInput(self: *Simulator) void {
|
||||
self.handleKeyboardInput();
|
||||
self.handleMouseInput();
|
||||
}
|
||||
|
||||
fn handleKeyboardInput(self: *Simulator) void {
|
||||
self.display_details = rl.isKeyDown(.left_alt);
|
||||
self.auto_continue = rl.isKeyDown(.left_control);
|
||||
}
|
||||
|
||||
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});
|
||||
};
|
||||
|
||||
self.node_man.temp_node = if (self.auto_continue) node else null;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Simulator) void {
|
||||
self.road_man.draw();
|
||||
self.node_man.draw(self.display_details);
|
||||
|
||||
rl.clearBackground(.light_gray);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user