Add dyn weapon separately
This commit is contained in:
parent
c588b7cab4
commit
0d7f5a6bf6
@ -21,7 +21,7 @@ use crate::{
|
||||
},
|
||||
soldier_mesh::SoldierMesh,
|
||||
ui::cli::CommandLinePanel,
|
||||
weapon::Raygun,
|
||||
weapon::{Raygun, Weapon},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
@ -83,7 +83,7 @@ pub struct LocalPlayer {
|
||||
soldier_mesh: Option<Gd<SoldierMesh>>,
|
||||
|
||||
pub player_id: Option<u16>,
|
||||
pub weapon: Option<Gd<Raygun>>,
|
||||
pub weapon: Option<DynGd<Node3D, dyn Weapon>>,
|
||||
|
||||
movement_dir: Vector3,
|
||||
y_speed: f32,
|
||||
@ -157,7 +157,7 @@ impl IPlayer for LocalPlayer {
|
||||
fn try_shoot(&mut self, to: Vector3, deal_damage: bool) -> bool {
|
||||
if self.shoot_cd <= 0. {
|
||||
if let Some(weapon) = &mut self.weapon {
|
||||
weapon.bind_mut().shoot(to, deal_damage);
|
||||
weapon.dyn_bind_mut().shoot(to, deal_damage);
|
||||
}
|
||||
self.shoot_cd = 0.2;
|
||||
true
|
||||
@ -314,7 +314,7 @@ pub struct RemotePlayer {
|
||||
soldier_mesh: Option<Gd<SoldierMesh>>,
|
||||
|
||||
pub player_id: Option<u16>,
|
||||
pub weapon: Option<Gd<Raygun>>,
|
||||
pub weapon: Option<DynGd<Node3D, dyn Weapon>>,
|
||||
|
||||
movement_dir: Vector3,
|
||||
y_speed: f32,
|
||||
@ -384,7 +384,7 @@ impl IPlayer for RemotePlayer {
|
||||
fn try_shoot(&mut self, to: Vector3, deal_damage: bool) -> bool {
|
||||
if self.shoot_cd <= 0. {
|
||||
if let Some(weapon) = &mut self.weapon {
|
||||
weapon.bind_mut().shoot(to, deal_damage);
|
||||
weapon.dyn_bind_mut().shoot(to, deal_damage);
|
||||
}
|
||||
self.shoot_cd = 0.2;
|
||||
true
|
||||
|
||||
@ -6,7 +6,7 @@ use godot::{
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
use crate::weapon::Raygun;
|
||||
use crate::weapon::{Raygun, Weapon};
|
||||
|
||||
#[derive(GodotClass)]
|
||||
#[class(base=Node3D, init)]
|
||||
@ -43,16 +43,19 @@ impl SoldierMesh {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn_raygun(&mut self) -> Option<Gd<Raygun>> {
|
||||
pub fn spawn_raygun(&mut self) -> Option<DynGd<Node3D, dyn Weapon>> {
|
||||
if let Some(raygun) = &self.raygun {
|
||||
let node = raygun.instantiate_as::<Raygun>();
|
||||
self.spawn_weapon(node)
|
||||
self.spawn_weapon(node.into_dyn().upcast())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_weapon(&mut self, node: Gd<Raygun>) -> Option<Gd<Raygun>> {
|
||||
fn spawn_weapon(
|
||||
&mut self,
|
||||
node: DynGd<Node3D, dyn Weapon>,
|
||||
) -> Option<DynGd<Node3D, dyn Weapon>> {
|
||||
if let Some(hand) = &mut self.hand_attachment {
|
||||
hand.add_child(&node);
|
||||
Some(node)
|
||||
|
||||
@ -3,6 +3,10 @@ use godot::{
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
pub trait Weapon {
|
||||
fn shoot(&mut self, to: Vector3, deal_damage: bool);
|
||||
}
|
||||
|
||||
#[derive(GodotClass)]
|
||||
#[class(base=Node3D, init)]
|
||||
pub struct Raygun {
|
||||
@ -16,8 +20,9 @@ pub struct Raygun {
|
||||
base: Base<Node3D>,
|
||||
}
|
||||
|
||||
impl Raygun {
|
||||
pub fn shoot(&mut self, to: Vector3, deal_damage: bool) {
|
||||
#[godot_dyn]
|
||||
impl Weapon for Raygun {
|
||||
fn shoot(&mut self, to: Vector3, deal_damage: bool) {
|
||||
if let Some(emitter) = &mut self.sound_emitter {
|
||||
emitter.play();
|
||||
}
|
||||
@ -43,8 +48,6 @@ pub struct Beam {
|
||||
pub source: Option<Vector3>,
|
||||
pub target: Option<Vector3>,
|
||||
|
||||
alive_for: f64,
|
||||
|
||||
distance: f32,
|
||||
girth: f32,
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user