Refactor code
This commit is contained in:
parent
71c5ca7947
commit
e2c02d45ae
@ -23,10 +23,10 @@ use crate::{
|
|||||||
},
|
},
|
||||||
PeerKind, SyncPackage, TelegrenadeSync,
|
PeerKind, SyncPackage, TelegrenadeSync,
|
||||||
},
|
},
|
||||||
util::NetVector3,
|
util::{NetTransform, NetVector3},
|
||||||
},
|
},
|
||||||
player::{
|
player::{
|
||||||
DamageSource, IPlayer, NetTransform,
|
DamageSource, IPlayer,
|
||||||
ball::Ball,
|
ball::Ball,
|
||||||
buffs::{Buff, BuffKind},
|
buffs::{Buff, BuffKind},
|
||||||
local_player::LocalPlayer,
|
local_player::LocalPlayer,
|
||||||
|
|||||||
@ -16,6 +16,7 @@ pub mod popup_queue;
|
|||||||
pub mod replay;
|
pub mod replay;
|
||||||
pub mod team_resource;
|
pub mod team_resource;
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
struct MyExtension;
|
struct MyExtension;
|
||||||
|
|
||||||
|
|||||||
@ -5,9 +5,9 @@ use godot::{classes::CharacterBody3D, prelude::*};
|
|||||||
use crate::{
|
use crate::{
|
||||||
game::{Game, GameManager},
|
game::{Game, GameManager},
|
||||||
map::{pickup::Pickup, triggers::Triggerable},
|
map::{pickup::Pickup, triggers::Triggerable},
|
||||||
net::util::NetVector3,
|
net::util::{NetTransform, NetVector3},
|
||||||
player::{
|
player::{
|
||||||
IPlayer, NetTransform, ball::Ball, local_player::LocalPlayer, remote_player::RemotePlayer,
|
IPlayer, ball::Ball, local_player::LocalPlayer, remote_player::RemotePlayer,
|
||||||
telegrenade::Telegrenade,
|
telegrenade::Telegrenade,
|
||||||
},
|
},
|
||||||
replay::{ReplayPlayer, ReplayPlayerState, StandaloneReplayManager},
|
replay::{ReplayPlayer, ReplayPlayerState, StandaloneReplayManager},
|
||||||
|
|||||||
@ -15,8 +15,11 @@ use crate::{
|
|||||||
opts::{GameOption, GameOptionValue, GameOptions},
|
opts::{GameOption, GameOptionValue, GameOptions},
|
||||||
},
|
},
|
||||||
game_settings::GameSettingsManager,
|
game_settings::GameSettingsManager,
|
||||||
net::{net_stats::Stats, util::NetVector3},
|
net::{
|
||||||
player::{DamageSource, NetTransform, weapon::WeaponType},
|
net_stats::Stats,
|
||||||
|
util::{NetTransform, NetVector3},
|
||||||
|
},
|
||||||
|
player::{DamageSource, weapon::WeaponType},
|
||||||
popup_queue::{Popup, PopupQueue},
|
popup_queue::{Popup, PopupQueue},
|
||||||
ui::cli::{CliColor, CommandLinePanel},
|
ui::cli::{CliColor, CommandLinePanel},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -27,79 +27,8 @@ impl From<NetVector3> for Vector3 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cast_ray(
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||||
from: Vector3,
|
pub struct NetTransform {
|
||||||
to: Vector3,
|
pub location: NetVector3,
|
||||||
world: &Option<Gd<World3D>>,
|
pub rotation: NetVector3,
|
||||||
exclude: &Array<Rid>,
|
|
||||||
) -> Option<RaycastResult> {
|
|
||||||
let ray = PhysicsRayQueryParameters3D::create(from, to);
|
|
||||||
if let Some(mut ray) = ray {
|
|
||||||
ray.set_collision_mask(0b1111);
|
|
||||||
ray.set_hit_back_faces(false);
|
|
||||||
ray.set_hit_from_inside(false);
|
|
||||||
ray.set_collide_with_bodies(true);
|
|
||||||
ray.set_exclude(&exclude);
|
|
||||||
if let Some(world) = world
|
|
||||||
&& let Some(mut space) = world.get_direct_space_state()
|
|
||||||
{
|
|
||||||
let results = space.intersect_ray(&ray);
|
|
||||||
if let Some(position) = results.get("position")
|
|
||||||
&& let Some(collider) = results.get("collider")
|
|
||||||
&& let Some(normal) = results.get("normal")
|
|
||||||
{
|
|
||||||
Some(RaycastResult {
|
|
||||||
position: Vector3::from_variant(&position),
|
|
||||||
normal: Vector3::from_variant(&normal),
|
|
||||||
collider: Gd::from_variant(&collider),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn shotgun_ray(
|
|
||||||
from: Vector3,
|
|
||||||
to: Vector3,
|
|
||||||
up: Vector3,
|
|
||||||
world: &Option<Gd<World3D>>,
|
|
||||||
exclude: &Array<Rid>,
|
|
||||||
density: u8,
|
|
||||||
angle: f32,
|
|
||||||
distance: f32,
|
|
||||||
) -> Vec<RaycastResult> {
|
|
||||||
let mut results = Vec::new();
|
|
||||||
|
|
||||||
let original_dir = (to - from).normalized();
|
|
||||||
let step = angle / density as f32;
|
|
||||||
|
|
||||||
let up = up.normalized();
|
|
||||||
let right = original_dir.cross(up).normalized();
|
|
||||||
|
|
||||||
for y in 0..density {
|
|
||||||
let y_rot = -(angle / 2.) + step * y as f32;
|
|
||||||
for x in 0..density {
|
|
||||||
let x_rot = -(angle / 2.) + step * x as f32;
|
|
||||||
let dir = original_dir.rotated(up, x_rot).rotated(right, y_rot);
|
|
||||||
let to = from + dir * distance;
|
|
||||||
if let Some(res) = cast_ray(from, to, world, exclude) {
|
|
||||||
results.push(res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct RaycastResult {
|
|
||||||
pub position: Vector3,
|
|
||||||
pub normal: Vector3,
|
|
||||||
pub collider: Gd<Object>,
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,11 @@ use godot::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
net::{network_manager::BallSync, util::NetVector3},
|
net::{
|
||||||
player::{DamageSource, IPlayer, NetTransform, shootable::Shootable},
|
network_manager::BallSync,
|
||||||
|
util::{NetTransform, NetVector3},
|
||||||
|
},
|
||||||
|
player::{DamageSource, IPlayer, shootable::Shootable},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(GodotClass)]
|
#[derive(GodotClass)]
|
||||||
|
|||||||
@ -19,10 +19,10 @@ use crate::{
|
|||||||
Package::{self, Jump, Punch, Shoot, ThrowTelegrenade},
|
Package::{self, Jump, Punch, Shoot, ThrowTelegrenade},
|
||||||
PeerKind,
|
PeerKind,
|
||||||
},
|
},
|
||||||
util::cast_ray,
|
util::NetTransform,
|
||||||
},
|
},
|
||||||
player::{
|
player::{
|
||||||
DamageSource, DamageSourceSignal, IPlayer, NetTransform, PlayerCommon,
|
DamageSource, DamageSourceSignal, IPlayer, PlayerCommon,
|
||||||
buffs::{Buff, Buffs},
|
buffs::{Buff, Buffs},
|
||||||
effects::PlayerEffects,
|
effects::PlayerEffects,
|
||||||
remote_player::RemotePlayer,
|
remote_player::RemotePlayer,
|
||||||
@ -33,6 +33,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
replay::ReplayEvent,
|
replay::ReplayEvent,
|
||||||
ui::{cli::CommandLinePanel, cooldown_icon::CooldownIcon, hud::Hud},
|
ui::{cli::CommandLinePanel, cooldown_icon::CooldownIcon, hud::Hud},
|
||||||
|
util::cast_ray,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(GodotClass)]
|
#[derive(GodotClass)]
|
||||||
|
|||||||
@ -10,16 +10,14 @@ use serde::{Deserialize, Serialize};
|
|||||||
use crate::{
|
use crate::{
|
||||||
game::{Game, opts::GameOption},
|
game::{Game, opts::GameOption},
|
||||||
map::{killbox::KillboxKind, pickup::PickupKind},
|
map::{killbox::KillboxKind, pickup::PickupKind},
|
||||||
net::{
|
net::{network_manager::NetworkManager, util::NetTransform},
|
||||||
network_manager::NetworkManager,
|
|
||||||
util::{NetVector3, shotgun_ray},
|
|
||||||
},
|
|
||||||
player::{
|
player::{
|
||||||
buffs::{Buff, Buffs},
|
buffs::{Buff, Buffs},
|
||||||
shootable::Shootable,
|
shootable::Shootable,
|
||||||
telegrenade::Telegrenade,
|
telegrenade::Telegrenade,
|
||||||
weapon::{BallWeapon, Weapon, WeaponType},
|
weapon::{BallWeapon, Weapon, WeaponType},
|
||||||
},
|
},
|
||||||
|
util::shotgun_ray,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod ball;
|
pub mod ball;
|
||||||
@ -34,12 +32,6 @@ pub mod soldier_mesh;
|
|||||||
pub mod telegrenade;
|
pub mod telegrenade;
|
||||||
pub mod weapon;
|
pub mod weapon;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
||||||
pub struct NetTransform {
|
|
||||||
pub location: NetVector3,
|
|
||||||
pub rotation: NetVector3,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait IPlayer {
|
pub trait IPlayer {
|
||||||
fn get_player_id(&self) -> Option<u16>;
|
fn get_player_id(&self) -> Option<u16>;
|
||||||
fn get_y_speed(&self) -> f32;
|
fn get_y_speed(&self) -> f32;
|
||||||
|
|||||||
@ -8,9 +8,9 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
game::Game,
|
game::Game,
|
||||||
net::util::{RaycastResult, cast_ray, shotgun_ray},
|
|
||||||
player::{DamageSource, shootable::Shootable},
|
player::{DamageSource, shootable::Shootable},
|
||||||
replay::StandaloneReplayManager,
|
replay::StandaloneReplayManager,
|
||||||
|
util::{RaycastResult, cast_ray, shotgun_ray},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait Weapon {
|
pub trait Weapon {
|
||||||
|
|||||||
@ -18,9 +18,12 @@ use crate::{
|
|||||||
Map,
|
Map,
|
||||||
pickup::{PickupKind, ReplayVisibility},
|
pickup::{PickupKind, ReplayVisibility},
|
||||||
},
|
},
|
||||||
net::{network_manager::BallSync, util::NetVector3},
|
net::{
|
||||||
|
network_manager::BallSync,
|
||||||
|
util::{NetTransform, NetVector3},
|
||||||
|
},
|
||||||
player::{
|
player::{
|
||||||
DamageSource, IPlayer, NetTransform, ball::Ball, buffs::Buffs, remote_player::RemotePlayer,
|
DamageSource, IPlayer, ball::Ball, buffs::Buffs, remote_player::RemotePlayer,
|
||||||
telegrenade::Telegrenade, weapon::WeaponType,
|
telegrenade::Telegrenade, weapon::WeaponType,
|
||||||
},
|
},
|
||||||
popup_queue::{Popup, PopupQueue},
|
popup_queue::{Popup, PopupQueue},
|
||||||
|
|||||||
81
rust/src/util.rs
Normal file
81
rust/src/util.rs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
use godot::{
|
||||||
|
classes::{PhysicsRayQueryParameters3D, World3D},
|
||||||
|
prelude::*,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn cast_ray(
|
||||||
|
from: Vector3,
|
||||||
|
to: Vector3,
|
||||||
|
world: &Option<Gd<World3D>>,
|
||||||
|
exclude: &Array<Rid>,
|
||||||
|
) -> Option<RaycastResult> {
|
||||||
|
let ray = PhysicsRayQueryParameters3D::create(from, to);
|
||||||
|
if let Some(mut ray) = ray {
|
||||||
|
ray.set_collision_mask(0b1111);
|
||||||
|
ray.set_hit_back_faces(false);
|
||||||
|
ray.set_hit_from_inside(false);
|
||||||
|
ray.set_collide_with_bodies(true);
|
||||||
|
ray.set_exclude(&exclude);
|
||||||
|
if let Some(world) = world
|
||||||
|
&& let Some(mut space) = world.get_direct_space_state()
|
||||||
|
{
|
||||||
|
let results = space.intersect_ray(&ray);
|
||||||
|
if let Some(position) = results.get("position")
|
||||||
|
&& let Some(collider) = results.get("collider")
|
||||||
|
&& let Some(normal) = results.get("normal")
|
||||||
|
{
|
||||||
|
Some(RaycastResult {
|
||||||
|
position: Vector3::from_variant(&position),
|
||||||
|
normal: Vector3::from_variant(&normal),
|
||||||
|
collider: Gd::from_variant(&collider),
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn shotgun_ray(
|
||||||
|
from: Vector3,
|
||||||
|
to: Vector3,
|
||||||
|
up: Vector3,
|
||||||
|
world: &Option<Gd<World3D>>,
|
||||||
|
exclude: &Array<Rid>,
|
||||||
|
density: u8,
|
||||||
|
angle: f32,
|
||||||
|
distance: f32,
|
||||||
|
) -> Vec<RaycastResult> {
|
||||||
|
let mut results = Vec::new();
|
||||||
|
|
||||||
|
let original_dir = (to - from).normalized();
|
||||||
|
let step = angle / density as f32;
|
||||||
|
|
||||||
|
let up = up.normalized();
|
||||||
|
let right = original_dir.cross(up).normalized();
|
||||||
|
|
||||||
|
for y in 0..density {
|
||||||
|
let y_rot = -(angle / 2.) + step * y as f32;
|
||||||
|
for x in 0..density {
|
||||||
|
let x_rot = -(angle / 2.) + step * x as f32;
|
||||||
|
let dir = original_dir.rotated(up, x_rot).rotated(right, y_rot);
|
||||||
|
let to = from + dir * distance;
|
||||||
|
if let Some(res) = cast_ray(from, to, world, exclude) {
|
||||||
|
results.push(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct RaycastResult {
|
||||||
|
pub position: Vector3,
|
||||||
|
pub normal: Vector3,
|
||||||
|
pub collider: Gd<Object>,
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user