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); 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 { let id = if let Some(id) = id {
id id
} else { } else {
@ -322,7 +322,7 @@ impl Game {
connection_addr: addr, connection_addr: addr,
data: NetPlayer { data: NetPlayer {
id, id,
name, name: name.unwrap_or(format!("Player {}", id)),
ping: 0, ping: 0,
team: 0, team: 0,
}, },

View File

@ -175,7 +175,7 @@ impl NetworkManager {
if let Some(game) = &mut Game::singleton() { if let Some(game) = &mut Game::singleton() {
let id = game.bind_mut().new_player( let id = game.bind_mut().new_player(
SocketAddr::from(([0, 0, 0, 0], 0)), SocketAddr::from(([0, 0, 0, 0], 0)),
"Host".to_owned(), Some("Host".to_owned()),
None, None,
); );
game.bind_mut().update_player_ready(id, true); 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) { if player.id != game.bind().self_id.unwrap_or(u16::MAX) {
game.bind_mut().new_player( game.bind_mut().new_player(
SocketAddr::from(([0, 0, 0, 0], 0)), SocketAddr::from(([0, 0, 0, 0], 0)),
player.name, Some(player.name),
Some(player.id), Some(player.id),
); );
} }
@ -178,8 +178,7 @@ impl NetworkManager {
if let Some(game) = &mut Game::singleton() { if let Some(game) = &mut Game::singleton() {
let self_id = let self_id =
game.bind_mut() game.bind_mut().new_player(connection.address, None, None);
.new_player(connection.address, String::new(), None);
peer.send_reliable( peer.send_reliable(
&connection.address, &connection.address,
Package::Players( Package::Players(

View File

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