const rl = @import("raylib"); const c = @import("../common/constants.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; 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 = ut.calculate_length(start.pos, end.pos); 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, display_info: 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); if (!display_info) return; var buf: [100]u8 = undefined; const entity = std.fmt.bufPrintZ(&buf, "{d}", .{self.id}) catch |err| { std.debug.panic("Could not allocate ID into string???: {}\n", .{err}); }; const distance = ut.getVectorP1P2(self.nodes[0].pos, self.nodes[1].pos); const entity_info_pos: rl.Vector2 = .{ .x = self.nodes[0].pos.x + distance.x / 2 - c.ROAD_SIZE / 2, .y = self.nodes[0].pos.y + distance.y / 2 - c.ROAD_SIZE / 2, }; rl.drawText(entity, @trunc(entity_info_pos.x), @trunc(entity_info_pos.y), c.ENTITY_DATA_TEXT_SIZE, c.ENTITY_DATA_TEXT_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 collides(self: *const Road, pos: rl.Vector2) bool { return rl.checkCollisionPointLine(pos, self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE); } /// Updates node reference old_node => new node; returns error if old_node does not exist pub fn updateNodeReference(self: *Road, old_node: *Node, new_node: *Node) !void { for (0..self.nodes.len) |i| { if (self.nodes[i] != old_node) continue; self.nodes[i] = new_node; // As nodes change, road's length must be recalculated self.length = ut.calculate_length(self.nodes[0].pos, self.nodes[1].pos); return; } return e.Entity.NotFound; } }; const std = @import("std"); const expect = std.testing.expect; test "valid road nodes" { // TODO rewrite } // TODO tests // test every case error for every function that can return an error