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

@@ -2,6 +2,7 @@ const std = @import("std");
const Vector2 = @import("raylib").Vector2;
const c = @import("../constants.zig");
const e = @import("../errors.zig");
const Node = @import("node.zig").Node;
const Road = @import("road.zig").Road;
@@ -18,10 +19,9 @@ pub const NodeManager = struct {
};
}
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) void {
pub fn deinit(self: *NodeManager, allocator: std.mem.Allocator) !void {
for (self.nodes.items) |node| {
node.deinit(allocator);
allocator.destroy(node);
try node.deinit(allocator);
}
self.nodes.deinit(allocator);
}
@@ -36,7 +36,7 @@ pub const NodeManager = struct {
var cur_node = Node.init(0, pos);
// Temporary road that is to be drawn as one in the making
const road: Road = .init(0, node, &cur_node);
road.draw();
road.draw(false);
node.*.draw(c.NODE_TEMP_COLOUR);
cur_node.draw(c.NODE_CURSOR_COLOUR);
@@ -64,15 +64,28 @@ pub const NodeManager = struct {
return id;
}
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) void {
pub fn clear(self: *NodeManager, allocator: std.mem.Allocator) !void {
self.temp_node = null;
for (self.nodes.items) |node| {
node.deinit(allocator);
allocator.destroy(node);
try node.deinit(allocator);
}
self.nodes.clearRetainingCapacity();
self.next_id = 0;
}
pub fn deleteNode(self: *NodeManager, allocator: std.mem.Allocator, node_to_delete: *Node) !void {
if (node_to_delete.roads.items.len != 0) return e.Entity.HasReferences;
for (0..self.nodes.items.len) |i| {
if (self.nodes.items[i] != node_to_delete) continue;
try node_to_delete.deinit(allocator);
_ = self.nodes.swapRemove(i);
return;
}
return e.Entity.NotFound;
}
};
const expect = std.testing.expect;
@@ -101,4 +114,5 @@ test "id tracking" {
}
// TODO tests
// force resize pointer test
// force resize pointer test
// deleting nodes