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, /// Constructor, which sets nodes for the road and calculates its length 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; } /// This makes the road ptr DEAD pub fn deinit(self: *Road, allocator: std.mem.Allocator) void { allocator.destroy(self); } /// 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); } /// Simple function which draws the node /// /// In the future as we improve and make roads more complex with multiple lanes and such /// it will gradually become more complex pub fn draw(self: *const Road, highlighted: bool) void { const colour = if (highlighted) c.ROAD_HIGHLIGHTED_COLOUR else c.ROAD_COLOUR; rl.drawLineEx(self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE, colour); } /// Important: after this function executes, this road is no longer reachable from its bounding nodes /// /// The function unreferences self (road) from its bounding nodes pub fn unreferenceNodes(self: *const Road) !void { try self.nodes[0].unreferenceRoad(self); try self.nodes[1].unreferenceRoad(self); } /// Checks whether pos coordinate is on the referenced road pub fn isHighlighted(self: *const Road, pos: rl.Vector2) bool { return rl.checkCollisionPointLine(pos, self.nodes[0].*.pos, self.nodes[1].*.pos, c.ROAD_SIZE); } }; 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); }