Added comments
This commit is contained in:
parent
3007f96ca4
commit
3c1d56a46b
@ -8,12 +8,17 @@ use serde::{Deserialize, Serialize};
|
|||||||
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
||||||
pub struct Whirlpool{
|
pub struct Whirlpool{
|
||||||
pub position: Vector2,
|
pub position: Vector2,
|
||||||
|
|
||||||
|
// Track if it needs removing
|
||||||
pub should_remove: bool,
|
pub should_remove: bool,
|
||||||
|
|
||||||
|
// variable for tracking rotation
|
||||||
pub rotation: f32,
|
pub rotation: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Whirlpool{
|
impl Whirlpool{
|
||||||
|
|
||||||
|
// hook to see if item needs removing
|
||||||
pub fn should_remove(&self) -> bool{
|
pub fn should_remove(&self) -> bool{
|
||||||
return self.should_remove;
|
return self.should_remove;
|
||||||
}
|
}
|
||||||
@ -37,6 +42,7 @@ impl EnemyBase for Whirlpool{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Whirlpool removed if shoot
|
||||||
fn handle_getting_attacked(&mut self, stun_duration: f64, current_time: f64) {
|
fn handle_getting_attacked(&mut self, stun_duration: f64, current_time: f64) {
|
||||||
self.should_remove = true;
|
self.should_remove = true;
|
||||||
}
|
}
|
||||||
|
@ -238,10 +238,12 @@ impl Screen for InGameScreen {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut iter_count = 0;
|
// Iterates over whirlpools and runs render and logic funcs
|
||||||
for whirlpool_mob in game_core.world.whirlpool.iter_mut(){
|
for whirlpool_mob in game_core.world.whirlpool.iter_mut(){
|
||||||
whirlpool_mob.handle_logic(&mut game_core.player, dt);
|
whirlpool_mob.handle_logic(&mut game_core.player, dt);
|
||||||
whirlpool_mob.render(&mut context_2d, &mut game_core.player, &mut game_core.resources, dt);
|
whirlpool_mob.render(&mut context_2d, &mut game_core.player, &mut game_core.resources, dt);
|
||||||
|
|
||||||
|
// Spawns 10 fish on spawn
|
||||||
if whirlpool_mob.should_remove(){
|
if whirlpool_mob.should_remove(){
|
||||||
for _ in 0..10{
|
for _ in 0..10{
|
||||||
game_core.world.fish.push(FishEntity::new(whirlpool_mob.position));
|
game_core.world.fish.push(FishEntity::new(whirlpool_mob.position));
|
||||||
@ -251,6 +253,7 @@ impl Screen for InGameScreen {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Removes whirlpools set for removal
|
||||||
game_core.world.whirlpool.retain(|x| !x.should_remove());
|
game_core.world.whirlpool.retain(|x| !x.should_remove());
|
||||||
|
|
||||||
|
|
||||||
|
@ -160,30 +160,33 @@ pub fn update_player_movement(
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Creates variable to calculate the distant
|
||||||
let mut movement_drift = Vector2::new(0.0, 0.0);
|
let mut movement_drift = Vector2::new(0.0, 0.0);
|
||||||
|
|
||||||
|
// Check each whirlpool for effects
|
||||||
for whirlpool in game_core.world.whirlpool.iter_mut(){
|
for whirlpool in game_core.world.whirlpool.iter_mut(){
|
||||||
|
|
||||||
|
|
||||||
|
// check if its in range and not to close
|
||||||
if game_core.player.position.distance_to(whirlpool.position) <= 100.0 && game_core.player.position.distance_to(whirlpool.position) >= 10.0{
|
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;
|
// Calculates info for formulas
|
||||||
let whirlpool_pose = whirlpool.position;
|
|
||||||
let net_pose = player_pose - whirlpool_pose;
|
|
||||||
|
|
||||||
|
|
||||||
|
// Deltas between positions
|
||||||
|
let net_pose = game_core.player.position - whirlpool.position;
|
||||||
|
|
||||||
|
// Angle between: UNITS: RADIANS
|
||||||
let angle = net_pose.y.atan2(net_pose.x);
|
let angle = net_pose.y.atan2(net_pose.x);
|
||||||
|
|
||||||
|
|
||||||
// Calculates force
|
// Calculates force
|
||||||
let force = 15.0 / game_core.player.position.distance_to(whirlpool.position) ;
|
let force = 15.0 / game_core.player.position.distance_to(whirlpool.position);
|
||||||
|
|
||||||
|
|
||||||
|
// Calculates componets of force
|
||||||
let mut force_x = (force as f32 * angle.cos()).clamp(-5.0, 5.0);
|
let mut force_x = (force as f32 * angle.cos()).clamp(-5.0, 5.0);
|
||||||
let mut force_y = (force as f32 * angle.sin()).clamp(-5.0, 5.0);
|
let mut force_y = (force as f32 * angle.sin()).clamp(-5.0, 5.0);
|
||||||
|
|
||||||
|
// Prevents Nan erros
|
||||||
if force_x.is_nan(){
|
if force_x.is_nan(){
|
||||||
force_x = 5.0 * net_pose.x;
|
force_x = 5.0 * net_pose.x;
|
||||||
}
|
}
|
||||||
@ -192,6 +195,7 @@ pub fn update_player_movement(
|
|||||||
force_y = 5.0 * net_pose.y;
|
force_y = 5.0 * net_pose.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds values to drift tracker
|
||||||
movement_drift.x -= force_x;
|
movement_drift.x -= force_x;
|
||||||
movement_drift.y -= force_y;
|
movement_drift.y -= force_y;
|
||||||
|
|
||||||
@ -199,7 +203,7 @@ pub fn update_player_movement(
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checks collision
|
||||||
game_core.player.position.x += movement_drift.x;
|
game_core.player.position.x += movement_drift.x;
|
||||||
for collider in game_core.world.colliders.iter() {
|
for collider in game_core.world.colliders.iter() {
|
||||||
if game_core.player.collides_with_rec(collider) {
|
if game_core.player.collides_with_rec(collider) {
|
||||||
|
Reference in New Issue
Block a user