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@9:3a0194c87afe, 2019-05-12 (annotated)
- Committer:
- el17ttds
- Date:
- Sun May 12 16:21:08 2019 +0000
- Revision:
- 9:3a0194c87afe
- Parent:
- 8:d1c04f0e4890
Final Submission. I have read and agreed with Statement of Academic Integrity.
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 | 9:3a0194c87afe | 30 | if ( (_x + 19 >= 37) && // Hero centrepoint - radius - enemyRadius |
el17ttds | 9:3a0194c87afe | 31 | (_x <= 48) && |
el17ttds | 9:3a0194c87afe | 32 | (_y + 19 >= 19) && |
el17ttds | 9:3a0194c87afe | 33 | (_y <= 27) ) { |
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 | 9:3a0194c87afe | 42 | _row = rand() % 5; |
el17ttds | 7:08f78909dda7 | 43 | _x = _map_x + 42 + _col * 20; |
el17ttds | 9:3a0194c87afe | 44 | _y = _map_y + 24 + _row * 20; |
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 | 9:3a0194c87afe | 50 | if ((_x >= 14) && (_x <= 60) && (_y >= 0) && (_y <= 52) ) { |
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 | 9:3a0194c87afe | 56 | // lcd.drawCircle( _x, _y, 10, FILL_BLACK); |
el17ttds | 9:3a0194c87afe | 57 | int const enemy[21][21] = { |
el17ttds | 9:3a0194c87afe | 58 | {0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0}, |
el17ttds | 9:3a0194c87afe | 59 | {0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}, |
el17ttds | 9:3a0194c87afe | 60 | {0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, |
el17ttds | 9:3a0194c87afe | 61 | {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, |
el17ttds | 9:3a0194c87afe | 62 | {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, |
el17ttds | 9:3a0194c87afe | 63 | {0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0}, |
el17ttds | 9:3a0194c87afe | 64 | {0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0}, |
el17ttds | 9:3a0194c87afe | 65 | {1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1}, |
el17ttds | 9:3a0194c87afe | 66 | {1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1}, |
el17ttds | 9:3a0194c87afe | 67 | {1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1}, |
el17ttds | 9:3a0194c87afe | 68 | {1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1}, |
el17ttds | 9:3a0194c87afe | 69 | {1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1}, |
el17ttds | 9:3a0194c87afe | 70 | {1,1,1,1,0,0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,1}, |
el17ttds | 9:3a0194c87afe | 71 | {1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1}, |
el17ttds | 9:3a0194c87afe | 72 | {0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0}, |
el17ttds | 9:3a0194c87afe | 73 | {0,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0}, |
el17ttds | 9:3a0194c87afe | 74 | {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0}, |
el17ttds | 9:3a0194c87afe | 75 | {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0}, |
el17ttds | 9:3a0194c87afe | 76 | {0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}, |
el17ttds | 9:3a0194c87afe | 77 | {0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0}, |
el17ttds | 9:3a0194c87afe | 78 | {0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0}, |
el17ttds | 4:3446009e2f38 | 79 | }; |
el17ttds | 9:3a0194c87afe | 80 | lcd.drawSprite(_x, _y, 21, 21, (int *)enemy); |
el17ttds | 4:3446009e2f38 | 81 | } |
el17ttds | 4:3446009e2f38 | 82 | |
el17ttds | 6:e8c03f264ffc | 83 | int Enemy::get_x() { |
el17ttds | 6:e8c03f264ffc | 84 | return _x; |
el17ttds | 4:3446009e2f38 | 85 | } |
el17ttds | 6:e8c03f264ffc | 86 | |
el17ttds | 6:e8c03f264ffc | 87 | int Enemy::get_y() { |
el17ttds | 6:e8c03f264ffc | 88 | return _y; |
el17ttds | 6:e8c03f264ffc | 89 | } |