109 lines
2.9 KiB
Rust
109 lines
2.9 KiB
Rust
use godot::{
|
|
classes::{AudioStreamPlayer3D, GpuParticles3D, PhysicsRayQueryParameters3D},
|
|
prelude::*,
|
|
};
|
|
|
|
use crate::{net::util::cast_ray, player::IPlayer};
|
|
|
|
pub trait Weapon {
|
|
fn shoot(&mut self, player: Rid, to: Vector3, deal_damage: bool);
|
|
}
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=Node3D, init)]
|
|
pub struct Raygun {
|
|
#[export]
|
|
sound_emitter: Option<Gd<AudioStreamPlayer3D>>,
|
|
#[export]
|
|
bullet_prefab: Option<Gd<PackedScene>>,
|
|
#[export]
|
|
bullet_source: Option<Gd<Node3D>>,
|
|
|
|
base: Base<Node3D>,
|
|
}
|
|
|
|
#[godot_dyn]
|
|
impl Weapon for Raygun {
|
|
fn shoot(&mut self, player: Rid, to: Vector3, deal_damage: bool) {
|
|
if let Some(emitter) = &mut self.sound_emitter {
|
|
emitter.play();
|
|
}
|
|
|
|
if let Some(source) = &self.bullet_source {
|
|
let target = cast_ray(
|
|
source.get_global_position(),
|
|
to,
|
|
self.base().get_world_3d(),
|
|
Array::from_iter(vec![player]),
|
|
);
|
|
|
|
let dir = (to - source.get_global_position()).normalized();
|
|
let beam_end = target
|
|
.as_ref()
|
|
.map(|r| r.position)
|
|
.unwrap_or(source.get_global_position() + dir * 100.);
|
|
|
|
if let Some(prefab) = &self.bullet_prefab {
|
|
let mut bullet = prefab.instantiate_as::<Beam>();
|
|
|
|
bullet.bind_mut().source = Some(source.get_global_position());
|
|
bullet.bind_mut().target = Some(beam_end);
|
|
|
|
if let Some(mut parent) = self.base().find_parent("map") {
|
|
parent.add_child(&bullet);
|
|
}
|
|
}
|
|
|
|
if deal_damage && let Some(target) = target {
|
|
if let Ok(mut player) = target.collider.try_dynify::<dyn IPlayer>() {
|
|
player.dyn_bind_mut().take_damage(100, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=Node3D, init)]
|
|
pub struct Beam {
|
|
pub source: Option<Vector3>,
|
|
pub target: Option<Vector3>,
|
|
|
|
distance: f32,
|
|
girth: f32,
|
|
|
|
base: Base<Node3D>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl INode3D for Beam {
|
|
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.07);
|
|
self.scale();
|
|
}
|
|
}
|
|
|
|
impl Beam {
|
|
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.01 {
|
|
self.base_mut().queue_free();
|
|
}
|
|
}
|
|
}
|