ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

Coin/Coin.cpp

Committer:
lewisgw
Date:
2019-04-06
Revision:
15:876c047a6ec9
Child:
16:331be5c7ed80

File content as of revision 15:876c047a6ec9:

#include "Coin.h"

// Define sprite arrays.
int coin_front[5][5] = {
  { 0,0,1,0,0 },
  { 0,1,0,1,0 },
  { 1,0,1,0,1 },
  { 0,1,0,1,0 },
  { 0,0,1,0,0 },
};

int coin_side[5][5] = {
  { 0,0,1,0,0 },
  { 0,0,1,0,0 },
  { 0,0,1,0,0 },
  { 0,0,1,0,0 },
  { 0,0,1,0,0 },
};

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

Coin::~Coin() {}

void Coin::init() {
  // Initialise starting position of the coin.
  _x = 10;
  _y = 33;
  _coin_counter = 0;
}

void Coin::generate_coin() {
  // Toggle the state of the coin every 3 loop iterations so it looks
  // like it is rotating.
  if (_coin_counter == 2) {
    _coin_counter = 0;
    _rotate_coin = !_rotate_coin;
  }
  _coin_counter++;      
}

void Coin::update_coin(int rand_x, int rand_y) {
  // Move the coin to a new random location
  if (rand_y > 50) {
    _y = 13;
  } else {
    _y = 33;
  }
  _x = rand_x;
  if (_x < 3 || _x > 80) {
    _x = 50;
  }
}

int * Coin::get_coin_sprite() {
  if (_rotate_coin) {
    return *coin_front;
  } else {
    return *coin_side;
  }
}

int Coin::get_coin_x() {
  return _x;
}

int Coin::get_coin_y() {
  return _y;
}