Shmup/src/shmup/player.cpp

341 lines
7.7 KiB
C++

#include "../config.h"
#include "player.h"
#include "bullet.h"
#include "enemy.h"
#include <num/num.h>
#include <gint/rtc.h>
#include "../utilities/fast_trig.h"
#include "../utilities/utilities.h"
#include "background.h"
#include "laser.h"
extern bopti_image_t img_Lifebar;
extern bopti_image_t *img_mainship1;
extern bopti_image_t img_player_ship1;
extern bopti_image_t img_player_ship2;
extern bopti_image_t img_player_ship3;
extern bopti_image_t img_player_ship4;
extern bopti_image_t img_player_ship5;
extern bopti_image_t img_player_ship6;
extern bopti_image_t img_player_ship7;
extern bopti_image_t img_player_ship8;
extern bopti_image_t img_Satellite_Lvl1;
extern Background MyBackground;
int laser_angle = 0;
int laser_incre = 1;
Player::Player( int16_t _x, int16_t _y, uint8_t _id )
{
img_mainship1 = &img_player_ship7;
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;
life0=10000;
if (ID==0) life = 10000;
lastshoot0 = rtc_ticks();
lastshoot1 = rtc_ticks();
lastshoot2 = rtc_ticks();
satellites = true;
satLevel = 1;
satNumber = 3;
satAngle = 0;
satRadius = 50;
satSpeed = 2;
}
Player::~Player()
{
for(unsigned int i=0; i<Lasers.size(); i++)
{
delete( Lasers[i] );
Lasers.erase( Lasers.begin() + i );
}
Lasers.clear();
}
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;
}
if(isLaser)
{
for(unsigned int i=0; i<Lasers.size(); i++)
{
Lasers[i]->sx = libnum::num(xmax);
Lasers[i]->sy = y;
}
laser_angle += laser_incre;
if (laser_angle>15 || laser_angle<0) laser_incre *= -1;
Lasers[1]->dx = FastCosInt( laser_angle );
Lasers[1]->dy = FastSinInt( laser_angle );
Lasers[2]->dx = FastCosInt( laser_angle );
Lasers[2]->dy = - FastSinInt( laser_angle );
}
}
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 );
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);
}
}
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 );
if (isLaser)
{
for(unsigned int i=0; i<Lasers.size(); i++)
Lasers[i]->Render();
}
if (ID==0) azrp_image_p8_effect(xmin, ymin, img_mainship1, 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_Impact( Laser *projectile )
{
Vector2D Start( projectile->sx, projectile->sy );
Vector2D Direction( projectile->dx, projectile->dy );
Vector2D BoxMin( xmin, ymin );
Vector2D BoxMax( xmax, ymax );
if (LineRectangle_Collision( Start, Direction, BoxMin, BoxMax))
{
life -= projectile->strength;
projectile->toberemoved = true;
return true;
}
else return false;
}
bool Player::Test_Impact( Bonus *bonus )
{
if (bonus->x >= xmin && bonus->x <= xmax && bonus->y >= ymin && bonus->y <= ymax )
{
if (bonus->ID==0)
{
life += 1000;
if (life>life0) life=life0;
}
else if (bonus->ID==1)
satNumber++;
bonus->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 if (shootID==LASER)
{
Activate_Laser();
return true;
}
else
{
Desactivate_Laser();
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 );
}
}
void Player::Activate_Laser( void )
{
if(isLaser==false)
{
isLaser = true;
Laser *mylaser1 = new Laser( (int) xmax, (int) y, 1, 0, LASER );
Lasers.push_back( mylaser1 );
Laser *mylaser2 = new Laser( (int) xmax, (int) y, 1, 0, LASER );
Lasers.push_back( mylaser2 );
Laser *mylaser3 = new Laser( (int) xmax, (int) y, 1, 0, LASER );
Lasers.push_back( mylaser3 );
}
}
void Player::Desactivate_Laser( void )
{
if(isLaser==true)
{
isLaser = false;
for(unsigned int i=0; i<Lasers.size(); i++)
{
delete( Lasers[i] );
Lasers.erase( Lasers.begin() + i );
}
Lasers.clear();
}
}
bool Player::Is_Laser_Activated( void )
{
return isLaser;
}