summaryrefslogtreecommitdiff
path: root/src/content/entities/entity_bullet_emitter.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/entities/entity_bullet_emitter.rs')
-rw-r--r--src/content/entities/entity_bullet_emitter.rs79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/content/entities/entity_bullet_emitter.rs b/src/content/entities/entity_bullet_emitter.rs
new file mode 100644
index 0000000..c58bd6b
--- /dev/null
+++ b/src/content/entities/entity_bullet_emitter.rs
@@ -0,0 +1,79 @@
+use glam::{IVec2, Vec2};
+use rapier2d::dynamics::RigidBodyBuilder;
+
+use crate::{
+ content::materials::MaterialId,
+ input::Input,
+ sim::{
+ cell::Cell,
+ entity::{EntityBehaviour, EntityCells, EntityDef, EntityUpdateCtx},
+ lib::force::apply_bullet,
+ sim_manager::SimCtx,
+ },
+};
+
+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 {
+ self.shot_timer -= delta_time;
+ if self.shot_timer <= 0.0 {
+ apply_bullet(
+ ctx,
+ update_ctx.entity_data.transform(ctx).unwrap().0,
+ target,
+ 400,
+ );
+ self.shot_timer = 0.1;
+ }
+
+ 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 / crate::config::CELLS_TO_METRES)
+ .gravity_scale(0.0)
+ .build();
+
+ EntityDef::from_cells_and_rb(
+ entity_cells,
+ rb,
+ Some(Box::new(BulletEmitterEntityBehaviour {
+ shot_timer: 0.0,
+ life,
+ target: None,
+ })),
+ )
+}