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
|
use glam::{IVec2, Vec2};
use rapier2d::dynamics::RigidBodyBuilder;
use crate::{
content::{materials::MaterialId, vfx::VfxMaterialId},
input::Input,
sim::{
cell::Cell,
entity::{EntityBehaviour, EntityCells, EntityDef, EntityUpdateCtx},
lib::force::apply_bullet,
sim_manager::SimCtx,
},
vfx::VfxLine,
};
struct BulletEmitterEntityBehaviour {
shot_timer: f32,
life: f32,
target: Option<Vec2>,
}
impl EntityBehaviour for BulletEmitterEntityBehaviour {
fn update(&mut self, update_ctx: &mut EntityUpdateCtx, ctx: &mut SimCtx, delta_time: f32) {
if self.target.is_none() && ctx.input_manager.pressed(Input::Action4) {
self.target = Some(ctx.input_manager.world_mouse_pos);
}
if let Some(target) = self.target {
let pos = update_ctx.entity_data.transform(ctx).unwrap().0;
let dir = (target - pos).normalize();
let from = pos + (dir * 4.0);
self.shot_timer -= delta_time;
if self.shot_timer <= 0.0 {
let stopped_at = apply_bullet(ctx, from, target, 70);
// 0.1 seconds to travel 200 cells
let lifetime = (stopped_at - from).length() / 1000.0;
ctx.vfx_writer.write_vfx_line(VfxLine::new(
from,
stopped_at,
1.3,
VfxMaterialId::Tracer,
lifetime,
));
self.shot_timer = 0.2;
}
self.life -= delta_time;
if self.life <= 0.0 {
update_ctx.deferred_destroy(update_ctx.entity_data.id);
}
}
}
}
pub fn entity_bullet_emitter_def(position: Vec2, life: f32) -> EntityDef {
let w = 6;
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),
};
let rb = RigidBodyBuilder::dynamic()
.translation(position)
.gravity_scale(0.0)
.build();
EntityDef::from_cells_and_rb(
entity_cells,
rb,
Some(Box::new(BulletEmitterEntityBehaviour {
shot_timer: 0.0,
life,
target: None,
})),
)
}
|