Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

People/People.cpp

Committer:
evanso
Date:
2020-05-15
Revision:
36:27aa597db3d2
Parent:
35:577c65bf914e
Child:
40:71f947254fda

File content as of revision 36:27aa597db3d2:

#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) {
    // Define variables
    sprite_x_length = 4;
    sprite_y_length = 5;
    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
    collision_with_alien();
    
    people_move_counter_++;
} 

void People::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 poeple
    move_direction();
    random_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::get_alien_collision_flag(){
    return alien_collision_flag;
}