aboutsummaryrefslogtreecommitdiffstats
path: root/src/shared/ability.rs
blob: 2c581e7a60c0ef0971b27b985be37474f9f6c71d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use crate::shared::player::*;
use crate::shared::projectile::*;
use crate::shared::*;
use bevy::ecs::system::*;
use bevy::utils::Duration;
use std::ops::*;

#[derive(Copy, Clone, PartialEq, Debug, Deserialize, Serialize)]
pub enum Ability {
    Activated(ActivatedAbility),
    Directional(DirectionalAbility),
    Targeted(TargetedAbility),
}

#[derive(Copy, Clone, PartialEq, Debug, Deserialize, Serialize)]
pub enum TargetedAbility {
    MeeleAttack,
    RangedAttack,
}

impl TargetedAbility {
    pub fn to_projectile(
        self,
        source_player: PlayerId,
        position: Vec2,
        target_player: PlayerId,
    ) -> Projectile {
        match self {
            TargetedAbility::MeeleAttack => Projectile {
                type_: ProjectileType::Instant(InstantProjectile { target_player }),
                source_player,
                damage: 5.,
            },
            TargetedAbility::RangedAttack => Projectile {
                type_: ProjectileType::Targeted(TargetedProjectile {
                    target_player,
                    position,
                }),
                source_player,
                damage: 6.,
            },
        }
    }
}

#[derive(Copy, Clone, PartialEq, Debug, Deserialize, Serialize)]
pub enum ActivatedAbility {
    Speed,
}

#[derive(Copy, Clone, PartialEq, Debug, Deserialize, Serialize)]
pub enum DirectionalAbility {
    Dash,
    Pull,
    Spear,
}

pub struct DirectionalAbilityActivation(
    pub fn(commands: &mut Commands, source_player: PlayerId, direction: Vec2) -> (),
);

impl DirectionalAbility {
    pub fn activate(self) -> DirectionalAbilityActivation {
        match self {
            DirectionalAbility::Dash => DirectionalAbilityActivation(dash_activation),
            DirectionalAbility::Pull => DirectionalAbilityActivation(pull_activation),
            DirectionalAbility::Spear => DirectionalAbilityActivation(spear_activation),
        }
    }
}

fn dash_activation(commands: &mut Commands, source_player: PlayerId, direction: Vec2) {
    commands.add(move |world: &mut World| {
        world.run_system_once(
            move |players: Query<(Entity, &PlayerId)>,
                  mut set: ParamSet<(
                Query<&mut PlayerPosition>,
                Query<(&PlayerId, &PlayerPosition)>,
            )>| {
                let Some(source_entity) = ({
                    let mut source_entity = None;
                    for (entity, player_id) in players.iter() {
                        if *player_id != source_player {
                            continue;
                        }
                        source_entity = Some(entity);
                        break;
                    }
                    source_entity
                }) else {
                    return;
                };

                let Some(source_position) = ({
                    let positions = set.p0();
                    if let Ok(position) = positions.get(source_entity) {
                        Some(*position)
                    } else {
                        None
                    }
                }) else {
                    return;
                };

                let dash_end = {
                    let dash_targets = set.p1();
                    dash_collision(
                        source_player,
                        source_position.0,
                        direction,
                        150.,
                        &dash_targets,
                    )
                };

                let mut positions = set.p0();
                if let Ok(mut position) = positions.get_mut(source_entity) {
                    position.0 = dash_end;
                }
            },
        )
    });
}

pub fn dash_collision(
    source_id: PlayerId,
    dash_start: Vec2,
    dash_direction: Vec2,
    dash_max_distance: f32,
    player_positions: &Query<(&PlayerId, &PlayerPosition)>,
) -> Vec2 {
    let mut dash_collision = dash_max_distance * dash_direction;
    let mut collision = false;
    for (player_id, position) in player_positions.iter() {
        if *player_id == source_id {
            continue;
        }

        let player_position = position.0 - dash_start;
        let player_projection = player_position.project_onto(dash_collision);
        let player_rejection = player_position - player_projection;
        let scalar_factor = player_projection.dot(dash_collision).signum()
            * player_projection.length()
            / dash_collision.length();

        if scalar_factor < 0. || scalar_factor > 1.0 {
            continue;
        }

        if player_rejection.length() < 2. * PLAYER_RADIUS {
            collision = true;
            dash_collision = player_projection;
        }
    }

    if collision {
        dash_start
            + (dash_collision.length() - 2. * PLAYER_RADIUS) * dash_collision.normalize_or_zero()
    } else {
        dash_start + dash_max_distance * dash_direction
    }
}

fn pull_activation(commands: &mut Commands, source_player: PlayerId, direction: Vec2) {
    commands.add(move |world: &mut World| {
        world.run_system_once(
            move |players: Query<(Entity, &PlayerId)>,
                  mut set: ParamSet<(
                Query<&mut PlayerPosition>,
                Query<(&PlayerId, &PlayerPosition)>,
            )>| {
                let Some(source_entity) = ({
                    let mut source_entity = None;
                    for (entity, player_id) in players.iter() {
                        if *player_id != source_player {
                            continue;
                        }
                        source_entity = Some(entity);
                        break;
                    }
                    source_entity
                }) else {
                    return;
                };

                let Some(source_position) = ({
                    let positions = set.p0();
                    if let Ok(position) = positions.get(source_entity) {
                        Some(*position)
                    } else {
                        None
                    }
                }) else {
                    return;
                };

                let Some((target_player, _, pull_end)) = ({
                    let pull_targets = set.p1();
                    pull_collision(
                        source_player,
                        source_position.0,
                        direction,
                        150.,
                        &pull_targets,
                    )
                }) else {
                    return;
                };

                let Some(target_entity) = ({
                    let mut target_entity = None;
                    for (entity, player_id) in players.iter() {
                        if *player_id != target_player {
                            continue;
                        }
                        target_entity = Some(entity);
                        break;
                    }
                    target_entity
                }) else {
                    return;
                };

                let mut positions = set.p0();
                if let Ok(mut position) = positions.get_mut(target_entity) {
                    position.0 = pull_end;
                }
            },
        )
    });
}

pub fn pull_collision(
    source_id: PlayerId,
    pull_start: Vec2,
    pull_direction: Vec2,
    pull_max_distance: f32,
    player_positions: &Query<(&PlayerId, &PlayerPosition)>,
) -> Option<(PlayerId, Vec2, Vec2)> {
    let mut pull_collision = pull_max_distance * pull_direction;
    let mut pull_player_id = None;
    let mut pull_player_position = None;
    for (player_id, position) in player_positions.iter() {
        if *player_id == source_id {
            continue;
        }

        let player_position = position.0 - pull_start;
        let player_projection = player_position.project_onto(pull_collision);
        let player_rejection = player_position - player_projection;
        let scalar_factor = player_projection.dot(pull_collision).signum()
            * player_projection.length()
            / pull_collision.length();

        if scalar_factor < 0. || scalar_factor > 1.0 {
            continue;
        }

        if player_rejection.length() < 2. * PLAYER_RADIUS {
            pull_player_id = Some(player_id);
            pull_player_position = Some(position.0);
            pull_collision = player_projection;
        }
    }

    if let Some(target_id) = pull_player_id {
        if let Some(target_position) = pull_player_position {
            let pull_direction = pull_start - target_position;
            Some((
                *target_id,
                target_position,
                target_position
                    + (pull_direction.length() - 2. * PLAYER_RADIUS)
                        * pull_direction.normalize_or_zero(),
            ))
        } else {
            None
        }
    } else {
        None
    }
}

fn spear_activation(commands: &mut Commands, source_player: PlayerId, direction: Vec2) {
    commands.add(move |world: &mut World| {
        world.run_system_once(
            move |mut commands: Commands, players: Query<(&PlayerId, &PlayerPosition)>| {
                for (id, position) in players.iter() {
                    if *id != source_player {
                        continue;
                    }
                    commands.spawn(ProjectileBundle::new(Projectile {
                        type_: ProjectileType::Free(FreeProjectile {
                            position: position.0,
                            direction,
                            starting_position: position.0,
                        }),
                        source_player,
                        damage: 15.,
                    }));
                }
            },
        )
    });
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum AbilitySlot {
    A,
    Q,
    W,
    E,
    R,
    F,
    G,
}

impl AbilitySlot {
    pub fn to_label(self) -> &'static str {
        match self {
            AbilitySlot::A => "A",
            AbilitySlot::Q => "Q",
            AbilitySlot::W => "W",
            AbilitySlot::E => "E",
            AbilitySlot::R => "R",
            AbilitySlot::F => "F",
            AbilitySlot::G => "G",
        }
    }

    pub fn all() -> Vec<Self> {
        vec![
            AbilitySlot::A,
            AbilitySlot::Q,
            AbilitySlot::W,
            AbilitySlot::E,
            AbilitySlot::R,
            AbilitySlot::F,
            AbilitySlot::G,
        ]
    }
}

impl Index<AbilitySlot> for [Duration; 7] {
    type Output = Duration;

    fn index(&self, ability_slot: AbilitySlot) -> &Self::Output {
        &self[ability_slot as usize]
    }
}

impl IndexMut<AbilitySlot> for [Duration; 7] {
    fn index_mut(&mut self, ability_slot: AbilitySlot) -> &mut Self::Output {
        &mut self[ability_slot as usize]
    }
}