ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stakeboarder.cpp Source File

Stakeboarder.cpp

00001 #include "Skateboarder.h"
00002                                                                                 
00003 // Define all sprite arrays.
00004 int skate_right[17][10] =   { 
00005   { 0,0,0,1,1,1,0,0,0,0 },
00006   { 0,0,0,1,1,1,1,0,0,0 },
00007   { 0,1,1,1,1,1,1,0,0,0 },
00008   { 0,0,0,1,0,1,1,0,0,0 },
00009   { 0,0,0,1,1,1,0,0,0,0 },
00010   { 0,0,0,1,1,0,0,0,0,0 },
00011   { 0,0,0,1,1,0,0,0,0,0 },
00012   { 0,1,1,1,1,1,1,1,0,0 },
00013   { 1,1,0,1,1,0,0,1,1,0 },
00014   { 1,0,0,1,1,0,0,0,0,0 },
00015   { 0,0,0,1,1,0,0,0,0,0 },
00016   { 0,0,0,1,1,0,0,0,0,0 },
00017   { 0,0,0,1,0,1,0,0,0,0 },
00018   { 0,0,0,1,0,0,1,0,0,0 },
00019   { 1,0,0,1,0,0,0,1,0,1 },
00020   { 0,1,1,1,1,1,1,1,1,0 },
00021   { 0,0,1,0,0,0,0,1,0,0 },  
00022 };
00023 
00024 int skate_left[17][10] =   { 
00025   { 0,0,0,0,1,1,1,0,0,0 },
00026   { 0,0,0,1,1,1,1,0,0,0 },
00027   { 0,0,0,1,1,1,1,1,1,0 },
00028   { 0,0,0,1,1,0,1,0,0,0 },
00029   { 0,0,0,0,1,1,1,0,0,0 },
00030   { 0,0,0,0,0,1,1,0,0,0 },
00031   { 0,0,0,0,0,1,1,0,0,0 },
00032   { 0,1,1,1,1,1,1,1,0,0 },
00033   { 0,0,0,0,1,1,1,1,1,0 },
00034   { 0,0,0,0,1,1,0,0,1,0 },
00035   { 0,0,0,0,1,1,0,0,1,0 },
00036   { 0,0,0,0,1,1,0,0,0,0 },
00037   { 0,0,0,1,0,1,0,0,0,0 },
00038   { 0,0,1,0,0,1,0,0,0,0 },
00039   { 1,1,0,0,0,1,0,0,0,1 },
00040   { 0,1,1,1,1,1,1,1,1,0 },
00041   { 0,0,0,1,0,0,0,0,1,0 },    
00042 };
00043 
00044 int skate_stand_left[17][10] =   { 
00045   { 0,0,0,0,1,1,0,0,0,0 },
00046   { 0,0,0,1,1,1,1,0,0,0 },
00047   { 0,0,0,1,1,1,1,1,1,0 },
00048   { 0,0,0,1,0,0,1,0,0,0 },
00049   { 0,0,0,1,1,1,1,0,0,0 },
00050   { 0,0,0,0,1,1,0,0,0,0 },
00051   { 0,0,0,1,1,1,1,0,0,0 },
00052   { 0,0,1,1,1,1,0,1,0,0 },
00053   { 1,1,0,1,1,1,0,1,0,0 },
00054   { 0,0,0,1,1,1,0,0,0,0 },
00055   { 0,0,1,1,1,1,0,0,0,0 },
00056   { 1,0,1,0,0,1,0,0,0,0 },
00057   { 1,1,0,0,0,1,0,0,0,0 },
00058   { 0,1,0,0,0,1,0,0,0,0 },
00059   { 1,0,1,0,0,1,0,0,0,0 },
00060   { 0,0,0,1,0,1,0,0,0,0 },
00061   { 0,0,1,0,1,1,1,0,0,0 },  
00062 };
00063 
00064 int skate_stand_right[17][10] =   { 
00065   { 0,0,0,1,1,0,0,0,0,0 },
00066   { 0,0,1,1,1,1,0,0,0,0 },
00067   { 1,1,1,1,1,1,0,0,0,0 },
00068   { 0,0,1,0,0,1,0,0,0,0 },
00069   { 0,0,1,1,1,1,0,0,0,0 },
00070   { 0,0,0,1,1,0,0,0,0,0 },
00071   { 0,0,1,1,1,1,0,0,0,0 },
00072   { 0,1,1,1,1,0,1,0,0,0 },
00073   { 1,0,1,1,1,0,0,1,0,0 },
00074   { 1,0,1,1,1,0,0,0,0,0 },
00075   { 0,0,1,1,1,1,1,0,0,0 },
00076   { 0,0,1,0,0,0,1,0,1,0 },
00077   { 0,0,1,0,0,0,1,1,0,0 },
00078   { 0,0,1,0,0,0,1,0,1,0 },
00079   { 0,0,1,0,0,1,0,0,0,0 },
00080   { 0,0,1,0,1,0,0,0,0,0 },
00081   { 0,1,1,1,0,1,0,0,0,0 }, 
00082 };
00083 
00084 int skate_duck_right[17][10] =   { 
00085   { 0,0,0,0,0,0,0,0,0,0 },
00086   { 0,0,0,0,0,0,0,0,0,0 },
00087   { 0,0,0,0,0,0,0,0,0,0 },
00088   { 0,0,0,0,0,0,0,0,0,0 },
00089   { 0,0,0,0,1,1,0,0,0,0 },
00090   { 0,0,0,1,1,1,1,0,0,0 },
00091   { 0,1,1,1,1,1,1,0,0,0 },
00092   { 0,0,0,1,0,0,1,0,0,0 },
00093   { 0,0,0,0,1,1,0,0,0,0 },
00094   { 0,0,1,1,1,1,1,1,0,0 },
00095   { 0,1,0,1,1,1,1,0,1,1 },
00096   { 1,0,0,1,1,1,1,0,0,0 },
00097   { 0,0,0,1,1,1,1,0,0,0 },
00098   { 0,0,1,1,0,0,1,1,0,0 },
00099   { 1,0,1,1,0,0,1,1,0,1 },
00100   { 0,1,1,1,1,1,1,1,1,0 },
00101   { 0,0,1,0,0,0,0,1,0,0 }, 
00102 };
00103 
00104 int skate_duck_left[17][10] =   {
00105   { 0,0,0,0,0,0,0,0,0,0 },
00106   { 0,0,0,0,0,0,0,0,0,0 },
00107   { 0,0,0,0,0,0,0,0,0,0 },
00108   { 0,0,0,0,0,0,0,0,0,0 },
00109   { 0,0,0,0,1,1,0,0,0,0 },
00110   { 0,0,0,1,1,1,1,0,0,0 },
00111   { 0,0,0,1,1,1,1,1,1,0 },
00112   { 0,0,0,1,0,0,1,0,0,0 },
00113   { 0,0,0,0,1,1,0,0,0,0 },
00114   { 0,0,1,1,1,1,1,1,0,0 },
00115   { 1,1,0,1,1,1,1,0,1,0 },
00116   { 0,0,0,1,1,1,1,0,0,1 },
00117   { 0,0,0,1,1,1,1,0,0,0 },
00118   { 0,0,1,1,0,0,1,1,0,0 },
00119   { 1,0,1,1,0,0,1,1,0,1 },
00120   { 0,1,1,1,1,1,1,1,1,0 },
00121   { 0,0,1,0,0,0,0,1,0,0 },   
00122 };
00123 
00124 // Constructor and destructor.
00125 Skateboarder::Skateboarder() {} 
00126 
00127 Skateboarder::~Skateboarder() {}
00128 
00129 void Skateboarder::set_x_position_and_sprite(
00130     float joy_x, 
00131     int moving_counter, 
00132     Skate_direction direction, 
00133     float joy_y) {
00134   // Set up the new x coord and use that to set the sprite.           
00135   _skate_direction = direction;
00136   _moving_counter = moving_counter;
00137   process_x_variables(joy_x);      
00138   _x = 40 + 0.4*_moving_counter;  // Moving counter is scaled for speed control.
00139   check_duck(joy_y);  // Needed to overwrite sprite if ducking.
00140 }
00141     
00142 void Skateboarder::process_x_variables(float joy_x) { 
00143   if (joy_x > float(0.1)) {  // If skater is moving right.
00144     _moving_counter = _moving_counter++;  // To be added to current X position.
00145     _skate_direction = Right;  // skate direction enum keeps track of last
00146     // direction for not moving situation.
00147     _sprite_value = Skate_right;
00148   } else if (joy_x < float(-0.1)) {  // If skater is moving left.
00149     _moving_counter = _moving_counter--;
00150     _skate_direction = Left;
00151     _sprite_value = Skate_left; 
00152   } else if (_skate_direction == Left) {  // Skater is not moving.
00153     _sprite_value = Stand_left;
00154   } else { 
00155     _sprite_value = Stand_right;
00156   } 
00157 }
00158     
00159 void Skateboarder::check_duck(float joy_y) {
00160   // Re-write Sprite_value with correct direction if the duck condition is met.
00161   if (joy_y < -0.1 && _skate_direction == Right) {
00162     _sprite_value = Skate_duck_right;         
00163   } else if (joy_y < -0.1 && _skate_direction == Left) {
00164     _sprite_value = Skate_duck_left;        
00165   }
00166 }
00167       
00168 void Skateboarder::set_y_position(
00169     bool jump, 
00170     int jump_counter, 
00171     int level_condition, 
00172     Gamepad &gamepad) {
00173   _jump_counter = jump_counter;
00174   _level_condition = level_condition; 
00175   // Jump condition. Jump from A button must be true and counter less than 35 to
00176   // limit double jump. 
00177   if (jump && _jump_counter < 35) {  
00178     _jump_counter = 40;  // Controls height and fall time.
00179     gamepad.tone(440, 0.05);  // Buzzer jump sound.
00180     wait(0.05);
00181     gamepad.tone(880, 0.05);
00182   }
00183   if (_jump_counter !=0) _jump_counter--;  // Keep falling until not jumping.       
00184   if (_level_condition == 1 && jump){  // If skater is under / on-top of upper 
00185   // platforms, will jump to / stay on upper platforms.
00186     _level = 5;
00187   } else if (_level_condition == 0) {
00188     _level = 23;
00189   }
00190   _y = _level - 0.5*_jump_counter;  // Level offsets Y for approprite platform 
00191   // and scaled jump counter to control height.      
00192 }
00193 
00194 int Skateboarder::get_x_position() {
00195   return _x;
00196 }
00197 
00198 int Skateboarder::get_moving_counter() {
00199   return _moving_counter;
00200 }
00201   
00202 int Skateboarder::get_y_position() {
00203   return _y;
00204 }
00205 
00206 int Skateboarder::get_jump_counter() {
00207   return _jump_counter;
00208 }
00209 
00210 Skate_direction Skateboarder::get_direction() {
00211   return _skate_direction;
00212 }
00213     
00214 Sprite_value Skateboarder::get_sprite_value() {
00215   return _sprite_value;
00216 }   
00217 
00218 int * Skateboarder::get_sprite(Sprite_value sprite) {
00219   // Return correct sprite.
00220   if (sprite == Skate_right) {
00221     return *skate_right;
00222   } else if (sprite == Skate_left) {
00223     return *skate_left;
00224   } else if (sprite == Stand_left) {
00225     return *skate_stand_left;
00226   } else if (sprite == Skate_duck_right) {
00227     return *skate_duck_right;
00228   } else if (sprite == Skate_duck_left) {
00229     return *skate_duck_left;
00230   } else {
00231     return *skate_stand_right;
00232   }
00233 } 
00234 
00235 void Skateboarder::fall(bool fall_flag, Gamepad &gamepad) {
00236   // Falling sequence. 
00237   _fall_flag = fall_flag;
00238   _y++;
00239   gamepad.tone(int(-15.8*_y + 1363), 0.05);  // Linear equation makes decending 
00240   // frequency sound.
00241   wait(0.005);  // Control fall time and fall sound speed.
00242   if (_y == 80) {  // Stop falling when y = 80. 
00243     _fall_flag = false;
00244     _reset_flag = true;
00245   }
00246 }
00247      
00248 bool Skateboarder::get_fall_flag() {
00249   return _fall_flag;
00250 }
00251      
00252 bool Skateboarder::get_reset_flag() {
00253   return _reset_flag;
00254 }
00255      
00256 void Skateboarder::set_reset_flag(bool flag) {
00257   if (flag) {
00258     _reset_flag = true;
00259   } else {
00260     _reset_flag = false;
00261   }
00262 }
00263      
00264