Setting up groundwork for vehicle (car) and pathfinding implementation
This commit is contained in:
98
src/infrastructure_helpers.odin
Normal file
98
src/infrastructure_helpers.odin
Normal file
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
import "common"
|
||||
import inf "infrastructure"
|
||||
import v "vehicles"
|
||||
|
||||
// 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))
|
||||
case .Car:
|
||||
mlen = u32(len(self.cars))
|
||||
}
|
||||
|
||||
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])
|
||||
for &car in self.cars do v.car_update_node_reference(&car, 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
|
||||
case .Car:
|
||||
unordered_remove(&self.cars, entity_index)
|
||||
if !swap_made do return {}, false
|
||||
// So far NOT needed as we don't reference the cars anywhere YET
|
||||
// In the future this might be the cause for failure!!!
|
||||
}
|
||||
|
||||
return {}, false
|
||||
}
|
||||
Reference in New Issue
Block a user