Move jumping code to PlayerMovement

This commit is contained in:
Sofia 2026-07-22 03:18:07 +03:00
parent 949585dc5c
commit 5d14a2675c
4 changed files with 24 additions and 14 deletions

View File

@ -310,7 +310,7 @@ impl Game {
self.self_id = Some(id);
}
pub fn new_player(&mut self, addr: SocketAddr, name: String, id: Option<u16>) -> u16 {
pub fn new_player(&mut self, addr: SocketAddr, name: Option<String>, id: Option<u16>) -> u16 {
let id = if let Some(id) = id {
id
} else {
@ -322,7 +322,7 @@ impl Game {
connection_addr: addr,
data: NetPlayer {
id,
name,
name: name.unwrap_or(format!("Player {}", id)),
ping: 0,
team: 0,
},

View File

@ -175,7 +175,7 @@ impl NetworkManager {
if let Some(game) = &mut Game::singleton() {
let id = game.bind_mut().new_player(
SocketAddr::from(([0, 0, 0, 0], 0)),
"Host".to_owned(),
Some("Host".to_owned()),
None,
);
game.bind_mut().update_player_ready(id, true);

View File

@ -73,7 +73,7 @@ impl NetworkManager {
if player.id != game.bind().self_id.unwrap_or(u16::MAX) {
game.bind_mut().new_player(
SocketAddr::from(([0, 0, 0, 0], 0)),
player.name,
Some(player.name),
Some(player.id),
);
}
@ -178,8 +178,7 @@ impl NetworkManager {
if let Some(game) = &mut Game::singleton() {
let self_id =
game.bind_mut()
.new_player(connection.address, String::new(), None);
game.bind_mut().new_player(connection.address, None, None);
peer.send_reliable(
&connection.address,
Package::Players(

View File

@ -58,6 +58,7 @@ pub trait IPlayer {
pub trait PlayerMovement {
fn player_update(&mut self, delta: f64);
fn try_jump(&mut self) -> bool;
fn drop_ball_common(&mut self, with_velocity: bool) -> bool;
fn get_look_dir(&self) -> Vector3;
}
@ -85,6 +86,15 @@ impl<T: IPlayer + ICharacterBody3D + WithBaseField> PlayerMovement for T {
}
}
fn try_jump(&mut self) -> bool {
if self.base().is_on_floor() {
self.set_y_speed(5.);
true
} else {
false
}
}
fn drop_ball_common(&mut self, with_velocity: bool) -> bool {
if let Some(weapon) = self.get_weapon()
&& let Ok(_) = weapon.try_cast::<BallWeapon>()
@ -219,12 +229,13 @@ impl IPlayer for LocalPlayer {
}
fn jump(&mut self) {
self.set_y_speed(5.);
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer {
PeerKind::Client(peer, addr) => peer.send_reliable(&addr, Jump(0)),
PeerKind::Server(peer, _) => {
peer.broadcast_reliable(Jump(self.player_id.unwrap_or(0)))
if self.try_jump() {
if let Some(peer) = &mut NetworkManager::singleton().bind_mut().peer {
match peer {
PeerKind::Client(peer, addr) => peer.send_reliable(&addr, Jump(0)),
PeerKind::Server(peer, _) => {
peer.broadcast_reliable(Jump(self.player_id.unwrap_or(0)))
}
}
}
}
@ -398,7 +409,7 @@ impl ICharacterBody3D for LocalPlayer {
let input = Input::singleton();
if self.base().is_on_floor() && input.is_action_just_pressed("jump") {
self.jump();
self.try_jump();
}
Vector3::RIGHT * input.is_action_pressed("right") as u32 as f32
@ -597,7 +608,7 @@ impl IPlayer for RemotePlayer {
}
fn jump(&mut self) {
self.set_y_speed(5.);
self.try_jump();
}
fn get_look_up(&self) -> f32 {