Shmup/src/enemy.cpp

108 lines
2.4 KiB
C++

#include "enemy.h"
#include "bullet.h"
#include <num/num.h>
extern bopti_image_t img_mainship2;
extern bopti_image_t img_Enemy_Blue_Lvl1;
extern bopti_image_t img_Enemy_Red_Lvl1;
Enemy::Enemy( int16_t _x, int16_t _y, uint8_t _id )
{
x = libnum::num( _x );
y = libnum::num( _y );
dirx = 1;
diry = -6;
ID = _id;
if (ID==0)
{
width = img_mainship2.width/2;
height = img_mainship2.height/2;
speed = 1;
}
else if (ID==1)
{
width = img_Enemy_Blue_Lvl1.width/2;
height = img_Enemy_Blue_Lvl1.height/2;
speed = 2;
}
else if (ID==2)
{
width = img_Enemy_Red_Lvl1.width/2;
height = img_Enemy_Red_Lvl1.height/2;
speed = 2;
}
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
toberemoved = false;
if (ID==0) life = 400;
else if (ID==1) life = 200;
else if (ID==2) life = 100;
}
Enemy::~Enemy()
{
if (hasTrajectory)
delete(pathToFollow);
}
void Enemy::Update( float dt )
{
if (!hasTrajectory)
{
libnum::num a = libnum::num( dt / 60000.0f );
x += a * libnum::num( dirx * speed );
y += a * libnum::num( diry * speed );
if (x<width || x>azrp_width-width) dirx=-1*dirx;
if (y<height || y>azrp_height-height) diry=-1*diry;
}
else
{
int tempX, tempY;
pathToFollow->CalculatePosition( dt, speed, true, &tempX, &tempY );
x = libnum::num( tempX );
y = libnum::num( tempY );
}
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
if (life<=0) toberemoved=true;
}
void Enemy::Render( void )
{
if (ID==0) azrp_image_p8_effect(xmin, ymin, &img_mainship2, DIMAGE_NONE);
if (ID==1) azrp_image_p8_effect(xmin, ymin, &img_Enemy_Blue_Lvl1, DIMAGE_NONE);
if (ID==2) azrp_image_p8_effect(xmin, ymin, &img_Enemy_Red_Lvl1, DIMAGE_NONE);
}
bool Enemy::Test_Impact( Bullet *projectile )
{
if (projectile->x >= xmin && projectile->x <= xmax && projectile->y >= ymin && projectile->y <= ymax )
{
life -= projectile->strength;
projectile->toberemoved = true;
return true;
}
else return false;
}
void Enemy::Set_Speed_Vector( uint8_t _sp, uint8_t _xd, uint8_t _yd)
{
speed = _sp;
dirx = _xd;
diry = _yd;
}