Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

NumBlocks: block-break particles

Mason Chenaaba0d1bacfc
diff --git a/games/numblocks/src/entity.c b/games/numblocks/src/entity.c
index a30c5a37..cb7080a0 100644
--- a/games/numblocks/src/entity.c
+++ b/games/numblocks/src/entity.c
@@ -8,6 +8,45 @@
Entity ents[N_ENT];
+/* ---------------------------------------------------------------- particles (EntityDiggingFX) */
+typedef struct { float x, y, z, vx, vy, vz; uint16_t c; uint8_t age, life; } Particle;
+#define N_PART 32
+Particle parts[N_PART];
+
+/* breaking a block: its bits, coloured like its texture, fly out and fall (EffectRenderer.addBlockDestroyEffects) */
+void particles_break(int x, int y, int z, int b) {
+ int tex = blk_tex[b][2];
+ for (int k = 0; k < 12; k++) {
+ Particle *p = &parts[rnd(N_PART)];
+ int u = rnd(16), v = rnd(16);
+ uint8_t q = tex_px[tex][(v * 16 + u) >> 1];
+ int i = (u & 1) ? q >> 4 : q & 15;
+ if (!i && (tex_flags[tex] & 0x20)) continue;
+ p->c = tex_pal[tex][i];
+ if (i >= (tex_flags[tex] & 0x1F)) p->c = 0x5CA9; /* (tinted: a grass green) */
+ p->x = x + 0.2f + rndf() * 0.6f, p->y = y + 0.2f + rndf() * 0.6f, p->z = z + 0.2f + rndf() * 0.6f;
+ p->vx = (p->x - x - 0.5f) * 0.3f + (rndf() - 0.5f) * 0.1f;
+ p->vy = (p->y - y - 0.5f) * 0.3f + 0.1f + rndf() * 0.1f;
+ p->vz = (p->z - z - 0.5f) * 0.3f + (rndf() - 0.5f) * 0.1f;
+ p->age = 0;
+ p->life = (uint8_t)(4 / (rndf() * 0.9f + 0.1f));
+ }
+}
+
+static void particles_tick(void) {
+ for (int i = 0; i < N_PART; i++) {
+ Particle *p = &parts[i];
+ if (p->age >= p->life) continue;
+ p->age++;
+ p->vy -= 0.04f;
+ float np[3] = {p->x, p->y, p->z}, v[3] = {p->vx, p->vy, p->vz};
+ bool ground = phys_move(np, v, 0.1f, 0.1f);
+ p->x = np[0], p->y = np[1], p->z = np[2];
+ p->vx = v[0] * 0.98f, p->vy = v[1] * 0.98f, p->vz = v[2] * 0.98f;
+ if (ground) p->vx *= 0.7f, p->vz *= 0.7f;
+ }
+}
+
Entity *ent_new(int type, float x, float y, float z) {
Entity *e = NULL;
for (int i = 0; i < N_ENT; i++)
@@ -102,6 +141,7 @@ static void item_tick(Entity *e) {
}
void ents_tick(void) {
+ particles_tick();
for (int i = 0; i < N_ENT; i++) {
Entity *e = &ents[i];
if (e->type == E_NONE) continue;
diff --git a/games/numblocks/src/nb.h b/games/numblocks/src/nb.h
index 311acbce..87107bfa 100644
--- a/games/numblocks/src/nb.h
+++ b/games/numblocks/src/nb.h
@@ -165,6 +165,7 @@ extern Entity ents[N_ENT];
Entity *ent_new(int type, float x, float y, float z);
void ent_drop(int id, int count, int dmg, float x, float y, float z, bool thrown);
void ents_tick(void);
+void particles_break(int x, int y, int z, int b); /* a block's bits flying out */
void mob_tick(Entity *e);
void mobs_spawn(void);
void explode(float x, float y, float z, float power);
diff --git a/games/numblocks/src/player.c b/games/numblocks/src/player.c
index 31cd86c3..a6bb84f8 100644
--- a/games/numblocks/src/player.c
+++ b/games/numblocks/src/player.c
@@ -276,6 +276,7 @@ static void break_block(int x, int y, int z, bool drops) {
if (b == B_AIR || depth > 8) return;
depth++;
if (b == B_CHEST || b == B_FURNACE || b == B_FURNACE_LIT) tiles_removed(x, y, z);
+ if (depth == 1 && blk_model[b] != M_LIQUID) particles_break(x, y, z, b);
world_set(x, y, z, B_AIR);
if (drops && pl.mode == 0) {
Stack out[2];
diff --git a/games/numblocks/src/render.c b/games/numblocks/src/render.c
index 07c42b58..52b7ff4b 100644
--- a/games/numblocks/src/render.c
+++ b/games/numblocks/src/render.c
@@ -342,7 +342,7 @@ static uint16_t trace(float dx, float dy, float dz) {
shp[0][0] = shp[0][1] = shp[0][2] = 0, shp[0][3] = shp[0][5] = 16, shp[0][4] = LIQH[lv];
nbx = 1;
} else nbx = block_boxes(b, x + vc_x0, y + vc_y0, z + vc_z0, shp);
- Hit h, hk;
+ Hit h = {0}, hk;
h.t = 1e9f;
for (int k = 0; k < nbx; k++)
if (hit_box(shp[k], lx, ly, lz, dx, dy, dz, &hk) && hk.t < h.t) h = hk;
@@ -920,8 +920,35 @@ static bool item_hit(const Draw *dr, const float *d, float *best_t, uint16_t *co
return true;
}
+/* the particles: little squares, a tenth of a block, lit where they are */
+typedef struct { float x, y, z, vx, vy, vz; uint16_t c; uint8_t age, life; } Particle;
+extern Particle parts[32];
+static void particles_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {
+ for (int i = 0; i < 32; i++) {
+ const Particle *p = &parts[i];
+ if (p->age >= p->life) continue;
+ float x = p->x - vc_x0, y = p->y - vc_y0, z = p->z - vc_z0, sx, sy;
+ if (!project(x, y, z, &sx, &sy)) continue;
+ float rx = x - ox, ry = y - oy, rz = z - oz;
+ float t = rx * fwx + ry * fwy + rz * fwz; /* (the ray's length along the view, as zbuf keeps it) */
+ int half = (int)(0.05f / t / TAN_V * RH / 2 + 0.5f);
+ if (half < 1) half = 1;
+ int cx = (int)floorf(x), cy = (int)floorf(y), cz = (int)floorf(z);
+ int lb = (unsigned)cx < VCX && (unsigned)cy < VCY && (unsigned)cz < VCZ ? vl[VC_I(cx, cy, cz)] : 15;
+ uint16_t c = shade565(p->c, light_shade(0, lb));
+ for (int yy = (int)sy - half + 1; yy <= (int)sy + half - 1 + (half == 1); yy++) {
+ if (yy < py0 || yy >= py0 + rows || yy < 0) continue;
+ for (int xx = (int)sx - half + 1; xx <= (int)sx + half - 1 + (half == 1); xx++) {
+ if ((unsigned)xx >= RW || zb[yy - py0][xx] <= t) continue;
+ cb[yy - py0][xx] = c;
+ }
+ }
+ }
+}
+
static void ents_strip(int py0, int rows, uint16_t (*cb)[RW], float (*zb)[RW]) {
Box bx[MAX_PARTS];
+ particles_strip(py0, rows, cb, zb);
for (int k = 0; k < ndraws; k++) {
const Draw *dr = &draws[k];
if (dr->y1 <= py0 || dr->y0 >= py0 + rows) continue;