diff --git a/src/common/structures.zig b/src/common/structures.zig index 96c64ac..1e5ac80 100644 --- a/src/common/structures.zig +++ b/src/common/structures.zig @@ -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: *Node, + road: *Road, }; /// Represents intersection data, mainly used in cases where we draw over existing roads diff --git a/src/infrastructure/node.zig b/src/infrastructure/node.zig index 1f484a0..781c3c5 100644 --- a/src/infrastructure/node.zig +++ b/src/infrastructure/node.zig @@ -42,8 +42,8 @@ pub const Node = struct { 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}); + const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{ self.id }) catch |err| { + std.debug.panic("Failed to allocate space for ID???: {}\n", .{ err }); }; const info_x = @as(i32, @trunc(self.pos.x)) - c.NODE_RADIUS / 2; diff --git a/src/main.zig b/src/main.zig index 228127b..f775471 100644 --- a/src/main.zig +++ b/src/main.zig @@ -20,7 +20,7 @@ pub fn main(init: std.process.Init) !void { const rand_impl: std.Random.IoSource = .{ .io = init.io }; - var sim: Simulator = .init(allocator, rand_impl); + var sim: Simulator = .init(allocator, &rand_impl); defer sim.deinit() catch |err| { std.debug.panic("Failed to deinitialise the sim: {}\n", .{err}); }; diff --git a/src/simulator.zig b/src/simulator.zig index fc9226b..a88bf0a 100644 --- a/src/simulator.zig +++ b/src/simulator.zig @@ -41,7 +41,7 @@ pub const Simulator = struct { random: std.Random, /// Constructor for convenience - pub fn init(new_allocator: std.mem.Allocator, rand_impl: std.Random.IoSource) Simulator { + pub fn init(new_allocator: std.mem.Allocator, rand_impl: *const std.Random.IoSource) Simulator { return .{ .allocator = new_allocator, .node_man = .init(), @@ -76,7 +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.car_man.draw(self.display_entity_info); self.drawRelatedSelectedEntities(); } @@ -103,7 +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); }, - // TODO car + .car => { + // TODO draw the origin and destination, connected by the pathfinding route + } } } @@ -201,6 +203,7 @@ pub const Simulator = struct { /// 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); } diff --git a/src/vehicles/car.zig b/src/vehicles/car.zig index 49dea70..fad13fb 100644 --- a/src/vehicles/car.zig +++ b/src/vehicles/car.zig @@ -1,15 +1,20 @@ const std = @import("std"); const rl = @import("raylib"); + +const c = @import("../common/constants.zig"); +const st = @import("../common/structures.zig"); const Node = @import("../infrastructure/node.zig").Node; pub const Car = struct { id: usize, pos: rl.Vector2, + inf: st.Infrastructure, - pub fn init(new_id: usize, spawn_node: *const Node) Car { + pub fn init(new_id: usize, spawn_node: *Node) Car { return .{ .id = new_id, .pos = spawn_node.pos, + .inf = .{ .node = spawn_node }, }; } @@ -17,7 +22,16 @@ pub const Car = struct { allocator.destroy(self); } - pub fn draw(self: *const Car) void { - rl.drawEllipseV(self.pos, 10, 20, .blue); + pub fn draw(self: *const Car, display_info: bool) void { + rl.drawEllipseV(self.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(self.pos.x), @trunc(self.pos.y), c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_COLOUR); } }; \ No newline at end of file diff --git a/src/vehicles/car_manager.zig b/src/vehicles/car_manager.zig index 1559317..712c36e 100644 --- a/src/vehicles/car_manager.zig +++ b/src/vehicles/car_manager.zig @@ -23,9 +23,9 @@ pub const CarManager = struct { self.cars.deinit(allocator); } - pub fn draw(self: *const CarManager) void { + pub fn draw(self: *const CarManager, display_info: bool) void { for (self.cars.items) |car| { - car.draw(); + car.draw(display_info); } } @@ -34,10 +34,19 @@ pub const CarManager = struct { return self.next_id; } - pub fn addCar(self: *CarManager, allocator: std.mem.Allocator, node_ref: *const Node) !void { + 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 { + // todo full impl + for (self.cars.items) |car| { + car.deinit(allocator); + } + self.cars.clearRetainingCapacity(); + self.next_id = 0; // reset + } }; \ No newline at end of file