ELEC2645 (2018/19) / Mbed 2 deprecated el17ttds

Dependencies:   mbed N5110_tf

Enemy/Enemy.cpp

Committer:
el17ttds
Date:
2019-05-08
Revision:
6:e8c03f264ffc
Parent:
5:096017b99011
Child:
7:08f78909dda7

File content as of revision 6:e8c03f264ffc:

#include "Enemy.h"

Enemy::Enemy() {

}

void Enemy::init() {
  _x = -1;
  _y = -1;
}

int Enemy::write(int enemy_true, int x1, int y1) {  // use collision mechanic to re-draw if both enemies are in 1 location
  if (enemy_true == 1) {
    _x = x1 + 42 + _col * 20; // x pos of left wall + screen width / 2 + x pos of enemy
    _y = y1 + 24 + _row * 10; // y pos of left wall + screen height / 2 + y pos of enemy
    return 1;
  } else if (enemy_true == 0) { // create new enemy only if required
    random_position();
    _x = x1 + 42 + _col * 20;
    _y = y1 + 24 + _row * 10;
    valid_position();
    return 1;
  } else {
    return -1;
  }
}

void Enemy::random_position() { // finds random postion for enemy out of 50 options
  _col = rand() % 5;
  _row = rand() % 10;
  // spacesi = _row * 5 + _col; :: int Shows space 0 - 49. Could be useful.
}

void Enemy::valid_position() { // if enemy spawns too close to hero, re write enemy
  if ((_x > 16) && (_x < 48) && (_y > 8) && (_y < 30)) {
    random_position();
  }
}

void Enemy::draw(N5110 &lcd) {
  lcd.drawCircle( _x, _y, 10, FILL_BLACK);
  /*
  int enemy[10][20] = {
    {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0},
    {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0},
    {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0},
    {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1},
    {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1},
    {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0},
    {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0},
    {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
  };
  for (_n = 0; _n < 10; _n++) { // Bitmap library encountered errors with lcd.drawSprite
    for (_m = 0; _m < 20; _m++) {
      if (enemy[_m][_n] == 1) {
        lcd.setPixel(_x, _y);
      } else {
        lcd.clearPixel(_x, _y);
      }
      _x++;
    }
    _y++;
  }
  */
}

int Enemy::get_x() {
  return _x;
}

int Enemy::get_y() {
  return _y;
}