Albert Tan-Mulligan / Mbed 2 deprecated ELEC2645_Project_el18ajst

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Enemy.cpp Source File

Enemy.cpp

00001 #include "Enemy.h"
00002 #include <Bitmap.h>
00003 Serial pce(USBTX, USBRX);
00004 //Constructor with seed, to spawn enemies at random places on edge of screen
00005 Enemy::Enemy(int seed)
00006 {
00007     srand(seed);
00008     _four = (rand()%4)+1;
00009     //pce.printf("%d",_four);
00010     if(_four == 1){
00011         _x = 0;
00012         _y = rand()%48;
00013         }
00014     else if(_four == 2){
00015         _x = 84;
00016         _y = rand()%48;
00017         }
00018     else if(_four == 3){
00019         _y = 0;
00020         _x = rand()%84;
00021         }
00022     else if(_four == 4){
00023         _y = 48;
00024         _x = rand()%84;
00025         }
00026 }
00027 
00028 Enemy::~Enemy()
00029 {
00030 
00031 }
00032 void Enemy::draw(N5110 &lcd)
00033 {
00034     lcd.drawRect(_x,_y,3,3,FILL_TRANSPARENT);
00035 }
00036 
00037 //updates enemy with player x and y so it can walk towards the player
00038 void Enemy::update(int player_x, int player_y)
00039 {
00040     if(_x<player_x){
00041         _x++;
00042         }
00043     else if(_x > player_x){
00044         _x--;
00045         } 
00046         
00047     if(_y < player_y){
00048         _y++;
00049         }
00050     else if (_y > player_y){
00051         _y--;
00052         }
00053 
00054 }
00055 //accessor functions
00056 int Enemy::get_x()
00057 {   
00058     return _x;
00059     
00060 }
00061 int Enemy::get_y()
00062 {   
00063     return _y;
00064     
00065 }
00066 //resets enemies each time it gets shot
00067 void Enemy::reset(int seed, N5110 &lcd){
00068     death_animation(lcd);
00069     srand(seed);
00070     _four = (rand()%4)+1;
00071     //pce.printf("%d", _four);
00072     if(_four == 1){
00073         _x = 0;
00074         _y = rand()%48;
00075         }
00076     else if(_four == 2){
00077         _x = 84;
00078         _y = rand()%48;
00079         }
00080     else if(_four == 3){
00081         _y = 0;
00082         _x = rand()%84;
00083         }
00084     else if(_four == 4){
00085         _y = 48;
00086         _x = rand()%84;
00087         } 
00088 }
00089 //Eenmy Death Animation
00090 void Enemy::death_animation(N5110 &lcd){
00091     int sprite_data[] = {
00092     1,0,1,0,1,
00093     0,1,1,1,0,
00094     1,1,0,1,1,
00095     0,1,1,1,0,
00096     1,0,1,0,1
00097     };
00098     Bitmap death(sprite_data, 5, 5);
00099     //Render Bitmap at x & y
00100     death.render(lcd, _x - 1, _y - 1);
00101     lcd.refresh();
00102     }