
Joshua O'hara 201291390
Dependencies: mbed
Armada/Armada.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 | 12:be491ab6e742 | 1 | #include "Armada.h" |
josh_ohara | 12:be491ab6e742 | 2 | |
josh_ohara | 12:be491ab6e742 | 3 | Armada::Armada() |
josh_ohara | 12:be491ab6e742 | 4 | { |
josh_ohara | 12:be491ab6e742 | 5 | } |
josh_ohara | 12:be491ab6e742 | 6 | |
josh_ohara | 12:be491ab6e742 | 7 | Armada::~Armada() |
josh_ohara | 12:be491ab6e742 | 8 | { |
josh_ohara | 12:be491ab6e742 | 9 | } |
josh_ohara | 12:be491ab6e742 | 10 | |
josh_ohara | 31:27c938ec2a11 | 11 | void Armada::init(int no_aliens, int alien_size, int column_size, int row_size, int level) |
josh_ohara | 31:27c938ec2a11 | 12 | { |
josh_ohara | 31:27c938ec2a11 | 13 | _x = 4; //start armada at (4,4) |
josh_ohara | 31:27c938ec2a11 | 14 | _y = 4; |
josh_ohara | 32:35fd31a945d0 | 15 | _step_x = alien_size + 6; //space between top left of each alien is 10 in x and y direction |
josh_ohara | 31:27c938ec2a11 | 16 | _step_y = alien_size + 6; |
josh_ohara | 32:35fd31a945d0 | 17 | _alien_number = no_aliens; //number of aliens in armada |
josh_ohara | 32:35fd31a945d0 | 18 | _alien_size = alien_size; //size of alien |
josh_ohara | 32:35fd31a945d0 | 19 | _column_size = column_size; //number of aliens in column of armada |
josh_ohara | 32:35fd31a945d0 | 20 | _row_size = row_size; //number of aliens in row of the armada |
josh_ohara | 31:27c938ec2a11 | 21 | create_armada(level); //create armada of aliens |
josh_ohara | 12:be491ab6e742 | 22 | } |
josh_ohara | 12:be491ab6e742 | 23 | |
josh_ohara | 31:27c938ec2a11 | 24 | void Armada::create_armada(int level) |
josh_ohara | 31:27c938ec2a11 | 25 | { |
josh_ohara | 31:27c938ec2a11 | 26 | for (int i = 0; i < _alien_number; i++) { //create a vector of _alien_number aliens |
josh_ohara | 31:27c938ec2a11 | 27 | int remainder_x_ = i%_column_size; //find the row index of the alien (e.g. the 3rd alien in the row), like x position in vector |
josh_ohara | 31:27c938ec2a11 | 28 | int remainder_y_ = i/_column_size; //find the column index of the alien (e.g. the 3rd alien in the column), like y position in vector |
josh_ohara | 31:27c938ec2a11 | 29 | int init_x_ = remainder_x_*_step_x; //starting x position is the row index multiplied by the spacing between the aliens |
josh_ohara | 31:27c938ec2a11 | 30 | int init_y_ = remainder_y_*_step_y; //starting y position is the column index multiplied by the spacing between the aliens |
josh_ohara | 31:27c938ec2a11 | 31 | Alien new_alien; //create a new alien |
josh_ohara | 31:27c938ec2a11 | 32 | new_alien.init(init_x_, init_y_, _alien_size, level); //initialise new alien |
josh_ohara | 31:27c938ec2a11 | 33 | _alien_fleet.push_back(new_alien); //add new alien to armada vector |
josh_ohara | 12:be491ab6e742 | 34 | } |
josh_ohara | 12:be491ab6e742 | 35 | } |
josh_ohara | 12:be491ab6e742 | 36 | |
josh_ohara | 31:27c938ec2a11 | 37 | void Armada::render(N5110 &lcd) |
josh_ohara | 31:27c938ec2a11 | 38 | { |
josh_ohara | 31:27c938ec2a11 | 39 | for (int i =0; i < _alien_fleet.size(); i++) { |
josh_ohara | 31:27c938ec2a11 | 40 | _alien_fleet[i].render(lcd); //draw aliens in armada |
josh_ohara | 12:be491ab6e742 | 41 | } |
josh_ohara | 12:be491ab6e742 | 42 | } |
josh_ohara | 12:be491ab6e742 | 43 | |
josh_ohara | 31:27c938ec2a11 | 44 | void Armada::update(Gamepad &pad, int counter, int level) |
josh_ohara | 31:27c938ec2a11 | 45 | { |
josh_ohara | 31:27c938ec2a11 | 46 | for (int i =0; i < _alien_fleet.size(); i++) { |
josh_ohara | 31:27c938ec2a11 | 47 | int _remainder_x = i%_column_size; |
josh_ohara | 31:27c938ec2a11 | 48 | _alien_fleet[i].update(_step_x, _remainder_x, pad, counter, level); //update each alien in the armada |
josh_ohara | 12:be491ab6e742 | 49 | } |
josh_ohara | 31:27c938ec2a11 | 50 | update_armada_life(); //update armada life variable |
josh_ohara | 12:be491ab6e742 | 51 | } |
josh_ohara | 13:b85f14d35be1 | 52 | |
josh_ohara | 31:27c938ec2a11 | 53 | vector<Alien> Armada::get_vector() |
josh_ohara | 31:27c938ec2a11 | 54 | { |
josh_ohara | 31:27c938ec2a11 | 55 | vector<Alien> v = _alien_fleet; //return a copy of the alien armada vector |
josh_ohara | 14:e88bcf5c0887 | 56 | return v; |
josh_ohara | 31:27c938ec2a11 | 57 | } |
josh_ohara | 14:e88bcf5c0887 | 58 | |
josh_ohara | 31:27c938ec2a11 | 59 | void Armada::set_life(int i, bool x) |
josh_ohara | 31:27c938ec2a11 | 60 | { |
josh_ohara | 31:27c938ec2a11 | 61 | _alien_fleet[i].set_life(x); //set the life of alien i in the vector |
josh_ohara | 31:27c938ec2a11 | 62 | } |
josh_ohara | 31:27c938ec2a11 | 63 | |
josh_ohara | 31:27c938ec2a11 | 64 | bool Armada::get_life(int i) |
josh_ohara | 31:27c938ec2a11 | 65 | { |
josh_ohara | 31:27c938ec2a11 | 66 | return _alien_fleet[i].get_life(); //return the life of alien i in the vector |
josh_ohara | 14:e88bcf5c0887 | 67 | } |
josh_ohara | 14:e88bcf5c0887 | 68 | |
josh_ohara | 31:27c938ec2a11 | 69 | vector<AlienBullet> Armada::get_alien_bullet_vector(int i) |
josh_ohara | 31:27c938ec2a11 | 70 | { |
josh_ohara | 31:27c938ec2a11 | 71 | vector<AlienBullet> v = _alien_fleet[i].get_bullet_vector(); //return the vector of bullets for alien i in the vector |
josh_ohara | 20:0b6f1cfc5be6 | 72 | return v; |
josh_ohara | 31:27c938ec2a11 | 73 | } |
josh_ohara | 20:0b6f1cfc5be6 | 74 | |
josh_ohara | 31:27c938ec2a11 | 75 | void Armada::set_bullet_hit(int n, int i, bool hit) |
josh_ohara | 31:27c938ec2a11 | 76 | { |
josh_ohara | 31:27c938ec2a11 | 77 | _alien_fleet[n].set_bullet_hit(i,hit); //set the hit value of bullet i in alien n's bullet vector |
josh_ohara | 31:27c938ec2a11 | 78 | } |
josh_ohara | 22:3e978b1d7958 | 79 | |
josh_ohara | 31:27c938ec2a11 | 80 | void Armada::update_armada_life() |
josh_ohara | 31:27c938ec2a11 | 81 | { |
josh_ohara | 31:27c938ec2a11 | 82 | _armada_life = false; |
josh_ohara | 31:27c938ec2a11 | 83 | for(int i = 0; i < _alien_fleet.size(); i++){ //set armada life to false and then back to true if at least one alien is alive |
josh_ohara | 31:27c938ec2a11 | 84 | if(_alien_fleet[i].get_life() == true){ //will return false if all aliens are dead |
josh_ohara | 31:27c938ec2a11 | 85 | _armada_life = true; |
josh_ohara | 22:3e978b1d7958 | 86 | //printf("1"); |
josh_ohara | 22:3e978b1d7958 | 87 | } |
josh_ohara | 22:3e978b1d7958 | 88 | } |
josh_ohara | 22:3e978b1d7958 | 89 | } |
josh_ohara | 22:3e978b1d7958 | 90 | |
josh_ohara | 31:27c938ec2a11 | 91 | bool Armada::get_armada_life() |
josh_ohara | 31:27c938ec2a11 | 92 | { |
josh_ohara | 31:27c938ec2a11 | 93 | return _armada_life; //return the life value of armada |
josh_ohara | 35:517b56b010df | 94 | } |
josh_ohara | 35:517b56b010df | 95 | |
josh_ohara | 35:517b56b010df | 96 | void Armada::set_alien_powerup_hit(int i, bool x) |
josh_ohara | 35:517b56b010df | 97 | { |
josh_ohara | 43:1ac200335a68 | 98 | _alien_fleet[i].set_powerup_hit(x); //sets hit value of alien i's powerup |
josh_ohara | 31:27c938ec2a11 | 99 | } |