added whirlpool

This commit is contained in:
wm-c 2021-04-25 20:18:11 -04:00
parent 2014ad42c8
commit c8da93bfdb
7 changed files with 153 additions and 12 deletions

View File

@ -29,5 +29,15 @@
"y": 100
}
}
]
],
"whirlpool": [
{
"position" : {
"x": 250,
"y": 250
},
"should_remove": false
}
]
}

View File

@ -1,3 +1,4 @@
pub mod base;
pub mod jellyfish;
pub mod octopus;
pub mod octopus;
pub mod whirlpool;

View File

@ -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<RaylibDrawHandle>,
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;
}
}

View File

@ -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);

View File

@ -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

View File

@ -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);
}
}
}
}

View File

@ -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<Rectangle>,
// Mobs
pub jellyfish: Vec<JellyFish>,
pub octopus: Vec<Octopus>,
pub whirlpool: Vec<Whirlpool>,
}
@ -49,6 +51,8 @@ impl World {
});
}
Ok(result)
}