Ben Evans / Mbed 2 deprecated Defender_Game

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Alien.cpp Source File

Alien.cpp

00001 #include "Alien.h"
00002 
00003 // Constant Definitions
00004 #define SPRITE_X_LENGTH 7
00005 #define SPRITE_Y_LENGTH 6
00006 
00007 const int k_alien_sprite[6][7] = {
00008     { 0,0,1,1,1,1,0,},
00009     { 0,1,0,1,1,0,1,},
00010     { 0,1,0,1,1,0,1,},
00011     { 0,0,1,1,1,1,0,},
00012     { 0,1,0,1,0,1,0,},
00013     { 1,0,0,1,0,0,1,},
00014 };
00015 
00016 Alien::Alien() {
00017 
00018 }
00019  
00020 Alien::~Alien() {
00021     
00022 }
00023 
00024 void Alien::init(Gamepad &pad, int position_x_start, int position_y_start) {
00025     // Assign values to variables
00026     sprite_x_length = SPRITE_X_LENGTH;
00027     sprite_y_length = SPRITE_Y_LENGTH;
00028     position_x_ = position_x_start;
00029     position_y_ = position_y_start;
00030     alien_move_counter_ = 0;
00031     srand(pad.read_adc()*64000);
00032     random_move_counter_ = 0;
00033     random_direction_ = 0;
00034     direction_ = rand()%2;
00035     collision_people_element_ = -1; 
00036     track_flag_ = false;
00037 }
00038  
00039 void Alien::draw_alien(N5110 &lcd,Vector2D spaceship_pos,Direction d_, 
00040 int map_length_, int position_x_map_, bool alien_collision) {
00041     // Moves alien with map movement 
00042     position_x_ += calc_sprite_movement(d_);
00043     
00044     // Moves alien at half speed of spaceship 
00045     if (alien_move_counter_%2 == 0) {
00046         
00047         // Alien tracks spaceship if abducted people
00048         if (track_flag_) {
00049             move_hunt_mode(spaceship_pos);
00050             
00051             // printf("tack_flag =  %d\n", track_flag_);
00052             // printf("pos_x = %d : pos y = %d\n",position_x_, position_y_);
00053             
00054         // If no abduction alien moves randomly 
00055         }else if (random_move_counter_ == 0 || alien_collision) {
00056             
00057             // Move alien to top of screen if a collision with person
00058             if (alien_collision) {
00059                 random_direction_ = 6;
00060                   
00061                 random_move_counter_ = 42;
00062                 alien_collision = false;
00063             }else{
00064                 set_random_move();
00065             }
00066             
00067         }
00068         // Stop alien going of map
00069         off_screen_x_y_checker(map_length_, position_x_map_); 
00070         
00071         // Move alien not tracking spaceship move randomly 
00072         if (!track_flag_) {
00073             move_direction();  
00074         }
00075         
00076         
00077         
00078         random_move_counter_ --; 
00079         alien_fire_counter_++;  
00080     }
00081     alien_move_counter_++;
00082     lcd.drawSprite(position_x_, position_y_, SPRITE_Y_LENGTH, SPRITE_X_LENGTH, 
00083     (int*)k_alien_sprite);
00084 }
00085 
00086 void Alien::off_screen_x_y_checker(int map_length_, int position_x_map_) {
00087     // Checks y position of alien and then alters alien movement direction if at 
00088     // edge  of map
00089     if (position_y_ < 9) {
00090         // Sets alien direction to one that increases y value 
00091         if (direction_) {
00092             random_direction_ = 1;
00093         }else{
00094             random_direction_ = 4;
00095         }
00096         
00097     }else if (position_y_ > 42) {
00098         // Sets alien direction to one that decrees y value 
00099         if (direction_) {
00100             random_direction_ = 2;
00101         }else{
00102             random_direction_ = 5;
00103         }
00104     } 
00105     
00106     // Loops the alien round if it reaches the end of the map 
00107     if (position_x_ <= (84 - map_length_)) {
00108        position_x_ += map_length_; 
00109     }else if (position_x_ >= map_length_) {
00110         position_x_ -= map_length_ + 10;
00111     }   
00112     
00113     // Debug and check variables when defined  
00114     #ifdef CALCULATE_ALIEN_MOVEMENT_DEBUG   
00115         printf("\nposition_x_map_ =  %d\n", position_x_map_);
00116         printf("map_length_  =  %d\n", map_length_ );
00117         printf("map_length_ + position_x_map_ =  %d\n", map_length_ + 
00118         position_x_map_);
00119     #endif
00120 }   
00121 
00122 void Alien::move_hunt_mode(Vector2D spaceship_pos) {
00123     if (spaceship_pos.x < position_x_) {
00124         position_x_ --;
00125     }else if (spaceship_pos.x > position_x_) {
00126         position_x_ ++;
00127     }
00128     
00129     if (spaceship_pos.y > position_y_) {
00130         position_y_ ++;
00131     }else if (spaceship_pos.y < position_y_) {
00132         position_y_ --;
00133     }
00134 }
00135 
00136 int Alien::get_alien_fire_counter() {
00137     return alien_move_counter_;
00138 } 
00139 
00140 void Alien::set_alien_x_pos(int position_x) {
00141     position_x_ = position_x;
00142 }
00143 
00144 void Alien::set_collision_people_element(int people_element) {
00145     collision_people_element_ = people_element;
00146 }
00147 
00148 int Alien::get_collision_people_element() {
00149     return collision_people_element_;
00150 } 
00151 
00152 void Alien::set_track_flag(bool track_flag) {
00153     track_flag_ = track_flag;
00154 }
00155 
00156 bool Alien::get_track_flag() {
00157     return track_flag_;
00158 }