![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
test 1 doc
Ennemy/Enemy.cpp@8:d19b30a6cd69, 2020-05-26 (annotated)
- Committer:
- joebarhouch
- Date:
- Tue May 26 22:15:42 2020 +0000
- Revision:
- 8:d19b30a6cd69
- Child:
- 9:9830d3a78572
Implemented enemies with AI that walk around with physics
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 | 8:d19b30a6cd69 | 74 | |
joebarhouch | 8:d19b30a6cd69 | 75 | bool Enemy::fell() |
joebarhouch | 8:d19b30a6cd69 | 76 | { |
joebarhouch | 8:d19b30a6cd69 | 77 | if( _enY + 6 > HEIGHT) { |
joebarhouch | 8:d19b30a6cd69 | 78 | _fell = true; |
joebarhouch | 8:d19b30a6cd69 | 79 | } else { |
joebarhouch | 8:d19b30a6cd69 | 80 | _fell = false; |
joebarhouch | 8:d19b30a6cd69 | 81 | } |
joebarhouch | 8:d19b30a6cd69 | 82 | return _fell; |
joebarhouch | 8:d19b30a6cd69 | 83 | } |
joebarhouch | 8:d19b30a6cd69 | 84 | |
joebarhouch | 8:d19b30a6cd69 | 85 | |
joebarhouch | 8:d19b30a6cd69 | 86 | Vector2D Enemy::get_pos() |
joebarhouch | 8:d19b30a6cd69 | 87 | { |
joebarhouch | 8:d19b30a6cd69 | 88 | Vector2D pos = {_enX, _enY}; |
joebarhouch | 8:d19b30a6cd69 | 89 | return pos; |
joebarhouch | 8:d19b30a6cd69 | 90 | } |