From 697183f9610a547314b447ee42b25d4d7a7b57a4 Mon Sep 17 00:00:00 2001 From: Marto Date: Sun, 26 Apr 2026 21:50:39 +0200 Subject: [PATCH] Car and pathfinding implementation setup --- src/common/constants.odin | 2 +- src/draw.odin | 4 ++-- src/infrastructure/node.odin | 5 +++++ src/infrastructure_helpers.odin | 5 +++++ src/input.odin | 1 + src/simulator.odin | 4 ++-- src/system/pathfinding.odin | 11 +++++++++++ src/vehicles/car.odin | 14 ++++++++++++-- 8 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/system/pathfinding.odin diff --git a/src/common/constants.odin b/src/common/constants.odin index 5ea2fad..bdb0f1d 100644 --- a/src/common/constants.odin +++ b/src/common/constants.odin @@ -16,7 +16,7 @@ NODE_RADIUS :: 20 // Size of designated radius that determines when position is within node's 'sphere' NODE_SNAP_RADIUS :: 3 * NODE_RADIUS // Default text size -TEXT_SIZE :: 50 +TEXT_SIZE :: 35 // Default road colour ROAD_COLOUR :: rl.BLACK diff --git a/src/draw.odin b/src/draw.odin index 37b8d13..8234d4f 100644 --- a/src/draw.odin +++ b/src/draw.odin @@ -73,6 +73,6 @@ draw_temp_road :: proc(self: ^Simulator, pos: rl.Vector2) { // Drawing UI text, mostly for debugging purposes @(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) + entity_count := fmt.ctprintf("Nodes: %d, Roads: %d, Cars: %d", len(self.nodes), len(self.roads), len(self.cars)) + rl.DrawText(entity_count, i32(len(entity_count)), common.HEIGHT - common.TEXT_SIZE, common.TEXT_SIZE, common.TEXT_COLOUR) } \ No newline at end of file diff --git a/src/infrastructure/node.odin b/src/infrastructure/node.odin index 4f29456..a54d7d6 100644 --- a/src/infrastructure/node.odin +++ b/src/infrastructure/node.odin @@ -58,4 +58,9 @@ node_update_road_reference :: proc(self: ^Node, old_ref: u32, new_ref: u32) -> b } return false +} + +// Returns whether this node is final +node_is_end :: proc(self: ^Node) -> bool { + return len(self.roads) == 1 } \ No newline at end of file diff --git a/src/infrastructure_helpers.odin b/src/infrastructure_helpers.odin index e89e645..df974c2 100644 --- a/src/infrastructure_helpers.odin +++ b/src/infrastructure_helpers.odin @@ -95,4 +95,9 @@ delete_entity :: proc(self: ^Simulator, entity_index: u32, type: common.Entity) } return {}, false +} + +// Returns a random node that has no cars on it +get_free_node :: proc(self: ^Simulator) -> u32 { + } \ No newline at end of file diff --git a/src/input.odin b/src/input.odin index 3858eca..930f815 100644 --- a/src/input.odin +++ b/src/input.odin @@ -20,6 +20,7 @@ handle_keyboard_input :: proc(self: ^Simulator) { if rl.IsKeyReleased(.C) { self.temp_node = nil + clear(&self.cars) clear(&self.roads) clear(&self.nodes) } diff --git a/src/simulator.odin b/src/simulator.odin index 7c11d18..d658d27 100644 --- a/src/simulator.odin +++ b/src/simulator.odin @@ -28,16 +28,16 @@ Simulator :: struct { // Destructor deinit :: proc(self: ^Simulator) { + delete(self.cars) + self.temp_node = nil self.highlighted_road = nil delete(self.roads) - for &node in self.nodes { inf.node_deinit(&node) } delete(self.nodes) - delete(self.cars) } // Functionality that runs every tick regardless of input diff --git a/src/system/pathfinding.odin b/src/system/pathfinding.odin new file mode 100644 index 0000000..c43bc05 --- /dev/null +++ b/src/system/pathfinding.odin @@ -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] + + +} + diff --git a/src/vehicles/car.odin b/src/vehicles/car.odin index 172c618..45c295e 100644 --- a/src/vehicles/car.odin +++ b/src/vehicles/car.odin @@ -1,8 +1,10 @@ package vehicles import "core:math/rand" +import rl "vendor:raylib" import "../common" +import inf "../infrastructure" Car :: struct { // Fuel level 0-100% @@ -16,14 +18,22 @@ Car :: struct { origin: u32, // Car's destination node destination: Maybe(u32), + // Tracks on which node car has been last + node_pos: u32, + road_pos: Maybe(u32), + actual_pos: rl.Vector2, } // Constructor -car_init :: proc(nodes_len: u32) -> Car { +car_init :: proc(nodes: []inf.Node) -> Car { + rand_origin := rand.uint32_max(nodes_len) + return { fuel_level = common.FUEL_MAX, max_speed = common.CAR_MAX_SPEED, - origin = rand.uint32_max(nodes_len) + origin = rand_origin, + node_pos = rand_origin, + actual_pos = } }