Car fundamentals implemented
This commit is contained in:
@@ -2,16 +2,24 @@ const Vector2 = @import("raylib").Vector2;
|
|||||||
|
|
||||||
const Road = @import("../infrastructure/road.zig").Road;
|
const Road = @import("../infrastructure/road.zig").Road;
|
||||||
const Node = @import("../infrastructure/node.zig").Node;
|
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
|
/// 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
|
/// 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;
|
/// 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)
|
/// 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) {
|
pub const Entity = union(enum) {
|
||||||
node: *Node,
|
node: *Node,
|
||||||
road: *Road,
|
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
|
/// Represents intersection data, mainly used in cases where we draw over existing roads
|
||||||
|
|||||||
@@ -42,8 +42,8 @@ pub const Node = struct {
|
|||||||
if (!display_info) return;
|
if (!display_info) return;
|
||||||
|
|
||||||
var buf: [100]u8 = undefined;
|
var buf: [100]u8 = undefined;
|
||||||
const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{self.id}) catch |err| {
|
const entity_info = std.fmt.bufPrintZ(&buf, "{d}", .{ self.id }) catch |err| {
|
||||||
std.debug.panic("Failed to allocate space for ID???: {}\n", .{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;
|
const info_x = @as(i32, @trunc(self.pos.x)) - c.NODE_RADIUS / 2;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
|
|
||||||
const rand_impl: std.Random.IoSource = .{ .io = init.io };
|
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| {
|
defer sim.deinit() catch |err| {
|
||||||
std.debug.panic("Failed to deinitialise the sim: {}\n", .{err});
|
std.debug.panic("Failed to deinitialise the sim: {}\n", .{err});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ pub const Simulator = struct {
|
|||||||
random: std.Random,
|
random: std.Random,
|
||||||
|
|
||||||
/// Constructor for convenience
|
/// 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 .{
|
return .{
|
||||||
.allocator = new_allocator,
|
.allocator = new_allocator,
|
||||||
.node_man = .init(),
|
.node_man = .init(),
|
||||||
@@ -76,7 +76,7 @@ pub const Simulator = struct {
|
|||||||
|
|
||||||
self.road_man.draw(highlighted_road, self.display_entity_info);
|
self.road_man.draw(highlighted_road, self.display_entity_info);
|
||||||
self.node_man.draw(pos, 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();
|
self.drawRelatedSelectedEntities();
|
||||||
}
|
}
|
||||||
@@ -103,7 +103,9 @@ pub const Simulator = struct {
|
|||||||
road.nodes[0].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
|
road.nodes[0].draw(c.NODE_RELATED_COLOUR, self.display_entity_info);
|
||||||
road.nodes[1].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)
|
/// Clearing node and road lists without deinitialising them (only the children)
|
||||||
fn clear(self: *Simulator) !void {
|
fn clear(self: *Simulator) !void {
|
||||||
self.highlighted_entity = null;
|
self.highlighted_entity = null;
|
||||||
|
self.car_man.clear(self.allocator);
|
||||||
self.road_man.clear(self.allocator);
|
self.road_man.clear(self.allocator);
|
||||||
try self.node_man.clear(self.allocator);
|
try self.node_man.clear(self.allocator);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const rl = @import("raylib");
|
const rl = @import("raylib");
|
||||||
|
|
||||||
|
const c = @import("../common/constants.zig");
|
||||||
|
const st = @import("../common/structures.zig");
|
||||||
const Node = @import("../infrastructure/node.zig").Node;
|
const Node = @import("../infrastructure/node.zig").Node;
|
||||||
|
|
||||||
pub const Car = struct {
|
pub const Car = struct {
|
||||||
id: usize,
|
id: usize,
|
||||||
pos: rl.Vector2,
|
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 .{
|
return .{
|
||||||
.id = new_id,
|
.id = new_id,
|
||||||
.pos = spawn_node.pos,
|
.pos = spawn_node.pos,
|
||||||
|
.inf = .{ .node = spawn_node },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,7 +22,16 @@ pub const Car = struct {
|
|||||||
allocator.destroy(self);
|
allocator.destroy(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(self: *const Car) void {
|
pub fn draw(self: *const Car, display_info: bool) void {
|
||||||
rl.drawEllipseV(self.pos, 10, 20, .blue);
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -23,9 +23,9 @@ pub const CarManager = struct {
|
|||||||
self.cars.deinit(allocator);
|
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| {
|
for (self.cars.items) |car| {
|
||||||
car.draw();
|
car.draw(display_info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,10 +34,19 @@ pub const CarManager = struct {
|
|||||||
return self.next_id;
|
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);
|
const car_ptr = try allocator.create(Car);
|
||||||
car_ptr.* = .init(self.getNextID(), node_ref);
|
car_ptr.* = .init(self.getNextID(), node_ref);
|
||||||
|
|
||||||
try self.cars.append(allocator, car_ptr);
|
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
|
||||||
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user