stun the jellyfish

This commit is contained in:
Evan Pratten 2021-04-24 18:28:27 -04:00
parent e8317e0619
commit 9493baedf5
6 changed files with 104 additions and 42 deletions

View File

@ -3,7 +3,12 @@ use raylib::prelude::*;
use crate::{player::Player, resources::GlobalResources}; use crate::{player::Player, resources::GlobalResources};
pub trait EnemyBase { pub trait EnemyBase {
fn render(&mut self, context_2d: &mut RaylibMode2D<RaylibDrawHandle>, resources: &mut GlobalResources); fn render(
&mut self,
context_2d: &mut RaylibMode2D<RaylibDrawHandle>,
resources: &mut GlobalResources,
dt: f64,
);
fn handle_logic(&mut self, player: &mut Player, dt: f64); fn handle_logic(&mut self, player: &mut Player, dt: f64);
fn handle_getting_attacked(&mut self); fn handle_getting_attacked(&mut self, stun_duration: f64);
} }

View File

@ -1,5 +1,8 @@
use super::base::EnemyBase; use super::base::EnemyBase;
use crate::{player::Player, resources::GlobalResources}; use crate::{
lib::utils::calculate_linear_slide, pallette::TRANSLUCENT_RED_64, player::Player,
resources::GlobalResources,
};
use raylib::prelude::*; use raylib::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -9,6 +12,8 @@ pub struct JellyFish {
#[serde(skip)] #[serde(skip)]
pub stunned_timer: f64, pub stunned_timer: f64,
#[serde(skip)]
pub max_stunned_time: f64,
#[serde(skip)] #[serde(skip)]
pub do_stun_player: bool, pub do_stun_player: bool,
@ -21,31 +26,51 @@ impl EnemyBase for JellyFish {
&mut self, &mut self,
context_2d: &mut raylib::prelude::RaylibMode2D<raylib::prelude::RaylibDrawHandle>, context_2d: &mut raylib::prelude::RaylibMode2D<raylib::prelude::RaylibDrawHandle>,
resources: &mut GlobalResources, resources: &mut GlobalResources,
dt: f64
) { ) {
let is_jelly_stunned = self.stunned_timer != 0.0;
// Simple sine position // Simple sine position
let v_trans = context_2d.get_time().sin(); let v_trans = if is_jelly_stunned {
0.0
} else {
context_2d.get_time().sin()
};
let trans_pose = Vector2 {
x: self.position.x,
y: self.position.y + (2.0 * v_trans as f32),
};
// Render the stun ring
if self.max_stunned_time > 0.0 && self.stunned_timer > 0.0 {
let stun_ring_radius =
calculate_linear_slide(self.stunned_timer / self.max_stunned_time);
context_2d.draw_circle_v(
trans_pose,
stun_ring_radius as f32 * 20.0,
TRANSLUCENT_RED_64,
);
self.stunned_timer -= dt;
}
// Render the jellyfish // Render the jellyfish
resources.jellyfish_animation_regular.draw( resources
context_2d, .jellyfish_animation_regular
Vector2 { .draw(context_2d, trans_pose, 0.0);
x: self.position.x,
y: self.position.y + (2.0 * v_trans as f32), // Only do stun loop if not stunned
}, if !is_jelly_stunned {
0.0, resources
); .jellyfish_animation_attack
resources.jellyfish_animation_attack.draw( .draw(context_2d, trans_pose, 0.0);
context_2d, }
Vector2 {
x: self.position.x, // Check if the jelly is in stun mode
y: self.position.y + (2.0 * v_trans as f32), self.do_stun_player = (resources
},
0.0,
);
self.do_stun_player = resources
.jellyfish_animation_attack .jellyfish_animation_attack
.get_current_frame_id(context_2d) .get_current_frame_id(context_2d)
== 13; == 13)
&& !is_jelly_stunned;
} }
fn handle_logic(&mut self, player: &mut Player, dt: f64) { fn handle_logic(&mut self, player: &mut Player, dt: f64) {
@ -53,7 +78,9 @@ impl EnemyBase for JellyFish {
if self.do_stun_player {} if self.do_stun_player {}
} }
fn handle_getting_attacked(&mut self) { fn handle_getting_attacked(&mut self, stun_duration: f64) {
todo!() println!("Attack");
self.stunned_timer = stun_duration;
self.max_stunned_time = stun_duration;
} }
} }

View File

@ -10,13 +10,13 @@ impl StunGun {
pub fn lvl1() -> Self { pub fn lvl1() -> Self {
Self { Self {
range: 30.0, range: 30.0,
duration: 0.5, duration: 0.75,
} }
} }
pub fn lvl2() -> Self { pub fn lvl2() -> Self {
Self { Self {
range: 60.0, range: 60.0,
duration: 0.75, duration: 1.25,
} }
} }
} }

View File

@ -3,7 +3,12 @@ mod playerlogic;
use raylib::prelude::*; use raylib::prelude::*;
use crate::{entities::enemy::base::EnemyBase, gamecore::{GameCore, GameState}, lib::wrappers::audio::player::AudioPlayer, pallette::{SKY, WATER}}; use crate::{
entities::enemy::base::EnemyBase,
gamecore::{GameCore, GameState},
lib::wrappers::audio::player::AudioPlayer,
pallette::{SKY, WATER},
};
use super::screen::Screen; use super::screen::Screen;
@ -64,11 +69,7 @@ impl InGameScreen {
) { ) {
// Render every collider // Render every collider
for collider in game_core.world.colliders.iter() { for collider in game_core.world.colliders.iter() {
context_2d.draw_rectangle_lines_ex( context_2d.draw_rectangle_lines_ex(collider, 1, Color::RED);
collider,
1,
Color::RED,
);
} }
} }
} }
@ -110,7 +111,7 @@ impl Screen for InGameScreen {
// Render the world // Render the world
self.render_world(&mut context_2d, game_core); self.render_world(&mut context_2d, game_core);
if game_core.show_simple_debug_info{ if game_core.show_simple_debug_info {
self.render_colliders(&mut context_2d, game_core); self.render_colliders(&mut context_2d, game_core);
} }
@ -121,11 +122,13 @@ impl Screen for InGameScreen {
fish.render(&mut context_2d); fish.render(&mut context_2d);
} }
for jellyfish in game_core.world.jellyfish.iter_mut() { for jellyfish in game_core.world.jellyfish.iter_mut() {
jellyfish.render(&mut context_2d, &mut game_core.resources); jellyfish.render(&mut context_2d, &mut game_core.resources, dt);
} }
// Render Player // Render Player
game_core.player.render(&mut context_2d, &mut game_core.resources, dt); game_core
.player
.render(&mut context_2d, &mut game_core.resources, dt);
} }
// Render the hud // Render the hud

View File

@ -33,4 +33,11 @@ pub const WATER: Color = Color {
g: 66, g: 66,
b: 143, b: 143,
a: 255 a: 255
};
pub const TRANSLUCENT_RED_64: Color = Color {
r: 230,
g: 41,
b: 55,
a: 64,
}; };

View File

@ -1,6 +1,14 @@
use crate::{
entities::enemy::base::EnemyBase,
gamecore::{GameCore, GameProgress},
items::{AirBag, Flashlight, Flippers, StunGun},
lib::utils::calculate_linear_slide,
pallette::{TRANSLUCENT_WHITE_64, TRANSLUCENT_WHITE_96},
resources::GlobalResources,
world::World,
};
use raylib::prelude::*; use raylib::prelude::*;
use serde::{Serialize, Deserialize}; use serde::{Deserialize, Serialize};
use crate::{entities::enemy::base::EnemyBase, gamecore::{GameCore, GameProgress}, items::{AirBag, Flashlight, Flippers, StunGun}, lib::utils::{calculate_linear_slide}, pallette::{TRANSLUCENT_WHITE_64, TRANSLUCENT_WHITE_96}, resources::GlobalResources, world::World};
const AOE_RING_MAX_RADIUS: f32 = 60.0; const AOE_RING_MAX_RADIUS: f32 = 60.0;
const STUN_ATTACK_TIME: f64 = 0.75; const STUN_ATTACK_TIME: f64 = 0.75;
@ -10,7 +18,16 @@ pub struct PlayerInventory {
pub stun_gun: Option<StunGun>, pub stun_gun: Option<StunGun>,
pub air_bag: Option<AirBag>, pub air_bag: Option<AirBag>,
pub flashlight: Option<Flashlight>, pub flashlight: Option<Flashlight>,
pub flippers: Option<Flippers> pub flippers: Option<Flippers>,
}
impl PlayerInventory {
pub fn new() -> Self {
Self {
stun_gun: Some(StunGun::lvl1()), //TMP
..Default::default()
}
}
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -39,6 +56,7 @@ impl Player {
is_moving: true, is_moving: true,
radius: 4.5, radius: 4.5,
position: spawn.clone(), position: spawn.clone(),
inventory: PlayerInventory::new(),
..Default::default() ..Default::default()
} }
} }
@ -84,12 +102,12 @@ impl Player {
if self.inventory.stun_gun.is_some() && self.stun_timer == 0.0 { if self.inventory.stun_gun.is_some() && self.stun_timer == 0.0 {
self.attacking_timer = self.inventory.stun_gun.as_ref().unwrap().duration; self.attacking_timer = self.inventory.stun_gun.as_ref().unwrap().duration;
// Stun everything in reach // Stun everything in reach
let stun_reach = self.inventory.stun_gun.as_ref().unwrap().range; let stun_reach = self.inventory.stun_gun.as_ref().unwrap().range;
for jellyfish in world.jellyfish.iter_mut() { for jellyfish in world.jellyfish.iter_mut() {
if jellyfish.position.distance_to(self.position).abs() <= stun_reach { if jellyfish.position.distance_to(self.position).abs() <= stun_reach {
jellyfish.handle_getting_attacked(); jellyfish.handle_getting_attacked(self.attacking_timer);
} }
} }
} }
@ -151,7 +169,9 @@ impl Player {
// Calculate AOE ring // Calculate AOE ring
if self.is_stun_gun_active() { if self.is_stun_gun_active() {
let aoe_ring = calculate_linear_slide( self.attacking_timer / self.inventory.stun_gun.as_ref().unwrap().duration) as f32; let aoe_ring = calculate_linear_slide(
self.attacking_timer / self.inventory.stun_gun.as_ref().unwrap().duration,
) as f32;
self.attacking_timer = (self.attacking_timer - dt).max(0.0); self.attacking_timer = (self.attacking_timer - dt).max(0.0);
// Render attack AOE // Render attack AOE