ELEC2645 (2018/19) / Mbed 2 deprecated el17ttds

Dependencies:   mbed N5110_tf

Committer:
el17ttds
Date:
Wed May 08 19:05:36 2019 +0000
Revision:
6:e8c03f264ffc
Parent:
5:096017b99011
Child:
7:08f78909dda7
Bullets added but don't reset

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17ttds 4:3446009e2f38 1 #include "Enemy.h"
el17ttds 4:3446009e2f38 2
el17ttds 4:3446009e2f38 3 Enemy::Enemy() {
el17ttds 4:3446009e2f38 4
el17ttds 4:3446009e2f38 5 }
el17ttds 4:3446009e2f38 6
el17ttds 6:e8c03f264ffc 7 void Enemy::init() {
el17ttds 6:e8c03f264ffc 8 _x = -1;
el17ttds 6:e8c03f264ffc 9 _y = -1;
el17ttds 4:3446009e2f38 10 }
el17ttds 4:3446009e2f38 11
el17ttds 5:096017b99011 12 int Enemy::write(int enemy_true, int x1, int y1) { // use collision mechanic to re-draw if both enemies are in 1 location
el17ttds 5:096017b99011 13 if (enemy_true == 1) {
el17ttds 5:096017b99011 14 _x = x1 + 42 + _col * 20; // x pos of left wall + screen width / 2 + x pos of enemy
el17ttds 5:096017b99011 15 _y = y1 + 24 + _row * 10; // y pos of left wall + screen height / 2 + y pos of enemy
el17ttds 5:096017b99011 16 return 1;
el17ttds 4:3446009e2f38 17 } else if (enemy_true == 0) { // create new enemy only if required
el17ttds 4:3446009e2f38 18 random_position();
el17ttds 6:e8c03f264ffc 19 _x = x1 + 42 + _col * 20;
el17ttds 6:e8c03f264ffc 20 _y = y1 + 24 + _row * 10;
el17ttds 6:e8c03f264ffc 21 valid_position();
el17ttds 4:3446009e2f38 22 return 1;
el17ttds 4:3446009e2f38 23 } else {
el17ttds 5:096017b99011 24 return -1;
el17ttds 4:3446009e2f38 25 }
el17ttds 4:3446009e2f38 26 }
el17ttds 4:3446009e2f38 27
el17ttds 6:e8c03f264ffc 28 void Enemy::random_position() { // finds random postion for enemy out of 50 options
el17ttds 6:e8c03f264ffc 29 _col = rand() % 5;
el17ttds 6:e8c03f264ffc 30 _row = rand() % 10;
el17ttds 6:e8c03f264ffc 31 // spacesi = _row * 5 + _col; :: int Shows space 0 - 49. Could be useful.
el17ttds 6:e8c03f264ffc 32 }
el17ttds 6:e8c03f264ffc 33
el17ttds 6:e8c03f264ffc 34 void Enemy::valid_position() { // if enemy spawns too close to hero, re write enemy
el17ttds 6:e8c03f264ffc 35 if ((_x > 16) && (_x < 48) && (_y > 8) && (_y < 30)) {
el17ttds 6:e8c03f264ffc 36 random_position();
el17ttds 6:e8c03f264ffc 37 }
el17ttds 6:e8c03f264ffc 38 }
el17ttds 6:e8c03f264ffc 39
el17ttds 4:3446009e2f38 40 void Enemy::draw(N5110 &lcd) {
el17ttds 4:3446009e2f38 41 lcd.drawCircle( _x, _y, 10, FILL_BLACK);
el17ttds 4:3446009e2f38 42 /*
el17ttds 4:3446009e2f38 43 int enemy[10][20] = {
el17ttds 4:3446009e2f38 44 {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
el17ttds 4:3446009e2f38 45 {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0},
el17ttds 4:3446009e2f38 46 {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0},
el17ttds 4:3446009e2f38 47 {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0},
el17ttds 4:3446009e2f38 48 {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1},
el17ttds 4:3446009e2f38 49 {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1},
el17ttds 4:3446009e2f38 50 {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0},
el17ttds 4:3446009e2f38 51 {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0},
el17ttds 4:3446009e2f38 52 {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0},
el17ttds 4:3446009e2f38 53 {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
el17ttds 4:3446009e2f38 54 };
el17ttds 6:e8c03f264ffc 55 for (_n = 0; _n < 10; _n++) { // Bitmap library encountered errors with lcd.drawSprite
el17ttds 6:e8c03f264ffc 56 for (_m = 0; _m < 20; _m++) {
el17ttds 6:e8c03f264ffc 57 if (enemy[_m][_n] == 1) {
el17ttds 6:e8c03f264ffc 58 lcd.setPixel(_x, _y);
el17ttds 6:e8c03f264ffc 59 } else {
el17ttds 6:e8c03f264ffc 60 lcd.clearPixel(_x, _y);
el17ttds 6:e8c03f264ffc 61 }
el17ttds 6:e8c03f264ffc 62 _x++;
el17ttds 6:e8c03f264ffc 63 }
el17ttds 6:e8c03f264ffc 64 _y++;
el17ttds 6:e8c03f264ffc 65 }
el17ttds 4:3446009e2f38 66 */
el17ttds 4:3446009e2f38 67 }
el17ttds 4:3446009e2f38 68
el17ttds 6:e8c03f264ffc 69 int Enemy::get_x() {
el17ttds 6:e8c03f264ffc 70 return _x;
el17ttds 4:3446009e2f38 71 }
el17ttds 6:e8c03f264ffc 72
el17ttds 6:e8c03f264ffc 73 int Enemy::get_y() {
el17ttds 6:e8c03f264ffc 74 return _y;
el17ttds 6:e8c03f264ffc 75 }