From b7655130753dcec0a6763d40b9bc699c5e8e81e7 Mon Sep 17 00:00:00 2001 From: wm-c Date: Mon, 26 Apr 2021 16:23:33 -0400 Subject: [PATCH] Expanding pufferfish --- assets/worlds/mainworld.json | 8 ++++++- src/entities/enemy/pufferfish.rs | 37 ++++++++++++++++++++++++++++-- src/logic/ingame/playerlogic.rs | 39 ++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/assets/worlds/mainworld.json b/assets/worlds/mainworld.json index bceb923..7b8a9bb 100644 --- a/assets/worlds/mainworld.json +++ b/assets/worlds/mainworld.json @@ -46,7 +46,13 @@ "position" : { "x": 400, "y": 150 - } + }, + "is_knocking_back": false, + "time_knocking_back": 0.0, + "inflate_timer": 0.0, + "size": 6.0, + "is_stunned": false + } ] diff --git a/src/entities/enemy/pufferfish.rs b/src/entities/enemy/pufferfish.rs index 57080b3..9031c1c 100644 --- a/src/entities/enemy/pufferfish.rs +++ b/src/entities/enemy/pufferfish.rs @@ -8,7 +8,12 @@ use super::base::EnemyBase; #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct Pufferfish{ pub position: Vector2, + pub is_knocking_back: bool, + pub time_knocking_back: f64, + pub inflate_timer: f64, + pub size: f32, + pub is_stunned: bool, } @@ -20,11 +25,39 @@ impl EnemyBase for Pufferfish{ resources: &mut crate::resources::GlobalResources, dt: f64, ) { - context_2d.draw_circle(self.position.x as i32, self.position.y as i32, 12.0, Color::RED); + + + + + context_2d.draw_circle(self.position.x as i32, self.position.y as i32, self.size, Color::RED); } fn handle_logic(&mut self, player: &mut crate::player::Player, dt: f64) { - + if self.position.distance_to(player.position).abs() <= self.size + 6.0{ + self.is_knocking_back = true; + + } + + if self.is_knocking_back{ + self.time_knocking_back += dt; + } + + if self.time_knocking_back >= 0.5{ + self.is_knocking_back = false; + self.time_knocking_back = 0.0; + } + + if self.position.distance_to(player.position).abs() <= 100.0{ + self.inflate_timer += dt; + }else{ + self.inflate_timer = 0.0; + } + + self.size = (6.0 * (1.0 + self.inflate_timer / 1.0)).clamp(6.0, 12.0) as f32; + + + + } fn handle_getting_attacked(&mut self, stun_duration: f64, current_time: f64) { diff --git a/src/logic/ingame/playerlogic.rs b/src/logic/ingame/playerlogic.rs index cf9a397..4dc5172 100644 --- a/src/logic/ingame/playerlogic.rs +++ b/src/logic/ingame/playerlogic.rs @@ -202,6 +202,45 @@ pub fn update_player_movement( } + for pufferfish in game_core.world.pufferfish.iter_mut(){ + + if pufferfish.is_knocking_back{ + // Calculates info for formulas + + // Deltas between positions + let net_pose = game_core.player.position - pufferfish.position; + + // Angle between: UNITS: RADIANS + let angle = net_pose.y.atan2(net_pose.x); + + + // Calculates force + let force = 1.0 / game_core.player.position.distance_to(pufferfish.position); + + // Calculates componets of force + let mut force_x = (force as f32 * angle.cos()).clamp(-1.0, 1.0); + let mut force_y = (force as f32 * angle.sin()).clamp(-1.0, 1.0); + + // Prevents Nan erros + if force_x.is_nan(){ + force_x = 1.0 * net_pose.x; + } + + if force_y.is_nan(){ + force_y = 1.0 * net_pose.y; + } + + game_core.player.additional_vel.x += force_x; + game_core.player.additional_vel.y += force_y; + + should_apply_friction = false; + + } + + + + } + if should_apply_friction { game_core.player.additional_vel.x /= PLAYER_FRICTION; game_core.player.additional_vel.y /= PLAYER_FRICTION;