Ikenna Adrian Ozoemena 201157039

Dependencies:   mbed

Enemy/Enemy.cpp

Committer:
ikenna1
Date:
2019-05-07
Revision:
40:90c7a893d513
Parent:
39:7824f9080f59
Child:
41:e1fa36c0492e

File content as of revision 40:90c7a893d513:

#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, int seno)
{
    // initialize seeker
    _seeker1_xpos = rand_no(68);
    _seeker1_ypos = 0;
    _seeker2_xpos = rand_no(68);
    _seeker2_ypos = 0;
    _seeker3_xpos = rand_no(68);
    _seeker3_ypos = 0;
    _seno = seno;
    _shno = shno;
    // initialize shooter
    _vx1 = 1;
    _vx2 = 1;
    _vx3 = 1;
    _vy1 = 1;
    _vy2 = 1;
    _vy3 = 1;
    // for now just starting them spaced apart add randomizer later

    _shooter_xpos1 = rand_no(68);
    _shooter_ypos1 = 1;
    _shooter_xpos2 = rand_no(68);
    _shooter_ypos2 = 1;
    _shooter_xpos3 = rand_no(68);
    _shooter_ypos3 = 1;

}
void Enemy::set_noshooters(int no_shooters)
{
    _shno = no_shooters;
}
void Enemy::set_noseekers(int no_seekers)
{
    _seno = no_seekers;
}
// Draw the ship ***Note: figure out how to change ship type e.g from basic to devotion
void Enemy::draw_seeker(N5110 &lcd)
{
    if(_seno == 1) {
        lcd.drawSprite(_seeker1_xpos,_seeker1_ypos,7,9,(int *)seeker);
    }
    if(_seno == 2) {
        lcd.drawSprite(_seeker1_xpos,_seeker1_ypos,7,9,(int *)seeker);
        lcd.drawSprite(_seeker2_xpos,_seeker2_ypos,7,9,(int *)seeker);
    }
    if(_seno == 3) {
        lcd.drawSprite(_seeker1_xpos,_seeker1_ypos,7,9,(int *)seeker);
        lcd.drawSprite(_seeker2_xpos,_seeker2_ypos,7,9,(int *)seeker);
        lcd.drawSprite(_seeker3_xpos,_seeker3_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)
{
    if(_seno == 1) {
        update_seeker1(ship_xpos,ship_ypos);
    }
    if(_seno == 2) {
        update_seeker1(ship_xpos,ship_ypos);
        update_seeker2(ship_xpos,ship_ypos);
    }
    if(_seno == 3) {
        update_seeker1(ship_xpos,ship_ypos);
        update_seeker2(ship_xpos,ship_ypos);
        update_seeker3(ship_xpos,ship_ypos);
    }
    //printf("seno = %d\n",_seno);
}
void Enemy::update_seeker1(int ship_xpos, int ship_ypos)
{
    int rangex = _seeker1_xpos - ship_xpos;
    int rangey = _seeker1_ypos - ship_ypos;

    // make seeker move towards ship
    if(rangey < 0) {
        if(rangex < 0) {
            _seeker1_xpos = _seeker1_xpos + 2;
        } else if(rangex == 0) {
            _seeker1_xpos = _seeker1_xpos;
        } else {
            _seeker1_xpos = _seeker1_xpos - 2;
        }
    }

    // reset seeker to top
    if(_seeker1_ypos > 54) {
        reset_seeker(1);
    }
    _seeker1_ypos = _seeker1_ypos + 2;
    // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos);
}
void Enemy::update_seeker2(int ship_xpos, int ship_ypos)
{
    int rangex = _seeker2_xpos - ship_xpos;
    int rangey = _seeker2_ypos - ship_ypos;

    // make seeker move towards ship
    if(rangey < 0) {
        if(rangex < 0) {
            _seeker2_xpos = _seeker2_xpos + 2;
        } else if(rangex == 0) {
            _seeker2_xpos = _seeker2_xpos;
        } else {
            _seeker2_xpos = _seeker2_xpos - 2;
        }
    }

    // reset seeker to top
    if(_seeker2_ypos > 54) {
        reset_seeker(2);
    }
    _seeker2_ypos = _seeker2_ypos + 2;
    // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos);
}
void Enemy::update_seeker3(int ship_xpos, int ship_ypos)
{
    int rangex = _seeker3_xpos - ship_xpos;
    int rangey = _seeker3_ypos - ship_ypos;

    // make seeker move towards ship
    if(rangey < 0) {
        if(rangex < 0) {
            _seeker3_xpos = _seeker3_xpos + 2;
        } else if(rangex == 0) {
            _seeker3_xpos = _seeker3_xpos;
        } else {
            _seeker3_xpos = _seeker3_xpos - 2;
        }
    }

    // reset seeker to top
    if(_seeker3_ypos > 54) {
        reset_seeker(3);
    }
    _seeker3_ypos = _seeker3_ypos + 2;
    // printf("seeker_xpos , seeker_ypos = %d, %d\n", _seeker_xpos, _seeker_ypos);
}
void Enemy::update_shooter(int ship_xpos, int ship_ypos)
{
    Vector2D shooter1pos = shooter_motion(_shooter_xpos1,_shooter_ypos1,ship_xpos,ship_ypos,_shwx1,_shwy1,_vx1,_vy1);
    Vector2D shooter2pos = shooter_motion(_shooter_xpos2,_shooter_ypos2,ship_xpos,ship_ypos,_shwx2,_shwy2,_vx2,_vy2);
    Vector2D shooter3pos = shooter_motion(_shooter_xpos3,_shooter_ypos3,ship_xpos,ship_ypos,_shwx3,_shwy3,_vx3,_vy3);
    
    _shooter_xpos1 = shooter1pos.x;
    _shooter_ypos1 = shooter1pos.y;
    _shooter_xpos2 = shooter2pos.x;
    _shooter_ypos2 = shooter2pos.y;
    _shooter_xpos3 = shooter3pos.x;
    _shooter_ypos3 = shooter3pos.y;
    //printf("_shooter_xpos1 = %d,_shooter_ypos1 = %d,projy = %d\n",_shooter_xpos1 ,_shooter_ypos1,_shwy1);
}
Vector2D Enemy::shooter_motion(int shooter_xpos,int shooter_ypos,int ship_xpos, int ship_ypos, int projx, int projy,int vx, int vy)
{
    int high_bar =10;
    int low_bar = 25;
    int rangex = shooter_xpos - ship_xpos;
    int rangey = shooter_ypos - ship_ypos;
    if(high_bar <= projy &&  projy <= low_bar){
        if(rangex > 0){
        // avoid ship
            shooter_xpos = shooter_xpos + vx;
        }
        if(rangex < 0){
            shooter_xpos = shooter_xpos - vx;
        } 
    }
    else{
        // track ship
        if(rangex > 0){
            shooter_xpos = shooter_xpos - vx;
        }
        if(rangex < 0){
            shooter_xpos = shooter_xpos + vx;
        }
    }
    if(shooter_xpos < 1) {
        shooter_xpos = 1;
    }
    if(shooter_xpos > 84 - 8 - 11) {
        shooter_xpos = 84 - 8 - 11;
    }
    //printf("_shooter_xpos1 = %d,_shooter_ypos1 = %d,projy = %d\n",_shooter_xpos1 ,_shooter_ypos1,_shwy1);
    // shooter_ypos = shooter_ypos + 1;
    return{shooter_xpos,shooter_ypos};
    
}

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(int seekno)
{
    if(seekno == 1) {
        _seeker1_xpos = rand_no(68);
        _seeker1_ypos = 0;
    }
    if(seekno == 2) {
        _seeker2_xpos = rand_no(68);
        _seeker2_ypos = 0;
    }
    if(seekno == 3) {
        _seeker3_xpos = rand_no(68);
        _seeker3_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(int seekno)
{
    if(seekno == 1) {
        Vector2D seeker_pos = {_seeker1_xpos,_seeker1_ypos};
        return seeker_pos;
    }
    if(seekno == 2) {
        Vector2D seeker_pos = {_seeker2_xpos,_seeker2_ypos};
        return seeker_pos;
    }
    if(seekno == 3) {
        Vector2D seeker_pos = {_seeker3_xpos,_seeker3_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;
}
void Enemy::sh_scaling(float time_elapsed)
{
    // add if functions for shno
    // correct movement
    int vmax = 3;
    int time = floor(time_elapsed);
    if(time%10 == 0) {
        if(_vx1 > -vmax && _vx1 < vmax) {
            _vx1 = _vx1 * 1.1;
        }
        if(_vx2 > - vmax && _vx2 < vmax) {
            _vx2 = _vx2 * 1.1;
        }
        if(_vx3 > -vmax && _vx3 < vmax) {
            _vx3 = _vx3 * 1.1;
        }
        if(_vy1 > -vmax && _vy1 < vmax) {
            _vy1 = _vy1 * 1.1;
        }
        if(_vy2 > -vmax && _vy2 < vmax) {
            _vy2 = _vy2 * 1.1;
        }
        if(_vy3 > -vmax && _vy3 < vmax) {
            _vy3 = _vy3 * 1.1;
        }
    }
    // printf("time = %d, _vx1 = %f\n",time,_vx1);
}
int Enemy::closest(int x1, int y1, int x2, int y2)
{
    int rangex = abs(x1 - x2);
    int rangey = abs(y1 - y2);
    int distance = (rangex+rangey)/2;
    return distance;
}

Vector2D Enemy::find_closest(int ship_xpos,int ship_ypos, int seno, int shno)
{
    // get the distance for all enemies
    int sh1 = closest(ship_xpos,ship_ypos,_shooter_xpos1,_shooter_ypos1);
    int sh2 = closest(ship_xpos,ship_ypos,_shooter_xpos2,_shooter_ypos2);
    int sh3 = closest(ship_xpos,ship_ypos,_shooter_xpos3,_shooter_ypos3);
    int se1 = closest(ship_xpos,ship_ypos,_seeker1_xpos,_seeker1_ypos);
    int se2 = closest(ship_xpos,ship_ypos,_seeker2_xpos,_seeker2_ypos);
    int se3 = closest(ship_xpos,ship_ypos,_seeker3_xpos,_seeker3_ypos);
    printf("sh1 = %d\n",sh1);
    printf("sh2 = %d\n",sh2);
    printf("sh3 = %d\n",sh3);
    printf("se1 = %d\n",se1);
    printf("se2 = %d\n",se2);
    printf("se3 = %d\n",se3);
    
    int close[6] = {sh1,sh2,sh3,se1,se2,se3};
    // find index of the smallest element
    int index;
    int smallest = close[0];
    for(int i=0; i<6; i=i+1) {
      if(smallest>close[i]) {
         smallest=close[i];
         index = i;
      }
    }
    printf("index = %d, _shno = %d, _seno = %d\n",index,shno,seno);
    // return the position of the closest enemy
    if(index == 0 && shno >= 1){
       printf("shooter1\n"); 
       return {_shooter_xpos1,_shooter_ypos1};
    }
    if(index == 1 && shno >= 2){
       // printf("shooter_xpos2 = %d, shooter_ypos2 = %d\n",_shooter_xpos2,_shooter_ypos2); 
       return {_shooter_xpos2,_shooter_ypos2}; 
    }
    if(index == 2 && shno >= 3){
       printf("shooter3\n");
       return {_shooter_xpos3,_shooter_ypos3}; 
    }
    if(index == 3 && seno >= 1 ){
       printf("seeker1\n"); 
       return {_seeker1_xpos,_seeker1_ypos}; 
    }
    if(index == 4 && seno >= 2){
       printf("seeker2\n"); 
       return {_seeker2_xpos,_seeker2_ypos}; 
    }
    if(index == 5 && seno >= 3){
       printf("seeker3\n");
       return {_seeker3_xpos,_seeker3_ypos};  
    }
    
}