package main import rl "vendor:raylib" import "common" import "core:fmt" import inf "infrastructure" // PLAN AREA // TODO implement deleting of roads // TODO text/debug // TODO full colour constant utilisation Simulator :: struct { // Stores all nodes nodes: [dynamic]inf.Node, // Stores all roads roads: [dynamic]inf.Road, // Tracks the temporary node location temp_node_index: 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, } // Constructor init :: proc() -> Simulator { return { nodes = nil, roads = nil, temp_node_index = nil, show_details = false, auto_continue = false, } } // Destructor deinit :: proc(self: ^Simulator) { self.temp_node_index = nil delete(self.roads) for &node in self.nodes { inf.node_deinit(&node) } delete(self.nodes) } // 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) 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) { cur_node_index := get_node_or_new(self, pos) if temp, ok := self.temp_node_index.?; ok { data := get_intersecting_roads(self, 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 return } 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) // draw roads for &road in self.roads { start := road.nodes[0] end := road.nodes[1] rl.DrawLineEx(self.nodes[start].pos, self.nodes[end].pos, common.ROAD_SIZE, common.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) } } // 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) @(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.. bool { road := &self.roads[road_to_update] for i in 0..