From 3c1d56a46b1cb66e312faea1c68ec416c561d0b4 Mon Sep 17 00:00:00 2001 From: wm-c Date: Sun, 25 Apr 2021 20:58:34 -0400 Subject: [PATCH] Added comments --- src/entities/enemy/whirlpool.rs | 6 ++++++ src/logic/ingame/mod.rs | 5 ++++- src/logic/ingame/playerlogic.rs | 22 +++++++++++++--------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/entities/enemy/whirlpool.rs b/src/entities/enemy/whirlpool.rs index 3be4db4..a2b7bf0 100644 --- a/src/entities/enemy/whirlpool.rs +++ b/src/entities/enemy/whirlpool.rs @@ -8,12 +8,17 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Default, Clone)] pub struct Whirlpool{ pub position: Vector2, + + // Track if it needs removing pub should_remove: bool, + + // variable for tracking rotation pub rotation: f32, } impl Whirlpool{ + // hook to see if item needs removing pub fn should_remove(&self) -> bool{ 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) { self.should_remove = true; } diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index 104d428..85bebba 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -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(){ whirlpool_mob.handle_logic(&mut game_core.player, 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(){ for _ in 0..10{ 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()); diff --git a/src/logic/ingame/playerlogic.rs b/src/logic/ingame/playerlogic.rs index de0543a..093ece8 100644 --- a/src/logic/ingame/playerlogic.rs +++ b/src/logic/ingame/playerlogic.rs @@ -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); + + // Check each whirlpool for effects 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{ - let player_pose = game_core.player.position; - let whirlpool_pose = whirlpool.position; - let net_pose = player_pose - whirlpool_pose; - + // Calculates info for formulas + // 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); // 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_y = (force as f32 * angle.sin()).clamp(-5.0, 5.0); + // Prevents Nan erros if force_x.is_nan(){ force_x = 5.0 * net_pose.x; } @@ -192,6 +195,7 @@ pub fn update_player_movement( force_y = 5.0 * net_pose.y; } + // Adds values to drift tracker movement_drift.x -= force_x; movement_drift.y -= force_y; @@ -199,7 +203,7 @@ pub fn update_player_movement( } - + // Checks collision game_core.player.position.x += movement_drift.x; for collider in game_core.world.colliders.iter() { if game_core.player.collides_with_rec(collider) {