summaryrefslogtreecommitdiff
path: root/src/content/entities/entity_grenade.rs
blob: f6c82e884ff36747306a4dbf55bb526bdb06e7f4 (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
use glam::{IVec2, Vec2};

use crate::{
    content::materials::MaterialId,
    sim::{
        cell::cell::Cell,
        entity::{EntityBehaviour, EntityCells, EntityDef, EntityUpdateCtx},
        lib::force::apply_explosion,
        sim_manager::SimCtx,
    },
};

struct GrenadeEntityBehaviour {
    pub fuse: f32,
}

impl EntityBehaviour for GrenadeEntityBehaviour {
    fn update(&mut self, update_ctx: &mut EntityUpdateCtx, ctx: &mut SimCtx, delta_time: f32) {
        self.fuse -= delta_time;
        if self.fuse <= 0.0 {
            apply_explosion(
                ctx,
                update_ctx.entity_data.transform(ctx).unwrap().0,
                15,
                Vec2::new(0.0, -0.6),
                200.0,
            );
            update_ctx.deferred_destroy(update_ctx.entity_data.id);
        }
    }
}

pub fn entity_grenade_def(position: Vec2, fuse: f32) -> EntityDef {
    let w = 3;
    let h = 3;
    let mut raw_cells = vec![Cell::void(); (w * h) as usize];

    for x in 0..w {
        for y in 0..h {
            let cell_idx = x + y * w;
            raw_cells[cell_idx as usize] = Cell::from_material(MaterialId::Steel);
            raw_cells[cell_idx as usize].set_entity_integrated(true);
        }
    }

    let entity_cells = EntityCells {
        cells: raw_cells,
        size: IVec2::new(w, h),
    };

    EntityDef::from_cells(
        position,
        entity_cells,
        Some(Box::new(GrenadeEntityBehaviour { fuse })),
    )
}