Using implicit pointer derefs + fixing crashes on clearing entities
This commit is contained in:
@@ -44,7 +44,7 @@ pub const NodeManager = struct {
|
|||||||
const road: Road = .init(0, node, &cur_node);
|
const road: Road = .init(0, node, &cur_node);
|
||||||
road.draw(false);
|
road.draw(false);
|
||||||
|
|
||||||
node.*.draw(c.NODE_TEMP_COLOUR);
|
node.draw(c.NODE_TEMP_COLOUR);
|
||||||
cur_node.draw(c.NODE_CURSOR_COLOUR);
|
cur_node.draw(c.NODE_CURSOR_COLOUR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,6 +77,7 @@ pub const NodeManager = struct {
|
|||||||
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void {
|
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void {
|
||||||
self.temp_node = null;
|
self.temp_node = null;
|
||||||
for (self.nodes.items) |node| {
|
for (self.nodes.items) |node| {
|
||||||
|
node.roads.clearRetainingCapacity();
|
||||||
try node.deinit(allocator);
|
try node.deinit(allocator);
|
||||||
}
|
}
|
||||||
self.nodes.clearRetainingCapacity();
|
self.nodes.clearRetainingCapacity();
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ pub const Road = struct {
|
|||||||
const start = self.nodes[0];
|
const start = self.nodes[0];
|
||||||
const end = self.nodes[1];
|
const end = self.nodes[1];
|
||||||
|
|
||||||
const x_diff = end.*.pos.x - start.*.pos.x;
|
const x_diff = end.pos.x - start.pos.x;
|
||||||
const y_diff = end.*.pos.y - start.*.pos.y;
|
const y_diff = end.pos.y - start.pos.y;
|
||||||
const square_diff = x_diff * x_diff + y_diff * y_diff;
|
const square_diff = x_diff * x_diff + y_diff * y_diff;
|
||||||
|
|
||||||
return @sqrt(square_diff);
|
return @sqrt(square_diff);
|
||||||
@@ -46,7 +46,7 @@ pub const Road = struct {
|
|||||||
/// it will gradually become more complex
|
/// it will gradually become more complex
|
||||||
pub fn draw(self: *const Road, highlighted: bool) void {
|
pub fn draw(self: *const Road, highlighted: bool) void {
|
||||||
const colour = if (highlighted) c.ROAD_HIGHLIGHTED_COLOUR else c.ROAD_COLOUR;
|
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);
|
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
|
/// Important: after this function executes, this road is no longer reachable from its bounding nodes
|
||||||
@@ -59,7 +59,7 @@ pub const Road = struct {
|
|||||||
|
|
||||||
/// Checks whether pos coordinate is on the referenced road
|
/// Checks whether pos coordinate is on the referenced road
|
||||||
pub fn isHighlighted(self: *const Road, pos: rl.Vector2) bool {
|
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);
|
return rl.checkCollisionPointLine(pos, self.nodes[0].pos, self.nodes[1].pos, c.ROAD_SIZE);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ test "valid road nodes" {
|
|||||||
const start: Node = .init(34, .{.x = 500, .y = 500});
|
const start: Node = .init(34, .{.x = 500, .y = 500});
|
||||||
const start_ptr = try allocator.create(Node);
|
const start_ptr = try allocator.create(Node);
|
||||||
defer {
|
defer {
|
||||||
start_ptr.*.deinit(allocator);
|
start_ptr.deinit(allocator);
|
||||||
allocator.destroy(start_ptr);
|
allocator.destroy(start_ptr);
|
||||||
}
|
}
|
||||||
start_ptr.* = start;
|
start_ptr.* = start;
|
||||||
@@ -82,7 +82,7 @@ test "valid road nodes" {
|
|||||||
const end: Node = .init(227, .{.x = 600, .y = 500});
|
const end: Node = .init(227, .{.x = 600, .y = 500});
|
||||||
const end_ptr = try allocator.create(Node);
|
const end_ptr = try allocator.create(Node);
|
||||||
defer {
|
defer {
|
||||||
end_ptr.*.deinit(allocator);
|
end_ptr.deinit(allocator);
|
||||||
allocator.destroy(end_ptr);
|
allocator.destroy(end_ptr);
|
||||||
}
|
}
|
||||||
end_ptr.* = end;
|
end_ptr.* = end;
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ pub const Simulator = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (self.node_man.temp_node) |temp| {
|
if (self.node_man.temp_node) |temp| {
|
||||||
if (temp.*.id == cur_node.*.id) return;
|
if (temp.id == cur_node.id) return;
|
||||||
self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| {
|
self.road_man.addRoad(self.allocator, temp, cur_node) catch |err| {
|
||||||
std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err});
|
std.debug.panic("Failed to add a new road or assigning its nodes: {}\n", .{err});
|
||||||
};
|
};
|
||||||
@@ -107,8 +107,8 @@ pub const Simulator = struct {
|
|||||||
if (self.road_man.highlighted_road == null) return;
|
if (self.road_man.highlighted_road == null) return;
|
||||||
const h_road = self.road_man.highlighted_road.?;
|
const h_road = self.road_man.highlighted_road.?;
|
||||||
|
|
||||||
const start_node = h_road.*.nodes[0];
|
const start_node = h_road.nodes[0];
|
||||||
const end_node = h_road.*.nodes[1];
|
const end_node = h_road.nodes[1];
|
||||||
|
|
||||||
self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
|
self.road_man.deleteRoad(self.allocator, h_road) catch |err| {
|
||||||
std.debug.panic("Road deletion failed: {}\n", .{err});
|
std.debug.panic("Road deletion failed: {}\n", .{err});
|
||||||
|
|||||||
Reference in New Issue
Block a user