Compare commits
8 Commits
fdf672de4b
...
car-pathfi
| Author | SHA1 | Date | |
|---|---|---|---|
| eca857cda1 | |||
| 439482e5a1 | |||
| 3e6438418b | |||
| b388b1e08e | |||
| 4f62d5eb4c | |||
| 3274692631 | |||
| 1558d5f57c | |||
| c02b2a5121 |
11
build.zig
11
build.zig
@@ -28,7 +28,7 @@ pub fn build(b: *std.Build) void {
|
||||
// to our consumers. We must give it a name because a Zig package can expose
|
||||
// multiple modules and consumers will need to be able to specify which
|
||||
// module they want to access.
|
||||
// const mod = b.addModule("base_road_network", .{
|
||||
// const mod = b.addModule("traffic-simulator", .{
|
||||
// // The root source file is the "entry point" of this module. Users of
|
||||
// // this module will only be able to access public declarations contained
|
||||
// // in this file, which means that if you have declarations that you
|
||||
@@ -68,7 +68,7 @@ pub fn build(b: *std.Build) void {
|
||||
// If neither case applies to you, feel free to delete the declaration you
|
||||
// don't need and to put everything under a single module.
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "base_road_network",
|
||||
.name = "traffic-simulator",
|
||||
.use_llvm = true,
|
||||
.root_module = b.createModule(.{
|
||||
// b.createModule defines a new module just like b.addModule but,
|
||||
@@ -84,12 +84,12 @@ pub fn build(b: *std.Build) void {
|
||||
// List of modules available for import in source files part of the
|
||||
// root module.
|
||||
.imports = &.{
|
||||
// Here "base_road_network" is the name you will use in your source code to
|
||||
// import this module (e.g. `@import("base_road_network")`). The name is
|
||||
// Here "traffic-simulator" is the name you will use in your source code to
|
||||
// import this module (e.g. `@import("traffic-simulator")`). The name is
|
||||
// repeated because you are allowed to rename your imports, which
|
||||
// can be extremely useful in case of collisions (which can happen
|
||||
// importing modules from different packages).
|
||||
// .{ .name = "base_road_network", .module = mod },
|
||||
// .{ .name = "traffic-simulator", .module = mod },
|
||||
|
||||
// Raylib import
|
||||
.{ .name = "raylib", .module = raylib },
|
||||
@@ -132,6 +132,7 @@ pub fn build(b: *std.Build) void {
|
||||
}
|
||||
|
||||
const tests = b.addTest(.{
|
||||
.use_llvm = true,
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/test.zig"),
|
||||
.target = target,
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//
|
||||
// It is redundant to include "zig" in this name because it is already
|
||||
// within the Zig package namespace.
|
||||
.name = .base_road_network,
|
||||
.name = .traffic_simulator,
|
||||
// This is a [Semantic Version](https://semver.org/).
|
||||
// In a future version of Zig it will be used for package deduplication.
|
||||
.version = "0.0.0",
|
||||
@@ -22,7 +22,7 @@
|
||||
// original project's identity. Thus it is recommended to leave the comment
|
||||
// on the following line intact, so that it shows up in code reviews that
|
||||
// modify the field.
|
||||
.fingerprint = 0x8da3e26c9def0629, // Changing this has security and trust implications.
|
||||
.fingerprint = 0xf441f200e5d3277c, // Changing this has security and trust implications.
|
||||
// Tracks the earliest Zig version that the package considers to be a
|
||||
// supported use case.
|
||||
.minimum_zig_version = "0.16.0",
|
||||
|
||||
@@ -31,3 +31,5 @@ pub const TEXT_SIZE = 50;
|
||||
pub const ENTITY_DATA_TEXT_SIZE = TEXT_SIZE / 2;
|
||||
/// Text colour in which entity IDs are displayed if toggled
|
||||
pub const ENTITY_DATA_TEXT_COLOUR = clr.orange;
|
||||
|
||||
pub const CAR_SIZE = 20;
|
||||
|
||||
@@ -2,16 +2,24 @@ const Vector2 = @import("raylib").Vector2;
|
||||
|
||||
const Road = @import("../infrastructure/road.zig").Road;
|
||||
const Node = @import("../infrastructure/node.zig").Node;
|
||||
const Car = @import("../vehicles/car.zig").Car;
|
||||
|
||||
/// This is a simple equivalent to something like abstract class, so the simulator class has only one variable
|
||||
/// which tracks which object is highlighted and the logic that seeks to find the current highlighted class doesn't need
|
||||
/// to figure out which highlighted entity has priority in case of overlap;
|
||||
/// given that car can be on node and road, and node itself is on road(s)
|
||||
///
|
||||
/// TLDR: Can point to either entity type (node, road, etc.)
|
||||
/// TLDR: Can point to either `entity` type (node, road, etc.)
|
||||
pub const Entity = union(enum) {
|
||||
node: *Node,
|
||||
road: *Road,
|
||||
car: *Car,
|
||||
};
|
||||
|
||||
/// Similar to `Entity` but only for infrastructure entities, so road and node only (for now)
|
||||
pub const Infrastructure = union(enum) {
|
||||
node: *const Node,
|
||||
road: *const Road,
|
||||
};
|
||||
|
||||
/// Represents intersection data, mainly used in cases where we draw over existing roads
|
||||
|
||||
@@ -2,7 +2,7 @@ const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("../common/constants.zig");
|
||||
const e = @import("../errors.zig");
|
||||
const e = @import("../common/errors.zig");
|
||||
const st = @import("../common/structures.zig");
|
||||
const ut = @import("../common/utils.zig");
|
||||
const Road = @import("road.zig").Road;
|
||||
|
||||
@@ -2,7 +2,7 @@ const std = @import("std");
|
||||
const Vector2 = @import("raylib").Vector2;
|
||||
|
||||
const c = @import("../common/constants.zig");
|
||||
const e = @import("../errors.zig");
|
||||
const e = @import("../common/errors.zig");
|
||||
const Node = @import("node.zig").Node;
|
||||
const Road = @import("road.zig").Road;
|
||||
|
||||
@@ -79,10 +79,8 @@ pub const NodeManager = struct {
|
||||
|
||||
/// Gets next id, resets only on clear()
|
||||
fn getNextID(self: *NodeManager) usize {
|
||||
const id = self.next_id;
|
||||
self.next_id += 1;
|
||||
|
||||
return id;
|
||||
defer self.next_id += 1;
|
||||
return self.next_id;
|
||||
}
|
||||
|
||||
/// Clears all existing nodes connected, not deinitialisation
|
||||
@@ -97,7 +95,7 @@ pub const NodeManager = struct {
|
||||
}
|
||||
|
||||
/// Deletes node, returns error if node still has road references
|
||||
pub fn deleteNode(self: *NodeManager, allocator: std.mem.Allocator, node_to_delete: *Node) !void {
|
||||
pub fn removeNode(self: *NodeManager, allocator: std.mem.Allocator, node_to_delete: *Node) !void {
|
||||
for (0..self.nodes.items.len) |i| {
|
||||
if (self.nodes.items[i] != node_to_delete) continue;
|
||||
|
||||
@@ -128,7 +126,7 @@ pub const NodeManager = struct {
|
||||
self.temp_node = null;
|
||||
|
||||
if (node.roads.items.len != 0) return;
|
||||
self.deleteNode(allocator, node) catch |err| {
|
||||
self.removeNode(allocator, node) catch |err| {
|
||||
std.debug.panic("Failed to delete the temporary node: {}\n", .{err});
|
||||
};
|
||||
|
||||
@@ -143,7 +141,9 @@ test "id tracking" {
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var node_man: NodeManager = .init();
|
||||
defer node_man.deinit(allocator);
|
||||
defer node_man.deinit(allocator) catch |err| {
|
||||
std.debug.panic("Failed to deinit nodemanager: {}\n", .{err});
|
||||
};
|
||||
const n = 5;
|
||||
|
||||
for (0..n) |_| {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("../common/constants.zig");
|
||||
const e = @import("../errors.zig");
|
||||
const e = @import("../common/errors.zig");
|
||||
const st = @import("../common/structures.zig");
|
||||
const ut = @import("../common/utils.zig");
|
||||
const Node = @import("node.zig").Node;
|
||||
@@ -100,30 +100,7 @@ const std = @import("std");
|
||||
const expect = std.testing.expect;
|
||||
|
||||
test "valid road nodes" {
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const start: Node = .init(34, .{.x = 500, .y = 500});
|
||||
const start_ptr = try allocator.create(Node);
|
||||
defer {
|
||||
start_ptr.deinit(allocator);
|
||||
allocator.destroy(start_ptr);
|
||||
}
|
||||
start_ptr.* = start;
|
||||
|
||||
const end: Node = .init(227, .{.x = 600, .y = 500});
|
||||
const end_ptr = try allocator.create(Node);
|
||||
defer {
|
||||
end_ptr.deinit(allocator);
|
||||
allocator.destroy(end_ptr);
|
||||
}
|
||||
end_ptr.* = end;
|
||||
|
||||
const road: Road = .init(11, start_ptr, end_ptr);
|
||||
|
||||
try expect(road.nodes[0].id == 34);
|
||||
try expect(road.nodes[1].id == 227);
|
||||
// TODO rewrite
|
||||
}
|
||||
|
||||
// TODO tests
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const e = @import("../errors.zig");
|
||||
const e = @import("../common/errors.zig");
|
||||
const st = @import("../common/structures.zig");
|
||||
const ut = @import("../common/utils.zig");
|
||||
const Road = @import("road.zig").Road;
|
||||
@@ -48,10 +48,8 @@ pub const RoadManager = struct {
|
||||
|
||||
/// Returns the id, and increases it by one; used for generating ID's for new entities
|
||||
fn getNextID(self: *RoadManager) usize {
|
||||
const id = self.next_id;
|
||||
self.next_id += 1;
|
||||
|
||||
return id;
|
||||
defer self.next_id += 1;
|
||||
return self.next_id;
|
||||
}
|
||||
|
||||
/// Deinits all the roads, clears them but not deiniting the list itself; also resets the next ID var
|
||||
@@ -99,36 +97,7 @@ const Vector2 = @import("raylib").Vector2;
|
||||
const expect = std.testing.expect;
|
||||
|
||||
test "id tracking" {
|
||||
var gpa: std.heap.DebugAllocator(.{}) = .init;
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
var road_man: RoadManager = .init();
|
||||
defer road_man.deinit(allocator);
|
||||
const n = 5;
|
||||
|
||||
const start: Node = .init(0, .{.x = 0, .y = 0});
|
||||
const start_ptr = try allocator.create(Node);
|
||||
start_ptr.* = start;
|
||||
|
||||
const end: Node = .init(1, .{.x = 100, .y = 100});
|
||||
const end_ptr = try allocator.create(Node);
|
||||
end_ptr.* = end;
|
||||
|
||||
defer {
|
||||
start_ptr.deinit(allocator);
|
||||
end_ptr.deinit(allocator);
|
||||
|
||||
allocator.destroy(start_ptr);
|
||||
allocator.destroy(end_ptr);
|
||||
}
|
||||
|
||||
for (0..n) |_| {
|
||||
try road_man.addRoad(allocator, start_ptr, end_ptr);
|
||||
}
|
||||
|
||||
try expect(road_man.next_id == n);
|
||||
try expect(road_man.roads.items.len == n);
|
||||
// TODO rewrite
|
||||
}
|
||||
|
||||
// TODO tests
|
||||
|
||||
17
src/main.zig
17
src/main.zig
@@ -11,14 +11,16 @@ pub fn main(init: std.process.Init) !void {
|
||||
.msaa_4x_hint = true,
|
||||
.window_highdpi = true,
|
||||
});
|
||||
rl.initWindow(c.WIDTH, c.HEIGHT, "Base Road Network");
|
||||
rl.initWindow(c.WIDTH, c.HEIGHT, "Traffic Simulator");
|
||||
defer rl.closeWindow();
|
||||
|
||||
const monitor = 0;
|
||||
rl.setWindowMonitor(monitor);
|
||||
rl.setTargetFPS(rl.getMonitorRefreshRate(monitor));
|
||||
|
||||
var sim: Simulator = .init(allocator);
|
||||
const rand_impl: std.Random.IoSource = .{ .io = init.io };
|
||||
|
||||
var sim: Simulator = .init(allocator, &rand_impl);
|
||||
defer sim.deinit() catch |err| {
|
||||
std.debug.panic("Failed to deinitialise the sim: {}\n", .{err});
|
||||
};
|
||||
@@ -34,3 +36,14 @@ pub fn main(init: std.process.Init) !void {
|
||||
sim.draw(pos);
|
||||
}
|
||||
}
|
||||
|
||||
test "test rng" {
|
||||
const io = std.testing.io;
|
||||
const rng_impl: std.Random.IoSource = .{ .io = io };
|
||||
const rng = rng_impl.interface();
|
||||
|
||||
const n_max = 1000;
|
||||
const n_rng = rng.uintLessThan(usize, n_max);
|
||||
|
||||
std.debug.print("{d}\n", .{n_rng});
|
||||
}
|
||||
@@ -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: *const 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.display_entity_info);
|
||||
|
||||
self.drawRelatedSelectedEntities();
|
||||
}
|
||||
@@ -92,6 +103,9 @@ 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);
|
||||
},
|
||||
.car => {
|
||||
// TODO draw the origin and destination, connected by the pathfinding route
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,8 +126,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 +146,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", .{
|
||||
@@ -157,14 +171,6 @@ pub const Simulator = struct {
|
||||
std.debug.panic("Intersection selection failure: {}\n", .{err});
|
||||
};
|
||||
defer self.allocator.free(intersections);
|
||||
|
||||
// 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.splitRoadsByIntersections(intersections, temp, cur_node);
|
||||
|
||||
self.node_man.temp_node = if (self.auto_continue) cur_node else null;
|
||||
@@ -175,7 +181,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;
|
||||
@@ -183,20 +189,31 @@ pub const Simulator = struct {
|
||||
const start_node = h_road.nodes[0];
|
||||
const end_node = h_road.nodes[1];
|
||||
|
||||
self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
|
||||
std.debug.panic("Road deletion failed: {}\n", .{err});
|
||||
};
|
||||
try self.road_man.deleteRoad(self.allocator, h_road);
|
||||
|
||||
if (start_node.roads.items.len == 0)
|
||||
try self.node_man.deleteNode(self.allocator, start_node);
|
||||
if (start_node.roads.items.len == 0) {
|
||||
const cars = try self.car_man.getCarsOnInf(self.allocator, .{ .node = start_node });
|
||||
defer self.allocator.free(cars);
|
||||
|
||||
if (end_node.roads.items.len == 0)
|
||||
try self.node_man.deleteNode(self.allocator, end_node);
|
||||
// TODO replace with removeMultipleCars
|
||||
for (self.car_man.cars.items) |car| {
|
||||
try self.car_man.removeCar(self.allocator, car);
|
||||
}
|
||||
|
||||
try self.node_man.removeNode(self.allocator, start_node);
|
||||
}
|
||||
|
||||
if (end_node.roads.items.len == 0) {
|
||||
// TODO same as above
|
||||
try self.node_man.removeNode(self.allocator, end_node);
|
||||
// TODO after this is done, do the same with the function that removes roads
|
||||
}
|
||||
}
|
||||
|
||||
/// Clearing node and road lists without deinitialising them (only the children)
|
||||
fn clear(self: *Simulator) !void {
|
||||
self.highlighted_entity = null;
|
||||
self.car_man.clear(self.allocator);
|
||||
self.road_man.clear(self.allocator);
|
||||
try self.node_man.clear(self.allocator);
|
||||
}
|
||||
@@ -388,4 +405,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});
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -3,4 +3,5 @@ test {
|
||||
_ = @import("infrastructure/road.zig");
|
||||
_ = @import("infrastructure/node_manager.zig");
|
||||
_ = @import("infrastructure/road_manager.zig");
|
||||
_ = @import("main.zig");
|
||||
}
|
||||
55
src/vehicles/car.zig
Normal file
55
src/vehicles/car.zig
Normal file
@@ -0,0 +1,55 @@
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("../common/constants.zig");
|
||||
const st = @import("../common/structures.zig");
|
||||
const ut = @import("../common/utils.zig");
|
||||
const Node = @import("../infrastructure/node.zig").Node;
|
||||
|
||||
pub const Car = struct {
|
||||
/// Car ID, for easier debugging and identification
|
||||
id: usize,
|
||||
/// When car progresses along the road its location is determined via entity id and progress
|
||||
progress: f32,
|
||||
/// Tracks which infrastructure (pointer) the car is currently located at
|
||||
inf: st.Infrastructure,
|
||||
|
||||
pub fn init(new_id: usize, spawn_node: *Node) Car {
|
||||
return .{
|
||||
.id = new_id,
|
||||
.progress = 0,
|
||||
.inf = .{ .node = spawn_node },
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Car, allocator: std.mem.Allocator) void {
|
||||
allocator.destroy(self);
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Car, display_info: bool) void {
|
||||
var pos: rl.Vector2 = undefined;
|
||||
|
||||
switch (self.inf) {
|
||||
.node => |node| pos = node.pos,
|
||||
.road => |road| {
|
||||
const road_vector = ut.getVectorP1P2(road.nodes[0].pos, road.nodes[1].pos);
|
||||
pos = .{
|
||||
.x = road_vector.x / 2,
|
||||
.y = road_vector.y / 2,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
rl.drawEllipseV(pos, 20, 15, .red);
|
||||
|
||||
if (!display_info) return;
|
||||
|
||||
var buf: [100]u8 = undefined;
|
||||
const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{ self.id }) catch |err| {
|
||||
std.debug.panic("Failed to allocate space for ID???: {}\n", .{ err });
|
||||
};
|
||||
|
||||
|
||||
rl.drawText(entity_info, @trunc(pos.x), @trunc(pos.y), c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_COLOUR);
|
||||
}
|
||||
};
|
||||
95
src/vehicles/car_manager.zig
Normal file
95
src/vehicles/car_manager.zig
Normal file
@@ -0,0 +1,95 @@
|
||||
const std = @import("std");
|
||||
const Vector2 = @import("raylib").Vector2;
|
||||
|
||||
const e = @import("../common/errors.zig");
|
||||
const ut = @import("../common/structures.zig");
|
||||
const Node = @import("../infrastructure/node.zig").Node;
|
||||
const Car = @import("car.zig").Car;
|
||||
|
||||
pub const CarManager = struct {
|
||||
next_id: usize,
|
||||
cars: std.ArrayList(*Car),
|
||||
|
||||
pub fn init() CarManager {
|
||||
return .{
|
||||
.next_id = 0,
|
||||
.cars = .empty,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *CarManager, allocator: std.mem.Allocator) void {
|
||||
for (self.cars.items) |car| {
|
||||
car.deinit(allocator);
|
||||
}
|
||||
|
||||
self.cars.deinit(allocator);
|
||||
}
|
||||
|
||||
pub fn draw(self: *const CarManager, display_info: bool) void {
|
||||
for (self.cars.items) |car| {
|
||||
car.draw(display_info);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getNextID(self: *CarManager) usize {
|
||||
defer self.next_id += 1;
|
||||
return self.next_id;
|
||||
}
|
||||
|
||||
pub fn addCar(self: *CarManager, allocator: std.mem.Allocator, node_ref: *Node) !void {
|
||||
const car_ptr = try allocator.create(Car);
|
||||
car_ptr.* = .init(self.getNextID(), node_ref);
|
||||
|
||||
try self.cars.append(allocator, car_ptr);
|
||||
}
|
||||
|
||||
pub fn clear(self: *CarManager, allocator: std.mem.Allocator) void {
|
||||
for (self.cars.items) |car| {
|
||||
car.deinit(allocator);
|
||||
}
|
||||
self.cars.clearRetainingCapacity();
|
||||
self.next_id = 0;
|
||||
}
|
||||
|
||||
/// This function returns array of cars which are located on the node/road specified in the `inf` parameter
|
||||
pub fn getCarsOnInf(self: *const CarManager, allocator: std.mem.Allocator, inf: ut.Infrastructure) ![]*Car {
|
||||
var cars: std.ArrayList(*Car) = .empty;
|
||||
|
||||
const active_inf = std.meta.activeTag(inf);
|
||||
|
||||
for (self.cars.items) |car| {
|
||||
if (std.meta.activeTag(car.inf) != active_inf) continue;
|
||||
|
||||
switch (car.inf) {
|
||||
inline else => |a, tag| {
|
||||
const b = @field(inf, @tagName(tag));
|
||||
if (a == b) try cars.append(allocator, car);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cars.toOwnedSlice(allocator);
|
||||
}
|
||||
|
||||
/// Attempts to deinit and remove the (passed) car;
|
||||
/// returns an error if car doesn't exist in the list
|
||||
pub fn removeCar(self: *CarManager, allocator: std.mem.Allocator, car_to_delete: *Car) !void {
|
||||
for (self.cars.items, 0..) |car, index| {
|
||||
if (car != car_to_delete) continue;
|
||||
|
||||
car.deinit(allocator);
|
||||
_ = self.cars.swapRemove(index);
|
||||
return;
|
||||
}
|
||||
|
||||
return e.Entity.NotFound;
|
||||
}
|
||||
|
||||
/// TODO
|
||||
pub fn removeMultipleCars(self: *CarManager, allocator: std.mem.Allocator, cars_to_delete: []*Car) !void {
|
||||
_ = cars_to_delete; // autofix
|
||||
_ = allocator; // autofix
|
||||
_ = self; // autofix
|
||||
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user