
Joshua O'hara 201291390
Dependencies: mbed
Ship/Ship.cpp@44:3b904d25ee12, 2020-05-26 (annotated)
- Committer:
- josh_ohara
- Date:
- Tue May 26 15:15:46 2020 +0000
- Revision:
- 44:3b904d25ee12
- Parent:
- 43:1ac200335a68
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 |
---|---|---|---|
josh_ohara | 3:8a140aa1ddcd | 1 | #include "Ship.h" |
josh_ohara | 2:c2316b659b97 | 2 | |
josh_ohara | 8:86cb9a9f8a73 | 3 | |
josh_ohara | 8:86cb9a9f8a73 | 4 | Ship::Ship() |
josh_ohara | 8:86cb9a9f8a73 | 5 | { |
josh_ohara | 8:86cb9a9f8a73 | 6 | |
josh_ohara | 8:86cb9a9f8a73 | 7 | } |
josh_ohara | 8:86cb9a9f8a73 | 8 | |
josh_ohara | 8:86cb9a9f8a73 | 9 | Ship::~Ship() |
josh_ohara | 8:86cb9a9f8a73 | 10 | { |
josh_ohara | 8:86cb9a9f8a73 | 11 | |
josh_ohara | 8:86cb9a9f8a73 | 12 | } |
josh_ohara | 8:86cb9a9f8a73 | 13 | |
josh_ohara | 27:eb755a345b1f | 14 | void Ship::init(int height, int width) |
josh_ohara | 2:c2316b659b97 | 15 | { |
josh_ohara | 27:eb755a345b1f | 16 | _x = WIDTH/2 - width/2; //set the start x position to the middle of the screen |
josh_ohara | 27:eb755a345b1f | 17 | _y = HEIGHT - 2; //set the y psotion to the bottom of the screen, this will not change |
josh_ohara | 27:eb755a345b1f | 18 | _height = height; //height of the ship defined in main file and is input to init function |
josh_ohara | 27:eb755a345b1f | 19 | _width = width; //width of the ship defined in main file and is input to the init function |
josh_ohara | 27:eb755a345b1f | 20 | _speed = 0; //ship starts stationary, speed set by joystick |
josh_ohara | 27:eb755a345b1f | 21 | _life = true; //ship is set as alive until hit by alien or bullet |
josh_ohara | 27:eb755a345b1f | 22 | _ship_bullet_vector.init(); //initialising the class that creates the vector of ship bullets |
josh_ohara | 2:c2316b659b97 | 23 | } |
josh_ohara | 2:c2316b659b97 | 24 | |
josh_ohara | 8:86cb9a9f8a73 | 25 | |
josh_ohara | 8:86cb9a9f8a73 | 26 | void Ship::render(N5110 &lcd) |
josh_ohara | 8:86cb9a9f8a73 | 27 | { |
josh_ohara | 27:eb755a345b1f | 28 | _ship_bullet_vector.render(lcd); //draws the vector of bullets |
josh_ohara | 27:eb755a345b1f | 29 | |
josh_ohara | 36:78efa0e7bd31 | 30 | if(_life == true){ |
josh_ohara | 36:78efa0e7bd31 | 31 | lcd.setPixel(_x+_width/2,_y - 1,true); //draws the ship if alive |
josh_ohara | 37:90a0671d2ba7 | 32 | lcd.drawLine(_x,_y,_x+_width-1,_y,1); |
josh_ohara | 36:78efa0e7bd31 | 33 | lcd.setPixel(_x,_y+1,true); |
josh_ohara | 36:78efa0e7bd31 | 34 | lcd.setPixel(_x + _width - 1,_y+1,true); |
josh_ohara | 20:0b6f1cfc5be6 | 35 | } |
josh_ohara | 2:c2316b659b97 | 36 | } |
josh_ohara | 2:c2316b659b97 | 37 | |
josh_ohara | 37:90a0671d2ba7 | 38 | void Ship::update(Direction d, float mag, Gamepad &pad, N5110 &lcd, int counter, bool powerup) |
josh_ohara | 2:c2316b659b97 | 39 | { |
josh_ohara | 36:78efa0e7bd31 | 40 | _powerup = powerup; |
josh_ohara | 27:eb755a345b1f | 41 | _speed = int(mag*5.0f); //speed is set by offset of the joystick |
josh_ohara | 2:c2316b659b97 | 42 | |
josh_ohara | 27:eb755a345b1f | 43 | if (d == E) { //move ship right if joystick is moved right |
josh_ohara | 27:eb755a345b1f | 44 | _x+=_speed; //move ship by increase ing position by speed value |
josh_ohara | 27:eb755a345b1f | 45 | } |
josh_ohara | 27:eb755a345b1f | 46 | else if (d == NE) { //move ship right by half speed if joysick orientation has right component |
josh_ohara | 27:eb755a345b1f | 47 | _x+=0.5*_speed; |
josh_ohara | 27:eb755a345b1f | 48 | } |
josh_ohara | 9:8e695df3cc36 | 49 | else if (d == SE) { |
josh_ohara | 27:eb755a345b1f | 50 | _x+=0.5*_speed; |
josh_ohara | 27:eb755a345b1f | 51 | } |
josh_ohara | 27:eb755a345b1f | 52 | else if (d == W) { //same as above but for left |
josh_ohara | 27:eb755a345b1f | 53 | _x-=_speed; |
josh_ohara | 27:eb755a345b1f | 54 | } |
josh_ohara | 9:8e695df3cc36 | 55 | else if (d == NW) { |
josh_ohara | 27:eb755a345b1f | 56 | _x-=0.5*_speed; |
josh_ohara | 27:eb755a345b1f | 57 | } |
josh_ohara | 9:8e695df3cc36 | 58 | else if (d == SW) { |
josh_ohara | 27:eb755a345b1f | 59 | _x-=0.5*_speed; |
josh_ohara | 27:eb755a345b1f | 60 | } |
josh_ohara | 2:c2316b659b97 | 61 | |
josh_ohara | 27:eb755a345b1f | 62 | if (_x < 1) { //return ship to screen if ship goes off screen |
josh_ohara | 27:eb755a345b1f | 63 | _x = 1; |
josh_ohara | 27:eb755a345b1f | 64 | } |
josh_ohara | 27:eb755a345b1f | 65 | if (_x > WIDTH - _width - 1) { |
josh_ohara | 27:eb755a345b1f | 66 | _x = WIDTH - _width - 1; |
josh_ohara | 2:c2316b659b97 | 67 | } |
josh_ohara | 27:eb755a345b1f | 68 | |
josh_ohara | 37:90a0671d2ba7 | 69 | _ship_bullet_vector.update(pad, lcd, _x+4, _y, counter, _powerup); //update the ship bullet vector |
josh_ohara | 27:eb755a345b1f | 70 | |
josh_ohara | 27:eb755a345b1f | 71 | if (_life == false) { //turn on LEDs when ship dies |
josh_ohara | 27:eb755a345b1f | 72 | pad.leds_on(); |
josh_ohara | 2:c2316b659b97 | 73 | } |
josh_ohara | 2:c2316b659b97 | 74 | } |
josh_ohara | 2:c2316b659b97 | 75 | |
josh_ohara | 27:eb755a345b1f | 76 | Vector2D Ship::get_position() |
josh_ohara | 27:eb755a345b1f | 77 | { |
josh_ohara | 27:eb755a345b1f | 78 | Vector2D p = {_x,_y}; //create 2D vector of ship x and y position |
josh_ohara | 27:eb755a345b1f | 79 | return p; //return the vector |
josh_ohara | 3:8a140aa1ddcd | 80 | } |
josh_ohara | 4:18a1fc4c38e0 | 81 | |
josh_ohara | 27:eb755a345b1f | 82 | void Ship::set_life(bool life) |
josh_ohara | 27:eb755a345b1f | 83 | { |
josh_ohara | 27:eb755a345b1f | 84 | _life = life; //set life of the ship to input of function |
josh_ohara | 21:970807533b10 | 85 | } |
josh_ohara | 3:8a140aa1ddcd | 86 | |
josh_ohara | 27:eb755a345b1f | 87 | bool Ship::get_life() |
josh_ohara | 27:eb755a345b1f | 88 | { |
josh_ohara | 27:eb755a345b1f | 89 | return _life; //return the life of the ship (alive or dead) |
josh_ohara | 27:eb755a345b1f | 90 | } |
josh_ohara | 27:eb755a345b1f | 91 | |
josh_ohara | 27:eb755a345b1f | 92 | vector<ShipBullet> Ship::get_bullet_vector() |
josh_ohara | 27:eb755a345b1f | 93 | { |
josh_ohara | 43:1ac200335a68 | 94 | vector<ShipBullet> v = _ship_bullet_vector.get_vector(); //get the vector of ship bullets |
josh_ohara | 27:eb755a345b1f | 95 | return v; //return the vector |
josh_ohara | 27:eb755a345b1f | 96 | } |
josh_ohara | 27:eb755a345b1f | 97 | |
josh_ohara | 27:eb755a345b1f | 98 | void Ship::set_bullet_hit(int i, bool hit) |
josh_ohara | 27:eb755a345b1f | 99 | { |
josh_ohara | 27:eb755a345b1f | 100 | _ship_bullet_vector.set_hit(i,hit); //set the hit value of bullet with index i in the ship bullet vector |
josh_ohara | 21:970807533b10 | 101 | } |
josh_ohara | 2:c2316b659b97 | 102 | |
josh_ohara | 2:c2316b659b97 | 103 |