197 lines
6.5 KiB
Odin
197 lines
6.5 KiB
Odin
package main
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
import "common"
|
|
import "core:fmt"
|
|
import inf "infrastructure"
|
|
|
|
Simulator :: struct {
|
|
// Stores all nodes
|
|
nodes: [dynamic]inf.Node,
|
|
// Stores all roads
|
|
roads: [dynamic]inf.Road,
|
|
// Tracks the temporary node location
|
|
temp_node: Maybe(u32),
|
|
// Tracks the selected road
|
|
highlighted_road: Maybe(u32),
|
|
// Tracks whether the user wishes to see node's snapping radius
|
|
show_details: bool,
|
|
// Tracks whether after placing a road new one will start being placed
|
|
auto_continue: bool,
|
|
// Tracks whether the delete mode is activated
|
|
delete_mode: bool,
|
|
}
|
|
|
|
// Destructor
|
|
deinit :: proc(self: ^Simulator) {
|
|
self.temp_node = nil
|
|
self.highlighted_road = nil
|
|
|
|
delete(self.roads)
|
|
|
|
for &node in self.nodes {
|
|
inf.node_deinit(&node)
|
|
}
|
|
delete(self.nodes)
|
|
}
|
|
|
|
// Functionality that runs every tick regardless of input
|
|
update :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
update_highlighted_road(self, pos)
|
|
}
|
|
|
|
@private
|
|
demolish_road :: proc(self: ^Simulator) {
|
|
if !self.delete_mode do return
|
|
|
|
if road, ok := self.highlighted_road.?; ok do delete_road(self, road)
|
|
}
|
|
|
|
@private
|
|
create_road :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
// BUILD
|
|
cur_node_index := get_node_or_new(self, pos)
|
|
|
|
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 cur_node_index == temp do return
|
|
data := get_intersecting_roads(self, temp, cur_node_index)
|
|
split_roads_by_points(self, data, temp, cur_node_index)
|
|
|
|
self.temp_node = self.auto_continue ? cur_node_index : nil
|
|
return
|
|
}
|
|
|
|
self.temp_node = cur_node_index
|
|
}
|
|
|
|
// Returns data about roads that intersect the given 2 nodes (points)
|
|
@(private="file")
|
|
get_intersecting_roads :: proc(self: ^Simulator, start: u32, end: u32) -> []common.Intersection_Data {
|
|
intersections: [dynamic]common.Intersection_Data
|
|
collision_point: rl.Vector2
|
|
|
|
outer: for road, index in self.roads {
|
|
road_start_node := road.nodes[0]
|
|
road_end_node := road.nodes[1]
|
|
|
|
if !rl.CheckCollisionLines(self.nodes[start].pos, self.nodes[end].pos, self.nodes[road_start_node].pos, self.nodes[road_end_node].pos, &collision_point) do continue
|
|
|
|
// Save the collision info
|
|
data := common.Intersection_Data {
|
|
road = u32(index),
|
|
point = collision_point
|
|
}
|
|
|
|
node := inf.node_init(data.point)
|
|
// Here we check if the intersection points that were recorded before, are already within snapping radius of our current intersected point
|
|
for collision in intersections {
|
|
if (inf.node_within_snapping_radius(&node, collision.point)) do continue outer
|
|
}
|
|
|
|
// Here we check if our new intersected point node is too close to already established nodes
|
|
if _, ok := get_node_index_if_exists(self, data.point); ok do continue
|
|
|
|
append(&intersections, data)
|
|
}
|
|
|
|
return intersections[:]
|
|
}
|
|
|
|
// Given intersection data, the function splits all existing roads and adds new nodes on intersections
|
|
@(private="file")
|
|
split_roads_by_points :: proc(self: ^Simulator, intersections: []common.Intersection_Data, start: u32, end: u32) {
|
|
if len(intersections) == 0 {
|
|
add_road(self, start, end)
|
|
return
|
|
}
|
|
|
|
first_intersection_node := get_node_or_new(self, intersections[0].point)
|
|
add_road(self, start, first_intersection_node)
|
|
|
|
for i in 0..<len(intersections) {
|
|
intersection := intersections[i]
|
|
// The node created at the point of intersection
|
|
new_node := get_node_or_new(self, intersection.point)
|
|
|
|
// Pointer to the node that borders the road that was intersected
|
|
// This node and the new node will become nodes for the new road being created
|
|
road_old_node := self.roads[intersection.road].nodes[1]
|
|
|
|
// The old road that was intersected now borders the new node
|
|
// and the old node is removed from the road's end node reference,
|
|
// as is the end node's road reference
|
|
ok := update_node_reference(self, intersection.road, road_old_node, new_node)
|
|
if !ok do fmt.panicf("Failed to update the node reference to the Road ID=%d, because I couldn't find old reference ID=%d\n",
|
|
intersection.road, road_old_node)
|
|
|
|
// This adds the road (to the road manager) and also references the road at both nodes (pointers)
|
|
add_road(self, new_node, road_old_node)
|
|
|
|
if (i == len(intersections) - 1) do continue
|
|
|
|
node_start := get_node_or_new(self, intersection.point)
|
|
node_end := get_node_or_new(self, intersections[i + 1].point)
|
|
|
|
add_road(self, node_start, node_end)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
// Adds a new road into roads array, start and end are indexes of existing nodes
|
|
@(private="file")
|
|
add_road :: proc(self: ^Simulator, start: u32, end: u32) {
|
|
road := inf.road_init(start, end)
|
|
append(&self.roads, road)
|
|
|
|
road_index := u32(len(self.roads) - 1)
|
|
append(&self.nodes[start].roads, road_index)
|
|
append(&self.nodes[end].roads, road_index)
|
|
}
|
|
|
|
@(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
|
|
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] {
|
|
road.nodes[1] = result[1]
|
|
}
|
|
}
|
|
|
|
if len(end_node.roads) == 0 do delete_entity(self, road.nodes[1], .Node)
|
|
}
|
|
|
|
// 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
|
|
} |