Final improvements and refactorings
This commit is contained in:
@@ -3,20 +3,20 @@ package common
|
|||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
// Screen Width
|
// Screen Width
|
||||||
WIDTH :: 1920
|
WIDTH :: 1920
|
||||||
// Screen Height
|
// Screen Height
|
||||||
HEIGHT :: 1080
|
HEIGHT :: 1080
|
||||||
// Default Monitor
|
// Default Monitor
|
||||||
MONITOR :: 0
|
MONITOR :: 0
|
||||||
|
|
||||||
// Thickness of the road being drawn
|
// Thickness of the road being drawn
|
||||||
ROAD_SIZE :: 20
|
ROAD_SIZE :: 20
|
||||||
// Radius of the node
|
// Radius of the node
|
||||||
NODE_RADIUS :: 20
|
NODE_RADIUS :: 20
|
||||||
// Size of designated radius that determines when position is within node's 'sphere'
|
// Size of designated radius that determines when position is within node's 'sphere'
|
||||||
NODE_SNAP_RADIUS :: 3
|
NODE_SNAP_RADIUS :: 3
|
||||||
// Default text size
|
// Default text size
|
||||||
TEXT_SIZE :: 50
|
TEXT_SIZE :: 50
|
||||||
|
|
||||||
// Default road colour
|
// Default road colour
|
||||||
ROAD_COLOUR :: rl.BLACK
|
ROAD_COLOUR :: rl.BLACK
|
||||||
@@ -32,6 +32,3 @@ NODE_CURSOR_COLOUR :: rl.BLUE
|
|||||||
NODE_SNAP_COLOUR :: rl.PINK
|
NODE_SNAP_COLOUR :: rl.PINK
|
||||||
// The default colour of the text displayed
|
// The default colour of the text displayed
|
||||||
TEXT_COLOUR :: rl.BLACK
|
TEXT_COLOUR :: rl.BLACK
|
||||||
|
|
||||||
// Background Colour
|
|
||||||
BACKGROUND_COLOUR :: rl.LIGHTGRAY
|
|
||||||
@@ -10,6 +10,7 @@ Intersection_Data :: struct {
|
|||||||
point: rl.Vector2,
|
point: rl.Vector2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tracks the entity type
|
||||||
Entity :: enum {
|
Entity :: enum {
|
||||||
Node,
|
Node,
|
||||||
Road,
|
Road,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import "common"
|
|||||||
|
|
||||||
// Main drawing function
|
// Main drawing function
|
||||||
draw :: proc(self: ^Simulator, pos: rl.Vector2) {
|
draw :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
rl.ClearBackground(common.BACKGROUND_COLOUR)
|
rl.ClearBackground(rl.LIGHTGRAY)
|
||||||
|
|
||||||
// draw roads
|
// draw roads
|
||||||
for &road, index in self.roads {
|
for &road, index in self.roads {
|
||||||
@@ -33,12 +33,15 @@ draw :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|||||||
// draw temp road if exists
|
// draw temp road if exists
|
||||||
if val, ok := self.temp_node.?; ok {
|
if val, ok := self.temp_node.?; ok {
|
||||||
rl.DrawLineEx(self.nodes[val].pos, pos, common.ROAD_SIZE, common.ROAD_COLOUR)
|
rl.DrawLineEx(self.nodes[val].pos, pos, common.ROAD_SIZE, common.ROAD_COLOUR)
|
||||||
rl.DrawCircleV(pos, common.NODE_RADIUS, common.NODE_BUILD_COLOUR)
|
|
||||||
|
rl.DrawCircleV(self.nodes[val].pos, common.NODE_RADIUS, common.NODE_BUILD_COLOUR)
|
||||||
|
rl.DrawCircleV(pos, common.NODE_RADIUS, common.NODE_CURSOR_COLOUR)
|
||||||
}
|
}
|
||||||
|
|
||||||
draw_ui(self)
|
draw_ui(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Drawing UI text, mostly for debugging purposes
|
||||||
@(private="file")
|
@(private="file")
|
||||||
draw_ui :: proc(self: ^Simulator) {
|
draw_ui :: proc(self: ^Simulator) {
|
||||||
entity_count := fmt.ctprintf("Nodes: %d\nRoads: %d", len(self.nodes), len(self.roads))
|
entity_count := fmt.ctprintf("Nodes: %d\nRoads: %d", len(self.nodes), len(self.roads))
|
||||||
|
|||||||
@@ -36,8 +36,7 @@ node_within_snapping_radius :: proc(self: ^Node, pos: rl.Vector2) -> bool {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tries to remove the road reference from the node;
|
// Tries to remove the road reference from the node; returns false if failed
|
||||||
// Returns false if failed
|
|
||||||
node_unreference_road :: proc(self: ^Node, road_to_unref: u32) -> bool {
|
node_unreference_road :: proc(self: ^Node, road_to_unref: u32) -> bool {
|
||||||
for i in 0..<len(self.roads) {
|
for i in 0..<len(self.roads) {
|
||||||
if self.roads[i] != road_to_unref do continue
|
if self.roads[i] != road_to_unref do continue
|
||||||
@@ -49,6 +48,7 @@ node_unreference_road :: proc(self: ^Node, road_to_unref: u32) -> bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Attempts to update the existing road references with new one; returns false if it can't find the old reference
|
||||||
node_update_road_reference :: proc(self: ^Node, old_ref: u32, new_ref: u32) -> bool {
|
node_update_road_reference :: proc(self: ^Node, old_ref: u32, new_ref: u32) -> bool {
|
||||||
for &road in self.roads {
|
for &road in self.roads {
|
||||||
if road != old_ref do continue
|
if road != old_ref do continue
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ update :: proc(self: ^Simulator, pos: rl.Vector2) {
|
|||||||
update_highlighted_road(self, pos)
|
update_highlighted_road(self, pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implementation of removing the road after a click has been registered
|
||||||
@private
|
@private
|
||||||
demolish_road :: proc(self: ^Simulator) {
|
demolish_road :: proc(self: ^Simulator) {
|
||||||
if !self.delete_mode do return
|
if !self.delete_mode do return
|
||||||
@@ -48,6 +49,7 @@ demolish_road :: proc(self: ^Simulator) {
|
|||||||
if road, ok := self.highlighted_road.?; ok do delete_road(self, road)
|
if road, ok := self.highlighted_road.?; ok do delete_road(self, road)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implementation of road building after a click has been registered
|
||||||
@private
|
@private
|
||||||
create_road :: proc(self: ^Simulator, pos: rl.Vector2) {
|
create_road :: proc(self: ^Simulator, pos: rl.Vector2) {
|
||||||
// BUILD
|
// BUILD
|
||||||
@@ -153,6 +155,7 @@ add_road :: proc(self: ^Simulator, start: u32, end: u32) {
|
|||||||
append(&self.nodes[end].roads, road_index)
|
append(&self.nodes[end].roads, road_index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deletes the road which index was sent in, alongside deleting references of said road and removal of nodes if that road was their only connection
|
||||||
@(private="file")
|
@(private="file")
|
||||||
delete_road :: proc(self: ^Simulator, road_to_delete: u32) {
|
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
|
// First we need to unreference this road from surrounding nodes and then delete those nodes IF this was the last road connection
|
||||||
|
|||||||
Reference in New Issue
Block a user