Ikenna Adrian Ozoemena 201157039
Dependencies: mbed
Enemy/Enemy.cpp
- Committer:
- ikenna1
- Date:
- 2019-04-20
- Revision:
- 32:098fbc1222cd
- Parent:
- 31:c7bd3ed16840
- Child:
- 34:6d0786582d81
File content as of revision 32:098fbc1222cd:
#include "Enemy.h" Enemy::Enemy() { } Enemy::~Enemy() { } const int seeker[7][9] = { {1,1,0,0,0,0,0,1,1}, {1,0,1,0,0,0,1,0,1}, {1,0,0,1,1,1,0,0,1}, {0,1,0,0,0,0,0,1,0}, {0,0,1,0,0,0,1,0,0}, {0,0,1,1,0,1,1,0,0}, {0,0,0,0,1,0,0,0,0}, }; const int shooter[10][11] = { {0,0,1,0,0,0,0,0,1,0,0}, {0,1,0,1,0,0,0,1,0,1,0}, {1,0,0,1,0,1,0,1,0,0,1}, {1,0,0,1,1,0,1,1,0,0,1}, {0,1,0,0,0,0,0,0,0,1,0}, {0,1,0,0,1,1,1,0,0,1,0}, {0,0,1,0,0,1,0,0,1,0,0}, {0,0,0,1,0,0,0,1,0,0,0}, {0,0,0,0,1,0,1,0,0,0,0}, {0,0,0,0,0,1,0,0,0,0,0}, }; void Enemy::init(int seeker_xpos,int seeker_ypos,int sh_no) { // initialize seeker _seeker_xpos = seeker_xpos; _seeker_ypos = seeker_ypos; // initialize shooter _vx1 = 2; _vx2 = 2; _vx3 = 2; _vy1 = 1; _vy2 = 1; _vy3 = 1; // for now just starting them spaced apart add randomizer later if(sh_no == 1) { _shooter_xpos1 = 1; _shooter_ypos1 = 1; } if(sh_no == 2) { _shooter_xpos1 = 1; _shooter_ypos1 = 1; _shooter_xpos2 = 30; _shooter_ypos2 = 1; } if(sh_no == 3) { _shooter_xpos1 = 1; _shooter_ypos1 = 1; _shooter_xpos2 = 30; _shooter_ypos2 = 1; _shooter_xpos3 = 60; _shooter_ypos3 = 1; } } // Draw the ship ***Note: figure out how to change ship type e.g from basic to devotion void Enemy::draw_seeker(N5110 &lcd) { lcd.drawSprite(_seeker_xpos,_seeker_ypos,7,9,(int *)seeker); } void Enemy::draw_shooter(N5110 &lcd,int sh_no) { if(sh_no == 1) { lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter); } if(sh_no == 2) { lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter); lcd.drawSprite(_shooter_xpos2,_shooter_ypos2,10,11,(int *)shooter); } if(sh_no == 3) { lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter); lcd.drawSprite(_shooter_xpos2,_shooter_ypos2,10,11,(int *)shooter); lcd.drawSprite(_shooter_xpos3,_shooter_ypos3,10,11,(int *)shooter); } // printf("sh_no = %d, shooter_xpos1 = %d, shooter_ypos1 = %d/n",sh_no,_shooter_xpos1,_shooter_ypos1); } void Enemy::update_seeker(int ship_xpos, int ship_ypos) { int rangex = _seeker_xpos - ship_xpos; int rangey = _seeker_ypos - ship_ypos; // make seeker move towards ship if(rangey < 0) { if(rangex < 0) { _seeker_xpos = _seeker_xpos + 2; } else if(rangex == 0) { _seeker_xpos = _seeker_xpos; } else { _seeker_xpos = _seeker_xpos - 2; } } // reset seeker to top if(_seeker_ypos > 54) { _seeker_ypos = 0; } _seeker_ypos = _seeker_ypos + 2; // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos); } void Enemy::update_shooter(int ship_xpos, int ship_ypos) { int rangey1 = _shooter_ypos1 - ship_ypos; int rangey2 = _shooter_ypos2 - ship_ypos; int rangey3 = _shooter_ypos3 - ship_ypos; // make shooters move away from ship if(rangey1 < 0) { _shooter_ypos1 = _shooter_ypos1 - 1; } // make shooters move away from ship if(rangey2 < 0) { _shooter_ypos2 = _shooter_ypos2 - 1; } if(rangey3 < 0) { _shooter_ypos3 = _shooter_ypos3 - 1; } if(_shooter_ypos1 < 1) { _shooter_ypos1 = 1; } if(_shooter_ypos2 < 1) { _shooter_ypos2 = 1; } if(_shooter_ypos3 < 1) { _shooter_ypos3 = 1; } // make shooters move left and down _shooter_xpos1 = _shooter_xpos1 + _vx1; _shooter_ypos1 = _shooter_ypos1 + _vy1; _shooter_xpos2 = _shooter_xpos2 + _vx2; _shooter_ypos2 = _shooter_ypos2 + _vy2; _shooter_xpos3 = _shooter_xpos3 + _vx3; _shooter_ypos3 = _shooter_ypos3 + _vy3; if(_shooter_ypos1 > 38 ) { _vy1 = _vy1 * - 1; } if(_shooter_ypos1 < 1 ) { _vy1 = _vy1 * - 1; } if(_shooter_xpos1 > 41 ) { _vx1 = _vx1 * - 1; } if(_shooter_xpos1 < 1 ) { _vx1 = _vx1 * - 1; } if(_shooter_ypos2 > 38 ) { _vy2 = _vy2 * - 1; } if(_shooter_ypos2 < 1 ) { _vy2 = _vy2 * - 1; } if(_shooter_xpos2 > 84 - 8 - 11 ) { // 7 for border 11 for sprite width _vx2 = _vx2 * - 1; } if(_shooter_xpos2 < 1 ) { _vx2 = _vx2 * - 1; } if(_shooter_ypos3 > 38 ) { _vy3 = _vy3 * - 1; } if(_shooter_ypos3 < 1 ) { _vy3 = _vy3 * - 1; } if(_shooter_xpos3 > 84 - 8 - 11 ) { _vx3 = _vx3 * - 1; } if(_shooter_xpos3 < 1 ) { _vx3 = _vx3 * - 1; } // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos); printf("_shooter_xpos1 , _shooter_ypos1 = %d, %d\n",_shooter_ypos1,_shooter_xpos1); } void Enemy::draw_shw1(N5110 &lcd,Gamepad &pad) { _vshw1.x = 0; //Projectile doesn't move sideways. _vshw1.y = 2; //Projectile moves upwards on screen. //resets once projectile reaches bottom of screen if(_shwy1 >= 48) { reset1 = 0; } if(reset1 == 0) { _shwx1 = _shooter_xpos1 + 5; _shwy1 = _shooter_ypos1 + 11; reset1 = reset1 + 1; } lcd.drawRect(_shwx1,_shwy1,1,1,FILL_BLACK); // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset); } void Enemy::draw_shw2(N5110 &lcd,Gamepad &pad) { _vshw2.x = 0; //Projectile doesn't move sideways. _vshw2.y = 5; //Projectile moves upwards on screen. //resets once projectile reaches bottom of screen if(_shwy2 >= 48) { reset2= 0; } if(reset2 == 0) { _shwx2 = _shooter_xpos2 + 5; _shwy2 = _shooter_ypos2 + 11; reset2 = reset2 + 1; } lcd.drawRect(_shwx2,_shwy2,1,1,FILL_BLACK); // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset); } void Enemy::draw_shw3(N5110 &lcd,Gamepad &pad) { _vshw3.x = 0; //Projectile doesn't move sideways. _vshw3.y = 3; //Projectile moves upwards on screen. //resets once projectile reaches bottom of screen if(_shwy3 >= 48) { reset3 = 0; } if(reset3 == 0) { _shwx3 = _shooter_xpos3 + 5; _shwy3 = _shooter_ypos3 + 11; reset3 = reset3 + 1; } lcd.drawRect(_shwx3,_shwy3,1,1,FILL_BLACK); // printf("Ship x and y pos, reset = %d , %d ,%d \n", _ship_xpos, _ship_ypos, reset); } void Enemy::draw_shw(N5110 &lcd,Gamepad &pad,int sh_no) { if(sh_no == 1) { draw_shw1(lcd,pad); } if(sh_no == 2) { draw_shw1(lcd,pad); draw_shw2(lcd,pad); } if(sh_no == 3) { draw_shw1(lcd,pad); draw_shw2(lcd,pad); draw_shw3(lcd,pad); } } void Enemy::update_shw() { _shwx1 = _shwx1 + _vshw1.x; _shwy1 = _shwy1 + _vshw1.y; _shwx2 = _shwx2 + _vshw2.x; _shwy2 = _shwy2 + _vshw2.y; _shwx3 = _shwx3 + _vshw3.x; _shwy3 = _shwy3 + _vshw3.y; } Vector2D Enemy::get_sh1pos() { Vector2D pos = {_shwx1,_shwy1}; return pos; } Vector2D Enemy::get_sh2pos() { Vector2D pos = {_shwx2,_shwy2}; return pos; } Vector2D Enemy::get_sh3pos() { Vector2D pos = {_shwx3,_shwy3}; return pos; } /* void Weapons::set_pos(int xpos, int ypos) { _x = xpos; _y = ypos; }*/ void Enemy::reset_seeker() { _seeker_xpos = 0; _seeker_ypos = 0; } Vector2D Enemy::get_seekerpos() { Vector2D seeker_pos = {_seeker_xpos,_seeker_ypos}; return seeker_pos; }