Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

People/People.cpp

Committer:
evanso
Date:
2020-05-15
Revision:
34:85ccc16f24d2
Parent:
33:7fedd8029473
Child:
35:577c65bf914e

File content as of revision 34:85ccc16f24d2:

#include "People.h"

const int k_people_sprite[5][4] = {
    { 0,1,1,0,},
    { 1,1,1,1,},
    { 1,1,1,1,},
    { 0,1,1,0,},
    { 0,1,1,0,},
};

People::People() {

}
 
People::~People() {
    
}

void People::init(Gamepad &pad, int position_x_start) {
    position_x_ = position_x_start;
    position_y_ = 43;
    alien_collision_flag = false;
    people_move_counter_ = 0;
    random_move_counter_ = 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_ --;     
        }
    }
    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;
}