Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Revision:
34:85ccc16f24d2
Parent:
33:7fedd8029473
Child:
35:577c65bf914e
--- a/People/People.cpp	Thu May 14 22:41:16 2020 +0000
+++ b/People/People.cpp	Fri May 15 12:05:33 2020 +0000
@@ -19,21 +19,82 @@
 void People::init(Gamepad &pad, int position_x_start) {
     position_x_ = position_x_start;
     position_y_ = 43;
-}
-
-void People::draw_people(N5110 &lcd, Direction d_){
-    position_x_ += calc_people_movement(d_);
-    lcd.drawSprite(position_x_, position_y_, 5, 4, (int*)k_people_sprite);
+    alien_collision_flag = false;
+    people_move_counter_ = 0;
+    random_move_counter_ = 0;
 }
 
-int People::calc_people_movement(Direction d_){  
-    // moves the people in oposite direction to spaceship when it's position is 
-    // at min and max x positions and joystick has direction     
-    if (d_ == W || d_ == NW || d_ == SW){ 
-        return 2;
-    }else if (d_ == E || d_ == NE || d_ == SE){ 
-        return -2; 
-    }else {
-        return 0;
+void People::draw_people(N5110 &lcd, Direction d_, int map_length_, 
+int position_x_map_){
+    position_x_ += calc_sprite_movement(d_);
+    lcd.drawSprite(position_x_, position_y_, 5, 4, (int*)k_people_sprite);
+    off_screen_x_y_checker(map_length_, position_x_map_);
+    
+    // move alien to top of screen if collision with alien
+    if(alien_collision_flag){
+        //people moves on its own but at half speed of spaceship 
+        if (people_move_counter_%2 == 0) {
+            if (random_move_counter_ == 0){
+                //move alien to top of screen
+                random_direction_ = 6;   
+                random_move_counter_ = 43;
+            }
+            move_direction();
+            off_screen_x_y_checker(map_length_, position_x_map_);
+            random_move_counter_ --;     
+        }
     }
-}  
\ No newline at end of file
+    people_move_counter_++;
+} 
+
+void People::off_screen_x_y_checker(int map_length_, int position_x_map_){
+    // loops the people round if it reaches the end of the map 
+    if(position_x_ <= (84 - map_length_)){
+       position_x_ += map_length_; 
+    }else if (position_x_ >= map_length_){
+        position_x_ -= map_length_ + 10;
+    }   
+}   
+
+bool People::check_alien_collision(Alien alien) {
+    bool collision = false; 
+    Vector2D alien_pos = alien.get_pos();
+    
+    // checks collision of top left corner of alien with people     
+    if(alien_pos.x >= position_x_ &&
+    alien_pos.x <= (position_x_ + 4) && 
+    position_y_ <= alien_pos.y && 
+    alien_pos.y <= (position_y_ + 5)){
+        collision = true;
+        alien_collision_flag = true;
+        
+    // checks collision of top right corner of alien with people   
+    }else if((alien_pos.x + 7) >= position_x_ &&
+    (alien_pos.x + 7) <= (position_x_ + 4) && 
+    position_y_ <= alien_pos.y && 
+    alien_pos.y <= (position_y_ + 5)){
+        collision = true;
+        alien_collision_flag = true;
+        
+    // checks collision of bottom left corner of alien with people   
+    }else if(alien_pos.x >= position_x_ &&
+    alien_pos.x <= (position_x_ + 4) && 
+    position_y_ <= (alien_pos.y + 6)&& 
+   (alien_pos.y + 6) <= (position_y_ + 5)){
+        collision = true;
+        alien_collision_flag = true;
+        
+    // checks collision of bottom right corner of alien with people    
+    }else if((alien_pos.x + 7) >= position_x_ &&
+    (alien_pos.x + 7) <= (position_x_ + 4) && 
+    position_y_ <= (alien_pos.y + 6) && 
+    (alien_pos.y + 6) <= (position_y_ + 5)){
+        collision = true;
+        alien_collision_flag = true;
+    } 
+    return collision;
+}
+
+bool People::get_alien_collision_flag(){
+    return alien_collision_flag;
+}
\ No newline at end of file