1use crate::{
2 combat::{
3 self, Attack, AttackDamage, AttackEffect, CombatEffect, CombatRequirement, Damage,
4 DamageKind, GroupTarget,
5 },
6 comp::{
7 Body, CharacterState, StateUpdate,
8 ability::Dodgeable,
9 beam,
10 body::{biped_large, bird_large, golem},
11 character_state::OutputEvents,
12 object::Body::{Flamethrower, Lavathrower},
13 quadruped_medium,
14 },
15 event::LocalEvent,
16 outcome::Outcome,
17 resources::Secs,
18 states::{
19 behavior::{CharacterBehavior, JoinData},
20 utils::*,
21 },
22 terrain::Block,
23 util::Dir,
24};
25use hashbrown::HashMap;
26use serde::{Deserialize, Serialize};
27use std::time::Duration;
28use vek::*;
29
30#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
32pub struct StaticData {
33 pub buildup_duration: Duration,
35 pub recover_duration: Duration,
37 pub beam_duration: Secs,
39 pub damage: f32,
41 pub tick_rate: f32,
43 pub range: f32,
45 #[serde(default)]
47 pub dodgeable: Dodgeable,
48 #[serde(default)]
50 pub blockable: bool,
51 pub end_radius: f32,
54 pub damage_effect: Option<CombatEffect>,
56 pub energy_regen: f32,
58 pub energy_drain: f32,
60 pub ori_rate: f32,
62 pub move_efficiency: f32,
64 pub ability_info: AbilityInfo,
66 pub specifier: beam::FrontendSpecifier,
68}
69
70#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
71pub struct Data {
72 pub static_data: StaticData,
75 pub timer: Duration,
77 pub stage_section: StageSection,
79 pub aim_dir: Dir,
81 pub beam_offset: Vec3<f32>,
83}
84
85impl CharacterBehavior for Data {
86 fn behavior(&self, data: &JoinData, output_events: &mut OutputEvents) -> StateUpdate {
87 let mut update = StateUpdate::from(data);
88
89 let ori_rate = self.static_data.ori_rate;
90
91 handle_orientation(data, &mut update, ori_rate, None);
92 handle_move(data, &mut update, self.static_data.move_efficiency);
93 handle_jump(data, output_events, &mut update, 1.0);
94
95 let rel_vel = data.vel.0 - data.physics.ground_vel;
97 let body_offsets = beam_offsets(
99 data.body,
100 data.inputs.look_dir,
101 update.ori.look_vec(),
102 rel_vel,
103 data.physics.on_ground,
104 );
105
106 match self.stage_section {
107 StageSection::Buildup => {
108 if self.timer < self.static_data.buildup_duration {
109 if let CharacterState::BasicBeam(c) = &mut update.character {
111 c.timer = tick_attack_or_default(data, self.timer, None);
112 }
113 if matches!(data.body, Body::Object(Flamethrower | Lavathrower)) {
114 output_events.emit_local(LocalEvent::CreateOutcome(
116 Outcome::FlamethrowerCharge {
117 pos: data.pos.0 + *data.ori.look_dir() * (data.body.max_radius()),
118 },
119 ));
120 }
121 } else {
122 let attack = {
123 let energy = AttackEffect::new(
124 None,
125 CombatEffect::EnergyReward(self.static_data.energy_regen),
126 )
127 .with_requirement(CombatRequirement::AnyDamage);
128 let mut damage = AttackDamage::new(
129 Damage {
130 kind: DamageKind::Energy,
131 value: self.static_data.damage,
132 },
133 Some(GroupTarget::OutOfGroup),
134 rand::random(),
135 );
136 if let Some(effect) = &self.static_data.damage_effect {
137 damage = damage.with_effect(effect.clone());
138 }
139 let precision_mult =
140 combat::compute_precision_mult(data.inventory, data.msm);
141 Attack::new(Some(self.static_data.ability_info))
142 .with_damage(damage)
143 .with_precision(
144 precision_mult
145 * self
146 .static_data
147 .ability_info
148 .ability_meta
149 .precision_power_mult
150 .unwrap_or(1.0),
151 )
152 .with_blockable(self.static_data.blockable)
153 .with_effect(energy)
154 .with_combo_increment()
155 };
156
157 data.updater.insert(data.entity, beam::Beam {
159 attack,
160 dodgeable: self.static_data.dodgeable,
161 start_radius: 0.0,
162 end_radius: self.static_data.end_radius,
163 range: self.static_data.range,
164 duration: self.static_data.beam_duration,
165 tick_dur: Secs(1.0 / self.static_data.tick_rate as f64),
166 hit_entities: Vec::new(),
167 hit_durations: HashMap::new(),
168 specifier: self.static_data.specifier,
169 bezier: QuadraticBezier3 {
170 start: data.pos.0 + body_offsets,
171 ctrl: data.pos.0 + body_offsets,
172 end: data.pos.0 + body_offsets,
173 },
174 });
175 if let CharacterState::BasicBeam(c) = &mut update.character {
177 c.beam_offset = body_offsets;
178 c.timer = Duration::default();
179 c.stage_section = StageSection::Action;
180 }
181 }
182 },
183 StageSection::Action => {
184 if input_is_pressed(data, self.static_data.ability_info.input)
185 && (self.static_data.energy_drain <= f32::EPSILON
186 || update.energy.current() > 0.0)
187 {
188 let beam_dir = data.inputs.look_dir.merge_z(data.ori.look_dir());
192
193 if let CharacterState::BasicBeam(c) = &mut update.character {
194 c.beam_offset = body_offsets;
195 c.aim_dir = beam_dir;
196 c.timer = tick_attack_or_default(data, self.timer, None);
197 }
198
199 update
201 .energy
202 .change_by(-self.static_data.energy_drain * data.dt.0);
203 } else if let CharacterState::BasicBeam(c) = &mut update.character {
204 c.timer = Duration::default();
205 c.stage_section = StageSection::Recover;
206 }
207 },
208 StageSection::Recover => {
209 if self.timer < self.static_data.recover_duration {
210 if let CharacterState::BasicBeam(c) = &mut update.character {
211 c.timer = tick_attack_or_default(data, self.timer, None);
212 }
213 } else {
214 end_ability(data, &mut update);
216 data.updater.remove::<beam::Beam>(data.entity);
218 }
219 },
220 _ => {
221 end_ability(data, &mut update);
223 data.updater.remove::<beam::Beam>(data.entity);
225 },
226 }
227
228 handle_interrupts(data, &mut update, output_events);
230
231 update
232 }
233}
234
235fn height_offset(body: &Body, look_dir: Dir, velocity: Vec3<f32>, on_ground: Option<Block>) -> f32 {
236 match body {
237 Body::BirdLarge(b) => {
239 let height_factor = match b.species {
240 bird_large::Species::Phoenix => 0.5,
241 bird_large::Species::Cockatrice => 0.4,
242 _ => 0.3,
243 };
244 body.height() * height_factor
245 + if on_ground.is_none() {
246 (2.0 - velocity.xy().magnitude() * 0.25).max(-1.0)
247 } else {
248 0.0
249 }
250 },
251 Body::Golem(b) => {
252 let height_factor = match b.species {
253 golem::Species::Mogwai => 0.4,
254 _ => 0.9,
255 };
256 const DIR_COEFF: f32 = 2.0;
257 body.height() * height_factor + look_dir.z * DIR_COEFF
258 },
259 Body::BipedLarge(b) => match b.species {
260 biped_large::Species::Mindflayer => body.height() * 0.6,
261 biped_large::Species::SeaBishop => body.height() * 0.4,
262 biped_large::Species::Cursekeeper => body.height() * 0.8,
263 biped_large::Species::Gigasfire => body.height() * 0.18,
264 _ => body.height() * 0.5,
265 },
266 Body::QuadrupedMedium(b) => match b.species {
267 quadruped_medium::Species::Elephant => body.height() * 0.4,
268 _ => body.height() * 0.5,
269 },
270 _ => body.height() * 0.5,
271 }
272}
273
274pub fn beam_offsets(
275 body: &Body,
276 look_dir: Dir,
277 ori: Vec3<f32>,
278 velocity: Vec3<f32>,
279 on_ground: Option<Block>,
280) -> Vec3<f32> {
281 let dim = body.dimensions();
282 let (width, length) = (dim.x, dim.y);
284 let body_radius = match body {
285 Body::QuadrupedMedium(b) if matches!(b.species, quadruped_medium::Species::Elephant) => {
286 body.max_radius() * 1.4
287 },
288 _ => {
289 if length > width {
290 body.max_radius()
292 } else {
293 body.min_radius()
295 }
296 },
297 };
298 let body_offsets_z = height_offset(body, look_dir, velocity, on_ground);
299 Vec3::new(
300 body_radius * ori.x * 1.1,
301 body_radius * ori.y * 1.1,
302 body_offsets_z,
303 )
304}