1use common::{
2 combat::RiderEffects,
3 comp::{
4 Body, Buff, BuffCategory, BuffChange, Buffs, CharacterActivity, CharacterState, Collider,
5 ControlAction, Controller, InputKind, Mass, Ori, PhysicsState, Pos, Scale, Stats, Vel,
6 buff::DestInfo,
7 },
8 event::{BuffEvent, EmitExt},
9 event_emitters,
10 link::Is,
11 mounting::{Mount, Rider, VolumeRider},
12 resources::Time,
13 terrain::TerrainGrid,
14 uid::IdMaps,
15};
16use common_ecs::{Job, Origin, Phase, System};
17use specs::{Entities, Join, LendJoin, Read, ReadExpect, ReadStorage, WriteStorage};
18use vek::*;
19
20event_emitters! {
21 struct Events[EventEmitters] {
22 buff: BuffEvent,
23 }
24}
25
26#[derive(Default)]
28pub struct Sys;
29impl<'a> System<'a> for Sys {
30 type SystemData = (
31 Read<'a, IdMaps>,
32 Read<'a, Time>,
33 ReadExpect<'a, TerrainGrid>,
34 Events<'a>,
35 Entities<'a>,
36 WriteStorage<'a, Controller>,
37 ReadStorage<'a, Is<Rider>>,
38 ReadStorage<'a, Is<Mount>>,
39 ReadStorage<'a, Is<VolumeRider>>,
40 WriteStorage<'a, Pos>,
41 WriteStorage<'a, Vel>,
42 WriteStorage<'a, Ori>,
43 WriteStorage<'a, CharacterActivity>,
44 WriteStorage<'a, PhysicsState>,
45 ReadStorage<'a, Body>,
46 ReadStorage<'a, Scale>,
47 ReadStorage<'a, Collider>,
48 ReadStorage<'a, Buffs>,
49 ReadStorage<'a, Stats>,
50 ReadStorage<'a, Mass>,
51 ReadStorage<'a, RiderEffects>,
52 ReadStorage<'a, CharacterState>,
53 );
54
55 const NAME: &'static str = "mount";
56 const ORIGIN: Origin = Origin::Common;
57 const PHASE: Phase = Phase::Create;
58
59 fn run(
60 _job: &mut Job<Self>,
61 (
62 id_maps,
63 time,
64 terrain,
65 events,
66 entities,
67 mut controllers,
68 is_riders,
69 is_mounts,
70 is_volume_riders,
71 mut positions,
72 mut velocities,
73 mut orientations,
74 mut character_activities,
75 mut physics_states,
76 bodies,
77 scales,
78 colliders,
79 buffs,
80 stats,
81 masses,
82 rider_effects,
83 char_states,
84 ): Self::SystemData,
85 ) {
86 let mut emitters = events.get_emitters();
87 for (entity, is_mount, body, rider_effects) in
89 (&entities, &is_mounts, bodies.maybe(), rider_effects.maybe()).join()
90 {
91 let Some(rider_entity) = id_maps.uid_entity(is_mount.rider) else {
92 continue;
93 };
94
95 if let Some(rider_effects) = rider_effects
97 && let Some(target_buffs) = buffs.get(rider_entity)
98 {
99 for effect in rider_effects.0.iter() {
100 let emit_buff = !target_buffs.buffs.iter().any(|(_, buff)| {
101 buff.cat_ids.iter()
102 .any(|cat_id| matches!(cat_id, BuffCategory::FromLink(link) if link.is_link(is_mount.get_link())))
103 && buff.kind == effect.kind && buff.data.strength >= effect.data.strength
104 });
105
106 if emit_buff {
107 let dest_info = DestInfo {
108 stats: stats.get(rider_entity),
109 mass: masses.get(rider_entity),
110 };
111 let mut cat_ids = effect.cat_ids.clone();
112 cat_ids.push(BuffCategory::FromLink(
113 is_mount.get_link().downgrade().into_dyn(),
114 ));
115
116 emitters.emit(BuffEvent {
117 entity: rider_entity,
118 buff_change: BuffChange::Add(Buff::new(
119 effect.kind,
120 effect.data,
121 cat_ids,
122 common::comp::BuffSource::Character {
123 by: is_mount.mount,
124 tool_kind: None,
125 },
126 *time,
127 dest_info,
128 masses.get(entity),
129 )),
130 });
131 }
132 }
133 }
134 let Some(inputs_and_actions) = controllers.get_mut(rider_entity).map(|c| {
136 if body.is_some_and(|b| !b.has_free_will()) {
139 let actions = c
140 .actions
141 .extract_if(.., |action| match action {
142 ControlAction::StartInput { input: i, .. }
143 | ControlAction::CancelInput { input: i } => {
144 matches!(
145 i,
146 InputKind::Jump
147 | InputKind::WallJump
148 | InputKind::Fly
149 | InputKind::Roll
150 )
151 },
152 _ => false,
153 })
154 .collect();
155 Some((c.inputs.clone(), actions))
156 } else {
157 None
158 }
159 }) else {
160 continue;
161 };
162
163 let pos = positions.get(entity).copied();
165 let ori = orientations.get(entity).copied();
166 let vel = velocities.get(entity).copied();
167 if let (Some(pos), Some(ori), Some(vel)) = (pos, ori, vel) {
168 let mounter_body = bodies.get(rider_entity);
169 let mounting_offset = body.map_or(Vec3::unit_z(), Body::mount_offset)
170 * scales.get(entity).map_or(1.0, |s| s.0)
171 + mounter_body.map_or(Vec3::zero(), Body::rider_offset)
172 * scales.get(rider_entity).map_or(1.0, |s| s.0);
173 let _ =
174 positions.insert(rider_entity, Pos(pos.0 + ori.to_quat() * mounting_offset));
175
176 let should_set_ori = char_states
179 .get(rider_entity)
180 .is_none_or(|cs| !cs.can_look_while_mounted());
181
182 if should_set_ori {
183 let _ = orientations.insert(rider_entity, ori);
184 }
185
186 let _ = velocities.insert(rider_entity, vel);
187 }
188 if let Some((inputs, actions)) = inputs_and_actions
190 && let Some(controller) = controllers.get_mut(entity)
191 {
192 controller.inputs = inputs;
193 controller.actions = actions;
194 }
195 }
196
197 for (physics_state, _) in (
201 &mut physics_states,
202 is_riders.mask() | is_volume_riders.mask(),
203 )
204 .join()
205 {
206 *physics_state = PhysicsState::default();
207 }
208
209 for (entity, is_volume_rider) in (&entities, &is_volume_riders).join() {
211 if let Some((mat, _)) = is_volume_rider.pos.get_mount_mat(
212 &terrain,
213 &id_maps,
214 |e| positions.get(e).copied().zip(orientations.get(e).copied()),
215 &colliders,
216 ) {
217 if let Some(pos) = positions.get_mut(entity) {
218 pos.0 = mat.mul_point(Vec3::zero());
219 }
220 if let Some(ori) = orientations.get_mut(entity) {
221 *ori = Ori::from_unnormalized_vec(mat.mul_direction(Vec3::unit_y()))
222 .unwrap_or_default();
223 }
224 }
225 let v = match is_volume_rider.pos.kind {
226 common::mounting::Volume::Terrain => Vec3::zero(),
227 common::mounting::Volume::Entity(uid) => {
228 if let Some(v) = id_maps.uid_entity(uid).and_then(|e| velocities.get(e)) {
229 v.0
230 } else {
231 Vec3::zero()
232 }
233 },
234 };
235 if let Some(vel) = velocities.get_mut(entity) {
236 vel.0 = v;
237 }
238
239 if let Some(target_buffs) = buffs.get(entity)
245 && let Some(block_buffs) = is_volume_rider.block.mount_buffs()
246 {
247 for effect in block_buffs.iter() {
248 let emit_buff = !target_buffs.buffs.iter().any(|(_, buff)| {
249 buff.cat_ids.iter()
250 .any(|cat_id| matches!(cat_id, BuffCategory::FromLink(link) if link.is_link(is_volume_rider.get_link())))
251 && buff.kind == effect.kind && buff.data.strength >= effect.data.strength
252 });
253
254 if emit_buff {
255 let dest_info = DestInfo {
256 stats: stats.get(entity),
257 mass: masses.get(entity),
258 };
259 let mut cat_ids = effect.cat_ids.clone();
260 cat_ids.push(BuffCategory::FromLink(
261 is_volume_rider.get_link().downgrade().into_dyn(),
262 ));
263
264 emitters.emit(BuffEvent {
265 entity,
266 buff_change: BuffChange::Add(Buff::new(
267 effect.kind,
268 effect.data,
269 cat_ids,
270 common::comp::BuffSource::Block,
271 *time,
272 dest_info,
273 masses.get(entity),
274 )),
275 });
276 }
277 }
278 }
279
280 let inputs = controllers.get_mut(entity).map(|c| {
281 let actions: Vec<_> = c
282 .actions
283 .extract_if(.., |action| match action {
284 ControlAction::StartInput { input: i, .. }
285 | ControlAction::CancelInput { input: i } => {
286 matches!(
287 i,
288 InputKind::Jump
289 | InputKind::WallJump
290 | InputKind::Fly
291 | InputKind::Roll
292 )
293 },
294 _ => false,
295 })
296 .collect();
297 let inputs = c.inputs.clone();
298
299 (actions, inputs)
300 });
301
302 if is_volume_rider.block.is_controller()
303 && let Some((actions, inputs)) = inputs
304 {
305 if let Some(mut character_activity) = character_activities
306 .get_mut(entity)
307 .filter(|c| c.steer_dir != inputs.move_dir.y)
308 {
309 character_activity.steer_dir = inputs.move_dir.y;
310 }
311 match is_volume_rider.pos.kind {
312 common::mounting::Volume::Entity(uid) => {
313 if let Some(controller) =
314 id_maps.uid_entity(uid).and_then(|e| controllers.get_mut(e))
315 {
316 controller.inputs = inputs;
317 controller.actions = actions;
318 }
319 },
320 common::mounting::Volume::Terrain => {},
321 }
322 }
323 }
324 }
325}