85 lines
2.1 KiB
Rust
85 lines
2.1 KiB
Rust
use godot::{
|
|
classes::{AudioStreamPlayer3D, GpuParticles3D, PhysicsRayQueryParameters3D},
|
|
prelude::*,
|
|
};
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=Node3D, init)]
|
|
pub struct Weapon {
|
|
#[export]
|
|
sound_emitter: Option<Gd<AudioStreamPlayer3D>>,
|
|
#[export]
|
|
bullet_prefab: Option<Gd<PackedScene>>,
|
|
#[export]
|
|
bullet_source: Option<Gd<Node3D>>,
|
|
|
|
base: Base<Node3D>,
|
|
}
|
|
|
|
impl Weapon {
|
|
pub fn shoot(&mut self, to: Vector3, deal_damage: bool) {
|
|
if let Some(emitter) = &mut self.sound_emitter {
|
|
emitter.play();
|
|
}
|
|
|
|
if let Some(prefab) = &self.bullet_prefab {
|
|
let mut bullet = prefab.instantiate_as::<Bullet>();
|
|
|
|
if let Some(source) = &self.bullet_source {
|
|
bullet.bind_mut().source = Some(source.get_global_position());
|
|
}
|
|
bullet.bind_mut().target = Some(to);
|
|
|
|
if let Some(mut parent) = self.base().find_parent("map") {
|
|
parent.add_child(&bullet);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=Node3D, init)]
|
|
pub struct Bullet {
|
|
pub source: Option<Vector3>,
|
|
pub target: Option<Vector3>,
|
|
|
|
alive_for: f64,
|
|
|
|
distance: f32,
|
|
girth: f32,
|
|
|
|
base: Base<Node3D>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl INode3D for Bullet {
|
|
fn ready(&mut self) {
|
|
if let (Some(target), Some(source)) = (self.target, self.source) {
|
|
let midway_point = (target + source) / 2.;
|
|
self.distance = source.distance_to(target);
|
|
self.girth = 1.;
|
|
|
|
self.scale();
|
|
self.base_mut().look_at_from_position(source, target);
|
|
self.base_mut().set_global_position(midway_point);
|
|
}
|
|
}
|
|
|
|
fn process(&mut self, _delta: f32) {
|
|
self.girth = self.girth.lerp(0., 0.15);
|
|
self.scale();
|
|
}
|
|
}
|
|
|
|
impl Bullet {
|
|
fn scale(&mut self) {
|
|
let length = Vector3::BACK * self.distance;
|
|
let girth_scale = (Vector3::UP + Vector3::RIGHT) * self.girth;
|
|
self.base_mut().set_scale(length + girth_scale);
|
|
|
|
if self.girth < 0.05 {
|
|
self.base_mut().queue_free();
|
|
}
|
|
}
|
|
}
|