Refactor code
This commit is contained in:
parent
71c5ca7947
commit
e2c02d45ae
@ -23,10 +23,10 @@ use crate::{
|
||||
},
|
||||
PeerKind, SyncPackage, TelegrenadeSync,
|
||||
},
|
||||
util::NetVector3,
|
||||
util::{NetTransform, NetVector3},
|
||||
},
|
||||
player::{
|
||||
DamageSource, IPlayer, NetTransform,
|
||||
DamageSource, IPlayer,
|
||||
ball::Ball,
|
||||
buffs::{Buff, BuffKind},
|
||||
local_player::LocalPlayer,
|
||||
|
||||
@ -16,6 +16,7 @@ pub mod popup_queue;
|
||||
pub mod replay;
|
||||
pub mod team_resource;
|
||||
pub mod ui;
|
||||
pub mod util;
|
||||
|
||||
struct MyExtension;
|
||||
|
||||
|
||||
@ -5,9 +5,9 @@ use godot::{classes::CharacterBody3D, prelude::*};
|
||||
use crate::{
|
||||
game::{Game, GameManager},
|
||||
map::{pickup::Pickup, triggers::Triggerable},
|
||||
net::util::NetVector3,
|
||||
net::util::{NetTransform, NetVector3},
|
||||
player::{
|
||||
IPlayer, NetTransform, ball::Ball, local_player::LocalPlayer, remote_player::RemotePlayer,
|
||||
IPlayer, ball::Ball, local_player::LocalPlayer, remote_player::RemotePlayer,
|
||||
telegrenade::Telegrenade,
|
||||
},
|
||||
replay::{ReplayPlayer, ReplayPlayerState, StandaloneReplayManager},
|
||||
|
||||
@ -15,8 +15,11 @@ use crate::{
|
||||
opts::{GameOption, GameOptionValue, GameOptions},
|
||||
},
|
||||
game_settings::GameSettingsManager,
|
||||
net::{net_stats::Stats, util::NetVector3},
|
||||
player::{DamageSource, NetTransform, weapon::WeaponType},
|
||||
net::{
|
||||
net_stats::Stats,
|
||||
util::{NetTransform, NetVector3},
|
||||
},
|
||||
player::{DamageSource, weapon::WeaponType},
|
||||
popup_queue::{Popup, PopupQueue},
|
||||
ui::cli::{CliColor, CommandLinePanel},
|
||||
};
|
||||
|
||||
@ -27,79 +27,8 @@ impl From<NetVector3> for Vector3 {
|
||||
}
|
||||
}
|
||||
|
||||
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>,
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct NetTransform {
|
||||
pub location: NetVector3,
|
||||
pub rotation: NetVector3,
|
||||
}
|
||||
|
||||
@ -4,8 +4,11 @@ use godot::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
net::{network_manager::BallSync, util::NetVector3},
|
||||
player::{DamageSource, IPlayer, NetTransform, shootable::Shootable},
|
||||
net::{
|
||||
network_manager::BallSync,
|
||||
util::{NetTransform, NetVector3},
|
||||
},
|
||||
player::{DamageSource, IPlayer, shootable::Shootable},
|
||||
};
|
||||
|
||||
#[derive(GodotClass)]
|
||||
|
||||
@ -19,10 +19,10 @@ use crate::{
|
||||
Package::{self, Jump, Punch, Shoot, ThrowTelegrenade},
|
||||
PeerKind,
|
||||
},
|
||||
util::cast_ray,
|
||||
util::NetTransform,
|
||||
},
|
||||
player::{
|
||||
DamageSource, DamageSourceSignal, IPlayer, NetTransform, PlayerCommon,
|
||||
DamageSource, DamageSourceSignal, IPlayer, PlayerCommon,
|
||||
buffs::{Buff, Buffs},
|
||||
effects::PlayerEffects,
|
||||
remote_player::RemotePlayer,
|
||||
@ -33,6 +33,7 @@ use crate::{
|
||||
},
|
||||
replay::ReplayEvent,
|
||||
ui::{cli::CommandLinePanel, cooldown_icon::CooldownIcon, hud::Hud},
|
||||
util::cast_ray,
|
||||
};
|
||||
|
||||
#[derive(GodotClass)]
|
||||
|
||||
@ -10,16 +10,14 @@ use serde::{Deserialize, Serialize};
|
||||
use crate::{
|
||||
game::{Game, opts::GameOption},
|
||||
map::{killbox::KillboxKind, pickup::PickupKind},
|
||||
net::{
|
||||
network_manager::NetworkManager,
|
||||
util::{NetVector3, shotgun_ray},
|
||||
},
|
||||
net::{network_manager::NetworkManager, util::NetTransform},
|
||||
player::{
|
||||
buffs::{Buff, Buffs},
|
||||
shootable::Shootable,
|
||||
telegrenade::Telegrenade,
|
||||
weapon::{BallWeapon, Weapon, WeaponType},
|
||||
},
|
||||
util::shotgun_ray,
|
||||
};
|
||||
|
||||
pub mod ball;
|
||||
@ -34,12 +32,6 @@ pub mod soldier_mesh;
|
||||
pub mod telegrenade;
|
||||
pub mod weapon;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct NetTransform {
|
||||
pub location: NetVector3,
|
||||
pub rotation: NetVector3,
|
||||
}
|
||||
|
||||
pub trait IPlayer {
|
||||
fn get_player_id(&self) -> Option<u16>;
|
||||
fn get_y_speed(&self) -> f32;
|
||||
|
||||
@ -8,9 +8,9 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
game::Game,
|
||||
net::util::{RaycastResult, cast_ray, shotgun_ray},
|
||||
player::{DamageSource, shootable::Shootable},
|
||||
replay::StandaloneReplayManager,
|
||||
util::{RaycastResult, cast_ray, shotgun_ray},
|
||||
};
|
||||
|
||||
pub trait Weapon {
|
||||
|
||||
@ -18,9 +18,12 @@ use crate::{
|
||||
Map,
|
||||
pickup::{PickupKind, ReplayVisibility},
|
||||
},
|
||||
net::{network_manager::BallSync, util::NetVector3},
|
||||
net::{
|
||||
network_manager::BallSync,
|
||||
util::{NetTransform, NetVector3},
|
||||
},
|
||||
player::{
|
||||
DamageSource, IPlayer, NetTransform, ball::Ball, buffs::Buffs, remote_player::RemotePlayer,
|
||||
DamageSource, IPlayer, ball::Ball, buffs::Buffs, remote_player::RemotePlayer,
|
||||
telegrenade::Telegrenade, weapon::WeaponType,
|
||||
},
|
||||
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