diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-06-26 16:53:07 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-06-26 16:53:07 +0200 | 
| commit | 2ca6ac7ab329036d0155de2de4b0a11f3a785414 (patch) | |
| tree | 7472368efb282a4380b45931f3462e9930f48a36 | |
| parent | c4b0f8d698b574c711b1e205371adfd3e3339487 (diff) | |
| download | hurrycurry-2ca6ac7ab329036d0155de2de4b0a11f3a785414.tar hurrycurry-2ca6ac7ab329036d0155de2de4b0a11f3a785414.tar.bz2 hurrycurry-2ca6ac7ab329036d0155de2de4b0a11f3a785414.tar.zst | |
boosting
| -rw-r--r-- | client/player/controllable_player.gd | 36 | ||||
| -rw-r--r-- | client/project.godot | 6 | ||||
| -rw-r--r-- | server/src/customer/mod.rs | 8 | ||||
| -rw-r--r-- | server/src/customer/movement.rs | 39 | ||||
| -rw-r--r-- | server/src/customer/pathfinding.rs | 3 | ||||
| -rw-r--r-- | server/src/game.rs | 38 | ||||
| -rw-r--r-- | test-client/main.ts | 42 | ||||
| -rw-r--r-- | test-client/movement.ts | 55 | 
8 files changed, 145 insertions, 82 deletions
| diff --git a/client/player/controllable_player.gd b/client/player/controllable_player.gd index 62b15c66..a2321fef 100644 --- a/client/player/controllable_player.gd +++ b/client/player/controllable_player.gd @@ -17,11 +17,16 @@  class_name ControllablePlayer  extends Player - -const PLAYER_SPEED: float = 65.; +const PLAYER_FRICTION = 10 +const PLAYER_SPEED = 40 +const BOOST_FACTOR = 3 +const BOOST_DURATION = 0.3 +const BOOST_RESTORE = 0.5  var facing = Vector2(1, 0)  var velocity_ = Vector2(0, 0) +var stamina = 0 +var boosting = false  var target: Vector2i = Vector2i(0, 0) @@ -38,7 +43,8 @@ func _ready():  func _process(delta):  	var input = Input.get_vector("left", "right", "forward", "backwards") -	input = input.rotated(-game.camera.angle_target) +	var boost = Input.is_action_pressed("boost") +	input = input.rotated( - game.camera.angle_target)  	position_anim = position_  	rotation_anim = rotation_  	if Input.is_action_pressed("interact") or Input.is_action_just_released("interact"): @@ -49,23 +55,29 @@ func _process(delta):  			int(floor(position.z + cos(rotation.y)))  		)  	interact() -	update(delta, input) +	update(delta, input, boost)  	super(delta)  	character.walking = input.length_squared() > 0.1 -func update(dt: float, input: Vector2): -	var direction = input.limit_length(1.); +func update(dt: float, input: Vector2, boost: bool): +	input = input.limit_length(1.); +	if input.length() > 0.1: +		self.facing = input + (self.facing - input) * exp( - dt * 10.);  	rotation_ = atan2(self.facing.x, self.facing.y); -	if direction.length() > 0.1: -		self.facing = direction + (self.facing - direction) * exp(-dt * 10.); -	self.velocity_ += direction * dt * PLAYER_SPEED; +	boost = boost and input.length() > 0.1 +	boosting = boost and (boosting or stamina >= 1.0) and stamina > 0 +	if boosting: stamina -= dt / BOOST_DURATION +	else: stamina += dt / BOOST_RESTORE +	stamina = max(min(stamina, 1.0), 0.0) +	var speed = PLAYER_SPEED * (BOOST_FACTOR if boosting else 1) +	self.velocity_ += input * dt * speed;  	self.position_ += self.velocity_ * dt; -	self.velocity_ = self.velocity_ * exp(-dt * 15.); +	self.velocity_ = self.velocity_ * exp( - dt * 15.);  	collide(dt);  func collide(dt: float): -	for xo in range(-1,2): -		for yo in range(-1,2): +	for xo in range( - 1, 2): +		for yo in range( - 1, 2):  			var tile = Vector2i(xo, yo) + Vector2i(self.position_);  			if !game.get_tile_collision(tile): continue  			tile = Vector2(tile) diff --git a/client/project.godot b/client/project.godot index bb2b6896..679bd9e0 100644 --- a/client/project.godot +++ b/client/project.godot @@ -91,6 +91,12 @@ pause={  , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":6,"pressure":0.0,"pressed":true,"script":null)  ]  } +boost={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":1,"pressure":0.0,"pressed":false,"script":null) +] +}  [internationalization] diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index e6f999e6..ba65a5e2 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -25,7 +25,7 @@ use crate::{  };  use anyhow::{anyhow, Result};  use fake::{faker, Fake}; -use glam::{IVec2, Vec2}; +use glam::IVec2;  use log::debug;  use movement::MovementBase;  use pathfinding::{find_path, Path}; @@ -115,11 +115,7 @@ impl DemandState {              self.customers.insert(                  id,                  Customer { -                    movement: MovementBase { -                        position: data.customer_spawn, -                        facing: Vec2::X, -                        vel: Vec2::ZERO, -                    }, +                    movement: MovementBase::new(data.customer_spawn),                      state: CustomerState::Entering { path, chair },                  },              ); diff --git a/server/src/customer/movement.rs b/server/src/customer/movement.rs index 3fcf37aa..a189ddce 100644 --- a/server/src/customer/movement.rs +++ b/server/src/customer/movement.rs @@ -20,25 +20,54 @@ use glam::{IVec2, Vec2};  use std::collections::HashSet;  const PLAYER_SIZE: f32 = 0.4; -const PLAYER_SPEED: f32 = 65.; -pub const PLAYER_SPEED_LIMIT: f32 = f32::INFINITY; // 10.; +const PLAYER_FRICTION: f32 = 10.0; +const PLAYER_SPEED: f32 = 40.0; +const BOOST_FACTOR: f32 = 3.0; +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 update(&mut self, map: &HashSet<IVec2>, direction: Vec2, dt: f32) -> PacketS { +    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<IVec2>, +        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); -        self.vel += direction * dt * PLAYER_SPEED; +        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 * 15.).exp(); +        self.vel = self.vel * (-dt * PLAYER_FRICTION).exp();          collide_player(self, map);          PacketS::Position { diff --git a/server/src/customer/pathfinding.rs b/server/src/customer/pathfinding.rs index 5056975e..73999e0c 100644 --- a/server/src/customer/pathfinding.rs +++ b/server/src/customer/pathfinding.rs @@ -41,10 +41,11 @@ impl Path {              player.update(                  &walkable,                  (next - player.position).normalize_or_zero() * 0.5, +                false,                  dt,              )          } else { -            player.update(&walkable, Vec2::ZERO, dt) +            player.update(&walkable, Vec2::ZERO, false, dt)          }      }      pub fn is_done(&self) -> bool { diff --git a/server/src/game.rs b/server/src/game.rs index 595816e1..20f479c7 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -16,7 +16,7 @@  */  use crate::{ -    customer::{movement::PLAYER_SPEED_LIMIT, DemandState}, +    customer::DemandState,      data::Gamedata,      interaction::{interact, tick_tile, InteractEffect, TickEffect},      protocol::{ItemIndex, Message, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex}, @@ -244,29 +244,29 @@ impl Game {                      .get_mut(&player)                      .ok_or(anyhow!("player does not exist"))?; -                let dt = player.last_position_ts.elapsed().as_secs_f32(); -                let dist = pos.distance(player.position); -                let speed = dist / dt; -                let interact_dist = player -                    .interacting -                    .map(|p| (p.as_vec2() + Vec2::splat(0.5)).distance(player.position)) -                    .unwrap_or_default(); -                let movement_ok = speed < PLAYER_SPEED_LIMIT && dist < 1. && interact_dist < 2.; -                if movement_ok { -                    player.position = pos; -                    player.last_position_ts = Instant::now(); -                } +                // let dt = player.last_position_ts.elapsed().as_secs_f32(); +                // let dist = pos.distance(player.position); +                // let speed = dist / dt; +                // let interact_dist = player +                //     .interacting +                //     .map(|p| (p.as_vec2() + Vec2::splat(0.5)).distance(player.position)) +                //     .unwrap_or_default(); +                // let movement_ok = speed < PLAYER_SPEED_LIMIT && dist < 1. && interact_dist < 2.; +                // if movement_ok { +                player.position = pos; +                player.last_position_ts = Instant::now(); +                // }                  self.packet_out.push_back(PacketC::Position {                      player: pid,                      pos: player.position,                      rot,                  }); -                if !movement_ok { -                    bail!( -                        "{:?} moved to quickly. speed={speed:.02} dist={dist:.02}", -                        player.name -                    ) -                } +                // if !movement_ok { +                //     bail!( +                //         "{:?} moved to quickly. speed={speed:.02} dist={dist:.02}", +                //         player.name +                //     ) +                // }              }              PacketS::Collide { player, force } => {                  self.packet_out diff --git a/test-client/main.ts b/test-client/main.ts index 42c1a3e0..831513fc 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -17,7 +17,7 @@  */  /// <reference lib="dom" /> -import { player_movement_update } from "./movement.ts"; +import { MovementBase, update_movement } from "./movement.ts";  import { Gamedata, ItemIndex, Message, PacketC, PacketS, PlayerID, TileIndex } from "./protocol.ts";  import { V2, add_v2, lerp_exp_v2_mut, normalize, lerp_exp, sub_v2, length } from "./util.ts";  import { draw_ingame, draw_wait } from "./visual.ts"; @@ -62,18 +62,14 @@ export interface ItemData {      progress_warn?: boolean      remove_anim?: number  } -export interface PlayerData { -    x: number, -    y: number, +export interface PlayerData extends MovementBase {      name: string, -    rot: number,      item?: ItemData, -    facing: V2,      character: number,      anim_position: V2, -    vel: V2,      message?: MessageData,  } +  export interface TileData {      x: number      y: number @@ -112,14 +108,18 @@ function packet(p: PacketC) {              break;          case "add_player": {              players.set(p.id, { -                x: p.position[0], -                y: p.position[1], +                position: { +                    x: p.position[0], +                    y: p.position[1], +                },                  character: p.character,                  name: p.name,                  rot: 0,                  anim_position: { x: 0, y: 1 },                  facing: { x: 0, y: 1 },                  vel: { x: 0, y: 0 }, +                stamina: 0, +                boosting: false,              })              break;          } @@ -129,10 +129,10 @@ function packet(p: PacketC) {          case "position": {              const pl = players.get(p.player)!              const pos = { x: p.pos[0], y: p.pos[1] } -            const dist = length(sub_v2(pl, pos)); +            const dist = length(sub_v2(pl.position, pos));              if (p.player == my_id && dist < 3) return; // we know better where we are -            pl.x = pos.x -            pl.y = pos.y +            pl.position.x = pos.x +            pl.position.y = pos.y              pl.rot = p.rot              break;          } @@ -140,7 +140,7 @@ function packet(p: PacketC) {              const player = players.get(p.player)!              const tile = tiles.get(p.tile.toString())!              player.item = tile.item; -            player.item!.tracking = player +            player.item!.tracking = player.position              tile.item = undefined              break;          } @@ -163,7 +163,7 @@ function packet(p: PacketC) {              const player = players.get(p.player)!              if (player.item !== undefined && player.item !== null) items_removed.add(player.item)              player.item = undefined -            if (p.item !== undefined && p.item !== null) player.item = { kind: p.item, x: player.x + 0.5, y: player.y + 0.5 } +            if (p.item !== undefined && p.item !== null) player.item = { kind: p.item, x: player.position.x + 0.5, y: player.position.y + 0.5 }              break;          }          case "set_active": { @@ -234,8 +234,8 @@ export function get_interact_target(): V2 | undefined {      const me = players.get(my_id)      if (!me) return      return { -        x: Math.floor(me.x + Math.sin(me.rot)), -        y: Math.floor(me.y + Math.cos(me.rot)) +        x: Math.floor(me.position.x + Math.sin(me.rot)), +        y: Math.floor(me.position.y + Math.cos(me.rot))      }  } @@ -248,7 +248,7 @@ function set_interact(edge: boolean) {  function tick_update() {      const p = players.get(my_id)      if (!p) return -    send({ type: "position", pos: [p.x, p.y], rot: p.rot }) +    send({ type: "position", pos: [p.position.x, p.position.y], rot: p.rot })  }  function frame_update(dt: number) { @@ -260,14 +260,14 @@ function frame_update(dt: number) {          y: (+keys_down.has("KeyS") - +keys_down.has("KeyW"))      })      if (interacting) input.x *= 0, input.y *= 0 -    player_movement_update(p, dt, input) +    update_movement(p, dt, input, keys_down.has("KeyN"))      const update_item = (item: ItemData) => {          if (item.tracking) lerp_exp_v2_mut(item, item.tracking, dt * 10.)      }      for (const [pid, player] of players) { -        if (pid == my_id) player.anim_position.x = player.x, player.anim_position.y = player.y -        else lerp_exp_v2_mut(player.anim_position, player, dt * 15) +        if (pid == my_id) player.anim_position.x = player.position.x, player.anim_position.y = player.position.y +        else lerp_exp_v2_mut(player.anim_position, player.position, dt * 15)          if (player.item !== undefined && player.item !== null) update_item(player.item)          if (player.message) player.message.anim_size = lerp_exp(player.message.anim_size, 1, dt * 3)      } @@ -284,7 +284,7 @@ function frame_update(dt: number) {      }      remove.forEach(i => items_removed.delete(i)) -    lerp_exp_v2_mut(camera, p, dt * 10.) +    lerp_exp_v2_mut(camera, p.position, dt * 10.)      const it = get_interact_target() ?? { x: 0, y: 0 };      const possible = data.tile_interact[tiles.get([it.x, it.y].toString())?.kind ?? 0] ?? false diff --git a/test-client/movement.ts b/test-client/movement.ts index 38f4b47b..933b6af5 100644 --- a/test-client/movement.ts +++ b/test-client/movement.ts @@ -16,43 +16,62 @@  */  import { data } from "./main.ts"; -import { tiles, players, PlayerData } from "./main.ts"; +import { tiles, players } from "./main.ts";  import { V2, normalize, length, sub_v2, lerp_exp_v2_mut } from "./util.ts"; -export const PLAYER_SIZE = 0.4; -export const PLAYER_SPEED = 65; +export const PLAYER_SIZE = 0.4 +export const PLAYER_FRICTION = 10 +export const PLAYER_SPEED = 40 +export const BOOST_FACTOR = 3 +export const BOOST_DURATION = 0.3 +export const BOOST_RESTORE = 0.5 -export function player_movement_update(p: PlayerData, dt: number, input: V2) { +export interface MovementBase { +    position: V2, +    vel: V2, +    facing: V2, +    rot: number, +    boosting: boolean, +    stamina: number +} + +export function update_movement(p: MovementBase, dt: number, input: V2, boost: boolean) {      if (length(input) > 0.1) lerp_exp_v2_mut(p.facing, input, dt * 10.)      p.rot = Math.atan2(p.facing.x, p.facing.y) -    p.vel.x += input.x * dt * PLAYER_SPEED -    p.vel.y += input.y * dt * PLAYER_SPEED -    p.x += p.vel.x * dt -    p.y += p.vel.y * dt +    boost &&= length(input) > 0.1 +    p.boosting = boost && (p.boosting || p.stamina >= 1) && p.stamina > 0 +    if (p.boosting) p.stamina -= dt / BOOST_DURATION +    else p.stamina += dt / BOOST_RESTORE +    p.stamina = Math.max(Math.min(p.stamina, 1), 0) +    const speed = PLAYER_SPEED * (p.boosting ? BOOST_FACTOR : 1) +    p.vel.x += input.x * dt * speed +    p.vel.y += input.y * dt * speed +    p.position.x += p.vel.x * dt +    p.position.y += p.vel.y * dt      collide_player(p, dt) -    lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * 15.) +    lerp_exp_v2_mut(p.vel, { x: 0, y: 0 }, dt * PLAYER_FRICTION)  } -function collide_player(p: PlayerData, dt: number) { +function collide_player(p: MovementBase, dt: number) {      for (let xo = -1; xo <= 1; xo++) {          for (let yo = -1; yo <= 1; yo++) { -            const x = Math.floor(p.x) + xo -            const y = Math.floor(p.y) + yo +            const x = Math.floor(p.position.x) + xo +            const y = Math.floor(p.position.y) + yo              const tile = tiles.get([x, y].toString())              if (tile && !data.tile_collide[tile.kind]) continue -            const d = aabb_point_distance(x, y, x + 1, y + 1, p.x, p.y) +            const d = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y)              if (d > PLAYER_SIZE) continue              const h = 0.01 -            const d_sample_x = aabb_point_distance(x, y, x + 1, y + 1, p.x + h, p.y) -            const d_sample_y = aabb_point_distance(x, y, x + 1, y + 1, p.x, p.y + h) +            const d_sample_x = aabb_point_distance(x, y, x + 1, y + 1, p.position.x + h, p.position.y) +            const d_sample_y = aabb_point_distance(x, y, x + 1, y + 1, p.position.x, p.position.y + h)              const grad_x = (d_sample_x - d) / h              const grad_y = (d_sample_y - d) / h -            p.x += (PLAYER_SIZE - d) * grad_x -            p.y += (PLAYER_SIZE - d) * grad_y +            p.position.x += (PLAYER_SIZE - d) * grad_x +            p.position.y += (PLAYER_SIZE - d) * grad_y              const vdotn = (grad_x * p.vel.x) + (grad_y * p.vel.y)              p.vel.x -= grad_x * vdotn @@ -61,7 +80,7 @@ function collide_player(p: PlayerData, dt: number) {      }      for (const [_, player] of players) { -        const diff = sub_v2(p, player) +        const diff = sub_v2(p.position, player.position)          const d = length(diff)          if (d < 0.01) continue          if (d >= PLAYER_SIZE * 2) continue | 
