ELEC2645 (2019/20)
/
ELEC2645_Project_el18jb
test 1 doc
Ennemy/Enemy.cpp@9:9830d3a78572, 2020-05-27 (annotated)
- Committer:
- joebarhouch
- Date:
- Wed May 27 00:49:39 2020 +0000
- Revision:
- 9:9830d3a78572
- Parent:
- 8:d19b30a6cd69
- Child:
- 10:9317a62bd4d0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joebarhouch | 8:d19b30a6cd69 | 1 | #include "Enemy.h" |
joebarhouch | 8:d19b30a6cd69 | 2 | int s_enemy[] = { |
joebarhouch | 8:d19b30a6cd69 | 3 | |
joebarhouch | 8:d19b30a6cd69 | 4 | 1,1,1,1,1,1,1, |
joebarhouch | 8:d19b30a6cd69 | 5 | 1,1,1,0,1,0,1, |
joebarhouch | 8:d19b30a6cd69 | 6 | 1,1,1,0,1,0,1, |
joebarhouch | 8:d19b30a6cd69 | 7 | 1,1,1,1,1,1,1, |
joebarhouch | 8:d19b30a6cd69 | 8 | 1,1,1,1,1,1,1, |
joebarhouch | 8:d19b30a6cd69 | 9 | 0,1,1,0,1,1,0, |
joebarhouch | 8:d19b30a6cd69 | 10 | |
joebarhouch | 8:d19b30a6cd69 | 11 | }; |
joebarhouch | 8:d19b30a6cd69 | 12 | Enemy::Enemy(int spawnX, int spawnY) |
joebarhouch | 8:d19b30a6cd69 | 13 | { |
joebarhouch | 8:d19b30a6cd69 | 14 | _enY = spawnY; |
joebarhouch | 8:d19b30a6cd69 | 15 | _enX = spawnX; |
joebarhouch | 8:d19b30a6cd69 | 16 | if( spawnX > WIDTH/2) { |
joebarhouch | 8:d19b30a6cd69 | 17 | eDir = 1; //right |
joebarhouch | 8:d19b30a6cd69 | 18 | } else { |
joebarhouch | 8:d19b30a6cd69 | 19 | eDir = 0; //left |
joebarhouch | 8:d19b30a6cd69 | 20 | } |
joebarhouch | 8:d19b30a6cd69 | 21 | } |
joebarhouch | 8:d19b30a6cd69 | 22 | Enemy::~Enemy() {} |
joebarhouch | 8:d19b30a6cd69 | 23 | |
joebarhouch | 8:d19b30a6cd69 | 24 | |
joebarhouch | 8:d19b30a6cd69 | 25 | |
joebarhouch | 8:d19b30a6cd69 | 26 | void Enemy::update(int eYpos, bool efall) |
joebarhouch | 8:d19b30a6cd69 | 27 | { |
joebarhouch | 8:d19b30a6cd69 | 28 | |
joebarhouch | 8:d19b30a6cd69 | 29 | // go left or right depending on direction |
joebarhouch | 8:d19b30a6cd69 | 30 | switch (eDir) { |
joebarhouch | 8:d19b30a6cd69 | 31 | case 1: |
joebarhouch | 8:d19b30a6cd69 | 32 | _enX -= 2; |
joebarhouch | 8:d19b30a6cd69 | 33 | //switch direction when it hits a wall |
joebarhouch | 8:d19b30a6cd69 | 34 | if(_enX + 8 > WIDTH || _enX < 0) { |
joebarhouch | 8:d19b30a6cd69 | 35 | eDir = 0; |
joebarhouch | 8:d19b30a6cd69 | 36 | break; |
joebarhouch | 8:d19b30a6cd69 | 37 | } |
joebarhouch | 8:d19b30a6cd69 | 38 | break; |
joebarhouch | 8:d19b30a6cd69 | 39 | case 0: |
joebarhouch | 8:d19b30a6cd69 | 40 | _enX +=2; |
joebarhouch | 8:d19b30a6cd69 | 41 | //switch direction when it hits a wall |
joebarhouch | 8:d19b30a6cd69 | 42 | if(_enX + 8 > WIDTH || _enX < 0) { |
joebarhouch | 8:d19b30a6cd69 | 43 | eDir = 1; |
joebarhouch | 8:d19b30a6cd69 | 44 | break; |
joebarhouch | 8:d19b30a6cd69 | 45 | } |
joebarhouch | 8:d19b30a6cd69 | 46 | break; |
joebarhouch | 8:d19b30a6cd69 | 47 | } |
joebarhouch | 8:d19b30a6cd69 | 48 | |
joebarhouch | 8:d19b30a6cd69 | 49 | |
joebarhouch | 8:d19b30a6cd69 | 50 | //fall down |
joebarhouch | 8:d19b30a6cd69 | 51 | switch (efall) { |
joebarhouch | 8:d19b30a6cd69 | 52 | case 1: |
joebarhouch | 8:d19b30a6cd69 | 53 | _enY +=3; |
joebarhouch | 8:d19b30a6cd69 | 54 | break; |
joebarhouch | 8:d19b30a6cd69 | 55 | case 2: |
joebarhouch | 8:d19b30a6cd69 | 56 | _enY = eYpos; |
joebarhouch | 8:d19b30a6cd69 | 57 | break; |
joebarhouch | 8:d19b30a6cd69 | 58 | } |
joebarhouch | 8:d19b30a6cd69 | 59 | |
joebarhouch | 8:d19b30a6cd69 | 60 | |
joebarhouch | 8:d19b30a6cd69 | 61 | } |
joebarhouch | 8:d19b30a6cd69 | 62 | |
joebarhouch | 8:d19b30a6cd69 | 63 | |
joebarhouch | 8:d19b30a6cd69 | 64 | |
joebarhouch | 8:d19b30a6cd69 | 65 | |
joebarhouch | 8:d19b30a6cd69 | 66 | void Enemy::draw(N5110 &lcd) |
joebarhouch | 8:d19b30a6cd69 | 67 | { |
joebarhouch | 8:d19b30a6cd69 | 68 | Bitmap bit_en(s_enemy, 6, 7 ); |
joebarhouch | 8:d19b30a6cd69 | 69 | bit_en.render(lcd, _enX, _enY); |
joebarhouch | 8:d19b30a6cd69 | 70 | } |
joebarhouch | 8:d19b30a6cd69 | 71 | |
joebarhouch | 8:d19b30a6cd69 | 72 | |
joebarhouch | 8:d19b30a6cd69 | 73 | |
joebarhouch | 9:9830d3a78572 | 74 | void Enemy::set_pos(int x, int y) |
joebarhouch | 9:9830d3a78572 | 75 | { |
joebarhouch | 9:9830d3a78572 | 76 | _enX = x; |
joebarhouch | 9:9830d3a78572 | 77 | _enY = y; |
joebarhouch | 9:9830d3a78572 | 78 | |
joebarhouch | 9:9830d3a78572 | 79 | } |
joebarhouch | 9:9830d3a78572 | 80 | |
joebarhouch | 8:d19b30a6cd69 | 81 | |
joebarhouch | 8:d19b30a6cd69 | 82 | bool Enemy::fell() |
joebarhouch | 8:d19b30a6cd69 | 83 | { |
joebarhouch | 8:d19b30a6cd69 | 84 | if( _enY + 6 > HEIGHT) { |
joebarhouch | 8:d19b30a6cd69 | 85 | _fell = true; |
joebarhouch | 8:d19b30a6cd69 | 86 | } else { |
joebarhouch | 8:d19b30a6cd69 | 87 | _fell = false; |
joebarhouch | 8:d19b30a6cd69 | 88 | } |
joebarhouch | 8:d19b30a6cd69 | 89 | return _fell; |
joebarhouch | 8:d19b30a6cd69 | 90 | } |
joebarhouch | 8:d19b30a6cd69 | 91 | |
joebarhouch | 8:d19b30a6cd69 | 92 | |
joebarhouch | 8:d19b30a6cd69 | 93 | Vector2D Enemy::get_pos() |
joebarhouch | 8:d19b30a6cd69 | 94 | { |
joebarhouch | 8:d19b30a6cd69 | 95 | Vector2D pos = {_enX, _enY}; |
joebarhouch | 8:d19b30a6cd69 | 96 | return pos; |
joebarhouch | 8:d19b30a6cd69 | 97 | } |