Deletion implemented

This commit is contained in:
2026-04-29 15:52:33 +02:00
parent 750bad7f83
commit db16bafd6c
8 changed files with 171 additions and 22 deletions

View File

@@ -11,6 +11,7 @@ pub const Road = struct {
/// 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,
@@ -22,6 +23,11 @@ pub const Road = struct {
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];
@@ -34,8 +40,26 @@ pub const Road = struct {
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);
/// 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);
}
};