Mochu Yao explorer game

Dependencies:   mbed

explorer/explorer.cpp

Committer:
el17my
Date:
2020-04-23
Revision:
7:88c4ba6bb37b
Parent:
3:672d4bd8225d
Child:
8:201ef0618b7d

File content as of revision 7:88c4ba6bb37b:

#include "explorer.h"
//the explorer.cpp need to defined the module and movement of the explorer including the restarted point 
int move_right_sprite[10][10] =   { 
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,0,0,1,0,1,0,0,0 },
  { 0,0,1,1,1,1,1,0,0,0 },
  { 0,0,1,0,1,0,0,0,0,0 },
  { 0,0,0,0,1,0,0,0,0,0 },
  { 0,0,0,0,1,1,1,0,0,0 },
  { 0,0,0,1,0,0,0,1,0,0 },
  { 1,0,1,0,0,0,0,0,1,0 },
};

int move_left_sprite[10][10] =   { 
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,1,0,1,0,0,0,0,0 },
  { 0,0,1,1,1,1,1,0,0,0 },
  { 0,0,0,0,1,0,1,0,0,0 },
  { 0,0,0,0,1,0,0,0,0,0 },
  { 0,0,0,1,1,0,0,0,0,0 },
  { 0,0,1,0,0,1,0,0,0,0 },
  { 0,1,0,0,0,0,1,0,0,0 },
};

int stand_right_sprite[10][10] =   { 
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,0,0,1,0,1,0,0,0 },
  { 0,0,1,1,1,1,1,0,0,0 },
  { 0,0,1,0,1,0,0,0,0,0 },
  { 0,0,0,0,1,0,0,0,0,0 },
  { 0,0,0,0,1,0,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
};

int stand_left_sprite[10][10] =   { 
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
  { 0,0,0,1,1,1,0,0,0,0 },
  { 0,0,1,0,1,0,0,0,0,0 },
  { 0,0,1,1,1,1,1,0,0,0 },
  { 0,0,0,0,1,0,1,0,0,0 },
  { 0,0,0,0,1,0,0,0,0,0 },
  { 0,0,0,0,1,0,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
  { 0,0,0,1,0,1,0,0,0,0 },
};

Explorer::Explorer() {} 

Explorer::~Explorer() {}

void Explorer::set_x_coordinate(float joy_x, int speed, Player_direction direction)
{
  //this process is to set x and make sure the player can move          
  _player_direction = direction;
  _speed = speed;
  if (joy_x < float(-0.5)) {
    _speed = _speed--;//
    _player_direction = left;
    _explorer_sprite = Move_left;//makesure the value is the Move_left value in the enum 
  //now x is moving to the right than add to the X position
  } else if (joy_x > float(0.5)) {
    _speed = _speed++;
    _player_direction = right; 
    _explorer_sprite = Move_right;//makesure the value is the Move_right value in the enum  
  } else if (_player_direction == right) {  
    _explorer_sprite = Stand_right;// not moving so equals to enum Stand_right.
  } else { 
    _explorer_sprite = Stand_left;//the least situation is equals to enum Stand_right.
  }      
  _x = 0.5*_speed + 30;//this equation decides how fast the player move later I will update differnet modes to choose different speed
}
//this function is easy to understand because this is just choosing different sprite according to the direction
int * Explorer::get_form(explorer_sprite sprite){
  if (sprite == Move_right) {
    return *move_right_sprite;
  } else if (sprite == Move_left) {
    return *move_left_sprite;
  } else if (sprite == Stand_left) {
    return *stand_right_sprite;
  } else {
    return *stand_left_sprite;
  }
}

Player_direction Explorer::get_direction() {
  return _player_direction;
}    

explorer_sprite get_explorer_sprite() {
  return _explorer_sprite;
}
    
void Explorer::set_y_coordinate(bool ifjump, int jump_height, int level) {
  _level = level; 
  _jump_height = jump_height;
  // jump need to have this function
  // when you jump makesure the module will not be offscreen also if you fall from the surface you can not jump back again
  if (ifjump == 1) {  
    _jump_height = 30;  //the player can jump up to 30 height and then fall down 
  else if (_jumpheight !=0) { 
  _jumpheight-- }
  };
  //not offscreen and back to the surface        
  if (_level == 1 && ifjump){
    _level = ;
  } else if (_level == 0) {
    _level = ;
  }
  _y = _level - 0.5*_jumpheight;
}  
int Explorer::get_y() {
  return _y;
}

int Explorer::get_x() {
  return _x;
}
  
int Explorer::get_speed() {
  return _speed;
}
  
int Explorer::get_jumptime() {
  return _jumptime;
}

void Explorer::reset_flag(bool flag) {
  _r_flag = flag
  if (flag) {
    _r_flag = true;
  } else {
    _r_flag = false;
  }
}
 
void Explorer::fall(bool f_flag, Gamepad &gamepad) { 
  _f_flag = f_flag;
  if (f_flag == true) {
    gamepad.tone(int(1500+0.5*_y), 0.05);  // decending equation 
    _y--;
  else if(_y == -80) {  // Stop falling function 
    _f_flag = false;
    _r_flag = true;
    game.tone(1000, 0.05);
    wait(0.01);
    }
  }
}

bool Explorer::get_fall_flag() {
  return _f_flag;
}

bool Explorer::get_reset_flag() {
  return _r_flag;
}