package main import rl "vendor:raylib" import "common" import "core:fmt" import inf "infrastructure" import v "vehicles" Simulator :: struct { // Stores all nodes nodes: [dynamic]inf.Node, // Stores all roads roads: [dynamic]inf.Road, // Stores all cars cars: [dynamic]v.Car, // 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) { delete(self.cars) 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) } // Implementation of road building after a click has been registered @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..