Initial commit
This commit is contained in:
40
src/infrastructure/road.zig
Normal file
40
src/infrastructure/road.zig
Normal file
@@ -0,0 +1,40 @@
|
||||
const rl = @import("raylib");
|
||||
|
||||
const c = @import("../constants.zig");
|
||||
const Node = @import("node.zig").Node;
|
||||
|
||||
pub const Road = struct {
|
||||
/// Road ID, used for identification, particularly as we'll access all entities via pointers
|
||||
id: usize,
|
||||
/// Pointers to the nodes that encapsulated our road
|
||||
nodes: [2]*Node,
|
||||
/// Calculated road length
|
||||
length: f32,
|
||||
|
||||
pub fn init(new_id: usize, start: *Node, end: *Node) Road {
|
||||
var road: Road = .{
|
||||
.id = new_id,
|
||||
.nodes = .{start, end},
|
||||
.length = 0,
|
||||
};
|
||||
|
||||
road.length = road.calculate_length();
|
||||
return road;
|
||||
}
|
||||
|
||||
/// Calculates length of the road by taking its two nodes
|
||||
fn calculate_length(self: *const Road) f32 {
|
||||
const start = self.nodes[0];
|
||||
const end = self.nodes[1];
|
||||
|
||||
const x_diff = end.*.pos.x - start.*.pos.x;
|
||||
const y_diff = end.*.pos.y - start.*.pos.y;
|
||||
const square_diff = x_diff * x_diff + y_diff * y_diff;
|
||||
|
||||
return @sqrt(square_diff);
|
||||
}
|
||||
|
||||
pub fn draw(self: *const Road) void {
|
||||
rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, c.ROAD_COLOUR);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user