Mochu Yao explorer game

Dependencies:   mbed

explorer/explorer.cpp

Committer:
el17my
Date:
2020-04-28
Revision:
19:14c5427b30d1
Parent:
17:1b4ecc01b79f
Child:
20:20e6ba54e15c

File content as of revision 19:14c5427b30d1:

#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::init() {
  _f_flag = false;
  _player_direction = right;
  _r_flag = false;
  _speed = 0;//the player is standing at first
  _jump_height = 0;
  }

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.1)) {
    _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.1)) {
    _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.2*_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 Explorer::get_explorer_sprite() {
  return _explorer_sprite;
}
    
void Explorer::set_y_coordinate(bool ifjump, int jump_height) {
  _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 = 40; }  //the player can jump up to 30 height and then fall down 
  if (_jump_height !=0) { 
    _jump_height--;
    _y = 20 + _jump_height;
    if ((_line_4.left <= _x) && (_line_4.right >= _x) && (_y >= 40)) {
    _y = 40; _jump_height = 0;}
    else if ((_line_5.left <= _x) && (_line_5.right >= _x) && (_y >= 40)) {
    _y = 40; _jump_height = 0;}
    else if ((_line_6.left <= _x) && (_line_6.right >= _x) && (_y >= 40)) {
    _y = 40; _jump_height = 0;}
    else if ((_line_1.left <= _x) && (_line_1.right >= _x) && (_y < 40)) {
    _y = 20; _jump_height = 0;}
    else if ((_line_2.left <= _x) && (_line_2.right >= _x) && (_y < 40)) {
    _y = 20; _jump_height = 0;}
    else if ((_line_3.left <= _x) && (_line_3.right >= _x) && (_y < 40)) {
    _y = 20; _jump_height = 0;}
    else { _f_flag = true; }
  }
}
  
int Explorer::get_y() {
  return _y;
}

int Explorer::get_x() {
  return _x;
}
  
int Explorer::get_speed() {
  return _speed;
}
  
int Explorer::get_jump_height() {
  return _jump_height;
}

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

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

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