Shmup/src/player.cpp

207 lines
4.7 KiB
C++

#include "player.h"
#include "bullet.h"
#include "enemy.h"
#include <num/num.h>
#include <gint/rtc.h>
#include "fast_trig.h"
#include "background.h"
extern bopti_image_t img_Lifebar;
extern bopti_image_t img_mainship1;
extern bopti_image_t img_Satellite_Lvl1;
extern Background MyBackground;
Player::Player( int16_t _x, int16_t _y, uint8_t _id )
{
x = libnum::num(_x);
y = libnum::num(_y);
ID = _id;
width = img_mainship1.width/2;
height = img_mainship1.height/2;
speed = 10;
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
if (ID==0) life = 1000;
lastshoot0 = rtc_ticks();
lastshoot1 = rtc_ticks();
lastshoot2 = rtc_ticks();
satellites = true;
satLevel = 1;
satNumber = 6;
satAngle = 0;
satRadius = 50;
satSpeed = 2;
}
Player::~Player()
{
}
void Player::Update( float dt )
{
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
if(satellites)
{
satAngle += satSpeed * dt / 25000.0f;
if (satAngle>360.0f) satAngle-=360.0f;
}
}
void Player::Render( void )
{
azrp_subimage_p8_effect( (int) x - img_Lifebar.width/2, ymin - 10, &img_Lifebar, 0, 0, img_Lifebar.width, 7, DIMAGE_NONE );
if (ID==0) azrp_image_p8_effect(xmin, ymin, &img_mainship1, DIMAGE_NONE);
int w = img_Satellite_Lvl1.width/2;
int h = img_Satellite_Lvl1.height/2;
if(satellites)
{
int incangle = 360/satNumber;
for(int u=0; u<satNumber; u++)
{
int angle = (int) satAngle + u*incangle;
angle = angle % 360;
int xsat = (int) (x + FastCosInt( angle ) * libnum::num( satRadius) );
int ysat = (int) (y + FastSinInt( angle ) * libnum::num( satRadius) );
azrp_image_p8_effect(xsat-w, ysat-h, &img_Satellite_Lvl1, DIMAGE_NONE);
}
}
int16_t life0=1000;
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 Player::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;
}
bool Player::Test_Collision( Enemy *adverseship )
{
if (adverseship->xmax >= xmin && adverseship->xmin <= xmax && adverseship->ymax >= ymin && adverseship->ymin <= ymax )
{
life -= 50;
adverseship->life -= 25;
return true;
}
else return false;
}
void Player::Set_Speed( uint8_t _sp )
{
speed = _sp;
}
bool Player::Shoot_OK( uint32_t tempshoot, uint8_t shootID )
{
if (shootID==BULLET_NORMAL)
{
if(tempshoot-lastshoot0>8)
{
lastshoot0=tempshoot;
return true;
}
else return false;
}
else if (shootID==BULLET_BLUE)
{
if(tempshoot-lastshoot1>15)
{
lastshoot1=tempshoot;
return true;
}
else return false;
}
else if (shootID==BULLET_LASER)
{
if(tempshoot-lastshoot2>2)
{
lastshoot2=tempshoot;
return true;
}
else return false;
}
else return false;
}
void Player::Go_Left( float dt )
{
if( x > width/2+speed )
{
libnum::num a = libnum::num( dt / 60000.0f );
x -= a * libnum::num( speed );
this->Update( 0.0f );
}
}
void Player::Go_Right( float dt )
{
if(x < azrp_width-width/2-speed)
{
libnum::num a = libnum::num( dt / 60000.0f );
x += a * libnum::num( speed );
this->Update( 0.0f );
}
}
void Player::Go_Up( float dt )
{
if(y > height/2+speed)
{
libnum::num a = libnum::num( dt / 60000.0f );
y -= a * libnum::num( speed );
this->Update( 0.0f );
}
if (y<azrp_height/4)
{
libnum::num a = libnum::num( -1.0f * dt / 90000.0f );
MyBackground.IncYCoordinate( a );
}
}
void Player::Go_Down( float dt )
{
if(y < azrp_height -height/2 -speed)
{
libnum::num a = libnum::num( dt / 60000.0f );
y += a * libnum::num( speed );
this->Update( 0.0f );
}
if (y>3*azrp_height/4)
{
libnum::num a = libnum::num( dt / 90000.0f );
MyBackground.IncYCoordinate( a );
}
}