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
357
358
359
360
361
362
363
use crate::prelude::*;

/// The throwable crate item
#[derive(HasSchema, Default, Debug, Clone)]
#[type_data(metadata_asset("crate"))]
#[repr(C)]
pub struct CrateMeta {
    pub atlas: Handle<Atlas>,

    pub breaking_atlas: Handle<Atlas>,
    pub breaking_anim_frames: u32,
    pub breaking_anim_fps: f32,

    pub break_sound: Handle<AudioSource>,
    pub break_sound_volume: f64,
    pub bounce_sound: Handle<AudioSource>,
    pub bounce_sound_volume: f64,

    pub throw_velocity: f32,

    pub body_size: Vec2,
    pub grab_offset: Vec2,
    // How long to wait before despawning a thrown crate, if it hans't it anything yet.
    pub break_timeout: Duration,
    pub bounciness: f32,
    pub fin_anim: Ustr,
    pub crate_break_state_1: u32,
    pub crate_break_state_2: u32,
}

pub fn game_plugin(game: &mut Game) {
    CrateMeta::register_schema();
    game.init_shared_resource::<AssetServer>();
}

pub fn session_plugin(session: &mut Session) {
    session
        .stages
        .add_system_to_stage(CoreStage::PreUpdate, hydrate_crates)
        .add_system_to_stage(CoreStage::PostUpdate, update_idle_crates)
        .add_system_to_stage(CoreStage::PostUpdate, update_thrown_crates);
}

#[derive(Clone, HasSchema, Default)]
struct IdleCrate;

#[derive(Clone, HasSchema, Default)]
struct ThrownCrate {
    owner: Entity,
    damage_delay: Timer,
    break_timeout: Timer,
    crate_break_state: u8,
    was_colliding: bool,
}

fn hydrate_crates(
    game_meta: Root<GameMeta>,
    mut entities: ResMutInit<Entities>,
    mut hydrated: CompMut<MapElementHydrated>,
    mut element_handles: CompMut<ElementHandle>,
    assets: Res<AssetServer>,
    mut idle_crates: CompMut<IdleCrate>,
    mut atlas_sprites: CompMut<AtlasSprite>,
    mut animated_sprites: CompMut<AnimatedSprite>,
    mut bodies: CompMut<KinematicBody>,
    mut transforms: CompMut<Transform>,
    mut items: CompMut<Item>,
    mut item_throws: CompMut<ItemThrow>,
    mut item_grabs: CompMut<ItemGrab>,
    mut respawn_points: CompMut<DehydrateOutOfBounds>,
    mut spawner_manager: SpawnerManager,
) {
    let mut not_hydrated_bitset = hydrated.bitset().clone();
    not_hydrated_bitset.bit_not();
    not_hydrated_bitset.bit_and(element_handles.bitset());

    let spawner_entities = entities
        .iter_with_bitset(&not_hydrated_bitset)
        .collect::<Vec<_>>();

    for spawner_entity in spawner_entities {
        let transform = *transforms.get(spawner_entity).unwrap();
        let element_handle = *element_handles.get(spawner_entity).unwrap();
        let element_meta = assets.get(element_handle.0);

        let asset = assets.get(element_meta.data);
        let Ok(CrateMeta {
            atlas,
            fin_anim,
            grab_offset,
            body_size,
            throw_velocity,
            bounciness,
            ..
        }) = asset.try_cast_ref()
        else {
            continue;
        };

        hydrated.insert(spawner_entity, MapElementHydrated);

        let entity = entities.create();
        items.insert(entity, Item);
        idle_crates.insert(entity, IdleCrate);
        item_throws.insert(entity, ItemThrow::strength(*throw_velocity));
        item_grabs.insert(
            entity,
            ItemGrab {
                fin_anim: *fin_anim,
                sync_animation: false,
                grab_offset: *grab_offset,
            },
        );
        atlas_sprites.insert(entity, AtlasSprite::new(*atlas));
        respawn_points.insert(entity, DehydrateOutOfBounds(spawner_entity));
        transforms.insert(entity, transform);
        element_handles.insert(entity, element_handle);
        hydrated.insert(entity, MapElementHydrated);
        animated_sprites.insert(entity, default());
        bodies.insert(
            entity,
            KinematicBody {
                shape: ColliderShape::Rectangle { size: *body_size },
                has_mass: true,
                can_rotate: false,
                has_friction: true,
                gravity: game_meta.core.physics.gravity,
                bounciness: *bounciness,
                ..default()
            },
        );
        spawner_manager.create_spawner(spawner_entity, vec![entity])
    }
}

fn update_idle_crates(
    entities: Res<Entities>,
    mut items_used: CompMut<ItemUsed>,
    assets: Res<AssetServer>,
    element_handles: Comp<ElementHandle>,
    idle_crates: CompMut<IdleCrate>,
    player_inventories: PlayerInventories,
    mut commands: Commands,
) {
    for (entity, (_le_crate, element_handle)) in
        entities.iter_with((&idle_crates, &element_handles))
    {
        let element_meta = assets.get(element_handle.0);

        let asset = assets.get(element_meta.data);
        let Ok(CrateMeta { break_timeout, .. }) = asset.try_cast_ref() else {
            continue;
        };

        let break_timeout = *break_timeout;

        if let Some(Inv { player, .. }) = player_inventories.find_item(entity) {
            if items_used.remove(entity).is_some() {
                commands.add(PlayerCommand::set_inventory(player, None));
                commands.add(
                    move |mut idle: CompMut<IdleCrate>, mut thrown: CompMut<ThrownCrate>| {
                        idle.remove(entity);
                        thrown.insert(
                            entity,
                            ThrownCrate {
                                owner: player,
                                damage_delay: Timer::new(
                                    Duration::from_secs_f32(0.25),
                                    TimerMode::Once,
                                ),
                                break_timeout: Timer::new(break_timeout, TimerMode::Once),
                                was_colliding: false,
                                crate_break_state: 0,
                            },
                        );
                    },
                );
            }
        }
    }
}

fn update_thrown_crates(
    entities: Res<Entities>,
    mut hydrated: CompMut<MapElementHydrated>,
    assets: Res<AssetServer>,
    element_handles: Comp<ElementHandle>,
    mut thrown_crates: CompMut<ThrownCrate>,
    mut commands: Commands,
    mut atlas_sprites: CompMut<AtlasSprite>,
    players: Comp<PlayerIdx>,
    collision_world: CollisionWorld,
    mut bodies: CompMut<KinematicBody>,
    mut audio_center: ResMut<AudioCenter>,
    transforms: Comp<Transform>,
    spawners: Comp<DehydrateOutOfBounds>,
    invincibles: CompMut<Invincibility>,
    time: Res<Time>,
) {
    for (entity, (thrown_crate, element_handle, transform, atlas_sprite, body, spawner)) in entities
        .iter_with((
            &mut thrown_crates,
            &element_handles,
            &transforms,
            &mut atlas_sprites,
            &mut bodies,
            &spawners,
        ))
    {
        let element_meta = assets.get(element_handle.0);

        let asset = assets.get(element_meta.data);
        let Ok(CrateMeta {
            breaking_anim_frames,
            breaking_atlas,
            breaking_anim_fps,
            break_sound,
            break_sound_volume,
            bounce_sound,
            bounce_sound_volume,
            crate_break_state_1,
            crate_break_state_2,
            ..
        }) = asset.try_cast_ref()
        else {
            continue;
        };

        thrown_crate.damage_delay.tick(time.delta());
        thrown_crate.break_timeout.tick(time.delta());

        let colliding_with_tile = {
            let collider = collision_world.get_collider(entity);
            let aabb = collider.shape.compute_aabb(default());
            let width = aabb.extents().x + 2.0;
            let height = aabb.extents().y + 2.0;
            collision_world.tile_collision(
                Transform::from_translation(transform.translation),
                ColliderShape::Rectangle {
                    size: vec2(width, height),
                },
            ) != TileCollisionKind::Empty
        };

        if colliding_with_tile && !thrown_crate.was_colliding {
            thrown_crate.was_colliding = true;
            thrown_crate.crate_break_state += 1;
            audio_center.play_sound(*bounce_sound, *bounce_sound_volume);
        } else if !colliding_with_tile {
            thrown_crate.was_colliding = false;
        }

        match thrown_crate.crate_break_state {
            1 => {
                atlas_sprite.atlas = *breaking_atlas;
                atlas_sprite.index = *crate_break_state_1;
            }
            3 => {
                atlas_sprite.index = *crate_break_state_2;
            }
            _ => {}
        }

        let colliding_with_players = collision_world
            .actor_collisions_filtered(entity, |e| {
                players.contains(e)
                    && invincibles.get(e).is_none()
                    && thrown_crate.damage_delay.finished()
            })
            .into_iter()
            .collect::<Vec<_>>();

        for player_entity in &colliding_with_players {
            commands.add(PlayerCommand::kill(
                *player_entity,
                Some(transform.translation.xy()),
            ));
        }
        let kill_nearby_colliding: bool = kill_all_colliding_if_freshly_thrown(
            thrown_crate,
            &collision_world,
            &players,
            &invincibles,
            &mut commands,
            transform,
        );

        if !colliding_with_players.is_empty()
            || kill_nearby_colliding
            || thrown_crate.break_timeout.finished()
            || thrown_crate.crate_break_state >= 4
            || body.is_on_ground && body.velocity.length_squared() < 0.1
        {
            hydrated.remove(**spawner);

            let breaking_anim_frames = *breaking_anim_frames;
            let breaking_anim_fps = *breaking_anim_fps;
            let atlas = *breaking_atlas;

            audio_center.play_sound(*break_sound, *break_sound_volume);

            commands.add(
                move |mut entities: ResMutInit<Entities>,
                      mut transforms: CompMut<Transform>,
                      mut animated_sprites: CompMut<AnimatedSprite>,
                      mut lifetimes: CompMut<Lifetime>,
                      mut atlas_sprites: CompMut<AtlasSprite>| {
                    let pos = *transforms.get(entity).unwrap();
                    entities.kill(entity);
                    let breaking_anim_ent = entities.create();
                    atlas_sprites.insert(breaking_anim_ent, AtlasSprite { atlas, ..default() });
                    animated_sprites.insert(
                        breaking_anim_ent,
                        AnimatedSprite {
                            repeat: false,
                            fps: breaking_anim_fps,
                            frames: (1..breaking_anim_frames).collect(),
                            ..default()
                        },
                    );
                    lifetimes.insert(breaking_anim_ent, Lifetime::new(1.0));
                    transforms.insert(breaking_anim_ent, pos);
                },
            );
        }
    }
}

fn kill_all_colliding_if_freshly_thrown(
    thrown_crate: &ThrownCrate,
    collision_world: &CollisionWorld,
    players: &Comp<PlayerIdx>,
    invincibles: &CompMut<Invincibility>,
    commands: &mut Commands,
    transform: &Transform,
) -> bool {
    if thrown_crate.damage_delay.finished() {
        return false;
    }

    let colliding_with_players = collision_world
        .actor_collisions_filtered(thrown_crate.owner, |e| players.contains(e))
        .into_iter()
        .collect::<Vec<_>>();

    if !colliding_with_players.is_empty() {
        for player_entity in &colliding_with_players {
            if invincibles.get(*player_entity).is_none() {
                commands.add(PlayerCommand::kill(
                    *player_entity,
                    Some(transform.translation.xy()),
                ));
            }
        }
        commands.add(PlayerCommand::kill(
            thrown_crate.owner,
            Some(transform.translation.xy()),
        ));
        true
    } else {
        false
    }
}