From b444a74d8fc9c39679b6a163c39ed1cf54e231f2 Mon Sep 17 00:00:00 2001 From: Snowram Date: Wed, 22 Sep 2021 01:06:59 +0200 Subject: [PATCH] Consolidates projectile offsets into utils --- common/src/states/basic_ranged.rs | 31 +--------------------------- common/src/states/charged_ranged.rs | 31 +--------------------------- common/src/states/repeater_ranged.rs | 31 +--------------------------- common/src/states/utils.rs | 26 +++++++++++++++++++++++ server/src/sys/agent.rs | 8 +++---- 5 files changed, 33 insertions(+), 94 deletions(-) diff --git a/common/src/states/basic_ranged.rs b/common/src/states/basic_ranged.rs index 3e223fd51f..2b487ba2ef 100644 --- a/common/src/states/basic_ranged.rs +++ b/common/src/states/basic_ranged.rs @@ -10,7 +10,6 @@ use crate::{ use rand::{thread_rng, Rng}; use serde::{Deserialize, Serialize}; use std::time::Duration; -use vek::*; /// Separated out to condense update portions of character state #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] @@ -85,7 +84,7 @@ impl CharacterBehavior for Data { // Shoots all projectiles simultaneously for i in 0..self.static_data.num_projectiles { // Gets offsets - let body_offsets = projectile_offsets(data.body, update.ori.look_vec()); + let body_offsets = data.body.projectile_offsets(update.ori.look_vec()); let pos = Pos(data.pos.0 + body_offsets); // Adds a slight spread to the projectiles. First projectile has no spread, // and spread increases linearly with number of projectiles created. @@ -146,31 +145,3 @@ impl CharacterBehavior for Data { fn reset_state(data: &Data, join: &JoinData, update: &mut StateUpdate) { handle_input(join, update, data.static_data.ability_info.input); } - -fn height_offset(body: &Body) -> f32 { - match body { - Body::Golem(_) => body.height() * 0.4, - _ => body.eye_height(), - } -} - -pub fn projectile_offsets(body: &Body, ori: Vec3) -> Vec3 { - let dim = body.dimensions(); - // The width (shoulder to shoulder) and length (nose to tail) - let (width, length) = (dim.x, dim.y); - let body_radius = if length > width { - // Dachshund-like - body.max_radius() - } else { - // Cyclops-like - body.min_radius() - }; - - let body_offsets_z = height_offset(body); - - Vec3::new( - body_radius * ori.x * 1.1, - body_radius * ori.y * 1.1, - body_offsets_z, - ) -} diff --git a/common/src/states/charged_ranged.rs b/common/src/states/charged_ranged.rs index 60ee545ccd..c435d3695d 100644 --- a/common/src/states/charged_ranged.rs +++ b/common/src/states/charged_ranged.rs @@ -11,7 +11,6 @@ use crate::{ }; use serde::{Deserialize, Serialize}; use std::time::Duration; -use vek::*; /// Separated out to condense update portions of character state #[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] @@ -112,7 +111,7 @@ impl CharacterBehavior for Data { get_crit_data(data, self.static_data.ability_info); let buff_strength = get_buff_strength(data, self.static_data.ability_info); // Gets offsets - let body_offsets = projectile_offsets(data.body, update.ori.look_vec()); + let body_offsets = data.body.projectile_offsets(update.ori.look_vec()); let pos = Pos(data.pos.0 + body_offsets); let projectile = arrow.create_projectile( Some(*data.uid), @@ -192,31 +191,3 @@ impl CharacterBehavior for Data { update } } - -fn height_offset(body: &Body) -> f32 { - match body { - Body::Golem(_) => body.height() * 0.4, - _ => body.eye_height(), - } -} - -pub fn projectile_offsets(body: &Body, ori: Vec3) -> Vec3 { - let dim = body.dimensions(); - // The width (shoulder to shoulder) and length (nose to tail) - let (width, length) = (dim.x, dim.y); - let body_radius = if length > width { - // Dachshund-like - body.max_radius() - } else { - // Cyclops-like - body.min_radius() - }; - - let body_offsets_z = height_offset(body); - - Vec3::new( - body_radius * ori.x * 1.1, - body_radius * ori.y * 1.1, - body_offsets_z, - ) -} diff --git a/common/src/states/repeater_ranged.rs b/common/src/states/repeater_ranged.rs index 5b443ca399..a3606b9991 100644 --- a/common/src/states/repeater_ranged.rs +++ b/common/src/states/repeater_ranged.rs @@ -11,7 +11,6 @@ use crate::{ }; use serde::{Deserialize, Serialize}; use std::time::Duration; -use vek::*; #[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] /// Separated out to condense update portions of character state @@ -93,7 +92,7 @@ impl CharacterBehavior for Data { get_crit_data(data, self.static_data.ability_info); let buff_strength = get_buff_strength(data, self.static_data.ability_info); // Gets offsets - let body_offsets = projectile_offsets(data.body, update.ori.look_vec()); + let body_offsets = data.body.projectile_offsets(update.ori.look_vec()); let pos = Pos(data.pos.0 + body_offsets); let projectile = self.static_data.projectile.create_projectile( Some(*data.uid), @@ -169,31 +168,3 @@ impl CharacterBehavior for Data { update } } - -fn height_offset(body: &Body) -> f32 { - match body { - Body::Golem(_) => body.height() * 0.4, - _ => body.eye_height(), - } -} - -pub fn projectile_offsets(body: &Body, ori: Vec3) -> Vec3 { - let dim = body.dimensions(); - // The width (shoulder to shoulder) and length (nose to tail) - let (width, length) = (dim.x, dim.y); - let body_radius = if length > width { - // Dachshund-like - body.max_radius() - } else { - // Cyclops-like - body.min_radius() - }; - - let body_offsets_z = height_offset(body); - - Vec3::new( - body_radius * ori.x * 1.1, - body_radius * ori.y * 1.1, - body_offsets_z, - ) -} diff --git a/common/src/states/utils.rs b/common/src/states/utils.rs index 24bb320f3d..ddcfa0312b 100644 --- a/common/src/states/utils.rs +++ b/common/src/states/utils.rs @@ -238,6 +238,32 @@ impl Body { /// Returns how well a body can move backwards while strafing (0.0 = not at /// all, 1.0 = same as forward) pub fn reverse_move_factor(&self) -> f32 { 0.45 } + + /// Returns the position where a projectile should be fired relative to this + /// body + pub fn projectile_offsets(&self, ori: Vec3) -> Vec3 { + let body_offsets_z = match self { + Body::Golem(_) => self.height() * 0.4, + _ => self.eye_height(), + }; + + let dim = self.dimensions(); + // The width (shoulder to shoulder) and length (nose to tail) + let (width, length) = (dim.x, dim.y); + let body_radius = if length > width { + // Dachshund-like + self.max_radius() + } else { + // Cyclops-like + self.min_radius() + }; + + Vec3::new( + body_radius * ori.x * 1.1, + body_radius * ori.y * 1.1, + body_offsets_z, + ) + } } /// Handles updating `Components` to move player based on state of `JoinData` diff --git a/server/src/sys/agent.rs b/server/src/sys/agent.rs index 37da940657..c8112b25ca 100644 --- a/server/src/sys/agent.rs +++ b/server/src/sys/agent.rs @@ -28,7 +28,7 @@ use common::{ path::TraversalConfig, resources::{DeltaTime, Time, TimeOfDay}, rtsim::{Memory, MemoryItem, RtSimEntity, RtSimEvent}, - states::{basic_beam, basic_ranged, charged_ranged, repeater_ranged, utils::StageSection}, + states::{basic_beam, utils::StageSection}, terrain::{Block, TerrainGrid}, time::DayPeriod, trade::{TradeAction, TradePhase, TradeResult}, @@ -1832,7 +1832,7 @@ impl<'a> AgentData<'a> { projectile_speed, self.pos.0 + self.body.map_or(Vec3::zero(), |body| { - charged_ranged::projectile_offsets(body, self.ori.look_vec()) + body.projectile_offsets(self.ori.look_vec()) }), Vec3::new( tgt_data.pos.0.x, @@ -1847,7 +1847,7 @@ impl<'a> AgentData<'a> { projectile_speed, self.pos.0 + self.body.map_or(Vec3::zero(), |body| { - basic_ranged::projectile_offsets(body, self.ori.look_vec()) + body.projectile_offsets(self.ori.look_vec()) }), Vec3::new( tgt_data.pos.0.x, @@ -1862,7 +1862,7 @@ impl<'a> AgentData<'a> { projectile_speed, self.pos.0 + self.body.map_or(Vec3::zero(), |body| { - repeater_ranged::projectile_offsets(body, self.ori.look_vec()) + body.projectile_offsets(self.ori.look_vec()) }), Vec3::new( tgt_data.pos.0.x,