Mochu Yao explorer game

Dependencies:   mbed

item/item.cpp

Committer:
el17my
Date:
2020-04-21
Revision:
3:672d4bd8225d
Parent:
1:ed745421d8c4
Child:
7:88c4ba6bb37b

File content as of revision 3:672d4bd8225d:

#include "item.h"

// Define sprite arrays.
//this is the forward form of the item
int item_form[6][5] = {
  { 0,0,1,1,0,0},
  { 0,1,0,0,1,0},
  { 1,0,0,0,0,1},
  { 0,1,0,0,1,0},
  { 0,0,1,1,0,0},
};
//this is the vertical form of the item
int item_vertical[6][5] = {
  { 0,0,1,1,0,0},
  { 0,0,1,1,0,0},
  { 0,0,1,1,0,0},
  { 0,0,1,1,0,0},
  { 0,0,1,1,0,0},
};

// Constructor and destructor.
item::item() {} 

item::~item() {}

void item::init() {
  // Starting position of the item.
  _x = 40;
  _y = 40;
  _loop = 0;//the loop must be set to zero
}

void item::generate_item() {
  // make the item rotating every 4 loops
  for(_loop = 0; loop<4; _loop++;) {
  if (_loop == 3) {
    _loop = 0;
    _rotate_item = !_rotate_item;
  } 
  wait(0.05);    
}

void item::set_item(int random_x, int random_y) {
  // Set the item coords based on input values.
  // This function must have two rules
  //1 the item must be generated on the surface but not in the air
  //2 the item must be in the srceen or the explorer can not collect them
  if (_x < 6 || _x > 74) {  // Ensures the item does not generate off-screen.
    _x = 40;//go back to the intial position
  else if (_y < 5 || _y > 75) 
  {
    _y = 50; };//go back to the intial position
  //according to the height of the line, the item must be generated upper the line but can not be so high
  //if y >40, the item need to be one the second line and if is lower it has to be one the lower line
  if (random_y > 40) {  
    _y = 40; 
  } else {
    _y = 25;
  }
  _x = random_x;//x can be every random value between 6 and 74
}

int *item::get_item_form() {
  //by changing the form from forward to vertical each 4 loop the item can start rotating 
  if (_rotate_item) {
    return *item_form;
  } else if(!_rotate_item) {
    return *item_vertical;
  }
}

int item::get_item_x() {
  return _x;
}

int item::get_item_y() {
  return _y;
} 

}