Shmup/src/enemy.cpp

171 lines
4.0 KiB
C++

#include "enemy.h"
#include "bullet.h"
#include <num/num.h>
#include <gint/rtc.h>
extern bopti_image_t img_Lifebar;
extern bopti_image_t img_mainship2;
extern bopti_image_t img_Enemy_Blue_Lvl1;
extern bopti_image_t img_Enemy_Red_Lvl1;
extern std::vector<Bullet*> MyEnemiesBullets;
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;
life = 400;
}
else if (ID==1)
{
width = img_Enemy_Blue_Lvl1.width/2;
height = img_Enemy_Blue_Lvl1.height/2;
speed = 2;
life = 200;
}
else if (ID==2)
{
width = img_Enemy_Red_Lvl1.width/2;
height = img_Enemy_Red_Lvl1.height/2;
speed = 2;
life = 100;
}
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
toberemoved = false;
lastshottime = 0;
}
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
{
pathToFollow->CalculatePosition( dt, speed, true, &x, &y );
}
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
uint32_t tempshoot = rtc_ticks();
if (Shoot_OK( tempshoot ))
{
if ( ID==0 )
{
Bullet *b = new Bullet( xmin, (int) y, -3, 0, BULLET_ENEMY_BLUE );
MyEnemiesBullets.push_back( b );
}
else if ( ID==1 )
{
Bullet *b1 = new Bullet( xmin, (int) y, -5, 0, BULLET_ENEMY_BLUE );
MyEnemiesBullets.push_back( b1 );
Bullet *b2 = new Bullet( xmin, (int) y, -5, 0, BULLET_ENEMY_BLUE );
MyEnemiesBullets.push_back( b2 );
}
else if ( ID==2 )
{
Bullet *b = new Bullet( xmin, (int) y, -1, 0, BULLET_ENEMY_BLUE );
MyEnemiesBullets.push_back( b );
}
}
if (life<=0) toberemoved=true;
}
void Enemy::Render( void )
{
azrp_subimage_p8_effect( (int) x - img_Lifebar.width/2, ymin - 10, &img_Lifebar, 0, 0, img_Lifebar.width, 7, DIMAGE_NONE );
int life0=100;
if (ID==0)
{
azrp_image_p8_effect(xmin, ymin, &img_mainship2, DIMAGE_NONE);
life0 = 400;
}
else if (ID==1)
{
azrp_image_p8_effect(xmin, ymin, &img_Enemy_Blue_Lvl1, DIMAGE_NONE);
life0 = 200;
}
else if (ID==2)
{
azrp_image_p8_effect(xmin, ymin, &img_Enemy_Red_Lvl1, DIMAGE_NONE);
life0 = 100;
}
if (life>life0*2/3) azrp_subimage_p8_effect((int) x - img_Lifebar.width/2, ymin - 9, &img_Lifebar, 0, 7, (img_Lifebar.width*life)/life0, 5, DIMAGE_NONE );
else if (life>life0/3) azrp_subimage_p8_effect((int) x - img_Lifebar.width/2, ymin - 9, &img_Lifebar, 0, 12, (img_Lifebar.width*life)/life0, 5, DIMAGE_NONE );
else azrp_subimage_p8_effect((int) x - img_Lifebar.width/2, ymin - 9, &img_Lifebar, 0, 17, (img_Lifebar.width*life)/life0, 5, 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;
}
bool Enemy::Shoot_OK( uint32_t tempshoot )
{
if(tempshoot-lastshottime>15)
{
lastshottime=tempshoot;
return true;
}
else return false;
}