harry rance
/
Revised_Space_Invaders
Harry Rance 200925395 Embedded Systems Project
Boss.cpp@7:569f3fc70ac5, 2017-05-03 (annotated)
- Committer:
- harryrance
- Date:
- Wed May 03 16:33:20 2017 +0000
- Revision:
- 7:569f3fc70ac5
- Parent:
- 6:dca8b5e2ebe5
Committed with full documentation generated.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
harryrance | 2:50feb42b982c | 1 | #include "Boss.h" |
harryrance | 6:dca8b5e2ebe5 | 2 | //Constructor |
harryrance | 2:50feb42b982c | 3 | Boss::Boss() |
harryrance | 2:50feb42b982c | 4 | { |
harryrance | 2:50feb42b982c | 5 | |
harryrance | 2:50feb42b982c | 6 | } |
harryrance | 6:dca8b5e2ebe5 | 7 | //Destructor |
harryrance | 2:50feb42b982c | 8 | Boss::~Boss() |
harryrance | 2:50feb42b982c | 9 | { |
harryrance | 2:50feb42b982c | 10 | |
harryrance | 2:50feb42b982c | 11 | } |
harryrance | 6:dca8b5e2ebe5 | 12 | // Initialise function - initialises all parameters for the boss fight to begin with. |
harryrance | 3:43970d8d642e | 13 | void Boss::initialise(int x_origin, int y_origin, int health_x, int health_y, |
harryrance | 3:43970d8d642e | 14 | int health, int speed, int coins, int score) |
harryrance | 2:50feb42b982c | 15 | { |
harryrance | 6:dca8b5e2ebe5 | 16 | _x = x_origin; //sets the _x movement variable equal to the initialised x origin. |
harryrance | 6:dca8b5e2ebe5 | 17 | _boss_y = y_origin; //sets the _boss_y movement variable equal to the initialised y origin. |
harryrance | 2:50feb42b982c | 18 | _health_bar_constant = 52; |
harryrance | 2:50feb42b982c | 19 | _boss_lives = 3; |
harryrance | 6:dca8b5e2ebe5 | 20 | _heart_x = 35; //x and y origins for the first heart. |
harryrance | 2:50feb42b982c | 21 | _heart_y = 1; |
harryrance | 6:dca8b5e2ebe5 | 22 | _heart_var = 15; //sets distance between hearts. |
harryrance | 3:43970d8d642e | 23 | _boss_coins = coins; |
harryrance | 3:43970d8d642e | 24 | _boss_score = score; |
harryrance | 2:50feb42b982c | 25 | |
harryrance | 2:50feb42b982c | 26 | int direction = 0; |
harryrance | 6:dca8b5e2ebe5 | 27 | //movement direction and velocity indicator for the boss and bombs. |
harryrance | 2:50feb42b982c | 28 | if (direction == 0) { |
harryrance | 2:50feb42b982c | 29 | _velocity.x = speed; |
harryrance | 2:50feb42b982c | 30 | _b_velocity.y = -speed * 1.2; |
harryrance | 2:50feb42b982c | 31 | } else if (direction == 1) { |
harryrance | 2:50feb42b982c | 32 | _velocity.x = -speed; |
harryrance | 2:50feb42b982c | 33 | } |
harryrance | 2:50feb42b982c | 34 | } |
harryrance | 6:dca8b5e2ebe5 | 35 | //Draw function - calls the functions to be drawn on the LCD screen. |
harryrance | 2:50feb42b982c | 36 | void Boss::draw(N5110 &lcd) |
harryrance | 2:50feb42b982c | 37 | { |
harryrance | 2:50feb42b982c | 38 | boss_sprite(lcd); |
harryrance | 2:50feb42b982c | 39 | health_bar_draw(lcd); |
harryrance | 2:50feb42b982c | 40 | draw_bombs(lcd); |
harryrance | 2:50feb42b982c | 41 | draw_lives(lcd); |
harryrance | 2:50feb42b982c | 42 | } |
harryrance | 6:dca8b5e2ebe5 | 43 | //Update function - updates the velociy parameters for the boss and bomb movement. |
harryrance | 2:50feb42b982c | 44 | void Boss::update() |
harryrance | 2:50feb42b982c | 45 | { |
harryrance | 2:50feb42b982c | 46 | _x += _velocity.x; |
harryrance | 2:50feb42b982c | 47 | _y += _velocity.y; |
harryrance | 2:50feb42b982c | 48 | _b_x += _b_velocity.x; |
harryrance | 2:50feb42b982c | 49 | _b_y -= _b_velocity.y; |
harryrance | 2:50feb42b982c | 50 | } |
harryrance | 6:dca8b5e2ebe5 | 51 | //Boss Sprite function - sets the required pixels for the boss sprite to be displayes, with regards to the x direciton movement. |
harryrance | 2:50feb42b982c | 52 | void Boss::boss_sprite(N5110 &lcd) |
harryrance | 2:50feb42b982c | 53 | { |
harryrance | 2:50feb42b982c | 54 | _boss_x = _x/8; |
harryrance | 2:50feb42b982c | 55 | lcd.drawRect(_boss_x + 4, _boss_y - 14, 2, 2, 0); //1 |
harryrance | 2:50feb42b982c | 56 | lcd.drawRect(_boss_x + 16, _boss_y - 14, 2, 2, 0); //2 |
harryrance | 2:50feb42b982c | 57 | lcd.drawRect(_boss_x + 6, _boss_y - 12, 2, 2, 0); //3 |
harryrance | 2:50feb42b982c | 58 | lcd.drawRect(_boss_x + 14, _boss_y - 12, 2, 2, 0); //4 |
harryrance | 2:50feb42b982c | 59 | lcd.drawRect(_boss_x + 4, _boss_y - 10, 14, 2, 0); //5 |
harryrance | 2:50feb42b982c | 60 | lcd.drawRect(_boss_x + 2, _boss_y - 8, 4, 2, 0); //6 |
harryrance | 2:50feb42b982c | 61 | lcd.drawRect(_boss_x + 8, _boss_y - 8, 6, 2, 0); //7 |
harryrance | 2:50feb42b982c | 62 | lcd.drawRect(_boss_x + 16, _boss_y - 8, 4, 2, 0); //8 |
harryrance | 2:50feb42b982c | 63 | lcd.drawRect(_boss_x + 2, _boss_y - 6, 18, 2, 0); //9 |
harryrance | 2:50feb42b982c | 64 | lcd.drawRect(_boss_x, _boss_y - 6, 2, 6, 0); //10 |
harryrance | 2:50feb42b982c | 65 | lcd.drawRect(_boss_x + 20, _boss_y - 6, 2, 6, 0); //11 |
harryrance | 2:50feb42b982c | 66 | lcd.drawRect(_boss_x + 4, _boss_y - 4, 14, 2, 0); //12 |
harryrance | 2:50feb42b982c | 67 | lcd.drawRect(_boss_x + 4, _boss_y - 2, 2, 2, 0); //13 |
harryrance | 2:50feb42b982c | 68 | lcd.drawRect(_boss_x + 16, _boss_y - 2, 2, 2, 0); //14 |
harryrance | 2:50feb42b982c | 69 | lcd.drawRect(_boss_x + 6, _boss_y, 4, 2, 0); //15 |
harryrance | 2:50feb42b982c | 70 | lcd.drawRect(_boss_x + 12, _boss_y, 4, 2, 0); //16 |
harryrance | 2:50feb42b982c | 71 | } |
harryrance | 6:dca8b5e2ebe5 | 72 | //Health Bar Draw Function - draws the inner and outer sections of the health bar, and sets the length of the inner health bar |
harryrance | 6:dca8b5e2ebe5 | 73 | //with regards to the health integer variable. |
harryrance | 2:50feb42b982c | 74 | void Boss::health_bar_draw(N5110 &lcd) |
harryrance | 2:50feb42b982c | 75 | { |
harryrance | 2:50feb42b982c | 76 | lcd.drawRect(15, 8, 53, 5, 0); //health bar outline |
harryrance | 2:50feb42b982c | 77 | lcd.drawRect(16, 9, _health_bar_constant, 3, 1); //inner health bar |
harryrance | 2:50feb42b982c | 78 | } |
harryrance | 6:dca8b5e2ebe5 | 79 | //Draw Bombs function - sets pixels for the bombs and resets their y-position if they have reached the screen boundary. |
harryrance | 2:50feb42b982c | 80 | void Boss::draw_bombs(N5110 &lcd) |
harryrance | 2:50feb42b982c | 81 | { |
harryrance | 6:dca8b5e2ebe5 | 82 | _bomb_x = _x/8; //bombs move in the x direction at the same velocity as the boss in order to increase difficulty. |
harryrance | 2:50feb42b982c | 83 | _bomb_y = _b_y/8; |
harryrance | 2:50feb42b982c | 84 | lcd.setPixel(_bomb_x, _bomb_y); |
harryrance | 2:50feb42b982c | 85 | lcd.setPixel(_bomb_x + 1, _bomb_y - 1); |
harryrance | 2:50feb42b982c | 86 | lcd.setPixel(_bomb_x, _bomb_y - 2); |
harryrance | 2:50feb42b982c | 87 | lcd.setPixel(_bomb_x + 1, _bomb_y - 3); |
harryrance | 2:50feb42b982c | 88 | lcd.setPixel(_bomb_x, _bomb_y - 4); |
harryrance | 2:50feb42b982c | 89 | |
harryrance | 2:50feb42b982c | 90 | lcd.setPixel(_bomb_x + 20, _bomb_y); |
harryrance | 2:50feb42b982c | 91 | lcd.setPixel(_bomb_x + 21, _bomb_y - 1); |
harryrance | 2:50feb42b982c | 92 | lcd.setPixel(_bomb_x + 20, _bomb_y - 2); |
harryrance | 2:50feb42b982c | 93 | lcd.setPixel(_bomb_x + 21, _bomb_y - 3); |
harryrance | 2:50feb42b982c | 94 | lcd.setPixel(_bomb_x + 20, _bomb_y - 4); |
harryrance | 2:50feb42b982c | 95 | |
harryrance | 2:50feb42b982c | 96 | lcd.setPixel(_bomb_x + 10, _bomb_y); |
harryrance | 2:50feb42b982c | 97 | lcd.setPixel(_bomb_x + 11, _bomb_y - 1); |
harryrance | 2:50feb42b982c | 98 | lcd.setPixel(_bomb_x + 10, _bomb_y - 2); |
harryrance | 2:50feb42b982c | 99 | lcd.setPixel(_bomb_x + 11, _bomb_y - 3); |
harryrance | 2:50feb42b982c | 100 | lcd.setPixel(_bomb_x + 10, _bomb_y - 4); |
harryrance | 2:50feb42b982c | 101 | if (_b_y > 384){ |
harryrance | 2:50feb42b982c | 102 | _b_y = 200; |
harryrance | 2:50feb42b982c | 103 | } |
harryrance | 2:50feb42b982c | 104 | } |
harryrance | 6:dca8b5e2ebe5 | 105 | //Draw Lives function - sets the pixels in the shape of a heart, and draws 0, 1, 2 or 3 instances with regards to how many lives are left. |
harryrance | 2:50feb42b982c | 106 | void Boss::draw_lives(N5110 &lcd) |
harryrance | 2:50feb42b982c | 107 | { |
harryrance | 2:50feb42b982c | 108 | if (_boss_lives == 3) { |
harryrance | 2:50feb42b982c | 109 | _heart_var = 15; |
harryrance | 2:50feb42b982c | 110 | } else if (_boss_lives == 2) { |
harryrance | 2:50feb42b982c | 111 | _heart_var = 8; |
harryrance | 2:50feb42b982c | 112 | } else if (_boss_lives == 1) { |
harryrance | 2:50feb42b982c | 113 | _heart_var = 1; |
harryrance | 2:50feb42b982c | 114 | } |
harryrance | 2:50feb42b982c | 115 | |
harryrance | 2:50feb42b982c | 116 | if (_boss_lives > 0) { |
harryrance | 2:50feb42b982c | 117 | for(int i = 0; i < _heart_var; i += 7) { |
harryrance | 2:50feb42b982c | 118 | lcd.drawRect((_heart_x + i) - 2, _heart_y,2,1,0); |
harryrance | 2:50feb42b982c | 119 | lcd.drawRect((_heart_x + i) + 1, _heart_y,2,1,0); |
harryrance | 2:50feb42b982c | 120 | lcd.drawRect((_heart_x + i) - 3, _heart_y + 1,7,1,0); |
harryrance | 2:50feb42b982c | 121 | lcd.drawRect((_heart_x + i) - 4, _heart_y + 2,9,1,0); |
harryrance | 2:50feb42b982c | 122 | lcd.drawRect((_heart_x + i) - 3, _heart_y + 3,7,1,0); |
harryrance | 2:50feb42b982c | 123 | lcd.drawRect((_heart_x + i) - 2, _heart_y + 4,5,1,0); |
harryrance | 2:50feb42b982c | 124 | lcd.drawRect((_heart_x + i) - 1, _heart_y + 5,3,1,0); |
harryrance | 2:50feb42b982c | 125 | lcd.setPixel((_heart_x + i), _heart_y + 6); |
harryrance | 2:50feb42b982c | 126 | } |
harryrance | 2:50feb42b982c | 127 | } |
harryrance | 2:50feb42b982c | 128 | } |
harryrance | 6:dca8b5e2ebe5 | 129 | //Decrement health function - recudes boss health by 2 every time this is called. |
harryrance | 2:50feb42b982c | 130 | void Boss::decrement_health_constant() |
harryrance | 2:50feb42b982c | 131 | { |
harryrance | 2:50feb42b982c | 132 | _health_bar_constant -= 2; |
harryrance | 2:50feb42b982c | 133 | } |
harryrance | 6:dca8b5e2ebe5 | 134 | //Get Health function - returns the integer value of boss health. |
harryrance | 2:50feb42b982c | 135 | int Boss::get_health_bar_constant() |
harryrance | 2:50feb42b982c | 136 | { |
harryrance | 2:50feb42b982c | 137 | return _health_bar_constant; |
harryrance | 2:50feb42b982c | 138 | } |
harryrance | 6:dca8b5e2ebe5 | 139 | //Get Bomb Velocity function - returns a 2D vector with x and y members of the bomb velocity. |
harryrance | 2:50feb42b982c | 140 | Vector2D Boss::get_bomb_vel() |
harryrance | 2:50feb42b982c | 141 | { |
harryrance | 2:50feb42b982c | 142 | Vector2D b_vel = {_b_velocity.x,_b_velocity.y}; |
harryrance | 2:50feb42b982c | 143 | return b_vel; |
harryrance | 2:50feb42b982c | 144 | } |
harryrance | 6:dca8b5e2ebe5 | 145 | //Get Bomb Position function - returns a 2D vector with x and y members of the bomb position. |
harryrance | 2:50feb42b982c | 146 | Vector2D Boss::get_bomb_pos() |
harryrance | 2:50feb42b982c | 147 | { |
harryrance | 2:50feb42b982c | 148 | Vector2D b_pos = {_b_x,_b_y}; |
harryrance | 2:50feb42b982c | 149 | return b_pos; |
harryrance | 2:50feb42b982c | 150 | } |
harryrance | 6:dca8b5e2ebe5 | 151 | //Get Boss Velocity function - returns 2D vector with x and y memebers of the boss velocity. |
harryrance | 2:50feb42b982c | 152 | Vector2D Boss::get_velocity() |
harryrance | 2:50feb42b982c | 153 | { |
harryrance | 2:50feb42b982c | 154 | Vector2D v = {_velocity.x,_velocity.y}; |
harryrance | 2:50feb42b982c | 155 | return v; |
harryrance | 2:50feb42b982c | 156 | } |
harryrance | 6:dca8b5e2ebe5 | 157 | //Get Boss Position funciton - returns 2D vector with x and y members of the boss position. |
harryrance | 2:50feb42b982c | 158 | Vector2D Boss::get_pos() |
harryrance | 2:50feb42b982c | 159 | { |
harryrance | 2:50feb42b982c | 160 | Vector2D p = {_x,_y}; |
harryrance | 2:50feb42b982c | 161 | return p; |
harryrance | 2:50feb42b982c | 162 | } |
harryrance | 6:dca8b5e2ebe5 | 163 | // sets the velocity of the bomb in the x and y directions. |
harryrance | 2:50feb42b982c | 164 | void Boss::set_bomb_vel(Vector2D b_vel) |
harryrance | 2:50feb42b982c | 165 | { |
harryrance | 2:50feb42b982c | 166 | _b_velocity.x = b_vel.x; |
harryrance | 2:50feb42b982c | 167 | _b_velocity.y = b_vel.y; |
harryrance | 2:50feb42b982c | 168 | } |
harryrance | 6:dca8b5e2ebe5 | 169 | // sets the x and y position of the bombs. |
harryrance | 2:50feb42b982c | 170 | void Boss::set_bomb_pos(Vector2D b_pos) |
harryrance | 2:50feb42b982c | 171 | { |
harryrance | 2:50feb42b982c | 172 | _b_x = b_pos.x; |
harryrance | 2:50feb42b982c | 173 | _b_y = b_pos.y; |
harryrance | 2:50feb42b982c | 174 | } |
harryrance | 6:dca8b5e2ebe5 | 175 | // sets the x and y velocity of the boss. |
harryrance | 2:50feb42b982c | 176 | void Boss::set_velocity(Vector2D v) |
harryrance | 2:50feb42b982c | 177 | { |
harryrance | 2:50feb42b982c | 178 | _velocity.x = v.x; |
harryrance | 2:50feb42b982c | 179 | _velocity.y = v.y; |
harryrance | 2:50feb42b982c | 180 | } |
harryrance | 6:dca8b5e2ebe5 | 181 | // sets x and y position of the boss. |
harryrance | 2:50feb42b982c | 182 | void Boss::set_pos(Vector2D p) |
harryrance | 2:50feb42b982c | 183 | { |
harryrance | 2:50feb42b982c | 184 | _x = p.x; |
harryrance | 2:50feb42b982c | 185 | _y = p.y; |
harryrance | 2:50feb42b982c | 186 | } |
harryrance | 6:dca8b5e2ebe5 | 187 | // increments score by 100 if boss fight is complete. |
harryrance | 2:50feb42b982c | 188 | void Boss::add_boss_score() |
harryrance | 2:50feb42b982c | 189 | { |
harryrance | 2:50feb42b982c | 190 | _boss_score += 100; |
harryrance | 2:50feb42b982c | 191 | } |
harryrance | 6:dca8b5e2ebe5 | 192 | // returns integet value of the boss score. |
harryrance | 2:50feb42b982c | 193 | int Boss::get_boss_score() |
harryrance | 2:50feb42b982c | 194 | { |
harryrance | 2:50feb42b982c | 195 | return _boss_score; |
harryrance | 2:50feb42b982c | 196 | } |
harryrance | 6:dca8b5e2ebe5 | 197 | // increments coins by 1000 if boss fight is complete. |
harryrance | 2:50feb42b982c | 198 | void Boss::add_boss_coins() |
harryrance | 2:50feb42b982c | 199 | { |
harryrance | 2:50feb42b982c | 200 | _boss_coins += 1000; |
harryrance | 2:50feb42b982c | 201 | } |
harryrance | 6:dca8b5e2ebe5 | 202 | // returns integer value of amount of coins in boss fight. |
harryrance | 2:50feb42b982c | 203 | int Boss::get_boss_coins() |
harryrance | 2:50feb42b982c | 204 | { |
harryrance | 2:50feb42b982c | 205 | return _boss_coins; |
harryrance | 2:50feb42b982c | 206 | } |
harryrance | 6:dca8b5e2ebe5 | 207 | //increments the lives variable by 1 (used when purchasing lives). |
harryrance | 3:43970d8d642e | 208 | void Boss::inc_lives() |
harryrance | 3:43970d8d642e | 209 | { |
harryrance | 3:43970d8d642e | 210 | _boss_lives += 1; |
harryrance | 3:43970d8d642e | 211 | } |
harryrance | 6:dca8b5e2ebe5 | 212 | //decrements life variable by 1 each time user is hit by a bomb. |
harryrance | 3:43970d8d642e | 213 | void Boss::dec_lives() |
harryrance | 2:50feb42b982c | 214 | { |
harryrance | 2:50feb42b982c | 215 | _boss_lives -= 1; |
harryrance | 2:50feb42b982c | 216 | } |
harryrance | 6:dca8b5e2ebe5 | 217 | //returns integer value of remaining user lives. |
harryrance | 2:50feb42b982c | 218 | int Boss::lives() |
harryrance | 2:50feb42b982c | 219 | { |
harryrance | 2:50feb42b982c | 220 | return _boss_lives; |
harryrance | 2:50feb42b982c | 221 | } |
harryrance | 2:50feb42b982c | 222 |