diff --git a/infrastructure/node.odin b/infrastructure/node.odin index 7e00921..395c0be 100644 --- a/infrastructure/node.odin +++ b/infrastructure/node.odin @@ -4,11 +4,16 @@ import "../common" import rl "vendor:raylib" Node :: struct { + // Whether the node is reachable enabled: bool, + // This node's position pos: rl.Vector2, + // All of the roads that are connected to the node itself; + // Stores the index of the Road object that is stored within Simulator struct in roads dynamic array roads: [dynamic]u32, } +// Node Initialisation node_init :: proc(new_pos: rl.Vector2) -> Node { return { enabled = true, @@ -17,10 +22,24 @@ node_init :: proc(new_pos: rl.Vector2) -> Node { } } +// Returns whether passed pos(ition) is within this node's snapping radius node_within_snapping_radius :: proc(self: ^Node, pos: rl.Vector2) -> bool { return rl.CheckCollisionPointCircle( pos, self.pos, common.NODE_SNAP_RADIUS * common.NODE_RADIUS, ) +} + +// Tries to remove the road reference from the node; +// Returns false if failed +node_unreference_road :: proc(self: ^Node, road_to_unref: u32) -> bool { + for i in 0.. (u32, bool) { +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 } @@ -107,8 +106,8 @@ get_selected_node :: proc(self: ^Simulator, pos: rl.Vector2) -> (u32, bool) { // 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_or_create_node :: proc(self: ^Simulator, pos: rl.Vector2) -> u32 { - if node, ok := get_selected_node(self, pos); ok do return node +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) @@ -141,7 +140,7 @@ get_intersecting_roads :: proc(self: ^Simulator, start: u32, end: u32) -> []comm } // Here we check if our new intersected point node is too close to already established nodes - if _, ok := get_selected_node(self, data.point); ok do continue + if _, ok := get_node_index_if_exists(self, data.point); ok do continue append(&intersections, data) } @@ -157,17 +156,69 @@ split_roads_by_points :: proc(self: ^Simulator, intersections: []common.Intersec return } - first_intersection_node := get_or_create_node(self, intersections[0].point) + first_intersection_node := get_node_or_new(self, intersections[0].point) road := inf.road_init(start, first_intersection_node) append(&self.roads, road) - for intersection in intersections { - // TODO + for i in 0.. bool { + road := &self.roads[road_to_update] + + for i in 0..