From 137cf2f6a9c2dab2160401bc9047044d781dfb31 Mon Sep 17 00:00:00 2001 From: SlyVTT Date: Thu, 31 Aug 2023 13:41:11 +0200 Subject: [PATCH] major refactoring of code and project classes phase 2 --- CMakeLists.txt | 8 ++++++-- TODO.txt | 5 +++++ src/shmup/bonus.cpp | 12 ++---------- src/shmup/bonus.h | 21 ++++++--------------- src/shmup/boss.cpp | 17 ++--------------- src/shmup/boss.h | 24 ++++++------------------ src/shmup/enemy.cpp | 14 ++------------ src/shmup/enemy.h | 26 +++++++------------------- src/shmup/entity.cpp | 34 ++++++++++++++++++++++++++++++++++ src/shmup/entity.h | 39 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 109 insertions(+), 91 deletions(-) create mode 100644 src/shmup/entity.cpp create mode 100644 src/shmup/entity.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 599269e..b4a29fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,12 +15,14 @@ find_package(LibProf 2.4 REQUIRED) fxconv_declare_converters(assets-cg/converters.py) add_custom_command( - OUTPUT "${CMAKE_CURRENT_LIST_DIR}/assets-cg/Levels/Level2.json" + OUTPUT "${CMAKE_CURRENT_LIST_DIR}/assets-cg/Levels/Level1.json" + OUTPUT "${CMAKE_CURRENT_LIST_DIR}/assets-cg/Levels/Level2.json" COMMENT "Convert Tiled TMX map to usable JSON file" COMMAND tiled --export-tileset json Tileset_Space.tsx Tileset_Space.json COMMAND find | grep .*.tmx | sed 's/.tmx//g' | xargs -l bash -c 'tiled --export-map json $$0.tmx $$0.json' WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/assets-cg/Levels/ - DEPENDS assets-cg/Levels/Level2.tmx + DEPENDS assets-cg/Levels/Level1.tmx + assets-cg/Levels/Level2.tmx assets-cg/converters.py assets-cg/Levels/tileset.png assets-cg/Levels/Tileset_Space.tsx) @@ -43,6 +45,7 @@ set(SOURCES src/shaders/line.cpp src/shaders/starfieldshader.cpp + src/shmup/entity.cpp src/shmup/collections.cpp src/shmup/impact.cpp src/shmup/player.cpp @@ -55,6 +58,7 @@ set(SOURCES src/shmup/trajectory.cpp # ... ) + set(ASSETS_cg #assets-cg/font.png assets-cg/milifont.png diff --git a/TODO.txt b/TODO.txt index 1f0dc44..b7488ba 100644 --- a/TODO.txt +++ b/TODO.txt @@ -5,6 +5,11 @@ - [DONE] better keyboard management engine for keypressed() and keyreleased() events - Mettre une système d'ajustement de FPS mini avec switch des niveaux d'overclock à la volée de manière dynamique - [DONE] Mettre en place un système de collision en "pixel perfect" pour mode jeu "Asteroids Alert !!!" +- refactoriser les classes des entités "mobiles" + o class Entity (position, box, ID, trajectory) + ~~> class Enemy : public Entity (+shoot, +life) + ~~> class Boss : public Enemy (+multiple shoots, +multiple life) + ~~> class Bonus : public Entity (currentframe) ## Partie décors : diff --git a/src/shmup/bonus.cpp b/src/shmup/bonus.cpp index 41e8fd2..12707bc 100644 --- a/src/shmup/bonus.cpp +++ b/src/shmup/bonus.cpp @@ -1,6 +1,7 @@ #include "../config.h" #include "bonus.h" +#include "entity.h" #include #include @@ -8,13 +9,8 @@ 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 ) +Bonus::Bonus( int16_t _x, int16_t _y, uint8_t _id ) : Entity( _x, _y, _id ) { - x = libnum::num( _x ); - y = libnum::num( _y ); - - ID = _id; - if (ID==0) { width = img_life_bonus.width/2; @@ -33,11 +29,7 @@ Bonus::Bonus( int16_t _x, int16_t _y, uint8_t _id ) ymin = (int) y - height; ymax = (int) y + height; - toberemoved = false; - currentframe = libnum::num(0); - - accumulatedTime = 0.0f; } Bonus::~Bonus() diff --git a/src/shmup/bonus.h b/src/shmup/bonus.h index 36b4683..408359d 100644 --- a/src/shmup/bonus.h +++ b/src/shmup/bonus.h @@ -10,31 +10,22 @@ #include #include "trajectory.h" +#include "entity.h" -class Bonus + +class Bonus : public Entity { 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; + virtual void Update( float dt ) override; + virtual void Render( void ) override; private: int8_t dirx, diry; libnum::num currentframe; - - float accumulatedTime; + }; diff --git a/src/shmup/boss.cpp b/src/shmup/boss.cpp index d38de78..8bbad17 100644 --- a/src/shmup/boss.cpp +++ b/src/shmup/boss.cpp @@ -93,17 +93,10 @@ bool Is_Point_Inside_Triangle( int Px, int Py, int P1x, int P1y, int P2x, int P2 -Boss::Boss( int16_t _x, int16_t _y, uint8_t _id ) +Boss::Boss( int16_t _x, int16_t _y, uint8_t _id ) : Enemy( _x, _y, _id ) { - x = libnum::num(_x); - y = libnum::num(_y); - - ID = _id; - speed = 1; - toberemoved = false; - width = img_Boss1.width/2; height = img_Boss1.height/2; @@ -154,22 +147,15 @@ Boss::Boss( int16_t _x, int16_t _y, uint8_t _id ) Guns[i].toberemoved = false; } - accumulatedTime = 0.0f; - /* FOR DEBUGGING */ - // gint_world_switch( GINT_CALL( savedata ) ); - } Boss::~Boss() { - if (hasTrajectory) - pathToFollow->DeleteRegistry(); } void Boss::Update( float dt ) { - if (hasTrajectory) { pathToFollow->CalculatePosition( &accumulatedTime, dt, speed, true, &x, &y ); @@ -179,6 +165,7 @@ void Boss::Update( float dt ) xmax = (int) x + width; ymin = (int) y - height; ymax = (int) y + height; + rotAngle += rotSpeed * dt / 25000.0f; if (rotAngle>360.0f) rotAngle-=360.0f; diff --git a/src/shmup/boss.h b/src/shmup/boss.h index b4ab5e6..edf02e4 100644 --- a/src/shmup/boss.h +++ b/src/shmup/boss.h @@ -10,6 +10,7 @@ #include #include #include "bullet.h" +#include "enemy.h" #include "trajectory.h" @@ -29,29 +30,18 @@ typedef struct } BossGun; -class Boss +class Boss : public Enemy { public: Boss( int16_t _x, int16_t _y, uint8_t _id ); ~Boss(); - void Update( float dt ); - void Render( void ); + virtual void Update( float dt ) override; + virtual void Render( void ) override; + virtual bool Test_Impact( Bullet *projectile ) override; - bool Test_Impact( Bullet *projectile ); - - libnum::num x, y; // center position of the boss - uint8_t width, height; // width and height -for the hitbox - int16_t xmin, xmax, ymin, ymax; // square hitbox (to speed up the bullet impact calculations) - uint8_t ID; - int16_t life, life0; - uint8_t speed; // speed of the boss uint32_t lastshoot = 0; uint8_t rotSpeed; - bool toberemoved; - - bool hasTrajectory = false; - Trajectory *pathToFollow; private: float rotAngle; @@ -60,9 +50,7 @@ class Boss uint32_t lastshoot0 = 0; uint32_t lastshoot1 = 0; - float accumulatedTime; - - bool Shoot_OK( uint32_t tempshoot, uint8_t shootID ); + virtual bool Shoot_OK( uint32_t tempshoot, uint8_t shootID ) override; }; diff --git a/src/shmup/enemy.cpp b/src/shmup/enemy.cpp index 88483e7..47cffe7 100644 --- a/src/shmup/enemy.cpp +++ b/src/shmup/enemy.cpp @@ -15,16 +15,11 @@ extern std::vector MyEnemiesBullets; -Enemy::Enemy( int16_t _x, int16_t _y, uint8_t _id ) +Enemy::Enemy( int16_t _x, int16_t _y, uint8_t _id ) : Entity( _x, _y, _id ) { - x = libnum::num( _x ); - y = libnum::num( _y ); - dirx = 1; diry = -6; - ID = _id; - if (ID==0) { width = img_mainship2.width/2; @@ -52,17 +47,12 @@ Enemy::Enemy( int16_t _x, int16_t _y, uint8_t _id ) ymin = (int) y - height; ymax = (int) y + height; - toberemoved = false; - lastshottime = 0; - - accumulatedTime = 0.0f; } Enemy::~Enemy() { - if (hasTrajectory) - pathToFollow->DeleteRegistry(); + } void Enemy::Update( float dt ) diff --git a/src/shmup/enemy.h b/src/shmup/enemy.h index 79a9300..424d4e3 100644 --- a/src/shmup/enemy.h +++ b/src/shmup/enemy.h @@ -9,41 +9,29 @@ #include #include "bullet.h" +#include "entity.h" #include "trajectory.h" -class Enemy +class Enemy : public Entity { public: Enemy( int16_t _x, int16_t _y, uint8_t _id ); ~Enemy(); - void Update( float dt ); - void Render( void ); + virtual void Update( float dt ) override; + virtual void Render( void ) override; - bool Test_Impact( Bullet *projectile ); + virtual bool Test_Impact( Bullet *projectile ) override; void Set_Speed_Vector( uint8_t _sp, uint8_t _xd, uint8_t _yd); - void Set_Accumulated_Time( float value ); - - 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 bullet impact calculations) - uint8_t ID; - int16_t life; - uint8_t speed; // speed of this ennemy - bool toberemoved; - - bool hasTrajectory = false; - Trajectory *pathToFollow; + virtual void Set_Accumulated_Time( float value ); private: int8_t dirx, diry; // vector of the current direction of the ennemy (TODO : to implement more complex displacement pattern) uint32_t lastshottime; - float accumulatedTime; - - bool Shoot_OK( uint32_t tempshoot, uint8_t shootID ); + virtual bool Shoot_OK( uint32_t tempshoot, uint8_t shootID ); }; diff --git a/src/shmup/entity.cpp b/src/shmup/entity.cpp new file mode 100644 index 0000000..017ef2b --- /dev/null +++ b/src/shmup/entity.cpp @@ -0,0 +1,34 @@ +#include "entity.h" +#include + +Entity::Entity( int16_t _x, int16_t _y, uint8_t _id ) +{ + x = libnum::num(_x); + y = libnum::num(_y); + ID = _id; + toberemoved = false; + hasTrajectory = false; + accumulatedTime = 0.0f; +} + + +Entity::~Entity( void ) +{ + if (hasTrajectory) + pathToFollow->DeleteRegistry(); +} + +void Entity::Update( float dt ) +{ + +} + +void Entity::Render( void ) +{ + +} + +bool Entity::Test_Impact( Bullet *projectile ) +{ + +} \ No newline at end of file diff --git a/src/shmup/entity.h b/src/shmup/entity.h new file mode 100644 index 0000000..0c30e4d --- /dev/null +++ b/src/shmup/entity.h @@ -0,0 +1,39 @@ +#ifndef ENTITY_H +#define ENTITY_H + +#include +#include + +#include +#include +#include "bullet.h" +#include "trajectory.h" + + +class Entity +{ + public: + Entity( int16_t _x, int16_t _y, uint8_t _id ); + virtual ~Entity(); + + virtual void Update( float dt ); + virtual void Render( void ); + virtual bool Test_Impact( Bullet *projectile ); + + + libnum::num x, y; // center position of the boss + uint8_t width, height; // width and height -for the hitbox + int16_t xmin, xmax, ymin, ymax; // square hitbox (to speed up the bullet impact calculations) + uint8_t ID; + uint8_t speed; // speed of the boss + int16_t life; + bool toberemoved; + + bool hasTrajectory = false; + Trajectory *pathToFollow; + float accumulatedTime; + int16_t life0; +}; + + +#endif \ No newline at end of file