Expanding pufferfish
This commit is contained in:
parent
38e63414b0
commit
b765513075
@ -46,7 +46,13 @@
|
|||||||
"position" : {
|
"position" : {
|
||||||
"x": 400,
|
"x": 400,
|
||||||
"y": 150
|
"y": 150
|
||||||
}
|
},
|
||||||
|
"is_knocking_back": false,
|
||||||
|
"time_knocking_back": 0.0,
|
||||||
|
"inflate_timer": 0.0,
|
||||||
|
"size": 6.0,
|
||||||
|
"is_stunned": false
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
]
|
]
|
||||||
|
@ -8,7 +8,12 @@ use super::base::EnemyBase;
|
|||||||
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
||||||
pub struct Pufferfish{
|
pub struct Pufferfish{
|
||||||
pub position: Vector2,
|
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,
|
resources: &mut crate::resources::GlobalResources,
|
||||||
dt: f64,
|
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) {
|
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) {
|
fn handle_getting_attacked(&mut self, stun_duration: f64, current_time: f64) {
|
||||||
|
@ -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 {
|
if should_apply_friction {
|
||||||
game_core.player.additional_vel.x /= PLAYER_FRICTION;
|
game_core.player.additional_vel.x /= PLAYER_FRICTION;
|
||||||
game_core.player.additional_vel.y /= PLAYER_FRICTION;
|
game_core.player.additional_vel.y /= PLAYER_FRICTION;
|
||||||
|
Reference in New Issue
Block a user