Car and pathfinding implementation setup

This commit is contained in:
2026-04-26 21:50:39 +02:00
parent ee34acae34
commit 697183f961
8 changed files with 39 additions and 7 deletions

View File

@@ -16,7 +16,7 @@ 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_RADIUS NODE_SNAP_RADIUS :: 3 * NODE_RADIUS
// Default text size // Default text size
TEXT_SIZE :: 50 TEXT_SIZE :: 35
// Default road colour // Default road colour
ROAD_COLOUR :: rl.BLACK ROAD_COLOUR :: rl.BLACK

View File

@@ -73,6 +73,6 @@ draw_temp_road :: proc(self: ^Simulator, pos: rl.Vector2) {
// Drawing UI text, mostly for debugging purposes // 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, Roads: %d, Cars: %d", len(self.nodes), len(self.roads), len(self.cars))
rl.DrawText(entity_count, i32(common.WIDTH - 13 * len(entity_count)), common.HEIGHT - 2 * common.TEXT_SIZE, common.TEXT_SIZE, common.TEXT_COLOUR) rl.DrawText(entity_count, i32(len(entity_count)), common.HEIGHT - common.TEXT_SIZE, common.TEXT_SIZE, common.TEXT_COLOUR)
} }

View File

@@ -58,4 +58,9 @@ node_update_road_reference :: proc(self: ^Node, old_ref: u32, new_ref: u32) -> b
} }
return false return false
}
// Returns whether this node is final
node_is_end :: proc(self: ^Node) -> bool {
return len(self.roads) == 1
} }

View File

@@ -95,4 +95,9 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity)
} }
return {}, false return {}, false
}
// Returns a random node that has no cars on it
get_free_node :: proc(self: ^Simulator) -> u32 {
} }

View File

@@ -20,6 +20,7 @@ handle_keyboard_input :: proc(self: ^Simulator) {
if rl.IsKeyReleased(.C) { if rl.IsKeyReleased(.C) {
self.temp_node = nil self.temp_node = nil
clear(&self.cars)
clear(&self.roads) clear(&self.roads)
clear(&self.nodes) clear(&self.nodes)
} }

View File

@@ -28,16 +28,16 @@ Simulator :: struct {
// Destructor // Destructor
deinit :: proc(self: ^Simulator) { deinit :: proc(self: ^Simulator) {
delete(self.cars)
self.temp_node = nil self.temp_node = nil
self.highlighted_road = nil self.highlighted_road = nil
delete(self.roads) delete(self.roads)
for &node in self.nodes { for &node in self.nodes {
inf.node_deinit(&node) inf.node_deinit(&node)
} }
delete(self.nodes) delete(self.nodes)
delete(self.cars)
} }
// Functionality that runs every tick regardless of input // Functionality that runs every tick regardless of input

View File

@@ -0,0 +1,11 @@
package system
import inf "../infrastructure"
get_path_to_destination :: proc(source: u32, destination: u32, nodes: []inf.Node) {
source_node := nodes[source]
destination_node := nodes[destination]
}

View File

@@ -1,8 +1,10 @@
package vehicles package vehicles
import "core:math/rand" import "core:math/rand"
import rl "vendor:raylib"
import "../common" import "../common"
import inf "../infrastructure"
Car :: struct { Car :: struct {
// Fuel level 0-100% // Fuel level 0-100%
@@ -16,14 +18,22 @@ Car :: struct {
origin: u32, origin: u32,
// Car's destination node // Car's destination node
destination: Maybe(u32), destination: Maybe(u32),
// Tracks on which node car has been last
node_pos: u32,
road_pos: Maybe(u32),
actual_pos: rl.Vector2,
} }
// Constructor // Constructor
car_init :: proc(nodes_len: u32) -> Car { car_init :: proc(nodes: []inf.Node) -> Car {
rand_origin := rand.uint32_max(nodes_len)
return { return {
fuel_level = common.FUEL_MAX, fuel_level = common.FUEL_MAX,
max_speed = common.CAR_MAX_SPEED, max_speed = common.CAR_MAX_SPEED,
origin = rand.uint32_max(nodes_len) origin = rand_origin,
node_pos = rand_origin,
actual_pos =
} }
} }