particle manager & shatter

This commit is contained in:
KikooDX 2021-11-10 20:44:53 +01:00
parent ff0c0e9764
commit b21def5052
12 changed files with 97 additions and 1 deletions

View File

@ -19,6 +19,8 @@ set(SOURCES
src/util.c
src/camera.c
src/anim.c
src/shatter.c
src/particles.c
)
set(LEVELS
@ -27,6 +29,7 @@ set(LEVELS
set(ASSETS
res/tileset.png
res/shatter.png
${LEVELS}
)

View File

@ -11,6 +11,7 @@ struct Anim {
int life_ini;
int life;
int loop;
void (*callback)(struct Anim *);
};
struct Anim anim_new(bopti_image_t *, int x, int y, int frame_width,

9
inc/particles.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#include "anim.h"
#define MAX_PARTICLES 64
void particles_init(void);
void particles_add(struct Anim);
void particles_update(void);
void particles_draw(void);

3
inc/shatter.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
void shatter(int x, int y);

View File

@ -1,3 +1,6 @@
tileset.png:
type: bopti-image
name: bimg_tileset
shatter.png:
type: bopti-image
name: bimg_shatter

BIN
res/shatter.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 637 B

View File

@ -16,6 +16,7 @@ anim_new(bopti_image_t *texture, int x, int y, int frame_width,
anim.life_ini = frame_duration * texture->width / frame_width;
anim.life = anim.life_ini;
anim.loop = loop;
anim.callback = NULL;
return anim;
}
@ -27,6 +28,8 @@ anim_update(struct Anim *a)
a->life--;
if (a->life % a->frame_duration == 0)
a->frame++;
if (!a->life && a->callback != NULL)
a->callback(a);
/* loop */
if (!a->life && a->loop) {
a->frame = 0;

View File

@ -87,7 +87,7 @@ level_set(int x, int y, enum Tile v)
{
if (x < 0 || y < 0 || x >= self.width || y >= self.height)
return;
self.data[x + y * self.size] = v;
self.data[x + y * self.width] = v;
}
void

View File

@ -2,6 +2,7 @@
#include "conf.h"
#include "input.h"
#include "level.h"
#include "particles.h"
#include "player.h"
#include "vec.h"
#include <gint/cpu.h>
@ -25,6 +26,7 @@ main(void)
GINT_CALL(callback, &has_ticked));
timer_start(timer);
input_init();
particles_init();
level_load(&lvl_test);
player_init(level_find(TILE_PLAYER));
camera_init(player_pos());
@ -52,6 +54,7 @@ main(void)
static void
update(void)
{
particles_update();
input_update();
player_update();
camera_update();
@ -63,6 +66,7 @@ draw(void)
dclear(C_BLACK);
level_draw();
player_draw();
particles_draw();
dupdate();
}

39
src/particles.c Normal file
View File

@ -0,0 +1,39 @@
#include "particles.h"
#include "anim.h"
static struct Anim particles[MAX_PARTICLES];
void
particles_init(void)
{
int i = MAX_PARTICLES;
while (i-- > 0)
particles[i] = anim_new(NULL, 0, 0, 1, 0, 0);
}
void
particles_add(struct Anim a)
{
/* find empty spot */
int i = MAX_PARTICLES;
while (i-- > 0)
if (!particles[i].life)
break;
particles[i] = a;
}
void
particles_update(void)
{
int i = MAX_PARTICLES;
while (i-- > 0)
anim_update(&particles[i]);
}
void
particles_draw(void)
{
int i = MAX_PARTICLES;
while (i-- > 0)
anim_draw(&particles[i]);
}

View File

@ -3,6 +3,7 @@
#include "conf.h"
#include "input.h"
#include "level.h"
#include "shatter.h"
#include "util.h"
#include "vec.h"
#include <gint/display.h>
@ -41,6 +42,9 @@ player_update(void)
self.spd.y *= 1.0f - AIR_RESISTANCE;
self.spd.y += GRAVITY;
if (input_pressed(K_DOWN))
shatter(self.pos.x, self.pos.y + PLAYER_HEIGHT);
player_move(player_update_rem());
}

27
src/shatter.c Normal file
View File

@ -0,0 +1,27 @@
#include "shatter.h"
#include "anim.h"
#include "conf.h"
#include "level.h"
#include "particles.h"
#include "tile.h"
#include "vec.h"
#include <gint/display.h>
static void callback(struct Anim *);
void
shatter(int x, int y)
{
extern bopti_image_t bimg_shatter;
const int ax = (int)(x / TILE_SIZE) * TILE_SIZE;
const int ay = (int)(y / TILE_SIZE) * TILE_SIZE;
struct Anim p = anim_new(&bimg_shatter, ax, ay, TILE_SIZE, 2, false);
p.callback = callback;
particles_add(p);
}
static void
callback(struct Anim *a)
{
level_set_px(a->pos.x, a->pos.y, TILE_PLAYER);
}