Code restructuring
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
debug_build
|
debug_build
|
||||||
base-road-network
|
base-road-network
|
||||||
|
src.bin
|
||||||
|
|||||||
46
src/draw.odin
Normal file
46
src/draw.odin
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
|
import "common"
|
||||||
|
|
||||||
|
// Main drawing function
|
||||||
|
draw :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
|
rl.ClearBackground(common.BACKGROUND_COLOUR)
|
||||||
|
|
||||||
|
// draw roads
|
||||||
|
for &road, index in self.roads {
|
||||||
|
start := road.nodes[0]
|
||||||
|
end := road.nodes[1]
|
||||||
|
|
||||||
|
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
|
||||||
|
for &node in self.nodes {
|
||||||
|
// draws the snapping radius if key is held down
|
||||||
|
if self.show_details do rl.DrawCircleV(node.pos, common.NODE_SNAP_RADIUS * common.NODE_RADIUS, common.NODE_SNAP_COLOUR)
|
||||||
|
// draws the node
|
||||||
|
rl.DrawCircleV(node.pos, common.NODE_RADIUS, common.NODE_DONE_COLOUR)
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw temp road if exists
|
||||||
|
if val, ok := self.temp_node.?; ok {
|
||||||
|
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)
|
||||||
|
}
|
||||||
90
src/entity_management.odin
Normal file
90
src/entity_management.odin
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
|
import "common"
|
||||||
|
import inf "infrastructure"
|
||||||
|
|
||||||
|
// This function only returns the index to the node or if it doesn't exist bool in the tuple is false
|
||||||
|
@private
|
||||||
|
get_node_index_if_exists :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, bool) {
|
||||||
|
for &node, index in self.nodes {
|
||||||
|
if inf.node_within_snapping_radius(&node, pos) do return u32(index), true
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Given position, the function will attempt the return the pointer to the node in near vicinity,
|
||||||
|
// or if unsuccesful manually creating the node based on the position in the list and then returning the pointer to it
|
||||||
|
@private
|
||||||
|
get_node_or_new :: proc(self: ^Simulator, pos: rl.Vector2) -> u32 {
|
||||||
|
if node, ok := get_node_index_if_exists(self, pos); ok do return node
|
||||||
|
|
||||||
|
node := inf.node_init(pos)
|
||||||
|
append(&self.nodes, node)
|
||||||
|
|
||||||
|
return u32(len(self.nodes) - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempts to update node reference to the road;
|
||||||
|
// Returns false if the old reference doesn't exist
|
||||||
|
@private
|
||||||
|
update_node_reference :: proc(self: ^Simulator, road_to_update: u32, old_ref: u32, new_ref: u32) -> bool {
|
||||||
|
road := &self.roads[road_to_update]
|
||||||
|
|
||||||
|
for i in 0..<len(road.nodes) {
|
||||||
|
if road.nodes[i] != old_ref do continue
|
||||||
|
|
||||||
|
road.nodes[i] = new_ref
|
||||||
|
inf.node_unreference_road(&self.nodes[old_ref], road_to_update)
|
||||||
|
append(&self.nodes[new_ref].roads, road_to_update)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function that allows deleting of any entity within the entity list (nodes, roads, etc.) while ensuring valid references
|
||||||
|
// Returns swapped entities if they exist
|
||||||
|
@private
|
||||||
|
delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) -> ([2]u32, bool) {
|
||||||
|
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: bool
|
||||||
|
|
||||||
|
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 {}, false
|
||||||
|
|
||||||
|
for &road in self.roads do inf.road_update_node_reference(&road, index_change[0], index_change[1])
|
||||||
|
return index_change, true
|
||||||
|
|
||||||
|
case .Road:
|
||||||
|
unordered_remove(&self.roads, entity_index)
|
||||||
|
if !swap_made do return {}, false
|
||||||
|
|
||||||
|
for &node in self.nodes do inf.node_update_road_reference(&node, index_change[0], index_change[1])
|
||||||
|
return index_change, true
|
||||||
|
}
|
||||||
|
|
||||||
|
return {}, false
|
||||||
|
}
|
||||||
58
src/input.odin
Normal file
58
src/input.odin
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
|
import inf "infrastructure"
|
||||||
|
|
||||||
|
// Public input function that gets called in graphics library loop
|
||||||
|
handle_input :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
|
handle_keyboard_input(self)
|
||||||
|
handle_mouse_input(self, pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// General keyboard input event handler
|
||||||
|
@(private="file")
|
||||||
|
handle_keyboard_input :: proc(self: ^Simulator) {
|
||||||
|
self.show_details = rl.IsKeyDown(.LEFT_ALT)
|
||||||
|
self.auto_continue = rl.IsKeyDown(.LEFT_CONTROL)
|
||||||
|
self.delete_mode = rl.IsKeyDown(.LEFT_SHIFT)
|
||||||
|
|
||||||
|
if (rl.IsKeyReleased(.C)) {
|
||||||
|
self.temp_node = nil
|
||||||
|
clear(&self.roads)
|
||||||
|
clear(&self.nodes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generally mouse event handler
|
||||||
|
@(private="file")
|
||||||
|
handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
|
if (rl.IsMouseButtonReleased(.LEFT)) {
|
||||||
|
left_click_event(self, pos)
|
||||||
|
} else if (rl.IsMouseButtonReleased(.RIGHT)) {
|
||||||
|
right_click_event(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles left click functionality
|
||||||
|
@(private="file")
|
||||||
|
left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
|
demolish_road(self)
|
||||||
|
create_road(self, pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handles right click functionality
|
||||||
|
@(private="file")
|
||||||
|
right_click_event :: proc(self: ^Simulator) {
|
||||||
|
if self.temp_node == nil do return
|
||||||
|
index := self.temp_node.?
|
||||||
|
|
||||||
|
temp_node := &self.nodes[index]
|
||||||
|
self.temp_node = nil
|
||||||
|
if len(temp_node.roads) > 0 do return
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
@@ -20,8 +20,9 @@ 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)
|
||||||
|
update(&sim, pos)
|
||||||
|
|
||||||
draw(&sim, pos)
|
draw(&sim, pos)
|
||||||
}
|
}
|
||||||
@@ -6,16 +6,13 @@ import "common"
|
|||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import inf "infrastructure"
|
import inf "infrastructure"
|
||||||
|
|
||||||
// PLAN AREA
|
|
||||||
// TODO implement deleting of roads
|
|
||||||
|
|
||||||
Simulator :: struct {
|
Simulator :: struct {
|
||||||
// Stores all nodes
|
// Stores all nodes
|
||||||
nodes: [dynamic]inf.Node,
|
nodes: [dynamic]inf.Node,
|
||||||
// Stores all roads
|
// Stores all roads
|
||||||
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: Maybe(u32),
|
||||||
// Tracks the selected road
|
// Tracks the selected road
|
||||||
highlighted_road: Maybe(u32),
|
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
|
||||||
@@ -28,7 +25,8 @@ Simulator :: struct {
|
|||||||
|
|
||||||
// Destructor
|
// Destructor
|
||||||
deinit :: proc(self: ^Simulator) {
|
deinit :: proc(self: ^Simulator) {
|
||||||
self.temp_node_index = nil
|
self.temp_node = nil
|
||||||
|
self.highlighted_road = nil
|
||||||
|
|
||||||
delete(self.roads)
|
delete(self.roads)
|
||||||
|
|
||||||
@@ -43,137 +41,29 @@ update :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|||||||
update_highlighted_road(self, pos)
|
update_highlighted_road(self, pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Public input function that gets called in graphics library loop
|
@private
|
||||||
handle_input :: proc(self: ^Simulator, pos: rl.Vector2) {
|
demolish_road :: proc(self: ^Simulator) {
|
||||||
handle_keyboard_input(self)
|
if !self.delete_mode do return
|
||||||
handle_mouse_input(self, pos)
|
|
||||||
}
|
if road, ok := self.highlighted_road.?; ok do delete_road(self, road)
|
||||||
|
|
||||||
// General keyboard input event handler
|
|
||||||
@(private="file")
|
|
||||||
handle_keyboard_input :: proc(self: ^Simulator) {
|
|
||||||
self.show_details = rl.IsKeyDown(.LEFT_ALT)
|
|
||||||
self.auto_continue = rl.IsKeyDown(.LEFT_CONTROL)
|
|
||||||
self.delete_mode = rl.IsKeyDown(.LEFT_SHIFT)
|
|
||||||
|
|
||||||
if (rl.IsKeyReleased(.C)) {
|
|
||||||
self.temp_node_index = nil
|
|
||||||
clear(&self.roads)
|
|
||||||
clear(&self.nodes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generally mouse event handler
|
|
||||||
@(private="file")
|
|
||||||
handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
||||||
if (rl.IsMouseButtonReleased(.LEFT)) {
|
|
||||||
left_click_event(self, pos)
|
|
||||||
} else if (rl.IsMouseButtonReleased(.RIGHT)) {
|
|
||||||
right_click_event(self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handles left click functionality
|
|
||||||
@(private="file")
|
|
||||||
left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
||||||
// DELETE
|
|
||||||
if road, ok := self.highlighted_road.?; ok && self.delete_mode {
|
|
||||||
delete_road(self, road)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@private
|
||||||
|
create_road :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
// BUILD
|
// 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.?; ok {
|
||||||
// If both values are identical this means the user has to create a new road using the same nodes
|
// 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
|
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)
|
||||||
|
|
||||||
self.temp_node_index = self.auto_continue ? cur_node_index : nil
|
self.temp_node = self.auto_continue ? cur_node_index : nil
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.temp_node_index = cur_node_index
|
self.temp_node = 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)
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Main drawing function
|
|
||||||
draw :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
||||||
rl.ClearBackground(common.BACKGROUND_COLOUR)
|
|
||||||
|
|
||||||
// draw roads
|
|
||||||
for &road, index in self.roads {
|
|
||||||
start := road.nodes[0]
|
|
||||||
end := road.nodes[1]
|
|
||||||
|
|
||||||
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
|
|
||||||
for &node in self.nodes {
|
|
||||||
// draws the snapping radius if key is held down
|
|
||||||
if self.show_details do rl.DrawCircleV(node.pos, common.NODE_SNAP_RADIUS * common.NODE_RADIUS, common.NODE_SNAP_COLOUR)
|
|
||||||
// draws the node
|
|
||||||
rl.DrawCircleV(node.pos, common.NODE_RADIUS, common.NODE_DONE_COLOUR)
|
|
||||||
}
|
|
||||||
|
|
||||||
// draw temp road if exists
|
|
||||||
if val, ok := self.temp_node_index.?; ok {
|
|
||||||
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
|
|
||||||
@(private="file")
|
|
||||||
get_node_index_if_exists :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, bool) {
|
|
||||||
for &node, index in self.nodes {
|
|
||||||
if inf.node_within_snapping_radius(&node, pos) do return u32(index), true
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Given position, the function will attempt the return the pointer to the node in near vicinity,
|
|
||||||
// or if unsuccesful manually creating the node based on the position in the list and then returning the pointer to it
|
|
||||||
@(private="file")
|
|
||||||
get_node_or_new :: proc(self: ^Simulator, pos: rl.Vector2) -> u32 {
|
|
||||||
if node, ok := get_node_index_if_exists(self, pos); ok do return node
|
|
||||||
|
|
||||||
node := inf.node_init(pos)
|
|
||||||
append(&self.nodes, node)
|
|
||||||
|
|
||||||
return u32(len(self.nodes) - 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns data about roads that intersect the given 2 nodes (points)
|
// Returns data about roads that intersect the given 2 nodes (points)
|
||||||
@@ -263,41 +153,6 @@ add_road :: proc(self: ^Simulator, start: u32, end: u32) {
|
|||||||
append(&self.nodes[end].roads, road_index)
|
append(&self.nodes[end].roads, road_index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempts to update node reference to the road;
|
|
||||||
// Returns false if the old reference doesn't exist
|
|
||||||
@(private="file")
|
|
||||||
update_node_reference :: proc(self: ^Simulator, road_to_update: u32, old_ref: u32, new_ref: u32) -> bool {
|
|
||||||
road := &self.roads[road_to_update]
|
|
||||||
|
|
||||||
for i in 0..<len(road.nodes) {
|
|
||||||
if road.nodes[i] != old_ref do continue
|
|
||||||
|
|
||||||
road.nodes[i] = new_ref
|
|
||||||
inf.node_unreference_road(&self.nodes[old_ref], road_to_update)
|
|
||||||
append(&self.nodes[new_ref].roads, road_to_update)
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
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")
|
@(private="file")
|
||||||
delete_road :: proc(self: ^Simulator, road_to_delete: u32) {
|
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
|
// First we need to unreference this road from surrounding nodes and then delete those nodes IF this was the last road connection
|
||||||
@@ -316,6 +171,7 @@ delete_road :: proc(self: ^Simulator, road_to_delete: u32) {
|
|||||||
|
|
||||||
// After the remove we have to replace references
|
// After the remove we have to replace references
|
||||||
if len(start_node.roads) == 0 {
|
if len(start_node.roads) == 0 {
|
||||||
|
// If the id of the next node gets swapped we must update it accordingly
|
||||||
if result, ok := delete_entity(self, road.nodes[0], .Node); ok && result[0] == road.nodes[1] {
|
if result, ok := delete_entity(self, road.nodes[0], .Node); ok && result[0] == road.nodes[1] {
|
||||||
road.nodes[1] = result[1]
|
road.nodes[1] = result[1]
|
||||||
}
|
}
|
||||||
@@ -324,47 +180,18 @@ delete_road :: proc(self: ^Simulator, road_to_delete: u32) {
|
|||||||
if len(end_node.roads) == 0 do delete_entity(self, road.nodes[1], .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
|
// Keeps track of the selected/highlighted road
|
||||||
// Returns swapped entities if they exist
|
@(private="file")
|
||||||
delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) -> ([2]u32, bool) {
|
update_highlighted_road :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
mlen: u32
|
for road, index in self.roads {
|
||||||
// Stores data about old and new index in case the deleted index is not last, meaning the swap occurs
|
start_node := self.nodes[road.nodes[0]]
|
||||||
index_change: [2]u32
|
end_node := self.nodes[road.nodes[1]]
|
||||||
// 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: bool
|
|
||||||
|
|
||||||
switch type {
|
if !rl.CheckCollisionPointLine(pos, start_node.pos, end_node.pos, common.ROAD_SIZE) do continue
|
||||||
case .Node:
|
|
||||||
mlen = u32(len(self.nodes))
|
self.highlighted_road = u32(index)
|
||||||
case .Road:
|
return
|
||||||
mlen = u32(len(self.roads))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
last := mlen - 1
|
self.highlighted_road = nil
|
||||||
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 {}, false
|
|
||||||
for &road in self.roads do inf.road_update_node_reference(&road, index_change[0], index_change[1])
|
|
||||||
|
|
||||||
return index_change, true
|
|
||||||
|
|
||||||
case .Road:
|
|
||||||
unordered_remove(&self.roads, entity_index)
|
|
||||||
|
|
||||||
if !swap_made do return {}, false
|
|
||||||
|
|
||||||
for &node in self.nodes do inf.node_update_road_reference(&node, index_change[0], index_change[1])
|
|
||||||
|
|
||||||
return index_change, true
|
|
||||||
}
|
|
||||||
|
|
||||||
return {}, false
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user