284 lines
6.8 KiB
Rust
284 lines
6.8 KiB
Rust
use std::{collections::HashSet, f32::consts::PI};
|
|
|
|
use godot::{
|
|
classes::{AudioStreamPlayer3D, GpuParticles3D},
|
|
prelude::*,
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::{
|
|
game_manager::{Game, GameOption},
|
|
net::util::{cast_ray, shotgun_ray},
|
|
player::{DamageSource, IPlayer, ball::Ball, shootable::Shootable},
|
|
};
|
|
|
|
pub trait Weapon {
|
|
fn shoot(
|
|
&mut self,
|
|
player_rid: Rid,
|
|
player_id: u16,
|
|
from: Vector3,
|
|
to: Vector3,
|
|
up: Vector3,
|
|
deal_damage: bool,
|
|
) -> bool;
|
|
fn get_type(&self) -> WeaponType;
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, PartialOrd)]
|
|
pub enum WeaponType {
|
|
Raygun,
|
|
Shotgun,
|
|
Ball,
|
|
}
|
|
|
|
#[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>>,
|
|
|
|
#[export]
|
|
cooldown: f64,
|
|
|
|
cooldown_remaining: f64,
|
|
|
|
base: Base<Node3D>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl INode3D for Raygun {
|
|
fn process(&mut self, delta: f64) {
|
|
self.cooldown_remaining -= delta;
|
|
}
|
|
}
|
|
|
|
#[godot_dyn]
|
|
impl Weapon for Raygun {
|
|
fn shoot(
|
|
&mut self,
|
|
player: Rid,
|
|
player_id: u16,
|
|
from: Vector3,
|
|
to: Vector3,
|
|
_: Vector3,
|
|
deal_damage: bool,
|
|
) -> bool {
|
|
if self.cooldown_remaining > 0. {
|
|
return false;
|
|
}
|
|
|
|
if let Some(emitter) = &mut self.sound_emitter {
|
|
emitter.play();
|
|
}
|
|
|
|
if let Some(source) = &self.bullet_source {
|
|
let target = cast_ray(
|
|
from,
|
|
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);
|
|
|
|
self.run_deferred(move |_| {
|
|
if let Some(mut game) = Game::singleton()
|
|
&& let Some(map) = &mut game.bind_mut().current_map
|
|
{
|
|
map.add_child(&bullet);
|
|
}
|
|
});
|
|
}
|
|
|
|
if deal_damage && let Some(target) = target {
|
|
if let Ok(mut shootable) = target.collider.clone().try_dynify::<dyn Shootable>() {
|
|
godot_print!("{}", target.collider);
|
|
self.run_deferred(move |_| {
|
|
shootable.dyn_bind_mut().on_shoot(
|
|
DamageSource::Player(player_id),
|
|
100,
|
|
dir.normalized(),
|
|
);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
self.cooldown_remaining = self.cooldown;
|
|
|
|
true
|
|
}
|
|
|
|
fn get_type(&self) -> WeaponType {
|
|
WeaponType::Raygun
|
|
}
|
|
}
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=Node3D, init)]
|
|
pub struct Shotgun {
|
|
#[export]
|
|
sound_emitter: Option<Gd<AudioStreamPlayer3D>>,
|
|
#[export]
|
|
vfx: Option<Gd<GpuParticles3D>>,
|
|
|
|
#[export]
|
|
cooldown: f64,
|
|
|
|
cooldown_remaining: f64,
|
|
|
|
base: Base<Node3D>,
|
|
}
|
|
|
|
#[godot_api]
|
|
impl INode3D for Shotgun {
|
|
fn process(&mut self, delta: f64) {
|
|
self.cooldown_remaining -= delta;
|
|
}
|
|
}
|
|
|
|
#[godot_dyn]
|
|
impl Weapon for Shotgun {
|
|
fn shoot(
|
|
&mut self,
|
|
player: Rid,
|
|
player_id: u16,
|
|
from: Vector3,
|
|
to: Vector3,
|
|
up: Vector3,
|
|
deal_damage: bool,
|
|
) -> bool {
|
|
if self.cooldown_remaining > 0. {
|
|
return false;
|
|
}
|
|
|
|
if let Some(audio) = &mut self.sound_emitter {
|
|
audio.play();
|
|
}
|
|
if let Some(vfx) = &mut self.vfx {
|
|
vfx.set_emitting(true);
|
|
}
|
|
|
|
let mut colliders = HashSet::new();
|
|
|
|
for result in shotgun_ray(
|
|
from,
|
|
to,
|
|
up,
|
|
&self.base().get_world_3d(),
|
|
&Array::from_iter(vec![player]),
|
|
10,
|
|
PI / 2.,
|
|
10.,
|
|
) {
|
|
colliders.insert(result.collider);
|
|
}
|
|
|
|
if deal_damage {
|
|
for collider in colliders {
|
|
let collider_pos = if let Ok(node) = collider.clone().try_cast::<Node3D>() {
|
|
node.get_global_position()
|
|
} else {
|
|
Vector3::ZERO
|
|
};
|
|
let delta = collider_pos - from;
|
|
|
|
if let Ok(mut shootable) = collider.clone().try_dynify::<dyn Shootable>() {
|
|
godot_print!("{} ({})", collider, delta.length());
|
|
self.run_deferred(move |_| {
|
|
shootable.dyn_bind_mut().on_shoot(
|
|
DamageSource::Player(player_id),
|
|
(500. / delta.length()) as i32,
|
|
delta.normalized(),
|
|
);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
self.cooldown_remaining = self.cooldown;
|
|
|
|
true
|
|
}
|
|
|
|
fn get_type(&self) -> WeaponType {
|
|
WeaponType::Shotgun
|
|
}
|
|
}
|
|
|
|
#[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();
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(GodotClass)]
|
|
#[class(base=Node3D, init)]
|
|
pub struct BallWeapon {
|
|
base: Base<Node3D>,
|
|
}
|
|
|
|
#[godot_dyn]
|
|
impl Weapon for BallWeapon {
|
|
fn shoot(&mut self, _: Rid, _: u16, _: Vector3, _: Vector3, _: Vector3, _: bool) -> bool {
|
|
true
|
|
}
|
|
|
|
fn get_type(&self) -> WeaponType {
|
|
WeaponType::Ball
|
|
}
|
|
}
|