From ba57b0aa4a681ee635a109d510af70780594206c Mon Sep 17 00:00:00 2001 From: SlyVTT Date: Thu, 31 Aug 2023 21:34:34 +0200 Subject: [PATCH] enable non-looping trajectories with dully kill of enemies when reaching end --- TODO.txt | 2 +- .../__pycache__/converters.cpython-310.pyc | Bin 0 -> 1530 bytes src/shmup/bonus.cpp | 3 +- src/shmup/bonus.h | 6 +- src/shmup/boss.cpp | 3 +- src/shmup/boss.h | 2 +- src/shmup/collections.cpp | 74 +++++++++++++++++- src/shmup/enemy.cpp | 3 +- src/shmup/enemy.h | 2 +- src/shmup/entity.cpp | 5 +- src/shmup/entity.h | 3 +- src/shmup/trajectory.cpp | 18 ++++- src/shmup/trajectory.h | 2 +- src/utilities/utilities.cpp | 13 +-- 14 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 assets-cg/__pycache__/converters.cpython-310.pyc diff --git a/TODO.txt b/TODO.txt index b7488ba..a485585 100644 --- a/TODO.txt +++ b/TODO.txt @@ -5,7 +5,7 @@ - [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" +- [DONE] 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) diff --git a/assets-cg/__pycache__/converters.cpython-310.pyc b/assets-cg/__pycache__/converters.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c9120c5c729c7cc2323b59fb39dc945bede3850c GIT binary patch literal 1530 zcmZ8h&1)k!6qhub58LCfBwepBZCPlk4{P@W+Diz94LuYZmL?}dFq6^5o^-w2P?+EWoq>#djC~HL_ymg8g1(){8vK5v>=IL{J zS47%MgMwqQy*C6xwZc5RiMX7>j}g^&oE4?iPF~7-Edm~-LOU|x^GN=IxVeuv$CE{# zMiX(D+yrqpdCBuRY)bU zrSO3GxPvLCg!>@;=jaEZfcxJbeF>>cw8oMsxbYR);EF2B2wa5)lMd0^GWWV6l`W~V zS5OaSuWjaTF!P3}azJue1El5vRW5Vau0p&2NVzTaJ@|sZ?f~oth_zss2hltet!kLq z3aqQ9YOX!z1J_cGbz_4d4rT43p`9Tjr1Gma>$2W!qv;RPFdEd3RsC%|)q~US zsXcaJVxKpveboowJ+)uk^{;=fqxV&R>*x9h*}U3QU4_{@0InxPG(eYDjFj~o{;@_i zKx%NJG1s1qlgQMyMw50#Uh*(n(yO5~ZTc~YjC(DtjC z$%S?nQ9NJB(4g^Qn5Hr|r!a-7{2Z`-?o^lo!f((|jPhIv{XePFy9*9U0%-XqYD50g9$5^)5~!(Qzq!5yIVOw%}>&-fGL3(qvo zf;1ZKYg(U$CV9X#&5H=m#iJq#!bsZ%hsM>mD3TbKFfT!(S9zRi3pxliNiHJoT;7@- zbmLU=GL$8U)+|5!R9if{)HbyL99&)8$w+|ylh3tPNUp6Yxf~sshX`@gG~6@|Zx~W% z*fQyzZR?`DkA$yy6lpKJ_%@PICf(ZA?qvWKaCd_C&2`S+N;_sI;$@`qD~%Vrm0g^H z_xf&hL)4jn1MkUG$Urx1^~^ZY_I3+3KK@q{ZeFksOfd%1!%r;_cSwhPNSdUHo7ls9 z6m;YnIe^!|w;b%l*TX&X%&Kiio_5Uhn5W!SlYeB$yFj#CzCalculatePosition( &accumulatedTime, dt, speed, true, &x, &y ); + if (pathToFollow->CalculatePosition( &accumulatedTime, dt, speed, loopTrajectory, &x, &y ) == -1) + toberemoved=true; } xmin = (int) x - width; diff --git a/src/shmup/bonus.h b/src/shmup/bonus.h index 408359d..e96327d 100644 --- a/src/shmup/bonus.h +++ b/src/shmup/bonus.h @@ -24,11 +24,7 @@ class Bonus : public Entity private: int8_t dirx, diry; - libnum::num currentframe; - + libnum::num currentframe; }; - - - #endif \ No newline at end of file diff --git a/src/shmup/boss.cpp b/src/shmup/boss.cpp index 8bbad17..046d6f4 100644 --- a/src/shmup/boss.cpp +++ b/src/shmup/boss.cpp @@ -158,7 +158,8 @@ void Boss::Update( float dt ) { if (hasTrajectory) { - pathToFollow->CalculatePosition( &accumulatedTime, dt, speed, true, &x, &y ); + if (pathToFollow->CalculatePosition( &accumulatedTime, dt, speed, loopTrajectory, &x, &y ) == -1) + toberemoved=true; } xmin = (int) x - width; diff --git a/src/shmup/boss.h b/src/shmup/boss.h index edf02e4..4f416fd 100644 --- a/src/shmup/boss.h +++ b/src/shmup/boss.h @@ -38,7 +38,7 @@ class Boss : public Enemy virtual void Update( float dt ) override; virtual void Render( void ) override; - virtual bool Test_Impact( Bullet *projectile ) override; + bool Test_Impact( Bullet *projectile ) override; uint32_t lastshoot = 0; uint8_t rotSpeed; diff --git a/src/shmup/collections.cpp b/src/shmup/collections.cpp index 08e16cc..a883407 100644 --- a/src/shmup/collections.cpp +++ b/src/shmup/collections.cpp @@ -67,8 +67,6 @@ void Create_Enemies( void ) MyEnemies.push_back( e4 ); */ - - Vector2D *A = new Vector2D( 348, 112 ); Vector2D *B = new Vector2D( 371, 199 ); Vector2D *C = new Vector2D( 198, 149 ); @@ -77,7 +75,6 @@ void Create_Enemies( void ) Vector2D *F = new Vector2D( 198, 75 ); Vector2D *G = new Vector2D( 371, 25 ); - Trajectory *MyPath= new Trajectory(); MyPath->AddPoint( A ); MyPath->AddPoint( B ); @@ -88,6 +85,7 @@ void Create_Enemies( void ) MyPath->AddPoint( G ); MyTrajectories.push_back( MyPath ); + Enemy* e5 = new Enemy( 348, 112, 2); e5->hasTrajectory = true; e5->pathToFollow = MyPath; @@ -110,9 +108,79 @@ void Create_Enemies( void ) MyEnemies.push_back( e5 ); MyEnemies.push_back( e6 ); MyEnemies.push_back( e7 ); + + + Vector2D *A1 = new Vector2D( 450, 0 ); + Vector2D *B1 = new Vector2D( 400, 0 ); + Vector2D *C1 = new Vector2D( 350, 25 ); + Vector2D *D1 = new Vector2D( 300, 25 ); + Vector2D *E1 = new Vector2D( 250, 100 ); + Vector2D *F1 = new Vector2D( 100, 100 ); + Vector2D *G1 = new Vector2D( 50, 25 ); + Vector2D *H1 = new Vector2D( 0, 25 ); + Vector2D *I1 = new Vector2D( -100, 0 ); + + + Trajectory *MyPath1= new Trajectory(); + MyPath1->AddPoint( A1 ); + MyPath1->AddPoint( B1 ); + MyPath1->AddPoint( C1 ); + MyPath1->AddPoint( D1 ); + MyPath1->AddPoint( E1 ); + MyPath1->AddPoint( F1 ); + MyPath1->AddPoint( G1 ); + MyPath1->AddPoint( H1 ); + MyPath1->AddPoint( I1 ); + MyTrajectories.push_back( MyPath1 ); + + + Enemy* e8 = new Enemy( 450, 0, 1); + e8->hasTrajectory = true; + e8->pathToFollow = MyPath1; + MyPath1->AddRegistry(); + e8->Set_Accumulated_Time(1.0f); + + Enemy* e9 = new Enemy( 450, 0, 1); + e9->hasTrajectory = true; + e9->pathToFollow = MyPath1; + MyPath1->AddRegistry(); + e9->Set_Accumulated_Time(0.75f); + e9->Set_Loop_Trajectory(false); + + Enemy* e10 = new Enemy( 450, 0, 1); + e10->hasTrajectory = true; + e10->pathToFollow = MyPath1; + MyPath1->AddRegistry(); + e10->Set_Accumulated_Time(0.50f); + + Enemy* e11 = new Enemy( 450, 0, 1); + e11->hasTrajectory = true; + e11->pathToFollow = MyPath1; + MyPath1->AddRegistry(); + e11->Set_Accumulated_Time(0.25f); + e11->Set_Loop_Trajectory(false); + + Enemy* e12 = new Enemy( 450, 0, 1); + e12->hasTrajectory = true; + e12->pathToFollow = MyPath1; + MyPath1->AddRegistry(); + e12->Set_Accumulated_Time(0.0f); + + + + + + MyEnemies.push_back( e8 ); + MyEnemies.push_back( e9 ); + MyEnemies.push_back( e10 ); + MyEnemies.push_back( e11 ); + MyEnemies.push_back( e12 ); + } + + void Create_Explosion( uint16_t xexplosion, uint16_t yexplosion ) { srand(rtc_ticks()); diff --git a/src/shmup/enemy.cpp b/src/shmup/enemy.cpp index 47cffe7..1b351d3 100644 --- a/src/shmup/enemy.cpp +++ b/src/shmup/enemy.cpp @@ -68,7 +68,8 @@ void Enemy::Update( float dt ) } else { - pathToFollow->CalculatePosition( &accumulatedTime, dt, speed, true, &x, &y ); + if (pathToFollow->CalculatePosition( &accumulatedTime, dt, speed, loopTrajectory, &x, &y ) == -1) + toberemoved=true; } xmin = (int) x - width; diff --git a/src/shmup/enemy.h b/src/shmup/enemy.h index 424d4e3..4f3ca5e 100644 --- a/src/shmup/enemy.h +++ b/src/shmup/enemy.h @@ -22,7 +22,7 @@ class Enemy : public Entity virtual void Update( float dt ) override; virtual void Render( void ) override; - virtual bool Test_Impact( Bullet *projectile ) override; + virtual bool Test_Impact( Bullet *projectile ); void Set_Speed_Vector( uint8_t _sp, uint8_t _xd, uint8_t _yd); virtual void Set_Accumulated_Time( float value ); diff --git a/src/shmup/entity.cpp b/src/shmup/entity.cpp index 017ef2b..8648224 100644 --- a/src/shmup/entity.cpp +++ b/src/shmup/entity.cpp @@ -9,6 +9,7 @@ Entity::Entity( int16_t _x, int16_t _y, uint8_t _id ) toberemoved = false; hasTrajectory = false; accumulatedTime = 0.0f; + loopTrajectory = true; } @@ -28,7 +29,7 @@ void Entity::Render( void ) } -bool Entity::Test_Impact( Bullet *projectile ) +void Entity::Set_Loop_Trajectory( bool value ) { - + loopTrajectory = value; } \ No newline at end of file diff --git a/src/shmup/entity.h b/src/shmup/entity.h index 0c30e4d..292edb2 100644 --- a/src/shmup/entity.h +++ b/src/shmup/entity.h @@ -18,7 +18,7 @@ class Entity virtual void Update( float dt ); virtual void Render( void ); - virtual bool Test_Impact( Bullet *projectile ); + void Set_Loop_Trajectory( bool value ); libnum::num x, y; // center position of the boss @@ -30,6 +30,7 @@ class Entity bool toberemoved; bool hasTrajectory = false; + bool loopTrajectory; Trajectory *pathToFollow; float accumulatedTime; int16_t life0; diff --git a/src/shmup/trajectory.cpp b/src/shmup/trajectory.cpp index 356401f..d91d7c8 100644 --- a/src/shmup/trajectory.cpp +++ b/src/shmup/trajectory.cpp @@ -34,11 +34,19 @@ void Trajectory::DeleteRegistry( void ) registration--; } -void Trajectory::CalculatePosition( float *accumulatedTime, float time, uint16_t speed, bool looped, libnum::num *xreturn, libnum::num *yreturn ) +int8_t Trajectory::CalculatePosition( float *accumulatedTime, float time, uint16_t speed, bool looped, libnum::num *xreturn, libnum::num *yreturn ) { *accumulatedTime += speed * time / 2000000.0f; - if (*accumulatedTime>ControlPoints.size()) *accumulatedTime-=ControlPoints.size(); - if (*accumulatedTime<0) *accumulatedTime+=ControlPoints.size(); + + if( !looped && (*accumulatedTime>ControlPoints.size())) + return -1; + + if (*accumulatedTime>ControlPoints.size()) + *accumulatedTime -= ControlPoints.size(); + + if (*accumulatedTime<0) + *accumulatedTime += ControlPoints.size(); + libnum::num t = libnum::num( *accumulatedTime - (int) *accumulatedTime ); @@ -72,4 +80,6 @@ void Trajectory::CalculatePosition( float *accumulatedTime, float time, uint16_t *xreturn = tx; *yreturn = ty; -} \ No newline at end of file + + return 0; +} diff --git a/src/shmup/trajectory.h b/src/shmup/trajectory.h index da7a18d..c2bf674 100644 --- a/src/shmup/trajectory.h +++ b/src/shmup/trajectory.h @@ -19,7 +19,7 @@ class Trajectory void AddPoint( Vector2D *p ); void AddRegistry( void ); void DeleteRegistry( void ); - void CalculatePosition( float *accumulatedTime, float time, uint16_t speed, bool looped, libnum::num *xreturn, libnum::num *yreturn ); + int8_t CalculatePosition( float *accumulatedTime, float time, uint16_t speed, bool looped, libnum::num *xreturn, libnum::num *yreturn ); std::vector ControlPoints; bool isLoop; diff --git a/src/utilities/utilities.cpp b/src/utilities/utilities.cpp index f216315..5a92a09 100644 --- a/src/utilities/utilities.cpp +++ b/src/utilities/utilities.cpp @@ -62,17 +62,8 @@ bool Pixel_Perfect_Collision( SpriteLocator image1, SpriteLocator image2 ) if ( IMAGE_IS_P4(image1.image->format) || IMAGE_IS_P4(image2.image->format) ) return false; - - - - int astartx, aendx; - int astarty, aendy; int adeltax, adeltay; - - int bstartx, bendx; - int bstarty, bendy; int bdeltax, bdeltay; - int rows, columns; if (image1.x <= image2.x) @@ -191,13 +182,15 @@ bool Pixel_Perfect_Collision( SpriteLocator image1, SpriteLocator image2 ) d1 = data_u16_1[ adeltax + i ] == transp1 ? 0 : 1; else if (im1_P8) d1 = data_u8_1[ adeltax + i ] == transp1 ? 0 : 1; - + else d1 = 0; + /* d2 is set to 1 if pixel of image2 is not transparent and to 0 if transparent */ /* need to be format dependant here so quite time consumming test at each loop :( )*/ if (im2_P16) d2 = data_u16_2[ bdeltax + i ] == transp2 ? 0 : 1; else if (im2_P8) d2 = data_u8_2[ bdeltax + i ] == transp2 ? 0 : 1; + else d2 = 0; /* if d1 + d2 = 2 means that both coincident pixels are not transparent and then we have collision*/ if (d1 + d2 == 2) return true;