Ikenna Adrian Ozoemena 201157039
Dependencies: mbed
Enemy/Enemy.cpp
- Committer:
- ikenna1
- Date:
- 2019-05-04
- Revision:
- 37:8d8c8cce0bc7
- Parent:
- 35:3341f2bd0408
- Child:
- 38:4571537238ed
File content as of revision 37:8d8c8cce0bc7:
#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 shno) { // initialize seeker _seeker_xpos = rand_no(68); _seeker_ypos = 0; _shno = shno; // initialize shooter _vx1 = 1; _vx2 = 2; _vx3 = 3; _vy1 = 1; _vy2 = 2; _vy3 = 1; // for now just starting them spaced apart add randomizer later if(_shno == 1) { _shooter_xpos1 = rand_no(68); _shooter_ypos1 = 1; } if(_shno == 2) { _shooter_xpos1 = rand_no(68); _shooter_ypos1 = 1; _shooter_xpos2 = rand_no(68); _shooter_ypos2 = 1; } if(_shno == 3) { _shooter_xpos1 = rand_no(68); _shooter_ypos1 = 1; _shooter_xpos2 = rand_no(68); _shooter_ypos2 = 1; _shooter_xpos3 = rand_no(68); _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) { if(_shno == 1) { lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter); } if(_shno == 2) { lcd.drawSprite(_shooter_xpos1,_shooter_ypos1,10,11,(int *)shooter); lcd.drawSprite(_shooter_xpos2,_shooter_ypos2,10,11,(int *)shooter); } if(_shno == 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("_shno = %d, shooter_xpos1 = %d, shooter_ypos1 = %d/n",_shno,_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 if(_shno == 1){ _shooter_xpos1 = _shooter_xpos1 + _vx1; _shooter_ypos1 = _shooter_ypos1 + _vy1; } if(_shno == 2){ _shooter_xpos1 = _shooter_xpos1 + _vx1; _shooter_ypos1 = _shooter_ypos1 + _vy1; _shooter_xpos2 = _shooter_xpos2 + _vx2; _shooter_ypos2 = _shooter_ypos2 + _vy2; } if(_shno == 3){ _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) { if(_shno == 1) { draw_shw1(lcd,pad); } if(_shno == 2) { draw_shw1(lcd,pad); draw_shw2(lcd,pad); } if(_shno == 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_shwpos(int shno) { if(shno == 1){ Vector2D pos = {_shwx1,_shwy1}; return pos; } if(shno == 2){ Vector2D pos = {_shwx2,_shwy2}; return pos; } if(shno == 3){ Vector2D pos = {_shwx3,_shwy3}; return pos; } } /* void Weapons::set_pos(int xpos, int ypos) { _x = xpos; _y = ypos; }*/ void Enemy::reset_seeker() { _seeker_xpos = rand_no(68); _seeker_ypos = 0; } void Enemy::reset_shooter(int shooter) { if(shooter == 1){ _shooter_xpos1 = rand_no(68); _shooter_ypos1 = 5; } if(shooter == 2){ _shooter_xpos2 = rand_no(68); _shooter_ypos2 = 5; } if(shooter == 3){ _shooter_xpos3 = rand_no(68); _shooter_ypos3 = 5; } } Vector2D Enemy::get_seekerpos() { Vector2D seeker_pos = {_seeker_xpos,_seeker_ypos}; return seeker_pos; } Vector2D Enemy::get_shooterpos(int shno) { if(shno == 1){ Vector2D shooter_pos = {_shooter_xpos1,_shooter_ypos1}; return shooter_pos; } if(shno == 2){ Vector2D shooter_pos = {_shooter_xpos2,_shooter_ypos2}; return shooter_pos; } if(shno == 3){ Vector2D shooter_pos = {_shooter_xpos3,_shooter_ypos3}; return shooter_pos; } } int Enemy::rand_no(int scale) { srand(time(NULL)); int rand_no = (rand() %scale) + 1; // printf("random no = %d\n",rand_no); return rand_no; }