From c8da93bfdb5dcda164478dee3a6319458d0c3df7 Mon Sep 17 00:00:00 2001 From: wm-c Date: Sun, 25 Apr 2021 20:18:11 -0400 Subject: [PATCH] added whirlpool --- assets/worlds/mainworld.json | 12 +++++- src/entities/enemy/mod.rs | 3 +- src/entities/enemy/whirlpool.rs | 43 ++++++++++++++++++++ src/logic/ingame/mod.rs | 24 ++++++++--- src/logic/ingame/playerlogic.rs | 72 +++++++++++++++++++++++++++++++-- src/player.rs | 5 +++ src/world.rs | 6 ++- 7 files changed, 153 insertions(+), 12 deletions(-) create mode 100644 src/entities/enemy/whirlpool.rs diff --git a/assets/worlds/mainworld.json b/assets/worlds/mainworld.json index add0aa3..146d91d 100644 --- a/assets/worlds/mainworld.json +++ b/assets/worlds/mainworld.json @@ -29,5 +29,15 @@ "y": 100 } } - ] + ], + "whirlpool": [ + { + "position" : { + "x": 250, + "y": 250 + }, + "should_remove": false + } + + ] } \ No newline at end of file diff --git a/src/entities/enemy/mod.rs b/src/entities/enemy/mod.rs index 1189f2b..18acd78 100644 --- a/src/entities/enemy/mod.rs +++ b/src/entities/enemy/mod.rs @@ -1,3 +1,4 @@ pub mod base; pub mod jellyfish; -pub mod octopus; \ No newline at end of file +pub mod octopus; +pub mod whirlpool; \ No newline at end of file diff --git a/src/entities/enemy/whirlpool.rs b/src/entities/enemy/whirlpool.rs new file mode 100644 index 0000000..f0192b3 --- /dev/null +++ b/src/entities/enemy/whirlpool.rs @@ -0,0 +1,43 @@ +use raylib::prelude::*; + +use super::base::EnemyBase; + +use serde::{Deserialize, Serialize}; + + +#[derive(Debug, Serialize, Deserialize, Default, Clone)] +pub struct Whirlpool{ + pub position: Vector2, + pub should_remove: bool, +} + +impl Whirlpool{ + + pub fn should_remove(&self) -> bool{ + return self.should_remove; + } + +} + +impl EnemyBase for Whirlpool{ + fn render( + &mut self, + context_2d: &mut RaylibMode2D, + player: &mut crate::player::Player, + 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); + } + + fn handle_logic(&mut self, player: &mut crate::player::Player, dt: f64) { + + } + + fn handle_getting_attacked(&mut self, stun_duration: f64, current_time: f64) { + self.should_remove = true; + } + + + +} \ No newline at end of file diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index 383bd25..1ebedaf 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -3,14 +3,11 @@ mod playerlogic; use raylib::prelude::*; -use crate::{ - entities::enemy::base::EnemyBase, - gamecore::{GameCore, GameState}, - lib::wrappers::audio::player::AudioPlayer, - pallette::{SKY, WATER, WATER_DARK}, -}; +use crate::{entities::enemy::{base::EnemyBase, whirlpool::Whirlpool}, gamecore::{GameCore, GameState}, lib::wrappers::audio::player::AudioPlayer, pallette::{SKY, WATER, WATER_DARK}}; use super::screen::Screen; +use crate::entities::fish::FishEntity; + pub enum InGameState { BUYING, @@ -210,6 +207,21 @@ impl Screen for InGameScreen { ); } + let mut iter_count = 0; + for whirlpool_mob in game_core.world.whirlpool.iter_mut(){ + whirlpool_mob.handle_logic(&mut game_core.player, dt); + whirlpool_mob.render(&mut context_2d, &mut game_core.player, &mut game_core.resources, dt); + if whirlpool_mob.should_remove(){ + for _ in 0..10{ + game_core.world.fish.push(FishEntity::new(whirlpool_mob.position)); + } + } + + + } + + game_core.world.whirlpool.retain(|x| !x.should_remove()); + // Render transponder game_core.resources.transponder.draw(&mut context_2d, game_core.world.end_position + Vector2::new(0.0, -50.0), 0.0); diff --git a/src/logic/ingame/playerlogic.rs b/src/logic/ingame/playerlogic.rs index 91e2cef..804706b 100644 --- a/src/logic/ingame/playerlogic.rs +++ b/src/logic/ingame/playerlogic.rs @@ -24,7 +24,7 @@ pub fn update_player_movement( // Handle player movement let mouse_pose = draw_handle.get_mouse_position(); let mouse_world_pose = draw_handle.get_screen_to_world2D(mouse_pose, game_core.master_camera); - let raw_movement_direction = mouse_world_pose - game_core.player.position; + let mut raw_movement_direction = mouse_world_pose - game_core.player.position; let mut normalized_movement_direction = raw_movement_direction; normalized_movement_direction.normalize(); @@ -153,6 +153,67 @@ pub fn update_player_movement( player_real_movement *= game_core.player.inventory.flippers.as_ref().unwrap().speed_increase; } + + + + + let mut movement_drift = Vector2::new(0.0, 0.0); + for whirlpool in game_core.world.whirlpool.iter_mut(){ + + + + if game_core.player.position.distance_to(whirlpool.position) <= 100.0 && game_core.player.position.distance_to(whirlpool.position) >= 10.0{ + + let player_pose = game_core.player.position; + let whirlpool_pose = whirlpool.position; + let net_pose = player_pose - whirlpool_pose; + + + + let angle = net_pose.y.atan2(net_pose.x); + + + + // Calculates force + let force = 1.0; + + + 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); + + if force_x.is_nan(){ + force_x = 1.0 * net_pose.x; + } + + if force_y.is_nan(){ + force_y = 1.0 * net_pose.y; + } + + movement_drift.x -= force_x; + movement_drift.y -= force_y; + + } + + } + + + game_core.player.position.x += movement_drift.x; + for collider in game_core.world.colliders.iter() { + if game_core.player.collides_with_rec(collider) { + game_core.player.position.x -= movement_drift.x; + break; + } + } + + game_core.player.position.y += movement_drift.y; + for collider in game_core.world.colliders.iter() { + if game_core.player.collides_with_rec(collider) { + game_core.player.position.y -= movement_drift.y; + break; + } + } + + // Handle movement and collisions if raw_movement_direction.distance_to(Vector2::zero()) > game_core.player.size.y / 2.0 && !game_core.player.is_stunned() @@ -187,7 +248,12 @@ pub fn update_player_movement( // Handle updating the stun timer if player_stunned { game_core.player.stun_timer -= dt; - } + } + + + + + // Move the camera to follow the player let direction_from_cam_to_player = @@ -197,7 +263,7 @@ pub fn update_player_movement( // Camera only moves if you get close to the edge of the screen if player_screen_position.distance_to(window_center).abs() > 100.0 { - game_core.master_camera.target += player_real_movement; + game_core.master_camera.target += player_real_movement + movement_drift; } // If the player is not on screen, snap the camera to them diff --git a/src/player.rs b/src/player.rs index 0a78a20..e0e80fa 100644 --- a/src/player.rs +++ b/src/player.rs @@ -126,6 +126,11 @@ impl Player { if octopus.current_position.distance_to(self.position).abs() <= stun_reach { octopus.handle_getting_attacked(self.attacking_timer, current_time); } + } + for whirlpool in world.whirlpool.iter_mut() { + if whirlpool.position.distance_to(self.position).abs() <= stun_reach { + whirlpool.handle_getting_attacked(self.attacking_timer, current_time); + } } } } diff --git a/src/world.rs b/src/world.rs index ed58b00..0f62d1d 100644 --- a/src/world.rs +++ b/src/world.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use std::io::Read; use failure::Error; -use crate::{entities::{enemy::{jellyfish::JellyFish, octopus::Octopus}, fish::FishEntity}, player::Player}; +use crate::{entities::{enemy::{jellyfish::JellyFish, octopus::Octopus, whirlpool::Whirlpool}, fish::FishEntity}, player::Player}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct World { @@ -21,8 +21,10 @@ pub struct World { #[serde(skip)] pub colliders: Vec, + // Mobs pub jellyfish: Vec, pub octopus: Vec, + pub whirlpool: Vec, } @@ -49,6 +51,8 @@ impl World { }); } + + Ok(result) }