ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Revision:
21:20478f086bc2
Parent:
20:a8cad4e044ea
Child:
26:4253656c0755
--- a/Skateboarder/Stakeboarder.cpp	Tue Apr 16 19:19:43 2019 +0000
+++ b/Skateboarder/Stakeboarder.cpp	Sat Apr 20 14:46:50 2019 +0000
@@ -1,6 +1,6 @@
 #include "Skateboarder.h"
-
-// Define all the different possible sprite arrays.
+                                                                                
+// Define all sprite arrays.
 int skate_right[17][10] =   { 
   { 0,0,0,1,1,1,0,0,0,0 },
   { 0,0,0,1,1,1,1,0,0,0 },
@@ -129,7 +129,7 @@
 void Skateboarder::set_x_position_and_sprite(
     float joy_x, 
     int moving_counter, 
-    Skate_Direction direction, 
+    Skate_direction direction, 
     float joy_y) {     
   _skate_direction = direction;
   _moving_counter = moving_counter;
@@ -138,19 +138,17 @@
   check_duck(joy_y); 
 }
     
-void Skateboarder::process_x_variables(float joy_x) {
-  // Sets the value of the Sprite_value enum according to joystick direction 
-  // and previous direction if the joystick is not moving. Adds or subtracts
-  // one from moving counter for horizontal movement. 
-  if (joy_x > float(0.1)) {
-    _moving_counter = _moving_counter++;
-    _skate_direction = Right;
+void Skateboarder::process_x_variables(float joy_x) { 
+  if (joy_x > float(0.1)) {  // If skater is moving right.
+    _moving_counter = _moving_counter++;  // To be added to current X position.
+    _skate_direction = Right;  // skate direction enum keeps track of last
+    // direction for not moving situation.
     _sprite_value = Skate_right;
-  } else if (joy_x < float(-0.1)) {
+  } else if (joy_x < float(-0.1)) {  // If skater is moving left.
     _moving_counter = _moving_counter--;
     _skate_direction = Left;
     _sprite_value = Skate_left; 
-  } else if (_skate_direction == Left) {
+  } else if (_skate_direction == Left) {  // Skater is not moving.
     _sprite_value = Stand_left;
   } else { 
     _sprite_value = Stand_right;
@@ -158,38 +156,39 @@
 }
     
 void Skateboarder::check_duck(float joy_y) {
-  // Re-write the Sprite_value if the duck condition is met.
+  // Re-write Sprite_value with correct direction if the duck condition is met.
   if (joy_y < -0.1 && _skate_direction == Right) {
     _sprite_value = Skate_duck_right;         
   } else if (joy_y < -0.1 && _skate_direction == Left) {
     _sprite_value = Skate_duck_left;        
   }
 }
-    
-    
-void Skateboarder::set_y_position(bool jump, int jump_counter, int level_condition, Gamepad &gamepad) {
+      
+void Skateboarder::set_y_position(
+    bool jump, 
+    int jump_counter, 
+    int level_condition, 
+    Gamepad &gamepad) {
   _jump_counter = jump_counter;
   _level_condition = level_condition; 
-  // Set jump counter to 40 if button is pressed, jump counter condition is to restrict 
-  // double jump. Keep subtracting jump counter from default y position, subtracting
-  // one from the jump counter on every iteration until it is equal to 0.
-  if (jump && _jump_counter < 25) {
-    _jump_counter = 40;
-    gamepad.tone(440, 0.05);
+  if (jump && _jump_counter < 25) {  // Jump condition. Jump counter must be < 
+  // 25 to limit double jump. 
+    _jump_counter = 40;  // Controls height and fall time.
+    gamepad.tone(440, 0.05);  // Buzzer jump sound.
     wait(0.05);
     gamepad.tone(880, 0.05);
   }
-  if (_jump_counter !=0) _jump_counter--;        
-  if(_level_condition == 1 && jump){
+  if (_jump_counter !=0) _jump_counter--;         
+  if (_level_condition == 1 && jump){  // If skater is under / on-top of upper 
+  // platforms, will jump to / stay on upper platforms.
     _level = 5;
   } else if (_level_condition == 0) {
     _level = 23;
   }
-  _y = _level - 0.5*_jump_counter;        
+  _y = _level - 0.5*_jump_counter;  // Level offsets Y for approprite platform 
+  // and scaled jump counter to control height.      
 }
 
-
-
 int Skateboarder::get_x_position() {
   return _x;
 }
@@ -206,7 +205,7 @@
   return _jump_counter;
 }
 
-Skate_Direction Skateboarder::get_direction() {
+Skate_direction Skateboarder::get_direction() {
   return _skate_direction;
 }
     
@@ -233,9 +232,10 @@
 void Skateboarder::fall(bool fall_flag, Gamepad &gamepad) {
   _fall_flag = fall_flag;
   _y++;
-  gamepad.tone(int(-15.8*_y + 1363), 0.05);
-  wait(0.005);
-  if (_y == 80) {
+  gamepad.tone(int(-15.8*_y + 1363), 0.05);  // Linear equation makes decending 
+  // frequency sound.
+  wait(0.005);  // Control fall time and fall sound speed.
+  if (_y == 80) {  // Stop falling. 
     _fall_flag = false;
     _reset_flag = true;
   }