From c9e7801f3517965e2d495e706e86c856b447c62f Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Sun, 5 Feb 2023 16:49:56 +0100 Subject: [PATCH] Added the Bonus System --- CMakeLists.txt | 4 + assets-cg/Sprites/Bonus/fxconv-metadata.txt | 5 ++ assets-cg/Sprites/Bonus/life_bonus.png | Bin 0 -> 130 bytes assets-cg/Sprites/Bonus/sat_bonus.png | Bin 0 -> 137 bytes src/bonus.cpp | 92 ++++++++++++++++++++ src/bonus.h | 41 +++++++++ src/main.cpp | 27 +++++- src/player.cpp | 15 +++- src/player.h | 5 +- 9 files changed, 184 insertions(+), 5 deletions(-) create mode 100644 assets-cg/Sprites/Bonus/fxconv-metadata.txt create mode 100644 assets-cg/Sprites/Bonus/life_bonus.png create mode 100644 assets-cg/Sprites/Bonus/sat_bonus.png create mode 100644 src/bonus.cpp create mode 100644 src/bonus.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 90cd411..a7c8700 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/assets-cg/Sprites/Bonus/fxconv-metadata.txt b/assets-cg/Sprites/Bonus/fxconv-metadata.txt new file mode 100644 index 0000000..99767a5 --- /dev/null +++ b/assets-cg/Sprites/Bonus/fxconv-metadata.txt @@ -0,0 +1,5 @@ +*.png: + type: bopti-image + name_regex: (.*)\.png img_\1 + section: .data + profile: p8_rgb565a diff --git a/assets-cg/Sprites/Bonus/life_bonus.png b/assets-cg/Sprites/Bonus/life_bonus.png new file mode 100644 index 0000000000000000000000000000000000000000..c207594934ab9758d6e38d989e78fc9b7ecd935b GIT binary patch literal 130 zcmeAS@N?(olHy`uVBq!ia0vp^oFL4>1|%O$WD@{Vo}Mm_Arez-CpdC381gX9z4X7n zg8Q9nYB#U7dZ4q@l0&Xpp0jSpEbwFQx$Gl$Vw=0TO8Cm=J&#jvG3`_7-d*5(>szq= dkGuCp<5vH4xwvy??iQe-44$rjF6*2UngAHvESmrT literal 0 HcmV?d00001 diff --git a/assets-cg/Sprites/Bonus/sat_bonus.png b/assets-cg/Sprites/Bonus/sat_bonus.png new file mode 100644 index 0000000000000000000000000000000000000000..b6540caddd298911deec1486a54b64f6770b6fb4 GIT binary patch literal 137 zcmeAS@N?(olHy`uVBq!ia0vp^oFL4>1|%O$WD@{V0iG_7Are!2104Ad7;rQlyZr04 zUC#cP_3w^2N%-E16OeQ{g!P2+l#NIU7=NCecw#sP59z8bS lZ*lY+)BS!TH~(@l#KmmUdbd>i=?tLR44$rjF6*2UngCFkG3x*T literal 0 HcmV?d00001 diff --git a/src/bonus.cpp b/src/bonus.cpp new file mode 100644 index 0000000..7ed414e --- /dev/null +++ b/src/bonus.cpp @@ -0,0 +1,92 @@ +#include "bonus.h" +#include +#include + +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 (xazrp_width-width) dirx=-1*dirx; + if (yazrp_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); + } +} diff --git a/src/bonus.h b/src/bonus.h new file mode 100644 index 0000000..0d805d8 --- /dev/null +++ b/src/bonus.h @@ -0,0 +1,41 @@ +#ifndef BONUS_H +#define BONUS_H + +#include +#include + +#include +#include + +#include +#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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 34e40dc..f458492 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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 MyPlayerBullets; std::vector MyEnemiesBullets; std::vector MyEnemies; std::vector MyImpacts; +std::vector 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; iUpdate( 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(); diff --git a/src/player.cpp b/src/player.cpp index e572e77..ce1d413 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -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 ) diff --git a/src/player.h b/src/player.h index 2681a03..d090454 100644 --- a/src/player.h +++ b/src/player.h @@ -10,7 +10,7 @@ #include #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 );