XJEL2645 (19/20) / Mbed 2 deprecated Explorer

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers explorer.cpp Source File

explorer.cpp

00001 #include "explorer.h"
00002 //the explorer.cpp need to defined the module and movement of the explorer including the restarted point 
00003 int move_right_sprite[10][10] =   { 
00004   { 0,0,0,1,1,1,0,0,0,0 },
00005   { 0,0,0,1,0,1,0,0,0,0 },
00006   { 0,0,0,1,1,1,0,0,0,0 },
00007   { 0,0,0,0,1,0,1,0,0,0 },
00008   { 0,0,1,1,1,1,1,0,0,0 },
00009   { 0,0,1,0,1,0,0,0,0,0 },
00010   { 0,0,0,0,1,0,0,0,0,0 },
00011   { 0,0,0,0,1,1,1,0,0,0 },
00012   { 0,0,0,1,0,0,0,1,0,0 },
00013   { 1,0,1,0,0,0,0,0,1,0 },
00014 };
00015 
00016 int move_left_sprite[10][10] =   { 
00017   { 0,0,0,1,1,1,0,0,0,0 },
00018   { 0,0,0,1,0,1,0,0,0,0 },
00019   { 0,0,0,1,1,1,0,0,0,0 },
00020   { 0,0,1,0,1,0,0,0,0,0 },
00021   { 0,0,1,1,1,1,1,0,0,0 },
00022   { 0,0,0,0,1,0,1,0,0,0 },
00023   { 0,0,0,0,1,0,0,0,0,0 },
00024   { 0,0,0,1,1,0,0,0,0,0 },
00025   { 0,0,1,0,0,1,0,0,0,0 },
00026   { 0,1,0,0,0,0,1,0,0,0 },
00027 };
00028 
00029 int stand_right_sprite[10][10] =   { 
00030   { 0,0,0,1,1,1,0,0,0,0 },
00031   { 0,0,0,1,0,1,0,0,0,0 },
00032   { 0,0,0,1,1,1,0,0,0,0 },
00033   { 0,0,0,0,1,0,1,0,0,0 },
00034   { 0,0,1,1,1,1,1,0,0,0 },
00035   { 0,0,1,0,1,0,0,0,0,0 },
00036   { 0,0,0,0,1,0,0,0,0,0 },
00037   { 0,0,0,0,1,0,0,0,0,0 },
00038   { 0,0,0,1,0,1,0,0,0,0 },
00039   { 0,0,0,1,0,1,0,0,0,0 },
00040 };
00041 
00042 int stand_left_sprite[10][10] =   { 
00043   { 0,0,0,1,1,1,0,0,0,0 },
00044   { 0,0,0,1,0,1,0,0,0,0 },
00045   { 0,0,0,1,1,1,0,0,0,0 },
00046   { 0,0,1,0,1,0,0,0,0,0 },
00047   { 0,0,1,1,1,1,1,0,0,0 },
00048   { 0,0,0,0,1,0,1,0,0,0 },
00049   { 0,0,0,0,1,0,0,0,0,0 },
00050   { 0,0,0,0,1,0,0,0,0,0 },
00051   { 0,0,0,1,0,1,0,0,0,0 },
00052   { 0,0,0,1,0,1,0,0,0,0 },
00053 };
00054 
00055 Explorer::Explorer() {} 
00056 
00057 Explorer::~Explorer() {}
00058 void Explorer::init() {
00059   _f_flag = false;
00060   _player_direction = right;
00061   _r_flag = false;
00062   _speed = 0;//the player is standing at first.
00063   _jump_height = 0;
00064   }
00065 
00066 void Explorer::set_x_coordinate(float joy_x, int speed, Player_direction direction)
00067 {
00068   //this process is to set x and make sure the player can move          
00069   _player_direction = direction;
00070   _speed = speed;
00071   if (joy_x < float(-0.05)) {
00072     _speed = _speed--;//
00073     _player_direction = left;
00074     _explorer_sprite = Move_left;//makesure the value is the Move_left value in the enum 
00075   //now x is moving to the right than add to the X position
00076   } else if (joy_x > float(0.05)) {
00077     _speed = _speed++;
00078     _player_direction = right; 
00079     _explorer_sprite = Move_right;//makesure the value is the Move_right value in the enum  
00080   } else if (_player_direction == right) {  
00081     _explorer_sprite = Stand_right;// not moving so equals to enum Stand_right.
00082   } else { 
00083     _explorer_sprite = Stand_left;//the least situation is equals to enum Stand_right.
00084   }      
00085   _x = 0.5*_speed + 30;//this equation decides how fast the player move later I will update differnet modes to choose different speed
00086 }
00087 //this function is easy to understand because this is just choosing different sprite according to the direction
00088 int * Explorer::get_form(Explorer_sprite sprite){
00089   if (sprite == Move_right) {
00090     return *move_right_sprite;
00091   } else if (sprite == Move_left) {
00092     return *move_left_sprite;
00093   } else if (sprite == Stand_left) {
00094     return *stand_right_sprite;
00095   } else {
00096     return *stand_left_sprite;
00097   }
00098 }
00099 
00100 Player_direction Explorer::get_direction() {
00101   return _player_direction;
00102 }    
00103 
00104 Explorer_sprite Explorer::get_explorer_sprite() {
00105   return _explorer_sprite;
00106 }
00107     
00108 void Explorer::set_y_coordinate(bool ifjump, int jump_height, int y_flag) {
00109   _jump_height = jump_height;
00110   _y_flag = y_flag;
00111   if (ifjump && _jump_height < 40) {  
00112     _jump_height = 30; }
00113   if (_jump_height !=0) _speed--;  // Keep falling until not jumping.
00114   if ((_line_4.left <= _x - 5) && (_line_4.right >= _x)){
00115     _y_flag = 1;}
00116     else if ((_line_5.left <= _x - 5) && (_line_5.right >= _x)) {
00117     _y_flag = 1;}
00118     else if ((_line_6.left <= _x - 5) && (_line_6.right >= _x)) {
00119     _y_flag = 1;}
00120     else if ((_line_1.left <= _x) && (_line_1.right >= _x)){
00121     _y_flag = 0; }
00122     else if ((_line_2.left <= _x) && (_line_2.right >= _x)){
00123     _y_flag = 0;}
00124     else if ((_line_3.left <= _x) && (_line_3.right >= _x)){
00125     _y_flag = 0;}    
00126   if (_y_flag == 1 && ifjump) { 
00127     _platform = 5;
00128   } else if (_y_flag == 0) {
00129     _platform = 25;
00130   }
00131   _y = _platform - 0.5*_jump_height;  
00132 }
00133   
00134 int Explorer::get_y() {
00135   return _y;
00136 }
00137 
00138 int Explorer::get_x() {
00139   return _x;
00140 }
00141   
00142 int Explorer::get_speed() {
00143   return _speed;
00144 }
00145 
00146 int Explorer::get_y_flag() {
00147     return _y_flag; 
00148 }
00149   
00150 int Explorer::get_jump_height() {
00151   return _jump_height;
00152 }
00153 
00154 void Explorer::reset_flag(bool flag) {
00155    if (flag) {
00156     _r_flag = true;
00157   } else {
00158     _r_flag = false;
00159   }
00160 }
00161 
00162  
00163 void Explorer::fall(bool f_flag, Gamepad &gamepad) { 
00164   _f_flag = f_flag;  // decending equation
00165   if (_f_flag == true) { 
00166     _y++; }
00167   else if(_y == 80) {  // Stop falling function 
00168     _f_flag = false;
00169     _r_flag = true;
00170     gamepad.tone(1000, 0.05);
00171     wait(0.01);
00172   }
00173 }
00174 
00175 bool Explorer::get_fall_flag() {
00176   return _f_flag;
00177 }
00178 
00179 bool Explorer::get_reset_flag() {
00180   return _r_flag;
00181 }