added ennemies and bullet impacts

This commit is contained in:
Sylvain PILLOT 2023-01-21 14:04:58 +01:00
parent cdce9ab3c5
commit 13c384eeae
6 changed files with 183 additions and 20 deletions

View File

@ -16,7 +16,9 @@ set(SOURCES
src/utilities.cpp
src/particles.cpp
src/bullet.cpp
src/ennemy.cpp
src/starfieldshader.cpp
# ...
)
set(ASSETS_cg

View File

@ -24,11 +24,13 @@ Bullet::Bullet( uint16_t lx, uint16_t ly, uint8_t id )
{
sx = 0;
sy = libnum::num( -6 );
strength = 5;
}
else if (ID==1)
{
sx = 0;
sy = libnum::num( -3 );
strength = 2;
}
toberemoved = false;

View File

@ -17,6 +17,8 @@ class Bullet
libnum::num x, y;
libnum::num sx, sy;
uint8_t strength;
bool toberemoved;
};

79
src/ennemy.cpp Normal file
View File

@ -0,0 +1,79 @@
#include "ennemy.h"
#include "bullet.h"
#include <num/num.h>
extern bopti_image_t img_mainship2;
Ennemy::Ennemy( int16_t _x, int16_t _y, uint8_t _id )
{
x = libnum::num( _x );
y = libnum::num( _y );
dirx = -6;
diry = 1;
ID = _id;
if (ID==0)
{
width = img_mainship2.width/2;
height = img_mainship2.height/2;
speed = 1;
}
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
toberemoved = false;
if (ID==0) life = 100;
}
Ennemy::~Ennemy()
{
}
void Ennemy::Update( float dt )
{
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;
xmin = (int) x - width;
xmax = (int) x + width;
ymin = (int) y - height;
ymax = (int) y + height;
if (life<=0) toberemoved=true;
}
void Ennemy::Render( void )
{
if (ID==0) azrp_image_p8_effect(xmin, ymin, &img_mainship2, IMAGE_VFLIP);
}
bool Ennemy::Test_Impact( Bullet *projectile )
{
if (projectile->x >= xmin && projectile->x <= xmax && projectile->y >= ymin && projectile->y <= ymax )
{
life -= projectile->strength;
projectile->toberemoved = true;
return true;
}
else return false;
}
void Ennemy::Set_Speed_Vector( uint8_t _sp, uint8_t _xd, uint8_t _yd)
{
speed = _sp;
dirx = _xd;
diry = _yd;
}

42
src/ennemy.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef ENNEMY_H
#define ENNEMY_H
#include <azur/azur.h>
#include <azur/gint/render.h>
#include <cstdint>
#include <stdlib.h>
#include <num/num.h>
#include "bullet.h"
class Ennemy
{
public:
Ennemy( int16_t _x, int16_t _y, uint8_t _id );
~Ennemy();
void Update( float dt );
void Render( void );
bool Test_Impact( Bullet *projectile );
void Set_Speed_Vector( uint8_t _sp, uint8_t _xd, uint8_t _yd);
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;
private:
int8_t dirx, diry; // vector of the current direction of the ennemy (TODO : to implement more complex displacement pattern)
};
#endif

View File

@ -23,6 +23,7 @@
#include "utilities.h"
#include "particles.h"
#include "bullet.h"
#include "ennemy.h"
#include <vector>
#include <algorithm>
@ -44,19 +45,18 @@ bool exitToOS = false;
std::vector<Particle*> MyParticles;
std::vector<Bullet*> MyPlayerBullets;
std::vector<Ennemy*> MyEnnemies;
Starfield *MyStarField;
uint8_t texttodraw=1;
extern bopti_image_t img_mainship1, img_mainship2;
extern bopti_image_t img_mainship1;
uint8_t movement = -1;
uint16_t playerX = 0;
uint16_t playerY = 0;
uint16_t bossX = 0;
uint16_t bossY = 0;
static void hook_prefrag(int id, void *fragment, int size)
@ -116,17 +116,29 @@ void Create_Player_Shoot( uint8_t id )
}
void Create_Explosion( void )
void Create_Ennemies( void )
{
if(MyParticles.size()>=1) return;
Ennemy* e1 = new Ennemy( 198, 50, 0);
e1->Set_Speed_Vector( 1, -6, 1 );
MyEnnemies.push_back( e1 );
Ennemy* e2 = new Ennemy( 198, 50, 0);
e2->Set_Speed_Vector( 1, 6, 1 );
MyEnnemies.push_back( e2 );
Ennemy* e3 = new Ennemy( 198, 50, 0);
e3->Set_Speed_Vector( 1, -3, 3 );
MyEnnemies.push_back( e3 );
Ennemy* e4 = new Ennemy( 198, 50, 0);
e4->Set_Speed_Vector( 1, 3, 3 );
MyEnnemies.push_back( e4 );
}
void Create_Explosion( uint16_t xexplosion, uint16_t yexplosion )
{
srand(rtc_ticks());
uint16_t xexplosion = 198;//rand() % X_RESOL;
uint16_t yexplosion = 112;//rand() % Y_RESOL;
for(int i=0; i<=50; i++)
{
Particle *p = new Particle( xexplosion, yexplosion, i );
@ -140,6 +152,21 @@ static void update( float dt )
// all update stuff depending on time will be done here
for(auto& e : MyEnnemies)
e->Update( dt );
for(unsigned int i=0; i<MyEnnemies.size(); i++)
{
// Check if the property toberemoved has been set to "true" for particle deletion
if (MyEnnemies[i]->toberemoved == true)
{
Create_Explosion( (int) MyEnnemies[i]->x, (int) MyEnnemies[i]->y );
delete( MyEnnemies[i] );
MyEnnemies.erase( MyEnnemies.begin() + i );
}
}
for(unsigned int i=0; i<MyParticles.size(); i++)
{
MyParticles[i]->Update( dt );
@ -165,6 +192,16 @@ static void update( float dt )
}
}
for(unsigned int i=0; i<MyPlayerBullets.size(); i++)
{
for( unsigned j=0; j<MyEnnemies.size(); j++ )
{
if(MyEnnemies[j]->Test_Impact(MyPlayerBullets[i])==true)
{
//TODO : we can create a list of impacts here, to be rendered later on
}
}
}
//MyStarField->Update( dt );
}
@ -180,9 +217,7 @@ static void get_inputs( void )
}
movement = 0;
if(keydown(KEY_F3)) {Create_Explosion();}
//if(keydown(KEY_F3)) {Create_Explosion();}
if(keydown(KEY_F1)) {Create_Player_Shoot(0);}
if(keydown(KEY_F2)) {Create_Player_Shoot(1);}
@ -316,12 +351,12 @@ int main(void)
azrp_starfield_init( 100 );
Create_Ennemies( );
playerX = azrp_width/2;
playerY = azrp_height-img_mainship1.height/2;
bossX = azrp_width/2 ;
bossY = img_mainship2.height/2;
usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL };
usb_open(interfaces, GINT_CALL_NULL);
@ -361,7 +396,7 @@ int main(void)
#if(BIAS)
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>=1 && !MyEnnemies.empty()) Azur_draw_text(1,21, "Ennrmy Life= %d", MyEnnemies[0]->life );
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 );
if (texttodraw>=2) Azur_draw_text(1,51, ">Total = %.3f ml secs", (float) elapsedTime / 1000.0f );
@ -382,7 +417,8 @@ int main(void)
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& e : MyEnnemies)
e->Render();
for(auto& p : MyParticles)
p->Render();