Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers People.cpp Source File

People.cpp

00001 #include "People.h"
00002 
00003 // Constant Definitions
00004 #define SPRITE_X_LENGTH 4
00005 #define SPRITE_Y_LENGTH 5
00006 #define STARTING_Y_POS 42
00007 
00008 const int k_people_sprite[SPRITE_Y_LENGTH][SPRITE_X_LENGTH] = {
00009     { 0,1,1,0,},
00010     { 1,1,1,1,},
00011     { 1,1,1,1,},
00012     { 0,1,1,0,},
00013     { 0,1,1,0,},
00014 };
00015 
00016 People::People() {
00017 
00018 }
00019  
00020 People::~People() {
00021     
00022 }
00023 
00024 void People::init(Gamepad &pad, int position_x_start) {
00025     // Define variables
00026     sprite_x_length = SPRITE_X_LENGTH;
00027     sprite_y_length = SPRITE_Y_LENGTH;
00028     position_x_ = position_x_start;
00029     position_y_ = STARTING_Y_POS;
00030     alien_collision_flag = false;
00031     people_move_counter_ = 0;
00032     random_move_counter_ = 0;
00033 }
00034 
00035 void People::draw_people(N5110 &lcd, Direction d_, int map_length_, 
00036 int position_x_map_) {
00037     position_x_ += calc_sprite_movement(d_);
00038     lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH, SPRITE_X_LENGTH, 
00039     (int*)k_people_sprite);
00040     off_screen_x_y_checker(map_length_, position_x_map_);
00041     
00042     // Move alien to top of screen if collision with alien
00043     collision_with_alien();
00044     
00045     people_move_counter_++;
00046 } 
00047 
00048 void People::collision_with_alien() {
00049 if(alien_collision_flag && !alien_track) {
00050     // People move on there own but at half speed of spaceship 
00051     if (people_move_counter_%2 == 0) {
00052         if (random_move_counter_ == 0) {
00053             // Move alien to top of screen
00054             random_direction_ = 6;   
00055             random_move_counter_ = 43;
00056         }
00057         
00058     // Move people
00059     move_direction();
00060     random_move_counter_ --;     
00061     }
00062 }    
00063 }
00064 
00065 void People::off_screen_x_y_checker(int map_length_, int position_x_map_) {
00066     // Loops the people round if it reaches the end of the map 
00067     if(position_x_ <= (84 - map_length_)) {
00068        position_x_ += map_length_; 
00069     }else if (position_x_ >= map_length_) {
00070         position_x_ -= map_length_ + 10;
00071     }   
00072 }   
00073 
00074 bool People::get_alien_collision_flag() {
00075     return alien_collision_flag;
00076 }