ELEC2645 (2019/20)
/
ELEC2645_Project_el18jb
test 1 doc
Ennemy/Enemy.cpp@15:9ea5269b4cd4, 2020-05-27 (annotated)
- Committer:
- joebarhouch
- Date:
- Wed May 27 07:52:55 2020 +0000
- Revision:
- 15:9ea5269b4cd4
- Parent:
- 11:b3024ab59fa5
el18jb; VOLATILE!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joebarhouch | 8:d19b30a6cd69 | 1 | #include "Enemy.h" |
joebarhouch | 10:9317a62bd4d0 | 2 | #include "EnemySprites.h" |
joebarhouch | 10:9317a62bd4d0 | 3 | //constructor |
joebarhouch | 10:9317a62bd4d0 | 4 | Enemy::Enemy(bool type, int spawnX, int spawnY) |
joebarhouch | 8:d19b30a6cd69 | 5 | { |
joebarhouch | 8:d19b30a6cd69 | 6 | _enY = spawnY; |
joebarhouch | 8:d19b30a6cd69 | 7 | _enX = spawnX; |
joebarhouch | 10:9317a62bd4d0 | 8 | |
joebarhouch | 8:d19b30a6cd69 | 9 | if( spawnX > WIDTH/2) { |
joebarhouch | 10:9317a62bd4d0 | 10 | xDir = 1; //right |
joebarhouch | 8:d19b30a6cd69 | 11 | } else { |
joebarhouch | 10:9317a62bd4d0 | 12 | xDir = 0; //left |
joebarhouch | 8:d19b30a6cd69 | 13 | } |
joebarhouch | 10:9317a62bd4d0 | 14 | |
joebarhouch | 10:9317a62bd4d0 | 15 | if ( spawnY > HEIGHT/2) { |
joebarhouch | 10:9317a62bd4d0 | 16 | yDir = 1; //up |
joebarhouch | 10:9317a62bd4d0 | 17 | } else { |
joebarhouch | 10:9317a62bd4d0 | 18 | yDir = 0; //down |
joebarhouch | 10:9317a62bd4d0 | 19 | } |
joebarhouch | 10:9317a62bd4d0 | 20 | _type = type; |
joebarhouch | 10:9317a62bd4d0 | 21 | |
joebarhouch | 8:d19b30a6cd69 | 22 | } |
joebarhouch | 10:9317a62bd4d0 | 23 | |
joebarhouch | 10:9317a62bd4d0 | 24 | //deconstructor |
joebarhouch | 8:d19b30a6cd69 | 25 | Enemy::~Enemy() {} |
joebarhouch | 8:d19b30a6cd69 | 26 | |
joebarhouch | 8:d19b30a6cd69 | 27 | |
joebarhouch | 10:9317a62bd4d0 | 28 | //update |
joebarhouch | 11:b3024ab59fa5 | 29 | void Enemy::update(int _ev) |
joebarhouch | 8:d19b30a6cd69 | 30 | { |
joebarhouch | 8:d19b30a6cd69 | 31 | |
joebarhouch | 11:b3024ab59fa5 | 32 | if( _type == TYPE_HOR ) { |
joebarhouch | 10:9317a62bd4d0 | 33 | // go left or right depending on direction |
joebarhouch | 10:9317a62bd4d0 | 34 | switch (xDir) { |
joebarhouch | 10:9317a62bd4d0 | 35 | case 1: |
joebarhouch | 11:b3024ab59fa5 | 36 | _enX -= _ev; |
joebarhouch | 10:9317a62bd4d0 | 37 | //switch direction when it hits a wall |
joebarhouch | 10:9317a62bd4d0 | 38 | if(_enX + 7 > WIDTH || _enX < 0) { |
joebarhouch | 10:9317a62bd4d0 | 39 | xDir = 0; |
joebarhouch | 10:9317a62bd4d0 | 40 | break; |
joebarhouch | 10:9317a62bd4d0 | 41 | } |
joebarhouch | 8:d19b30a6cd69 | 42 | break; |
joebarhouch | 10:9317a62bd4d0 | 43 | case 0: |
joebarhouch | 11:b3024ab59fa5 | 44 | _enX += _ev; |
joebarhouch | 10:9317a62bd4d0 | 45 | //switch direction when it hits a wall |
joebarhouch | 10:9317a62bd4d0 | 46 | if(_enX + 7 > WIDTH || _enX < 0) { |
joebarhouch | 10:9317a62bd4d0 | 47 | xDir = 1; |
joebarhouch | 10:9317a62bd4d0 | 48 | break; |
joebarhouch | 10:9317a62bd4d0 | 49 | } |
joebarhouch | 8:d19b30a6cd69 | 50 | break; |
joebarhouch | 10:9317a62bd4d0 | 51 | } |
joebarhouch | 8:d19b30a6cd69 | 52 | } |
joebarhouch | 8:d19b30a6cd69 | 53 | |
joebarhouch | 11:b3024ab59fa5 | 54 | if( _type == TYPE_VERT ) { |
joebarhouch | 10:9317a62bd4d0 | 55 | // go up or down depending on direction |
joebarhouch | 10:9317a62bd4d0 | 56 | switch (yDir) { |
joebarhouch | 10:9317a62bd4d0 | 57 | case 1: |
joebarhouch | 11:b3024ab59fa5 | 58 | _enY -= _ev; |
joebarhouch | 10:9317a62bd4d0 | 59 | //switch direction when it hits a wall |
joebarhouch | 10:9317a62bd4d0 | 60 | if(_enY + 5 > HEIGHT || _enY < 0) { |
joebarhouch | 10:9317a62bd4d0 | 61 | yDir = 0; |
joebarhouch | 10:9317a62bd4d0 | 62 | break; |
joebarhouch | 10:9317a62bd4d0 | 63 | } |
joebarhouch | 10:9317a62bd4d0 | 64 | break; |
joebarhouch | 10:9317a62bd4d0 | 65 | case 0: |
joebarhouch | 11:b3024ab59fa5 | 66 | _enY += _ev; |
joebarhouch | 10:9317a62bd4d0 | 67 | //switch direction when it hits a wall |
joebarhouch | 10:9317a62bd4d0 | 68 | if(_enY + 5 > HEIGHT || _enY < 0) { |
joebarhouch | 10:9317a62bd4d0 | 69 | yDir = 1; |
joebarhouch | 10:9317a62bd4d0 | 70 | break; |
joebarhouch | 10:9317a62bd4d0 | 71 | } |
joebarhouch | 10:9317a62bd4d0 | 72 | break; |
joebarhouch | 10:9317a62bd4d0 | 73 | } |
joebarhouch | 8:d19b30a6cd69 | 74 | } |
joebarhouch | 8:d19b30a6cd69 | 75 | } |
joebarhouch | 8:d19b30a6cd69 | 76 | |
joebarhouch | 8:d19b30a6cd69 | 77 | |
joebarhouch | 8:d19b30a6cd69 | 78 | |
joebarhouch | 10:9317a62bd4d0 | 79 | //draw |
joebarhouch | 8:d19b30a6cd69 | 80 | void Enemy::draw(N5110 &lcd) |
joebarhouch | 8:d19b30a6cd69 | 81 | { |
joebarhouch | 10:9317a62bd4d0 | 82 | switch(_type) { |
joebarhouch | 10:9317a62bd4d0 | 83 | case 1: // vertical |
joebarhouch | 10:9317a62bd4d0 | 84 | drawVertical(lcd); |
joebarhouch | 10:9317a62bd4d0 | 85 | break; |
joebarhouch | 10:9317a62bd4d0 | 86 | case 0: // horizontal |
joebarhouch | 10:9317a62bd4d0 | 87 | drawHoriz(lcd); |
joebarhouch | 10:9317a62bd4d0 | 88 | break; |
joebarhouch | 10:9317a62bd4d0 | 89 | } |
joebarhouch | 8:d19b30a6cd69 | 90 | } |
joebarhouch | 8:d19b30a6cd69 | 91 | |
joebarhouch | 10:9317a62bd4d0 | 92 | // sub draw |
joebarhouch | 10:9317a62bd4d0 | 93 | void Enemy::drawVertical(N5110 &lcd) |
joebarhouch | 10:9317a62bd4d0 | 94 | { |
joebarhouch | 10:9317a62bd4d0 | 95 | if (yDir == 0 ) { |
joebarhouch | 10:9317a62bd4d0 | 96 | Bitmap vertDown(s_enemy2_inv, 5, 7); |
joebarhouch | 10:9317a62bd4d0 | 97 | vertDown.render(lcd, _enX, _enY); |
joebarhouch | 10:9317a62bd4d0 | 98 | } else { |
joebarhouch | 10:9317a62bd4d0 | 99 | Bitmap vertUp(s_enemy2, 5, 7); |
joebarhouch | 10:9317a62bd4d0 | 100 | vertUp.render(lcd, _enX, _enY); |
joebarhouch | 8:d19b30a6cd69 | 101 | |
joebarhouch | 10:9317a62bd4d0 | 102 | } |
joebarhouch | 8:d19b30a6cd69 | 103 | |
joebarhouch | 10:9317a62bd4d0 | 104 | } |
joebarhouch | 10:9317a62bd4d0 | 105 | |
joebarhouch | 10:9317a62bd4d0 | 106 | // sub draw |
joebarhouch | 10:9317a62bd4d0 | 107 | void Enemy::drawHoriz(N5110 &lcd) |
joebarhouch | 10:9317a62bd4d0 | 108 | { |
joebarhouch | 10:9317a62bd4d0 | 109 | if (xDir == 1) { |
joebarhouch | 10:9317a62bd4d0 | 110 | Bitmap horLeft(s_enemy1_inv, 5, 7); |
joebarhouch | 10:9317a62bd4d0 | 111 | horLeft.render(lcd, _enX, _enY); |
joebarhouch | 10:9317a62bd4d0 | 112 | } else { |
joebarhouch | 10:9317a62bd4d0 | 113 | Bitmap horRight(s_enemy1, 5, 7); |
joebarhouch | 10:9317a62bd4d0 | 114 | horRight.render(lcd, _enX, _enY); |
joebarhouch | 10:9317a62bd4d0 | 115 | |
joebarhouch | 10:9317a62bd4d0 | 116 | } |
joebarhouch | 10:9317a62bd4d0 | 117 | |
joebarhouch | 10:9317a62bd4d0 | 118 | } |
joebarhouch | 10:9317a62bd4d0 | 119 | |
joebarhouch | 10:9317a62bd4d0 | 120 | //mutator |
joebarhouch | 9:9830d3a78572 | 121 | void Enemy::set_pos(int x, int y) |
joebarhouch | 9:9830d3a78572 | 122 | { |
joebarhouch | 9:9830d3a78572 | 123 | _enX = x; |
joebarhouch | 9:9830d3a78572 | 124 | _enY = y; |
joebarhouch | 9:9830d3a78572 | 125 | |
joebarhouch | 9:9830d3a78572 | 126 | } |
joebarhouch | 9:9830d3a78572 | 127 | |
joebarhouch | 8:d19b30a6cd69 | 128 | |
joebarhouch | 10:9317a62bd4d0 | 129 | //accessor |
joebarhouch | 8:d19b30a6cd69 | 130 | Vector2D Enemy::get_pos() |
joebarhouch | 8:d19b30a6cd69 | 131 | { |
joebarhouch | 8:d19b30a6cd69 | 132 | Vector2D pos = {_enX, _enY}; |
joebarhouch | 8:d19b30a6cd69 | 133 | return pos; |
joebarhouch | 8:d19b30a6cd69 | 134 | } |