Added the Bonus System

This commit is contained in:
Sylvain PILLOT 2023-02-05 16:49:56 +01:00
parent 28718ae6d0
commit c9e7801f35
9 changed files with 184 additions and 5 deletions

View File

@ -42,6 +42,7 @@ set(SOURCES
src/enemy.cpp
src/starfieldshader.cpp
src/background.cpp
src/bonus.cpp
src/point2D.cpp
src/trajectory.cpp
@ -55,6 +56,9 @@ set(ASSETS_cg
assets-cg/Sprites/Explosions/emp_circ.png
assets-cg/Sprites/Explosions/fill_circ.png
assets-cg/Sprites/Bonus/life_bonus.png
assets-cg/Sprites/Bonus/sat_bonus.png
assets-cg/Sprites/Bullets/bullet_normal.png
assets-cg/Sprites/Bullets/bullet_blue.png
assets-cg/Sprites/Bullets/bullet_laser.png

View File

@ -0,0 +1,5 @@
*.png:
type: bopti-image
name_regex: (.*)\.png img_\1
section: .data
profile: p8_rgb565a

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 B

92
src/bonus.cpp Normal file
View File

@ -0,0 +1,92 @@
#include "bonus.h"
#include <num/num.h>
#include <gint/rtc.h>
extern bopti_image_t img_emp_circ;
extern bopti_image_t img_life_bonus;
extern bopti_image_t img_sat_bonus;
Bonus::Bonus( int16_t _x, int16_t _y, uint8_t _id )
{
x = libnum::num( _x );
y = libnum::num( _y );
ID = _id;
if (ID==0)
{
width = img_life_bonus.width/2;
height = img_life_bonus.height/2;
speed = 0;
}
else if (ID==1)
{
width = img_sat_bonus.width/2;
height = img_sat_bonus.height/2;
speed = 0;
}
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
toberemoved = false;
currentframe = libnum::num(0);
}
Bonus::~Bonus()
{
if (hasTrajectory)
delete(pathToFollow);
}
void Bonus::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;
libnum::num a = libnum::num( dt / 150000.0f );
currentframe += a;
if (currentframe >7 ) currentframe = libnum::num(0);
}
void Bonus::Render( void )
{
uint8_t dximg = (int) currentframe * 15;
uint8_t sz = (int) currentframe;
azrp_subimage_p8( (int) x-sz, (int) y-sz, &img_emp_circ, dximg+7-sz, 7-sz, sz*2+1, sz*2+1, DIMAGE_NONE );
if (ID==0)
{
azrp_image_p8_effect(xmin, ymin, &img_life_bonus, DIMAGE_NONE);
}
else if (ID==1)
{
azrp_image_p8_effect(xmin, ymin, &img_sat_bonus, DIMAGE_NONE);
}
}

41
src/bonus.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef BONUS_H
#define BONUS_H
#include <azur/azur.h>
#include <azur/gint/render.h>
#include <cstdint>
#include <stdlib.h>
#include <num/num.h>
#include "trajectory.h"
class Bonus
{
public:
Bonus( int16_t _x, int16_t _y, uint8_t _id );
~Bonus();
void Update( float dt );
void Render( void );
libnum::num x, y; // center position of the ennemy
uint8_t width, height; // width and height -for the hitbox
int16_t xmin, xmax, ymin, ymax; // square hitbox (to speed up the collision calculations)
uint8_t ID;
uint8_t speed;
bool toberemoved;
bool hasTrajectory = false;
Trajectory *pathToFollow;
private:
int8_t dirx, diry;
libnum::num currentframe;
};
#endif

View File

@ -32,7 +32,7 @@
#include "particles.h"
#include "bullet.h"
#include "enemy.h"
#include "bonus.h"
#include "impact.h"
#include "background.h"
@ -76,6 +76,7 @@ std::vector<Bullet*> MyPlayerBullets;
std::vector<Bullet*> MyEnemiesBullets;
std::vector<Enemy*> MyEnemies;
std::vector<Impact*> MyImpacts;
std::vector<Bonus*> MyBonuses;
Background MyBackground;
@ -146,6 +147,9 @@ static void update( float dt )
// Check if the property toberemoved has been set to "true" for particle deletion
if (MyEnemies[i]->toberemoved == true)
{
Bonus *b = new Bonus( (int) MyEnemies[i]->x, (int) MyEnemies[i]->y, rand() % 2 );
MyBonuses.push_back( b );
Create_Explosion( (int) MyEnemies[i]->x, (int) MyEnemies[i]->y );
delete( MyEnemies[i] );
MyEnemies.erase( MyEnemies.begin() + i );
@ -192,7 +196,6 @@ static void update( float dt )
if(MyPlayer->Test_Impact(MyEnemiesBullets[i])==true)
{
//TODO : we can create a list of impacts here, to be rendered later on
Create_Impact( (int) MyEnemiesBullets[i]->x, (int) MyEnemiesBullets[i]->y );
}
@ -205,6 +208,23 @@ static void update( float dt )
}
}
for(unsigned int i=0; i<MyBonuses.size(); i++)
{
MyBonuses[i]->Update( dt );
if (MyPlayer->Test_Impact(MyBonuses[i]) == true)
{
// TODO : put stuff to highlight the bonus
}
if (MyBonuses[i]->toberemoved == true)
{
delete( MyBonuses[i] );
MyBonuses.erase( MyBonuses.begin() + i );
}
}
MyBackground.Update( dt );
@ -236,6 +256,9 @@ static void render( void )
for(auto& p : MyParticles)
p->Render();
for(auto& b : MyBonuses)
b->Render();
MyPlayer->Render();

View File

@ -37,7 +37,7 @@ Player::Player( int16_t _x, int16_t _y, uint8_t _id )
satellites = true;
satLevel = 1;
satNumber = 6;
satNumber = 3;
satAngle = 0;
satRadius = 50;
satSpeed = 2;
@ -106,6 +106,19 @@ bool Player::Test_Impact( Bullet *projectile )
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;
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 )

View File

@ -10,7 +10,7 @@
#include <num/num.h>
#include "bullet.h"
#include "enemy.h"
#include "bonus.h"
class Player
{
@ -21,7 +21,8 @@ class Player
void Update( float dt );
void Render( void );
bool Test_Impact( Bullet *projectile );
bool Test_Impact( Bullet *projectile );
bool Test_Impact( Bonus *bonus );
bool Test_Collision( Enemy *adverseship );
void Set_Speed( uint8_t _sp );