Implemented cancel building functionality

This commit is contained in:
2026-04-25 02:25:45 +02:00
parent 064ef7f63e
commit c14ac533dc
3 changed files with 28 additions and 8 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
debug_build

View File

@@ -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(

View File

@@ -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..<len(intersections) {
intersection := intersections[i]
@@ -206,7 +221,6 @@ split_roads_by_points :: proc(self: ^Simulator, intersections: []common.Intersec
last_intersection_road := intersections[len(intersections) - 1]
last_intersection_node := get_node_or_new(self, last_intersection_road.point)
add_road(self, last_intersection_node, end)
}