From 7c83fe7eeaac3736d5a59b270bbbbde59780e536 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 16 Jul 2024 16:21:37 +0200 Subject: item rendering and movement --- server/protocol/src/lib.rs | 2 + server/protocol/src/movement.rs | 113 +++++++++++++++++++++++++++++++++++++ server/src/customer/mod.rs | 6 +- server/src/customer/movement.rs | 108 ----------------------------------- server/src/customer/pathfinding.rs | 4 +- 5 files changed, 119 insertions(+), 114 deletions(-) create mode 100644 server/protocol/src/movement.rs delete mode 100644 server/src/customer/movement.rs (limited to 'server') diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 895af376..f2a6dd27 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -28,6 +28,8 @@ use std::{ pub use glam; +pub mod movement; + pub const VERSION: (u32, u32) = (1, 0); pub const BINCODE_CONFIG: Configuration> = diff --git a/server/protocol/src/movement.rs b/server/protocol/src/movement.rs new file mode 100644 index 00000000..486da816 --- /dev/null +++ b/server/protocol/src/movement.rs @@ -0,0 +1,113 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + Copyright 2024 tpart + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +*/ +use crate::{ + glam::{IVec2, Vec2}, + PacketS, +}; +use std::collections::HashSet; + +const PLAYER_SIZE: f32 = 0.4; +const PLAYER_FRICTION: f32 = 10.0; +const PLAYER_SPEED: f32 = 55.0; +const BOOST_FACTOR: f32 = 2.5; +const BOOST_DURATION: f32 = 0.3; +const BOOST_RESTORE: f32 = 0.5; + +pub struct MovementBase { + pub position: Vec2, + pub facing: Vec2, + pub rotation: f32, + pub velocity: Vec2, + pub boosting: bool, + pub stamina: f32, +} + +impl MovementBase { + pub fn new(position: Vec2) -> Self { + Self { + position, + facing: Vec2::X, + velocity: Vec2::ZERO, + boosting: false, + stamina: 0., + rotation: 0., + } + } + pub fn update( + &mut self, + map: &HashSet, + direction: Vec2, + mut boost: bool, + dt: f32, + ) -> PacketS { + let direction = direction.clamp_length_max(1.); + if direction.length() > 0.1 { + self.facing = direction + (self.facing - direction) * (-dt * 10.).exp(); + } + self.rotation = self.facing.x.atan2(self.facing.y); + boost &= direction.length() > 0.1; + self.boosting = boost && (self.boosting || self.stamina >= 1.) && self.stamina > 0.; + self.stamina += if self.boosting { + -dt / BOOST_DURATION + } else { + dt / BOOST_RESTORE + }; + self.stamina = self.stamina.max(0.).min(1.); + let speed = PLAYER_SPEED * if self.boosting { BOOST_FACTOR } else { 1. }; + self.velocity += direction * dt * speed; + self.position += self.velocity * dt; + self.velocity = self.velocity * (-dt * PLAYER_FRICTION).exp(); + collide_player(self, map); + + PacketS::Position { + pos: self.position, + boosting: self.boosting, + rot: self.rotation, + } + } +} + +pub fn collide_player(p: &mut MovementBase, map: &HashSet) { + for xo in -1..=1 { + for yo in -1..=1 { + let tile = IVec2::new(xo, yo) + p.position.as_ivec2(); + if map.contains(&tile) { + continue; + } + let tile = tile.as_vec2(); + let d = aabb_point_distance(tile, tile + Vec2::ONE, p.position); + if d > PLAYER_SIZE { + continue; + } + let h = 0.01; + let d_sample_x = + aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(h, 0.)); + let d_sample_y = + aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(0., h)); + let grad = (Vec2::new(d_sample_x, d_sample_y) - d) / h; + + p.position += (PLAYER_SIZE - d) * grad; + p.velocity -= grad * grad.dot(p.velocity); + } + } +} + +pub fn aabb_point_distance(min: Vec2, max: Vec2, p: Vec2) -> f32 { + (p - p.clamp(min, max)).length() +} diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index aeaa7ae1..bf385927 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -15,15 +15,15 @@ along with this program. If not, see . */ -pub mod movement; mod pathfinding; use crate::{data::Gamedata, game::Tile}; use anyhow::{anyhow, Result}; use fake::{faker, Fake}; -use hurrycurry_protocol::{glam::IVec2, DemandIndex, Message, PacketS, PlayerID}; +use hurrycurry_protocol::{ + glam::IVec2, movement::MovementBase, DemandIndex, Message, PacketS, PlayerID, +}; use log::info; -use movement::MovementBase; use pathfinding::{find_path, Path}; use rand::{random, thread_rng}; use std::{ diff --git a/server/src/customer/movement.rs b/server/src/customer/movement.rs deleted file mode 100644 index 34ed5b16..00000000 --- a/server/src/customer/movement.rs +++ /dev/null @@ -1,108 +0,0 @@ -/* - Hurry Curry! - a game about cooking - Copyright 2024 metamuffin - Copyright 2024 tpart - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, version 3 of the License only. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -*/ -use hurrycurry_protocol::{glam::{IVec2, Vec2}, PacketS}; -use std::collections::HashSet; - -const PLAYER_SIZE: f32 = 0.4; -const PLAYER_FRICTION: f32 = 10.0; -const PLAYER_SPEED: f32 = 55.0; -const BOOST_FACTOR: f32 = 2.5; -const BOOST_DURATION: f32 = 0.3; -const BOOST_RESTORE: f32 = 0.5; - -pub struct MovementBase { - pub position: Vec2, - pub facing: Vec2, - pub vel: Vec2, - pub boosting: bool, - pub stamina: f32, -} - -impl MovementBase { - pub fn new(position: Vec2) -> Self { - Self { - position, - facing: Vec2::X, - vel: Vec2::ZERO, - boosting: false, - stamina: 0., - } - } - pub fn update( - &mut self, - map: &HashSet, - direction: Vec2, - mut boost: bool, - dt: f32, - ) -> PacketS { - let direction = direction.clamp_length_max(1.); - if direction.length() > 0.1 { - self.facing = direction + (self.facing - direction) * (-dt * 10.).exp(); - } - let rot = self.facing.x.atan2(self.facing.y); - boost &= direction.length() > 0.1; - self.boosting = boost && (self.boosting || self.stamina >= 1.) && self.stamina > 0.; - self.stamina += if self.boosting { - -dt / BOOST_DURATION - } else { - dt / BOOST_RESTORE - }; - self.stamina = self.stamina.max(0.).min(1.); - let speed = PLAYER_SPEED * if self.boosting { BOOST_FACTOR } else { 1. }; - self.vel += direction * dt * speed; - self.position += self.vel * dt; - self.vel = self.vel * (-dt * PLAYER_FRICTION).exp(); - collide_player(self, map); - - PacketS::Position { - pos: self.position, - boosting: false, - rot, - } - } -} - -pub fn collide_player(p: &mut MovementBase, map: &HashSet) { - for xo in -1..=1 { - for yo in -1..=1 { - let tile = IVec2::new(xo, yo) + p.position.as_ivec2(); - if map.contains(&tile) { - continue; - } - let tile = tile.as_vec2(); - let d = aabb_point_distance(tile, tile + Vec2::ONE, p.position); - if d > PLAYER_SIZE { - continue; - } - let h = 0.01; - let d_sample_x = - aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(h, 0.)); - let d_sample_y = - aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(0., h)); - let grad = (Vec2::new(d_sample_x, d_sample_y) - d) / h; - - p.position += (PLAYER_SIZE - d) * grad; - p.vel -= grad * grad.dot(p.vel); - } - } -} - -pub fn aabb_point_distance(min: Vec2, max: Vec2, p: Vec2) -> f32 { - (p - p.clamp(min, max)).length() -} diff --git a/server/src/customer/pathfinding.rs b/server/src/customer/pathfinding.rs index 2b9f98f4..d1e1e997 100644 --- a/server/src/customer/pathfinding.rs +++ b/server/src/customer/pathfinding.rs @@ -15,10 +15,8 @@ along with this program. If not, see . */ -use super::movement::MovementBase; use hurrycurry_protocol::{ - glam::{IVec2, Vec2}, - PacketS, + glam::{IVec2, Vec2}, movement::MovementBase, PacketS }; use log::trace; use std::{ -- cgit v1.2.3-70-g09d2