Joshua O'hara 201291390

Dependencies:   mbed

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?

UserRevisionLine numberNew contents of line
josh_ohara 9:8e695df3cc36 1 #include "Alien.h"
josh_ohara 6:5bea67cc96f9 2
josh_ohara 44:3b904d25ee12 3
josh_ohara 24:ff5af5a013b5 4 void Alien::init(int x, int y, int size, int level) {
josh_ohara 43:1ac200335a68 5 _alive = true; //alien life is true
josh_ohara 43:1ac200335a68 6 _shoot = false; //set shoot flag to false to be turned true by flag set function
josh_ohara 43:1ac200335a68 7 _powerup = true; //set powerup flag to true, will be turned to false when alien dies so alien can only drop 1 powerup
josh_ohara 43:1ac200335a68 8 _create_powerup = false; //create powerup flag false as no powerup has been created
josh_ohara 43:1ac200335a68 9 _x = x; //set starting position
josh_ohara 33:d8284dee58db 10 _y = y;
josh_ohara 43:1ac200335a68 11 _size = size; //set size (width)
josh_ohara 43:1ac200335a68 12 _speed = 1 + 0.3 * level; //set speed in x diretion, increases with level to make game get harder
josh_ohara 43:1ac200335a68 13 _alien_bullet_vector.init(); //initialise the alien's vector of bullets
josh_ohara 27:eb755a345b1f 14 // printf(" speed = %2d ",Speed);
josh_ohara 18:828e9f6ddfdb 15
josh_ohara 9:8e695df3cc36 16 }
josh_ohara 9:8e695df3cc36 17
josh_ohara 27:eb755a345b1f 18 void Alien::render(N5110 &lcd)
josh_ohara 27:eb755a345b1f 19 {
josh_ohara 37:90a0671d2ba7 20 if(_alive == true){
josh_ohara 43:1ac200335a68 21 lcd.drawLine(_x,_y+1,_x+_size-1,_y+1,1); //draw alien (traingular pointing down)
josh_ohara 36:78efa0e7bd31 22 lcd.setPixel(_x,_y,true);
josh_ohara 36:78efa0e7bd31 23 lcd.setPixel(_x+_size-1,_y,true);
josh_ohara 36:78efa0e7bd31 24 lcd.setPixel(_x+_size/2,_y+2,true);
josh_ohara 27:eb755a345b1f 25 _alien_bullet_vector.render(lcd);
josh_ohara 10:9189419fda68 26 }
josh_ohara 43:1ac200335a68 27 if(_create_powerup == true){ //draw powerup if one has been created
josh_ohara 43:1ac200335a68 28 _powerup_vector[0].render(lcd); //we only need to check 0th powerup in vector as only one can be added to vector per alien
josh_ohara 35:517b56b010df 29 //printf("powerup render");
josh_ohara 35:517b56b010df 30 }
josh_ohara 9:8e695df3cc36 31 }
josh_ohara 6:5bea67cc96f9 32
josh_ohara 27:eb755a345b1f 33 Vector2D Alien::get_position()
josh_ohara 27:eb755a345b1f 34 {
josh_ohara 43:1ac200335a68 35 Vector2D p = {_x,_y}; //returns a 2d vector of the alien's position
josh_ohara 9:8e695df3cc36 36 return p;
josh_ohara 9:8e695df3cc36 37 }
josh_ohara 6:5bea67cc96f9 38
josh_ohara 27:eb755a345b1f 39 void Alien::update(int step_x, int remainder_x, Gamepad &pad, int counter, int level)
josh_ohara 27:eb755a345b1f 40 {
josh_ohara 43:1ac200335a68 41 _x+=_speed; //change xposition according to speed
josh_ohara 43:1ac200335a68 42 int counter_ = counter; //copy counter value to function variable
josh_ohara 43:1ac200335a68 43 int step_x_ = step_x; //copy step value to function variable
josh_ohara 43:1ac200335a68 44 int remainder_x_ = remainder_x; //copy remainder value to function variable
josh_ohara 43:1ac200335a68 45 if (_x < 1 + remainder_x_*step_x_) { //make alien change directions and move down screen if alien armada reaches edge of the screen
josh_ohara 43:1ac200335a68 46 _x = 1 + remainder_x_*step_x_; //using the remainder_x_ value (which shows this alien's position in the row, e.g 3 from left)
josh_ohara 43:1ac200335a68 47 _speed = -_speed; //and the distance between alien's, we can calculate at which point on the screen each alien
josh_ohara 43:1ac200335a68 48 _y = _y + 2; //should change directions
josh_ohara 9:8e695df3cc36 49 }
josh_ohara 43:1ac200335a68 50 if (_x > WIDTH - _size - 1 - (4-remainder_x_)*step_x_) { //same process but for other side of screen
josh_ohara 27:eb755a345b1f 51 _x = WIDTH - _size - 1 - (4-remainder_x_)*step_x_;
josh_ohara 27:eb755a345b1f 52 _speed = -_speed;
josh_ohara 27:eb755a345b1f 53 _y = _y + 2;
josh_ohara 9:8e695df3cc36 54 }
josh_ohara 43:1ac200335a68 55 shoot_flag_set(counter_,level); //runs flag set function
josh_ohara 43:1ac200335a68 56 _alien_bullet_vector.update(_x,_y,_shoot); //updates the vector of alien bullets
josh_ohara 43:1ac200335a68 57 _shoot = false; //sets shoot flag to false in case alien shot on this loop
josh_ohara 35:517b56b010df 58
josh_ohara 43:1ac200335a68 59 if(_powerup == true){ //runs the powerup flag set to determine if the create powerup flag will be set to true upon alien death
josh_ohara 43:1ac200335a68 60 create_powerup_flag_set(); //powerup flag set function will not run if alien is still alive so create powerup flag cannot be set to true
josh_ohara 43:1ac200335a68 61 if(_create_powerup == true){ //powerup flag will be set to false after alien dies so this block of code is only run 1 time after death
josh_ohara 43:1ac200335a68 62 PowerUp new_powerup_; //creating a new powerup only oncre
josh_ohara 43:1ac200335a68 63 new_powerup_.init(_x,_y,3); //initialising powerup
josh_ohara 43:1ac200335a68 64 _powerup_vector.push_back(new_powerup_); //adding powerup to a vector for use outside of function
josh_ohara 36:78efa0e7bd31 65 //printf(" powerup created ");
josh_ohara 35:517b56b010df 66
josh_ohara 35:517b56b010df 67 }
josh_ohara 35:517b56b010df 68 }
josh_ohara 43:1ac200335a68 69 if(_create_powerup == true){ //updates powerup if one has been created
josh_ohara 43:1ac200335a68 70 _powerup_vector[0].update(); //we only need to check 0th powerup in vector as only one can be added to vector per alien
josh_ohara 36:78efa0e7bd31 71 //printf("powerup update");
josh_ohara 33:d8284dee58db 72 }
josh_ohara 24:ff5af5a013b5 73 //printf(" speed = %2d ",Speed);
josh_ohara 9:8e695df3cc36 74 }
josh_ohara 6:5bea67cc96f9 75
josh_ohara 27:eb755a345b1f 76 void Alien::set_life(bool x)
josh_ohara 27:eb755a345b1f 77 {
josh_ohara 28:32763617ec5f 78 _alive = x; //sets the life of the alien
josh_ohara 15:dde4ce4bf7fe 79 }
josh_ohara 10:9189419fda68 80
josh_ohara 27:eb755a345b1f 81 bool Alien::get_life()
josh_ohara 27:eb755a345b1f 82 {
josh_ohara 22:3e978b1d7958 83 //printf("A = %2d ",Alive);
josh_ohara 28:32763617ec5f 84 return _alive; //returns the life of the alien
josh_ohara 15:dde4ce4bf7fe 85 }
josh_ohara 15:dde4ce4bf7fe 86
josh_ohara 33:d8284dee58db 87 void Alien::shoot_flag_set(int counter, int level)
josh_ohara 27:eb755a345b1f 88 {
josh_ohara 27:eb755a345b1f 89 int counter_ = counter;
josh_ohara 43:1ac200335a68 90 if(counter_%8 == 1){ //sets shoot to true every 8 main loop iterations (1 1/3 secs) and if a random number falls below a decided limit and the alien is alive
josh_ohara 43:1ac200335a68 91 int r_ = rand()%20; //test a random number so alien doesnt shoot every 8 loops
josh_ohara 38:6f50b548226e 92 if((r_ < level+1)&& //limit increases with level, so aliens will shoot more as game goes on
josh_ohara 27:eb755a345b1f 93 (_alive == true)) {
josh_ohara 27:eb755a345b1f 94 _shoot = true;
josh_ohara 19:43de9fd725ba 95 }
josh_ohara 18:828e9f6ddfdb 96 }
josh_ohara 18:828e9f6ddfdb 97 }
josh_ohara 18:828e9f6ddfdb 98
josh_ohara 27:eb755a345b1f 99 vector<AlienBullet> Alien::get_bullet_vector()
josh_ohara 27:eb755a345b1f 100 {
josh_ohara 27:eb755a345b1f 101 vector<AlienBullet> v = _alien_bullet_vector.get_vector();
josh_ohara 28:32763617ec5f 102 return v; //return the vector of alien bullets
josh_ohara 20:0b6f1cfc5be6 103 }
josh_ohara 20:0b6f1cfc5be6 104
josh_ohara 27:eb755a345b1f 105 void Alien::set_bullet_hit(int i, bool hit)
josh_ohara 27:eb755a345b1f 106 {
josh_ohara 28:32763617ec5f 107 _alien_bullet_vector.set_hit(i,hit); //sets the hit value of bullet i in this aliens bullet vector
josh_ohara 20:0b6f1cfc5be6 108 }
josh_ohara 33:d8284dee58db 109
josh_ohara 43:1ac200335a68 110 void Alien::create_powerup_flag_set()
josh_ohara 33:d8284dee58db 111 {
josh_ohara 43:1ac200335a68 112 if(_alive == false){ //check to see if ship has died
josh_ohara 43:1ac200335a68 113 _powerup = false; //set powerup flag to false as we only want to create 1 powerup per alien when alien dies
josh_ohara 43:1ac200335a68 114 int rand_ = rand()%6; //test a random number to set create powerup flag, as we dont want every alien death to yield a powerup drop
josh_ohara 35:517b56b010df 115 if(rand_ == 0){
josh_ohara 35:517b56b010df 116 _create_powerup = true;
josh_ohara 35:517b56b010df 117 }
josh_ohara 33:d8284dee58db 118 }
josh_ohara 33:d8284dee58db 119 }
josh_ohara 35:517b56b010df 120
josh_ohara 35:517b56b010df 121 Vector2D Alien::get_powerup_position()
josh_ohara 35:517b56b010df 122 {
josh_ohara 35:517b56b010df 123 Vector2D v;
josh_ohara 43:1ac200335a68 124 if(_create_powerup == true){ //returns powerup position if powerup has been created
josh_ohara 43:1ac200335a68 125 v = _powerup_vector[0].get_position(); //we only need to check 0th powerup in vector as only one can be added to vector per alien
josh_ohara 35:517b56b010df 126 }
josh_ohara 35:517b56b010df 127 return v;
josh_ohara 35:517b56b010df 128 }
josh_ohara 35:517b56b010df 129
josh_ohara 35:517b56b010df 130 void Alien::set_powerup_hit(bool x)
josh_ohara 35:517b56b010df 131 {
josh_ohara 43:1ac200335a68 132 if(_create_powerup == true){ //sets powerup hit if powerup has been created
josh_ohara 43:1ac200335a68 133 _powerup_vector[0].set_hit(x); //we only need to check 0th powerup in vector as only one can be added to vector per alien
josh_ohara 35:517b56b010df 134 }
josh_ohara 35:517b56b010df 135 }
josh_ohara 35:517b56b010df 136
josh_ohara 35:517b56b010df 137 bool Alien::get_powerup_hit()
josh_ohara 35:517b56b010df 138 {
josh_ohara 35:517b56b010df 139 bool x;
josh_ohara 43:1ac200335a68 140 if(_create_powerup == true){ //returns powerup hit value if powerup has been created
josh_ohara 43:1ac200335a68 141 x = _powerup_vector[0].get_hit(); //we only need to check 0th powerup in vector as only one can be added to vector per alien
josh_ohara 35:517b56b010df 142 }
josh_ohara 35:517b56b010df 143 return x;
josh_ohara 35:517b56b010df 144 }
josh_ohara 35:517b56b010df 145