From c14ac533dc9b38d6194b5aa3b06fd71980639efc Mon Sep 17 00:00:00 2001 From: Marto Date: Sat, 25 Apr 2026 02:25:45 +0200 Subject: [PATCH] Implemented cancel building functionality --- .gitignore | 1 + infrastructure/node.odin | 7 ++++++- simulator.odin | 28 +++++++++++++++++++++------- 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4bab22b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +debug_build diff --git a/infrastructure/node.odin b/infrastructure/node.odin index 395c0be..0ee4c16 100644 --- a/infrastructure/node.odin +++ b/infrastructure/node.odin @@ -13,7 +13,7 @@ Node :: struct { roads: [dynamic]u32, } -// Node Initialisation +// Constructor node_init :: proc(new_pos: rl.Vector2) -> Node { return { enabled = true, @@ -22,6 +22,11 @@ node_init :: proc(new_pos: rl.Vector2) -> Node { } } +// Destructor +node_deinit :: proc(self: ^Node) { + delete(self.roads) +} + // Returns whether passed pos(ition) is within this node's snapping radius node_within_snapping_radius :: proc(self: ^Node, pos: rl.Vector2) -> bool { return rl.CheckCollisionPointCircle( diff --git a/simulator.odin b/simulator.odin index e6732b9..2586337 100644 --- a/simulator.odin +++ b/simulator.odin @@ -7,7 +7,6 @@ import "core:fmt" import inf "infrastructure" // PLAN AREA -// TODO implement right click 'cancel building functionali' // TODO implement deleting of roads Simulator :: struct { @@ -63,7 +62,11 @@ handle_keyboard_input :: proc(self: ^Simulator) { // Generally mouse event handler @(private="file") handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) { - if (rl.IsMouseButtonReleased(.LEFT)) do left_click_event(self, pos) + if (rl.IsMouseButtonReleased(.LEFT)) { + left_click_event(self, pos) + } else if (rl.IsMouseButtonReleased(.RIGHT)) { + right_click_event(self) + } } // Handles left click functionality @@ -82,6 +85,20 @@ left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) { self.temp_node_index = cur_node_index } +// Handles right click functionality +@(private="file") +right_click_event :: proc(self: ^Simulator) { + if self.temp_node_index == nil do return + index := self.temp_node_index.? + + temp_node := &self.nodes[index] + self.temp_node_index = nil + if len(temp_node.roads) > 0 do return + + inf.node_deinit(temp_node) + unordered_remove(&self.nodes, index) +} + // Main drawing function draw :: proc(self: ^Simulator, pos: rl.Vector2) { rl.ClearBackground(common.BACKGROUND_COLOUR) @@ -167,15 +184,13 @@ get_intersecting_roads :: proc(self: ^Simulator, start: u32, end: u32) -> []comm @(private="file") split_roads_by_points :: proc(self: ^Simulator, intersections: []common.Intersection_Data, start: u32, end: u32) { if len(intersections) == 0 { - road := inf.road_init(start, end) - append(&self.roads, road) + add_road(self, start, end) return } first_intersection_node := get_node_or_new(self, intersections[0].point) - road := inf.road_init(start, first_intersection_node) - append(&self.roads, road) + add_road(self, start, first_intersection_node) for i in 0..