|
|
|
|
@@ -8,8 +8,6 @@ import inf "infrastructure"
|
|
|
|
|
|
|
|
|
|
// PLAN AREA
|
|
|
|
|
// TODO implement deleting of roads
|
|
|
|
|
// TODO text/debug
|
|
|
|
|
// TODO full colour constant utilisation
|
|
|
|
|
|
|
|
|
|
Simulator :: struct {
|
|
|
|
|
// Stores all nodes
|
|
|
|
|
@@ -18,21 +16,14 @@ Simulator :: struct {
|
|
|
|
|
roads: [dynamic]inf.Road,
|
|
|
|
|
// Tracks the temporary node location
|
|
|
|
|
temp_node_index: 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
|
init :: proc() -> Simulator {
|
|
|
|
|
return {
|
|
|
|
|
nodes = nil,
|
|
|
|
|
roads = nil,
|
|
|
|
|
temp_node_index = nil,
|
|
|
|
|
show_details = false,
|
|
|
|
|
auto_continue = false,
|
|
|
|
|
}
|
|
|
|
|
// Tracks whether the delete mode is activated
|
|
|
|
|
delete_mode: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
|
@@ -47,6 +38,11 @@ deinit :: proc(self: ^Simulator) {
|
|
|
|
|
delete(self.nodes)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Functionality that runs every tick regardless of input
|
|
|
|
|
update :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
|
|
|
update_highlighted_road(self, pos)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Public input function that gets called in graphics library loop
|
|
|
|
|
handle_input :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
|
|
|
handle_keyboard_input(self)
|
|
|
|
|
@@ -58,6 +54,7 @@ handle_input :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
|
|
|
handle_keyboard_input :: proc(self: ^Simulator) {
|
|
|
|
|
self.show_details = rl.IsKeyDown(.LEFT_ALT)
|
|
|
|
|
self.auto_continue = rl.IsKeyDown(.LEFT_CONTROL)
|
|
|
|
|
self.delete_mode = rl.IsKeyDown(.LEFT_SHIFT)
|
|
|
|
|
|
|
|
|
|
if (rl.IsKeyReleased(.C)) {
|
|
|
|
|
self.temp_node_index = nil
|
|
|
|
|
@@ -79,9 +76,15 @@ handle_mouse_input :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
|
|
|
// Handles left click functionality
|
|
|
|
|
@(private="file")
|
|
|
|
|
left_click_event :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
|
|
|
// DELETE
|
|
|
|
|
if road, ok := self.highlighted_road.?; ok && self.delete_mode do delete_road(self, road)
|
|
|
|
|
|
|
|
|
|
// BUILD
|
|
|
|
|
cur_node_index := get_node_or_new(self, pos)
|
|
|
|
|
|
|
|
|
|
if temp, ok := self.temp_node_index.?; 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)
|
|
|
|
|
|
|
|
|
|
@@ -103,6 +106,8 @@ right_click_event :: proc(self: ^Simulator) {
|
|
|
|
|
if len(temp_node.roads) > 0 do return
|
|
|
|
|
|
|
|
|
|
inf.node_deinit(temp_node)
|
|
|
|
|
// We can safely call the remove here as the only way it will get deleted is if it's the only node aka it was created during our creation process
|
|
|
|
|
// Consequently this means that it will always be on last place and never swapped with anything
|
|
|
|
|
unordered_remove(&self.nodes, index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -111,11 +116,16 @@ draw :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
|
|
|
rl.ClearBackground(common.BACKGROUND_COLOUR)
|
|
|
|
|
|
|
|
|
|
// draw roads
|
|
|
|
|
for &road in self.roads {
|
|
|
|
|
for &road, index 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)
|
|
|
|
|
road_colour: rl.Color
|
|
|
|
|
if road, ok := self.highlighted_road.?; ok && road == u32(index) && self.delete_mode {
|
|
|
|
|
road_colour = common.ROAD_HIGHLIGHT_COLOUR
|
|
|
|
|
} else do road_colour = common.ROAD_COLOUR
|
|
|
|
|
|
|
|
|
|
rl.DrawLineEx(self.nodes[start].pos, self.nodes[end].pos, common.ROAD_SIZE, road_colour)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// draw nodes
|
|
|
|
|
@@ -129,7 +139,16 @@ draw :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|
|
|
|
// 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)
|
|
|
|
|
rl.DrawCircleV(pos, common.NODE_RADIUS, common.NODE_BUILD_COLOUR)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw_ui(self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@(private="file")
|
|
|
|
|
draw_ui :: proc(self: ^Simulator) {
|
|
|
|
|
entity_count := fmt.ctprintf("Nodes: %d\nRoads: %d", len(self.nodes), len(self.roads))
|
|
|
|
|
rl.DrawText(entity_count, i32(common.WIDTH - 13 * len(entity_count)), common.HEIGHT - 2 * common.TEXT_SIZE, common.TEXT_SIZE, common.TEXT_COLOUR)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This function only returns the index to the node or if it doesn't exist bool in the tuple is false
|
|
|
|
|
@@ -192,7 +211,6 @@ get_intersecting_roads :: proc(self: ^Simulator, start: u32, end: u32) -> []comm
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -260,3 +278,83 @@ update_node_reference :: proc(self: ^Simulator, road_to_update: u32, old_ref: u3
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@(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
|
|
|
|
|
// TODO implement in delete_entity to update references if first element is deleted
|
|
|
|
|
if len(start_node.roads) == 0 do delete_entity(self, road.nodes[0], .Node)
|
|
|
|
|
if len(end_node.roads) == 0 do delete_entity(self, road.nodes[1], .Node)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Function that allows deleting of any entity within the entity list (nodes, roads, etc.) while ensuring valid references
|
|
|
|
|
delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) {
|
|
|
|
|
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 := false
|
|
|
|
|
|
|
|
|
|
switch type {
|
|
|
|
|
case .Node:
|
|
|
|
|
mlen = u32(len(self.nodes))
|
|
|
|
|
case .Road:
|
|
|
|
|
mlen = u32(len(self.roads))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
for &road in self.roads {
|
|
|
|
|
ok := inf.road_update_node_reference(&road, index_change[0], index_change[1])
|
|
|
|
|
if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0])
|
|
|
|
|
}
|
|
|
|
|
case .Road:
|
|
|
|
|
unordered_remove(&self.roads, entity_index)
|
|
|
|
|
if !swap_made do return
|
|
|
|
|
|
|
|
|
|
for &node in self.nodes {
|
|
|
|
|
ok := inf.node_update_road_reference(&node, index_change[0], index_change[1])
|
|
|
|
|
if !ok do fmt.panicf("Failed to find old reference (%d) for road\n", index_change[0])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|