45 lines
1.0 KiB
Rust
45 lines
1.0 KiB
Rust
use godot::{
|
|
classes::{IRigidBody3D, RigidBody3D},
|
|
prelude::*,
|
|
};
|
|
|
|
use crate::player::{IPlayer, NetTransform};
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=RigidBody3D, init)]
|
|
pub struct Ball {
|
|
base: Base<RigidBody3D>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl IRigidBody3D for Ball {
|
|
fn ready(&mut self) {
|
|
self.base()
|
|
.signals()
|
|
.body_entered()
|
|
.connect_other(self, |s, body| s.on_collision(body));
|
|
}
|
|
}
|
|
|
|
#[godot_api]
|
|
impl Ball {
|
|
#[signal]
|
|
pub fn on_ball_pickup(player_id: u16);
|
|
|
|
pub fn get_transform(&self) -> NetTransform {
|
|
NetTransform {
|
|
location: self.base().get_global_position().into(),
|
|
rotation: self.base().get_global_rotation().into(),
|
|
}
|
|
}
|
|
|
|
fn on_collision(&mut self, node: Gd<Node>) {
|
|
godot_print!("ball collision");
|
|
if let Ok(player) = node.try_dynify::<dyn IPlayer>()
|
|
&& let Some(player_id) = player.dyn_bind().get_player_id()
|
|
{
|
|
self.run_deferred(move |s| s.signals().on_ball_pickup().emit(player_id));
|
|
}
|
|
}
|
|
}
|