[FEATURE] bullet shooting

This commit is contained in:
SemvdH
2021-05-20 16:18:18 +02:00
parent bc5ee01ae7
commit 8c7aa08ef7
4 changed files with 72 additions and 13 deletions

View File

@@ -5,6 +5,7 @@
#include <psp2/ctrl.h>
#include <psp2/kernel/processmgr.h>
#include <sys/types.h>
#include <vita2d.h>
@@ -23,10 +24,10 @@ size_t running = 1;
stick_data left_stick = {0, 0}, right_stick = {0, 0};
SceCtrlData pad;
size_t cross_pressed = 0;
uint8_t cross_pressed = 0;
size_t bullet_count = 0;
size_t current_bullet = 0;
uint8_t current_bullet = 0;
BULLET bullets[255];
vita2d_pgf *pgf;
@@ -34,6 +35,9 @@ vita2d_pvf *pvf;
SceUInt64 deltaTime = 0; // delta time in ms
SceKernelSysClock sysclock;
timing_timer bullet_timer = {0, 250, 0}; // 0 as starting time, 400 ms timeout, not elapsed
timer_t bullt = 0;
float x1_pos = 300, y1_pos = 50, x2_pos = 400, y2_pos = 50;
@@ -55,7 +59,7 @@ void generate_bullet()
bullets[current_bullet].active = 1;
bullets[current_bullet].x = x1_pos;
bullets[current_bullet].y = y1_pos;
bullets[current_bullet].color = RGBA8(100, 100, 0, 255);
bullets[current_bullet].color = RGBA8(255, 100, 0, 255);
current_bullet = (current_bullet + 1) % 254;
}
@@ -74,7 +78,7 @@ void init()
for (int i = 0; i < 255; i++)
{
BULLET temp = {0, 0, 100, RGBA8(0, i, 255, 255)};
BULLET temp = {0, 0, 100, RGBA8(0, i, 255, 255), 300.0};
bullets[i] = temp;
}
}
@@ -93,6 +97,9 @@ void update()
if (pad.buttons & SCE_CTRL_CROSS)
cross_pressed = 1;
timing_update_timer(&bullet_timer, deltaTime); // update timer
timing_check_timer_elapsed(&bullet_timer);
ctrl_input_get_leftstick(&pad, &left_stick);
if (abs(left_stick.x) > 15)
@@ -109,14 +116,19 @@ void update()
if (y1_pos >= SCREEN_HEIGTH)
y1_pos = SCREEN_HEIGTH - 1;
// TODO move to seperate file
if (cross_pressed)
generate_bullet();
{
if (bullet_timer.elapsed)
{
generate_bullet();
bullet_timer.elapsed = 0;
}
}
// TODO move to seperate file
for (int i = 0; i < 255; i++)
{
bullets[i].y -= 100.0 * (deltaTime / 1000.0);
bullets[i].y -= bullets[i].movement_speed * (deltaTime / 1000.0);
if (bullets[i].y <= 0)
bullets[i].active = 0;
@@ -141,6 +153,10 @@ void draw()
vita2d_pvf_draw_text(pvf, 700, 80, RGBA8(0, 255, 0, 255), 1.0f, fps);
char timertext[100];
sprintf(timertext, "time %lu", bullet_timer.time);
vita2d_pgf_draw_text(pgf, 10, 30, RGBA8(0, 255, 150, 255), 1.0f, timertext);
for (int i = 0; i < 255; i++)
{
sprites_draw_bullet(&bullets[i]);

View File

@@ -11,16 +11,18 @@ typedef struct player_sprite_t
/* data to be added, pos, active (?)*/
} PLAYER;
/**
* @brief a struct that holds a bullet sprite, basically a rectangle
* there will be at most a few bullets available
*/
typedef struct bullet_sprite_t
{
size_t active; // whether or not the bullet should be drawn (0 or 1)
float x; // the x position
float y; // the y position
unsigned int color; // color of the bullet
size_t active; // whether or not the bullet should be drawn (0 or 1)
float x; // the x position
float y; // the y position
unsigned int color; // color of the bullet
float movement_speed; // speed of the bullet (how much it should move each frame)
} BULLET;

View File

@@ -17,3 +17,19 @@ uint8_t timing_get_fps(SceUInt64 dt)
{
return (1 / (dt / 1000.0));
}
void timing_update_timer(timing_timer* timer, SceUInt64 dt)
{
timer->time += dt;
}
uint8_t timing_check_timer_elapsed(timing_timer* timer)
{
if (timer->time > timer->timeout)
{
timer->time = 0;
timer->elapsed = 1;
return 1;
}
return 0;
}

View File

@@ -2,6 +2,14 @@
#define TIMING_H
#include <psp2/kernel/processmgr.h>
#include <sys/types.h>
typedef struct
{
timer_t time;
SceUInt64 timeout;
uint8_t elapsed;
} timing_timer;
/**
* @brief gets the delta time in ms. (the duration since this function was last called)
@@ -9,7 +17,8 @@
* @param sysclock the system kernel clock pointer to use
* @return SceUInt64 the delta time in ms
*/
SceUInt64 timing_get_deltatime(SceKernelSysClock *sysclock);
SceUInt64
timing_get_deltatime(SceKernelSysClock *sysclock);
/**
* @brief calculates the fps based on the given delta time
@@ -18,4 +27,20 @@ SceUInt64 timing_get_deltatime(SceKernelSysClock *sysclock);
* @return uint8_t the fps
*/
uint8_t timing_get_fps(SceUInt64 dt);
/**
* @brief updates a timer with the last frametime
*
* @param timer the timer to update
* @param dt the deltatime
*/
void timing_update_timer(timing_timer* timer, SceUInt64 dt);
/**
* @brief checks if the given timer has elapsed or not
*
* @param timer the timer to check
* @return uint8_t 0 if false, nonzero if true
*/
uint8_t timing_check_timer_elapsed(timing_timer* timer);
#endif