Implemented basic car support, and restructured few tests and other errors; errors when rng gets called TODO fix

This commit is contained in:
2026-05-02 08:55:18 +02:00
parent b388b1e08e
commit 3e6438418b
12 changed files with 129 additions and 75 deletions

View File

@@ -6,8 +6,10 @@ 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 Car = @import("vehicles/car.zig").Car;
const NodeManager = @import("infrastructure/node_manager.zig").NodeManager;
const RoadManager = @import("infrastructure/road_manager.zig").RoadManager;
const CarManager = @import("vehicles/car_manager.zig").CarManager;
pub const Simulator = struct {
/// allocator for convenience
@@ -16,6 +18,8 @@ pub const Simulator = struct {
node_man: NodeManager,
/// 'class' tracking all the roads (and appropriate functions)
road_man: RoadManager,
/// 'class' tracking all the cars (and appropriate functions)
car_man: CarManager,
// vars
/// Tracks whether next road will start building from the node the last road was built at
auto_continue: bool,
@@ -31,24 +35,30 @@ pub const Simulator = struct {
show_connections: bool,
/// Toggle that tracks whether ID (or possibly something more in the future) of every entity is displayed in GUI
display_entity_info: bool,
/// Entity (car/road/node) that is highlighed (hovered over by a mouse)
highlighted_entity: ?st.Entity,
/// Interface for RNG
random: std.Random,
/// Constructor for convenience
pub fn init(new_allocator: std.mem.Allocator) Simulator {
pub fn init(new_allocator: std.mem.Allocator, rand_impl: std.Random.IoSource) Simulator {
return .{
.allocator = new_allocator,
.node_man = .init(),
.road_man = .init(),
.car_man = .init(),
.auto_continue = false,
.delete_mode = false,
.show_connections = false,
.display_entity_info = false,
.highlighted_entity = null,
.random = rand_impl.interface(),
};
}
/// Deinitialisation of node and road objects
pub fn deinit(self: *Simulator) !void {
self.car_man.deinit(self.allocator);
self.road_man.deinit(self.allocator);
try self.node_man.deinit(self.allocator);
}
@@ -66,6 +76,7 @@ pub const Simulator = struct {
self.road_man.draw(highlighted_road, self.display_entity_info);
self.node_man.draw(pos, self.display_entity_info);
self.car_man.draw();
self.drawRelatedSelectedEntities();
}
@@ -92,6 +103,7 @@ pub const Simulator = struct {
road.nodes[0].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
road.nodes[1].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
},
// TODO car
}
}
@@ -112,8 +124,8 @@ pub const Simulator = struct {
self.delete_mode = rl.isKeyDown(.left_shift);
self.show_connections = rl.isKeyDown(.left_alt) and !self.delete_mode;
if (rl.isKeyReleased(.n)) self.createCar();
if (rl.isKeyReleased(.tab)) self.display_entity_info = !self.display_entity_info;
if (rl.isKeyReleased(.c)) self.clear() catch |err| {
std.debug.panic("Failed to clear the entities: {}\n", .{err});
};
@@ -132,16 +144,16 @@ pub const Simulator = struct {
/// Function that handles functionality that executes upon left click
fn leftClickEvent(self: *Simulator, pos: rl.Vector2) void {
if (self.delete_mode and self.highlighted_entity != null and self.highlighted_entity.? == .road) {
self.delete_road() catch |err| {
self.deleteRoad() catch |err| {
std.debug.panic("Failed to delete the road: {}\n", .{err});
};
return;
}
self.new_road(pos);
self.createRoad(pos);
}
/// User initiated road building functionality
fn new_road(self: *Simulator, pos: rl.Vector2) void {
fn createRoad(self: *Simulator, pos: rl.Vector2) void {
if (self.show_connections) return;
const cur_node = self.node_man.getSelectedNode(self.allocator, pos) catch |err| {
std.debug.panic("Failed to append the newly created node at pos ({d}, {d}) to node list: {}\n", .{
@@ -167,7 +179,7 @@ pub const Simulator = struct {
}
/// User initiated road destroying functionality
fn delete_road(self: *Simulator) !void {
fn deleteRoad(self: *Simulator) !void {
// We can trust this because this only gets called if valid and if type is road
std.debug.assert(self.highlighted_entity != null and self.highlighted_entity.? == .road);
const h_road = self.highlighted_entity.?.road;
@@ -380,4 +392,17 @@ pub const Simulator = struct {
std.debug.panic("Failed to create a road of final intersection and end origin node: {}\n", .{err});
};
}
/// Creates a car and calls
fn createCar(self: *Simulator) void {
const nodes_len = self.node_man.nodes.items.len;
if (nodes_len == 0) return;
// Grab random node
const i = self.random.uintLessThan(usize, nodes_len);
const ref_node = self.node_man.nodes.items[i];
self.car_man.addCar(self.allocator, ref_node) catch |err| {
std.debug.panic("Unable to create a car or append it to the list of cars: {}\n", .{err});
};
}
};