Partial implementation of road deletion

This commit is contained in:
2026-04-25 13:16:50 +02:00
parent ac78f526df
commit edce3f67a6
6 changed files with 153 additions and 22 deletions

View File

@@ -19,15 +19,19 @@ NODE_SNAP_RADIUS :: 3
TEXT_SIZE :: 50 TEXT_SIZE :: 50
// Default road colour // Default road colour
ROAD_COLOUR :: rl.BLACK ROAD_COLOUR :: rl.BLACK
// Highlighted road colour
ROAD_HIGHLIGHT_COLOUR :: rl.GREEN
// Node colour once it's fully built // Node colour once it's fully built
NODE_DONE_COLOUR :: rl.BROWN NODE_DONE_COLOUR :: rl.BROWN
// Node colour while node is being built // Node colour while node is being built
NODE_BUILD_COLOUR :: rl.GOLD NODE_BUILD_COLOUR :: rl.ORANGE
// Node colour while being able to start building but not doing that yet // Node colour while being able to start building but not doing that yet
NODE_CURSOR_COLOUR :: rl.BLUE NODE_CURSOR_COLOUR :: rl.BLUE
// The colour of the overlay displaying the snap radius // The colour of the overlay displaying the snap radius
NODE_SNAP_COLOUR :: rl.PINK NODE_SNAP_COLOUR :: rl.PINK
// The default colour of the text displayed
TEXT_COLOUR :: rl.BLACK
// Background Colour // Background Colour
BACKGROUND_COLOUR :: rl.LIGHTGRAY BACKGROUND_COLOUR :: rl.LIGHTGRAY

View File

@@ -8,4 +8,9 @@ Intersection_Data :: struct {
road: u32, road: u32,
// The exact point of intersection // The exact point of intersection
point: rl.Vector2, point: rl.Vector2,
}
Entity :: enum {
Node,
Road,
} }

View File

@@ -46,5 +46,16 @@ node_unreference_road :: proc(self: ^Node, road_to_unref: u32) -> bool {
return true return true
} }
return false
}
node_update_road_reference :: proc(self: ^Node, old_ref: u32, new_ref: u32) -> bool {
for &road in self.roads {
if road != old_ref do continue
road = new_ref
return true
}
return false return false
} }

View File

@@ -10,4 +10,16 @@ road_init :: proc(start: u32, end: u32) -> Road {
return { return {
nodes = {start, end} nodes = {start, end}
} }
}
// Updates existing node reference to a new one; returns false if old ref was not found
road_update_node_reference :: proc(self: ^Road, old_ref: u32, new_ref: u32) -> bool {
for &node in self.nodes {
if node != old_ref do continue
node = new_ref
return true
}
return false
} }

View File

@@ -12,7 +12,7 @@ main :: proc() {
rl.SetWindowMonitor(common.MONITOR) rl.SetWindowMonitor(common.MONITOR)
rl.SetTargetFPS(rl.GetMonitorRefreshRate(common.MONITOR)) rl.SetTargetFPS(rl.GetMonitorRefreshRate(common.MONITOR))
sim := init() sim: Simulator
defer deinit(&sim) defer deinit(&sim)
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
@@ -20,6 +20,7 @@ main :: proc() {
defer rl.EndDrawing() defer rl.EndDrawing()
pos := rl.GetMousePosition() pos := rl.GetMousePosition()
update(&sim, pos)
handle_input(&sim, pos) handle_input(&sim, pos)
draw(&sim, pos) draw(&sim, pos)

View File

@@ -8,8 +8,6 @@ import inf "infrastructure"
// PLAN AREA // PLAN AREA
// TODO implement deleting of roads // TODO implement deleting of roads
// TODO text/debug
// TODO full colour constant utilisation
Simulator :: struct { Simulator :: struct {
// Stores all nodes // Stores all nodes
@@ -18,21 +16,14 @@ Simulator :: struct {
roads: [dynamic]inf.Road, roads: [dynamic]inf.Road,
// Tracks the temporary node location // Tracks the temporary node location
temp_node_index: Maybe(u32), temp_node_index: Maybe(u32),
// Tracks the selected road
highlighted_road: Maybe(u32),
// Tracks whether the user wishes to see node's snapping radius // Tracks whether the user wishes to see node's snapping radius
show_details: bool, show_details: bool,
// Tracks whether after placing a road new one will start being placed // Tracks whether after placing a road new one will start being placed
auto_continue: bool, auto_continue: bool,
} // Tracks whether the delete mode is activated
delete_mode: bool,
// Constructor
init :: proc() -> Simulator {
return {
nodes = nil,
roads = nil,
temp_node_index = nil,
show_details = false,
auto_continue = false,
}
} }
// Destructor // Destructor
@@ -47,6 +38,11 @@ deinit :: proc(self: ^Simulator) {
delete(self.nodes) delete(self.nodes)
} }
// Functionality that runs every tick regardless of input
update :: proc(self: ^Simulator, pos: rl.Vector2) {
update_highlighted_road(self, pos)
}
// Public input function that gets called in graphics library loop // Public input function that gets called in graphics library loop
handle_input :: proc(self: ^Simulator, pos: rl.Vector2) { handle_input :: proc(self: ^Simulator, pos: rl.Vector2) {
handle_keyboard_input(self) handle_keyboard_input(self)
@@ -58,6 +54,7 @@ handle_input :: proc(self: ^Simulator, pos: rl.Vector2) {
handle_keyboard_input :: proc(self: ^Simulator) { handle_keyboard_input :: proc(self: ^Simulator) {
self.show_details = rl.IsKeyDown(.LEFT_ALT) self.show_details = rl.IsKeyDown(.LEFT_ALT)
self.auto_continue = rl.IsKeyDown(.LEFT_CONTROL) self.auto_continue = rl.IsKeyDown(.LEFT_CONTROL)
self.delete_mode = rl.IsKeyDown(.LEFT_SHIFT)
if (rl.IsKeyReleased(.C)) { if (rl.IsKeyReleased(.C)) {
self.temp_node_index = nil self.temp_node_index = nil
@@ -79,9 +76,15 @@ handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) {
// Handles left click functionality // Handles left click functionality
@(private="file") @(private="file")
left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) { left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) {
// DELETE
if road, ok := self.highlighted_road.?; ok && self.delete_mode do delete_road(self, road)
// BUILD
cur_node_index := get_node_or_new(self, pos) cur_node_index := get_node_or_new(self, pos)
if temp, ok := self.temp_node_index.?; ok { if temp, ok := self.temp_node_index.?; ok {
// If both values are identical this means the user has to create a new road using the same nodes
if cur_node_index == temp do return
data := get_intersecting_roads(self, temp, cur_node_index) data := get_intersecting_roads(self, temp, cur_node_index)
split_roads_by_points(self, data, temp, cur_node_index) split_roads_by_points(self, data, temp, cur_node_index)
@@ -103,6 +106,8 @@ right_click_event :: proc(self: ^Simulator) {
if len(temp_node.roads) > 0 do return if len(temp_node.roads) > 0 do return
inf.node_deinit(temp_node) inf.node_deinit(temp_node)
// We can safely call the remove here as the only way it will get deleted is if it's the only node aka it was created during our creation process
// Consequently this means that it will always be on last place and never swapped with anything
unordered_remove(&self.nodes, index) unordered_remove(&self.nodes, index)
} }
@@ -111,11 +116,16 @@ draw :: proc(self: ^Simulator, pos: rl.Vector2) {
rl.ClearBackground(common.BACKGROUND_COLOUR) rl.ClearBackground(common.BACKGROUND_COLOUR)
// draw roads // draw roads
for &road in self.roads { for &road, index in self.roads {
start := road.nodes[0] start := road.nodes[0]
end := road.nodes[1] end := road.nodes[1]
rl.DrawLineEx(self.nodes[start].pos, self.nodes[end].pos, common.ROAD_SIZE, common.ROAD_COLOUR) road_colour: rl.Color
if road, ok := self.highlighted_road.?; ok && road == u32(index) && self.delete_mode {
road_colour = common.ROAD_HIGHLIGHT_COLOUR
} else do road_colour = common.ROAD_COLOUR
rl.DrawLineEx(self.nodes[start].pos, self.nodes[end].pos, common.ROAD_SIZE, road_colour)
} }
// draw nodes // draw nodes
@@ -129,7 +139,16 @@ draw :: proc(self: ^Simulator, pos: rl.Vector2) {
// draw temp road if exists // draw temp road if exists
if val, ok := self.temp_node_index.?; ok { if val, ok := self.temp_node_index.?; ok {
rl.DrawLineEx(self.nodes[val].pos, pos, common.ROAD_SIZE, common.ROAD_COLOUR) rl.DrawLineEx(self.nodes[val].pos, pos, common.ROAD_SIZE, common.ROAD_COLOUR)
rl.DrawCircleV(pos, common.NODE_RADIUS, common.NODE_BUILD_COLOUR)
} }
draw_ui(self)
}
@(private="file")
draw_ui :: proc(self: ^Simulator) {
entity_count := fmt.ctprintf("Nodes: %d\nRoads: %d", len(self.nodes), len(self.roads))
rl.DrawText(entity_count, i32(common.WIDTH - 13 * len(entity_count)), common.HEIGHT - 2 * common.TEXT_SIZE, common.TEXT_SIZE, common.TEXT_COLOUR)
} }
// This function only returns the index to the node or if it doesn't exist bool in the tuple is false // This function only returns the index to the node or if it doesn't exist bool in the tuple is false
@@ -192,7 +211,6 @@ get_intersecting_roads :: proc(self: ^Simulator, start: u32, end: u32) -> []comm
split_roads_by_points :: proc(self: ^Simulator, intersections: []common.Intersection_Data, start: u32, end: u32) { split_roads_by_points :: proc(self: ^Simulator, intersections: []common.Intersection_Data, start: u32, end: u32) {
if len(intersections) == 0 { if len(intersections) == 0 {
add_road(self, start, end) add_road(self, start, end)
return return
} }
@@ -259,4 +277,84 @@ update_node_reference :: proc(self: ^Simulator, road_to_update: u32, old_ref: u3
} }
return false return false
}
// Keeps track of the selected/highlighted road
@(private="file")
update_highlighted_road :: proc(self: ^Simulator, pos: rl.Vector2) {
for road, index in self.roads {
start_node := self.nodes[road.nodes[0]]
end_node := self.nodes[road.nodes[1]]
if !rl.CheckCollisionPointLine(pos, start_node.pos, end_node.pos, common.ROAD_SIZE) do continue
self.highlighted_road = u32(index)
return
}
self.highlighted_road = nil
}
@(private="file")
delete_road :: proc(self: ^Simulator, road_to_delete: u32) {
// First we need to unreference this road from surrounding nodes and then delete those nodes IF this was the last road connection
road := self.roads[road_to_delete]
// Pointers to the nodes bordering the road we wish to delete
start_node := &self.nodes[road.nodes[0]]
end_node := &self.nodes[road.nodes[1]]
// We unreference the road from the nodes bordering the road
start_unref := inf.node_unreference_road(start_node, road_to_delete)
end_unref := inf.node_unreference_road(end_node, road_to_delete)
if !start_unref || !end_unref do fmt.panicf("Failed to unreference one (or both) of the nodes of the Road ID=%d\n", road_to_delete)
// Now we delete the road
delete_entity(self, road_to_delete, .Road)
// After the remove we have to replace references
// TODO implement in delete_entity to update references if first element is deleted
if len(start_node.roads) == 0 do delete_entity(self, road.nodes[0], .Node)
if len(end_node.roads) == 0 do delete_entity(self, road.nodes[1], .Node)
}
// Function that allows deleting of any entity within the entity list (nodes, roads, etc.) while ensuring valid references
delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) {
mlen: u32
// Stores data about old and new index in case the deleted index is not last, meaning the swap occurs
index_change: [2]u32
// Tracks whether the removal of node/road will cause a swap in the (dynamic) array
// and thus forcing the pre-swapped reference to be updated
swap_made := false
switch type {
case .Node:
mlen = u32(len(self.nodes))
case .Road:
mlen = u32(len(self.roads))
}
last := mlen - 1
if entity_index != last {
index_change = {last, entity_index}
swap_made = true
}
switch type {
case .Node:
unordered_remove(&self.nodes, entity_index)
if !swap_made do return
for &road in self.roads {
ok := inf.road_update_node_reference(&road, index_change[0], index_change[1])
if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0])
}
case .Road:
unordered_remove(&self.roads, entity_index)
if !swap_made do return
for &node in self.nodes {
ok := inf.node_update_road_reference(&node, index_change[0], index_change[1])
if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0])
}
}
} }