diff --git a/CMakeLists.txt b/CMakeLists.txt index ae2d0d7..f6b321a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,18 +15,25 @@ set(SOURCES src/main.cpp src/utilities.cpp src/particles.cpp + src/bullet.cpp src/starfieldshader.cpp # ... ) set(ASSETS_cg assets-cg/font.png + assets-cg/Sprites/firstboom.png assets-cg/Sprites/emp_circ.png assets-cg/Sprites/fill_circ.png - - assets-cg/Sprites/fill_circ.png + + assets-cg/Sprites/bullet_normal.png + assets-cg/Sprites/bullet_blue.png + assets-cg/Sprites/mainship1.png assets-cg/Sprites/mainship2.png + + + # ... ) diff --git a/assets-cg/Sprites/bullet_blue.png b/assets-cg/Sprites/bullet_blue.png new file mode 100644 index 0000000..091cc0e Binary files /dev/null and b/assets-cg/Sprites/bullet_blue.png differ diff --git a/assets-cg/Sprites/bullet_normal.png b/assets-cg/Sprites/bullet_normal.png new file mode 100644 index 0000000..27c331a Binary files /dev/null and b/assets-cg/Sprites/bullet_normal.png differ diff --git a/src/bullet.cpp b/src/bullet.cpp new file mode 100644 index 0000000..7c18c6a --- /dev/null +++ b/src/bullet.cpp @@ -0,0 +1,68 @@ +#include "bullet.h" + +#include +#include + +#include +#include + +#include + + +extern bopti_image_t img_bullet_normal; +extern bopti_image_t img_bullet_blue; + + +Bullet::Bullet( uint16_t lx, uint16_t ly, uint8_t id ) +{ + x = libnum::num( lx ); + y = libnum::num( ly ); + + ID=id; + + if (ID==0) + { + sx = 0; + sy = libnum::num( -6 ); + } + else if (ID==1) + { + sx = 0; + sy = libnum::num( -3 ); + } + + toberemoved = false; +} + +Bullet::~Bullet() +{ + + +} + +void Bullet::Update( float dt ) +{ + libnum::num a = libnum::num( dt / 12000.0f ); + x += sx * a; + y += sy * a; + + if (y<-10) toberemoved=true; +} + +void Bullet::Render( ) +{ + uint16_t px = (int) x; + uint16_t py = (int) y; + + if (ID==0) + { + azrp_image_p8( px-4, py-11, &img_bullet_normal, DIMAGE_NONE ); + return; + } + else if (ID==1) + { + azrp_image_p8( px-4, py-11, &img_bullet_blue, DIMAGE_NONE ); + return; + } +} + diff --git a/src/bullet.h b/src/bullet.h new file mode 100644 index 0000000..cea0df7 --- /dev/null +++ b/src/bullet.h @@ -0,0 +1,23 @@ +#ifndef BULLET_H +#define BULLET_H + +#include +#include + +class Bullet +{ + public: + Bullet( uint16_t lx, uint16_t ly, uint8_t id ); + ~Bullet(); + void Update( float dt ); + void Render(); + + uint8_t ID; + + libnum::num x, y; + libnum::num sx, sy; + + bool toberemoved; +}; + +#endif //PARTICLES_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b673cce..ba3a981 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,7 +22,7 @@ #include "utilities.h" #include "particles.h" - +#include "bullet.h" #include #include @@ -43,6 +43,8 @@ bool exitToOS = false; #define NOBIAS (1-BIAS) std::vector MyParticles; +std::vector MyPlayerBullets; + Starfield *MyStarField; uint8_t texttodraw=1; @@ -94,6 +96,28 @@ static void hook_prefrag(int id, void *fragment, int size) MyStarField = new Starfield(); }*/ + +void Create_Player_Shoot( uint8_t id ) +{ + + if (id==0) + { + Bullet *b = new Bullet( playerX, playerY-21, id ); + MyPlayerBullets.push_back( b ); + } + else if (id==1) + { + Bullet *b1 = new Bullet( playerX-17, playerY, id ); + MyPlayerBullets.push_back( b1 ); + + Bullet *b2 = new Bullet( playerX+17, playerY, id ); + MyPlayerBullets.push_back( b2 ); + } + +} + + + void Create_Explosion( void ) { if(MyParticles.size()>=1) return; @@ -114,7 +138,8 @@ void Create_Explosion( void ) static void update( float dt ) { // all update stuff depending on time will be done here - + + for(unsigned int i=0; iUpdate( dt ); @@ -127,6 +152,19 @@ static void update( float dt ) } } + + for(unsigned int i=0; iUpdate( dt ); + + // Check if the property toberemoved has been set to "true" for particle deletion + if (MyPlayerBullets[i]->toberemoved == true) + { + delete( MyPlayerBullets[i] ); + MyPlayerBullets.erase( MyPlayerBullets.begin() + i ); + } + } + //MyStarField->Update( dt ); } @@ -144,7 +182,9 @@ static void get_inputs( void ) movement = 0; - if(keydown(KEY_SHIFT)) {Create_Explosion();} + if(keydown(KEY_F3)) {Create_Explosion();} + if(keydown(KEY_F1)) {Create_Player_Shoot(0);} + if(keydown(KEY_F2)) {Create_Player_Shoot(1);} if(keydown(KEY_EXIT)) {exitToOS = true; }; @@ -153,31 +193,31 @@ static void get_inputs( void ) if(keydown(KEY_9)) {record = false; }; if(keydown(KEY_DEL)) {textoutput = true;}; - if(keydown(KEY_F1)) {texttodraw=0;} - if(keydown(KEY_F2)) {texttodraw=1;} - if(keydown(KEY_F3)) {texttodraw=2;} - if(keydown(KEY_F4)) {texttodraw=3;} + if(keydown(KEY_SHIFT) && keydown(KEY_F1)) {texttodraw=0;} + if(keydown(KEY_SHIFT) && keydown(KEY_F2)) {texttodraw=1;} + if(keydown(KEY_SHIFT) && keydown(KEY_F3)) {texttodraw=2;} + if(keydown(KEY_SHIFT) && keydown(KEY_F4)) {texttodraw=3;} if(keydown(KEY_LEFT)) { - if(playerX>speed) + if(playerX>img_mainship1.width/2+speed) playerX-=speed; } if(keydown(KEY_RIGHT)) { - if(playerXspeed) + if(playerY>img_mainship1.height/2+speed) playerY-=speed; } if(keydown(KEY_DOWN)) { - if(playerY=1) Azur_draw_text(1,01, " FPS = %.0f", (float) (1000000.0f / elapsedTime) ); - if (texttodraw>=1) Azur_draw_text(1,11, "Parts = %d", MyParticles.size() ); + if (texttodraw>=1) Azur_draw_text(1,01, "FPS = %.0f", (float) (1000000.0f / elapsedTime) ); + if (texttodraw>=1) Azur_draw_text(1,11, "Part.= %d - Bull.= %d", MyParticles.size(), MyPlayerBullets.size() ); if (texttodraw>=2) Azur_draw_text(1,31, "Update = %.0f mc secs", (float) time_update ); if (texttodraw>=2) Azur_draw_text(1,41, "Render = %.0f mc secs", (float) time_render ); @@ -332,19 +372,22 @@ int main(void) if (texttodraw>=3) Azur_draw_text(1,101, "Mem Peak Used : %d", _uram_stats->peak_used_memory + extram_stats->peak_used_memory ); if (texttodraw>=3) Azur_draw_text(1,121, "Size of Particles : %d bytes", sizeof(Particle) ); - + if (texttodraw>=3) Azur_draw_text(1,131, "Size of Bullets : %d bytes", sizeof(Bullet) ); #endif + + azrp_starfield(); + + for(auto& b : MyPlayerBullets) + b->Render(); + + azrp_image_p8_effect(bossX-img_mainship2.width/2, bossY-img_mainship2.height/2, &img_mainship2, IMAGE_VFLIP); + for(auto& p : MyParticles) p->Render(); - - - azrp_starfield(); - azrp_image_p8_effect(bossX, bossY, &img_mainship2, IMAGE_VFLIP); - - azrp_image_p8(playerX, playerY, &img_mainship1, DIMAGE_NONE); + azrp_image_p8(playerX-img_mainship1.width/2, playerY-img_mainship1.height/2, &img_mainship1, DIMAGE_NONE); azrp_update();