Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Enemy/Enemy.cpp@8:d1c04f0e4890, 2019-05-11 (annotated)
- Committer:
- el17ttds
- Date:
- Sat May 11 08:23:54 2019 +0000
- Revision:
- 8:d1c04f0e4890
- Parent:
- 7:08f78909dda7
- Child:
- 9:3a0194c87afe
Full game.; Unfinished menu, tone glitch, score doesn't print, ammo and health to be displayed
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:08f78909dda7 | 13 | _map_x = x1; |
el17ttds | 7:08f78909dda7 | 14 | _map_y = y1; |
el17ttds | 5:096017b99011 | 15 | if (enemy_true == 1) { |
el17ttds | 7:08f78909dda7 | 16 | _x = _map_x + 42 + _col * 20; // x pos of left wall + screen width / 2 + x pos of enemy |
el17ttds | 7:08f78909dda7 | 17 | _y = _map_y + 24 + _row * 10; // y pos of left wall + screen height / 2 + y pos of enemy |
el17ttds | 5:096017b99011 | 18 | return 1; |
el17ttds | 4:3446009e2f38 | 19 | } else if (enemy_true == 0) { // create new enemy only if required |
el17ttds | 4:3446009e2f38 | 20 | random_position(); |
el17ttds | 4:3446009e2f38 | 21 | return 1; |
el17ttds | 4:3446009e2f38 | 22 | } else { |
el17ttds | 7:08f78909dda7 | 23 | _x = -1; |
el17ttds | 7:08f78909dda7 | 24 | _y = -1; |
el17ttds | 5:096017b99011 | 25 | return -1; |
el17ttds | 4:3446009e2f38 | 26 | } |
el17ttds | 4:3446009e2f38 | 27 | } |
el17ttds | 4:3446009e2f38 | 28 | |
el17ttds | 7:08f78909dda7 | 29 | bool Enemy::check_collision() { |
el17ttds | 8:d1c04f0e4890 | 30 | if ( (_x >= 29) && // Hero centrepoint - radius - enemyRadius |
el17ttds | 7:08f78909dda7 | 31 | (_x <= 55) && |
el17ttds | 7:08f78909dda7 | 32 | (_y >= 11) && |
el17ttds | 7:08f78909dda7 | 33 | (_y <= 37) ) { |
el17ttds | 7:08f78909dda7 | 34 | return 1; |
el17ttds | 7:08f78909dda7 | 35 | } else { |
el17ttds | 7:08f78909dda7 | 36 | return 0; |
el17ttds | 7:08f78909dda7 | 37 | } |
el17ttds | 7:08f78909dda7 | 38 | } |
el17ttds | 7:08f78909dda7 | 39 | |
el17ttds | 6:e8c03f264ffc | 40 | void Enemy::random_position() { // finds random postion for enemy out of 50 options |
el17ttds | 6:e8c03f264ffc | 41 | _col = rand() % 5; |
el17ttds | 6:e8c03f264ffc | 42 | _row = rand() % 10; |
el17ttds | 7:08f78909dda7 | 43 | _x = _map_x + 42 + _col * 20; |
el17ttds | 7:08f78909dda7 | 44 | _y = _map_y + 24 + _row * 10; |
el17ttds | 7:08f78909dda7 | 45 | valid_position(); |
el17ttds | 6:e8c03f264ffc | 46 | // spacesi = _row * 5 + _col; :: int Shows space 0 - 49. Could be useful. |
el17ttds | 6:e8c03f264ffc | 47 | } |
el17ttds | 6:e8c03f264ffc | 48 | |
el17ttds | 6:e8c03f264ffc | 49 | void Enemy::valid_position() { // if enemy spawns too close to hero, re write enemy |
el17ttds | 7:08f78909dda7 | 50 | if ((_x >= 24) && (_x <= 50) && (_y >= 6) && (_y <= 42)) { |
el17ttds | 6:e8c03f264ffc | 51 | random_position(); |
el17ttds | 6:e8c03f264ffc | 52 | } |
el17ttds | 6:e8c03f264ffc | 53 | } |
el17ttds | 6:e8c03f264ffc | 54 | |
el17ttds | 4:3446009e2f38 | 55 | void Enemy::draw(N5110 &lcd) { |
el17ttds | 4:3446009e2f38 | 56 | lcd.drawCircle( _x, _y, 10, FILL_BLACK); |
el17ttds | 4:3446009e2f38 | 57 | /* |
el17ttds | 7:08f78909dda7 | 58 | int enemy[10][20] = { CHANGE VALID POSITION DIMENSIONS AND COLLISION MECHANIC |
el17ttds | 4:3446009e2f38 | 59 | {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 60 | {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 61 | {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 62 | {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0}, |
el17ttds | 4:3446009e2f38 | 63 | {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1}, |
el17ttds | 4:3446009e2f38 | 64 | {1,1,1,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1}, |
el17ttds | 4:3446009e2f38 | 65 | {0,0,1,1,1,0,0,1,0,1,1,0,1,0,0,1,1,1,0,0}, |
el17ttds | 4:3446009e2f38 | 66 | {0,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 67 | {0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 68 | {0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 69 | }; |
el17ttds | 6:e8c03f264ffc | 70 | for (_n = 0; _n < 10; _n++) { // Bitmap library encountered errors with lcd.drawSprite |
el17ttds | 6:e8c03f264ffc | 71 | for (_m = 0; _m < 20; _m++) { |
el17ttds | 6:e8c03f264ffc | 72 | if (enemy[_m][_n] == 1) { |
el17ttds | 6:e8c03f264ffc | 73 | lcd.setPixel(_x, _y); |
el17ttds | 6:e8c03f264ffc | 74 | } else { |
el17ttds | 6:e8c03f264ffc | 75 | lcd.clearPixel(_x, _y); |
el17ttds | 6:e8c03f264ffc | 76 | } |
el17ttds | 6:e8c03f264ffc | 77 | _x++; |
el17ttds | 6:e8c03f264ffc | 78 | } |
el17ttds | 6:e8c03f264ffc | 79 | _y++; |
el17ttds | 6:e8c03f264ffc | 80 | } |
el17ttds | 4:3446009e2f38 | 81 | */ |
el17ttds | 4:3446009e2f38 | 82 | } |
el17ttds | 4:3446009e2f38 | 83 | |
el17ttds | 6:e8c03f264ffc | 84 | int Enemy::get_x() { |
el17ttds | 6:e8c03f264ffc | 85 | return _x; |
el17ttds | 4:3446009e2f38 | 86 | } |
el17ttds | 6:e8c03f264ffc | 87 | |
el17ttds | 6:e8c03f264ffc | 88 | int Enemy::get_y() { |
el17ttds | 6:e8c03f264ffc | 89 | return _y; |
el17ttds | 6:e8c03f264ffc | 90 | } |