From 945a3f45adbf509ef5e7986d55121a4899e0a1b3 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 19 Jun 2024 13:40:26 +0200 Subject: customer pathfinding --- server/src/customer.rs | 69 +++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 26 deletions(-) (limited to 'server/src') diff --git a/server/src/customer.rs b/server/src/customer.rs index 8d843fda..403b891e 100644 --- a/server/src/customer.rs +++ b/server/src/customer.rs @@ -4,10 +4,10 @@ use crate::{ protocol::{PacketC, PacketS, PlayerID}, }; use glam::{IVec2, Vec2}; -use log::error; +use log::{error, info}; use std::{ cmp::Ordering, - collections::{BinaryHeap, HashMap, HashSet, VecDeque}, + collections::{BinaryHeap, HashMap, HashSet}, sync::Arc, time::Duration, }; @@ -24,7 +24,7 @@ struct DemandState { } enum CustomerState { - WalkingToChair { target: Vec2 }, + WalkingToChair { path: Vec }, Waiting, } @@ -84,7 +84,6 @@ pub async fn customer(game: Arc>, mut grx: broadcast::Receiver, dt: f32) { if self.customers.is_empty() { @@ -95,25 +94,32 @@ impl DemandState { character: 0, }, )); + let path = find_path( + &self.walkable, + self.data.customer_spawn.as_ivec2(), + IVec2::new(20, 1), + ) + .expect("no path"); self.customers.push(Customer { id: -1, position: self.data.customer_spawn, facing: Vec2::X, vel: Vec2::ZERO, - state: CustomerState::WalkingToChair { - target: Vec2::new(2., 2.), - }, + state: CustomerState::WalkingToChair { path }, }); } for p in &mut self.customers { - match p.state { - CustomerState::WalkingToChair { target } => { - packets_out.push(( - p.id, - move_player(p, &self.walkable, target - p.position, dt), - )); - if target.distance(p.position) < 0.5 { + match &mut p.state { + CustomerState::WalkingToChair { path } => { + if let Some(next) = path.last().copied() { + info!("next {next}"); + if next.distance(p.position) < 0.6 { + path.pop(); + } + packets_out + .push((p.id, move_player(p, &self.walkable, next - p.position, dt))); + } else { p.state = CustomerState::Waiting; } } @@ -123,7 +129,7 @@ impl DemandState { } } -pub fn find_path(map: &HashSet, from: IVec2, to: IVec2) -> VecDeque { +pub fn find_path(map: &HashSet, from: IVec2, to: IVec2) -> Option> { #[derive(Debug, PartialEq, Eq)] struct Open(i32, IVec2, IVec2); impl PartialOrd for Open { @@ -141,8 +147,12 @@ pub fn find_path(map: &HashSet, from: IVec2, to: IVec2) -> VecDeque, from: IVec2, to: IVec2) -> VecDeque, direction: Vec2, dt: f32) -> PacketS { - let direction = direction.normalize(); + let direction = direction.normalize_or_zero(); if direction.length() > 0.1 { p.facing = direction + (p.facing - direction) * (-dt * 10.).exp(); } -- cgit v1.2.3-70-g09d2