
Joshua O'hara 201291390
Dependencies: mbed
Alien/Alien.cpp@38:6f50b548226e, 2020-05-25 (annotated)
- Committer:
- josh_ohara
- Date:
- Mon May 25 15:25:14 2020 +0000
- Revision:
- 38:6f50b548226e
- Parent:
- 37:90a0671d2ba7
- Child:
- 43:1ac200335a68
Safety commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
josh_ohara | 9:8e695df3cc36 | 1 | #include "Alien.h" |
josh_ohara | 6:5bea67cc96f9 | 2 | |
josh_ohara | 6:5bea67cc96f9 | 3 | |
josh_ohara | 24:ff5af5a013b5 | 4 | void Alien::init(int x, int y, int size, int level) { |
josh_ohara | 33:d8284dee58db | 5 | _alive = true; |
josh_ohara | 33:d8284dee58db | 6 | _shoot = false; |
josh_ohara | 33:d8284dee58db | 7 | _powerup = true; |
josh_ohara | 35:517b56b010df | 8 | _create_powerup = false; |
josh_ohara | 33:d8284dee58db | 9 | _x = x; |
josh_ohara | 33:d8284dee58db | 10 | _y = y; |
josh_ohara | 33:d8284dee58db | 11 | _size = size; |
josh_ohara | 33:d8284dee58db | 12 | _speed = 1 + 0.3 * level; |
josh_ohara | 35:517b56b010df | 13 | _alien_bullet_vector.init(); |
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 | 37:90a0671d2ba7 | 21 | lcd.drawLine(_x,_y+1,_x+_size-1,_y+1,1); |
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 | 35:517b56b010df | 27 | if(_create_powerup == true){ |
josh_ohara | 37:90a0671d2ba7 | 28 | _powerup_vector[0].render(lcd); |
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 | 33:d8284dee58db | 35 | Vector2D p = {_x,_y}; |
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 | 33:d8284dee58db | 41 | _x+=_speed; |
josh_ohara | 33:d8284dee58db | 42 | int counter_ = counter; |
josh_ohara | 28:32763617ec5f | 43 | int step_x_ = step_x; |
josh_ohara | 28:32763617ec5f | 44 | int remainder_x_ = remainder_x; |
josh_ohara | 33:d8284dee58db | 45 | if (_x < 1 + remainder_x_*step_x_) { |
josh_ohara | 33:d8284dee58db | 46 | _x = 1 + remainder_x_*step_x_; |
josh_ohara | 27:eb755a345b1f | 47 | _speed = -_speed; |
josh_ohara | 28:32763617ec5f | 48 | _y = _y + 2; |
josh_ohara | 9:8e695df3cc36 | 49 | } |
josh_ohara | 33:d8284dee58db | 50 | if (_x > WIDTH - _size - 1 - (4-remainder_x_)*step_x_) { |
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 | 33:d8284dee58db | 55 | shoot_flag_set(counter_,level); //runs flag set function |
josh_ohara | 33:d8284dee58db | 56 | _alien_bullet_vector.update(_x,_y,_shoot); |
josh_ohara | 35:517b56b010df | 57 | _shoot = false; |
josh_ohara | 35:517b56b010df | 58 | |
josh_ohara | 35:517b56b010df | 59 | if(_powerup == true){ |
josh_ohara | 35:517b56b010df | 60 | powerup_flag_set(); |
josh_ohara | 35:517b56b010df | 61 | if(_create_powerup == true){ |
josh_ohara | 35:517b56b010df | 62 | PowerUp new_powerup_; |
josh_ohara | 35:517b56b010df | 63 | new_powerup_.init(_x,_y,3); |
josh_ohara | 35:517b56b010df | 64 | _powerup_vector.push_back(new_powerup_); |
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 | 33:d8284dee58db | 69 | if(_create_powerup == true){ |
josh_ohara | 35:517b56b010df | 70 | _powerup_vector[0].update(); |
josh_ohara | 36:78efa0e7bd31 | 71 | //printf("powerup update"); |
josh_ohara | 33:d8284dee58db | 72 | } |
josh_ohara | 35:517b56b010df | 73 | |
josh_ohara | 35:517b56b010df | 74 | |
josh_ohara | 24:ff5af5a013b5 | 75 | //printf(" speed = %2d ",Speed); |
josh_ohara | 9:8e695df3cc36 | 76 | } |
josh_ohara | 6:5bea67cc96f9 | 77 | |
josh_ohara | 27:eb755a345b1f | 78 | void Alien::set_life(bool x) |
josh_ohara | 27:eb755a345b1f | 79 | { |
josh_ohara | 28:32763617ec5f | 80 | _alive = x; //sets the life of the alien |
josh_ohara | 15:dde4ce4bf7fe | 81 | } |
josh_ohara | 10:9189419fda68 | 82 | |
josh_ohara | 27:eb755a345b1f | 83 | bool Alien::get_life() |
josh_ohara | 27:eb755a345b1f | 84 | { |
josh_ohara | 22:3e978b1d7958 | 85 | //printf("A = %2d ",Alive); |
josh_ohara | 28:32763617ec5f | 86 | return _alive; //returns the life of the alien |
josh_ohara | 15:dde4ce4bf7fe | 87 | } |
josh_ohara | 15:dde4ce4bf7fe | 88 | |
josh_ohara | 33:d8284dee58db | 89 | void Alien::shoot_flag_set(int counter, int level) |
josh_ohara | 27:eb755a345b1f | 90 | { |
josh_ohara | 27:eb755a345b1f | 91 | int counter_ = counter; |
josh_ohara | 28:32763617ec5f | 92 | if(counter_%8 == 1){ //sets shoot to true every 8 main loop iterations and if a random number falls below a decided limit and the alien is alive |
josh_ohara | 33:d8284dee58db | 93 | int r_ = rand()%20; |
josh_ohara | 38:6f50b548226e | 94 | if((r_ < level+1)&& //limit increases with level, so aliens will shoot more as game goes on |
josh_ohara | 27:eb755a345b1f | 95 | (_alive == true)) { |
josh_ohara | 27:eb755a345b1f | 96 | _shoot = true; |
josh_ohara | 19:43de9fd725ba | 97 | } |
josh_ohara | 18:828e9f6ddfdb | 98 | } |
josh_ohara | 18:828e9f6ddfdb | 99 | } |
josh_ohara | 18:828e9f6ddfdb | 100 | |
josh_ohara | 27:eb755a345b1f | 101 | vector<AlienBullet> Alien::get_bullet_vector() |
josh_ohara | 27:eb755a345b1f | 102 | { |
josh_ohara | 27:eb755a345b1f | 103 | vector<AlienBullet> v = _alien_bullet_vector.get_vector(); |
josh_ohara | 28:32763617ec5f | 104 | return v; //return the vector of alien bullets |
josh_ohara | 20:0b6f1cfc5be6 | 105 | } |
josh_ohara | 20:0b6f1cfc5be6 | 106 | |
josh_ohara | 27:eb755a345b1f | 107 | void Alien::set_bullet_hit(int i, bool hit) |
josh_ohara | 27:eb755a345b1f | 108 | { |
josh_ohara | 28:32763617ec5f | 109 | _alien_bullet_vector.set_hit(i,hit); //sets the hit value of bullet i in this aliens bullet vector |
josh_ohara | 20:0b6f1cfc5be6 | 110 | } |
josh_ohara | 33:d8284dee58db | 111 | |
josh_ohara | 33:d8284dee58db | 112 | void Alien::powerup_flag_set() |
josh_ohara | 33:d8284dee58db | 113 | { |
josh_ohara | 35:517b56b010df | 114 | if(_alive == false){ |
josh_ohara | 34:853f0cf0ce03 | 115 | _powerup = false; |
josh_ohara | 36:78efa0e7bd31 | 116 | int rand_ = rand()%6; |
josh_ohara | 35:517b56b010df | 117 | if(rand_ == 0){ |
josh_ohara | 35:517b56b010df | 118 | _create_powerup = true; |
josh_ohara | 35:517b56b010df | 119 | } |
josh_ohara | 33:d8284dee58db | 120 | } |
josh_ohara | 33:d8284dee58db | 121 | } |
josh_ohara | 35:517b56b010df | 122 | |
josh_ohara | 35:517b56b010df | 123 | Vector2D Alien::get_powerup_position() |
josh_ohara | 35:517b56b010df | 124 | { |
josh_ohara | 35:517b56b010df | 125 | Vector2D v; |
josh_ohara | 35:517b56b010df | 126 | if(_create_powerup == true){ |
josh_ohara | 35:517b56b010df | 127 | v = _powerup_vector[0].get_position(); |
josh_ohara | 35:517b56b010df | 128 | } |
josh_ohara | 35:517b56b010df | 129 | return v; |
josh_ohara | 35:517b56b010df | 130 | } |
josh_ohara | 35:517b56b010df | 131 | |
josh_ohara | 35:517b56b010df | 132 | void Alien::set_powerup_hit(bool x) |
josh_ohara | 35:517b56b010df | 133 | { |
josh_ohara | 35:517b56b010df | 134 | if(_create_powerup == true){ |
josh_ohara | 35:517b56b010df | 135 | _powerup_vector[0].set_hit(x); |
josh_ohara | 35:517b56b010df | 136 | } |
josh_ohara | 35:517b56b010df | 137 | } |
josh_ohara | 35:517b56b010df | 138 | |
josh_ohara | 35:517b56b010df | 139 | bool Alien::get_powerup_hit() |
josh_ohara | 35:517b56b010df | 140 | { |
josh_ohara | 35:517b56b010df | 141 | bool x; |
josh_ohara | 35:517b56b010df | 142 | if(_create_powerup == true){ |
josh_ohara | 35:517b56b010df | 143 | x = _powerup_vector[0].get_hit(); |
josh_ohara | 35:517b56b010df | 144 | } |
josh_ohara | 35:517b56b010df | 145 | return x; |
josh_ohara | 35:517b56b010df | 146 | } |
josh_ohara | 35:517b56b010df | 147 |