diff --git a/vga_test/main/app.h b/vga_test/main/app.h index 729f8d9..645543e 100644 --- a/vga_test/main/app.h +++ b/vga_test/main/app.h @@ -13,6 +13,11 @@ typedef enum { CONTENT_IMAGE, CONTENT_HALLOWEEN, // new modes go at the end: the index is saved in NVS CONTENT_HAUNT, + CONTENT_GHOSTS, + CONTENT_EYES, + CONTENT_BATS, + CONTENT_PUMPKIN, + CONTENT_SCARE, CONTENT_COUNT, } content_id_t; diff --git a/vga_test/main/content.c b/vga_test/main/content.c index 5f137e5..9bd0a6a 100644 --- a/vga_test/main/content.c +++ b/vga_test/main/content.c @@ -25,6 +25,11 @@ static const struct { [CONTENT_IMAGE] = {"image", "Image"}, [CONTENT_HALLOWEEN] = {"halloween", "Halloween scene"}, [CONTENT_HAUNT] = {"haunt", "Haunting (projection)"}, + [CONTENT_GHOSTS] = {"ghosts", "Ghosts"}, + [CONTENT_EYES] = {"eyes", "Eyes in the dark"}, + [CONTENT_BATS] = {"bats", "Bats"}, + [CONTENT_PUMPKIN] = {"pumpkin", "Jack-o'-lantern"}, + [CONTENT_SCARE] = {"scare", "BOO! ghost"}, }; const char *content_key(content_id_t c) @@ -937,13 +942,23 @@ static bool halloween_draw(uint8_t *fb, int buf) // --------------------------------------------------------------------------- // Haunting, for projecting onto a scrim: black projects as nothing, so the -// background stays black and only the spooks are lit. +// background stays black and only the spooks are lit. The same code runs the +// solo modes (ghosts, eyes, bats, jack-o'-lantern, BOO!) with one kind each. -#define HAUNT_GHOSTS 3 -#define HAUNT_EYES 4 -#define HAUNT_BATS 4 +#define HAUNT_GHOSTS 6 +#define HAUNT_EYES 8 +#define HAUNT_BATS 10 #define HAUNT_SPRITES (HAUNT_GHOSTS + HAUNT_EYES + HAUNT_BATS + 1) +typedef enum { PUMPKINS_NONE, PUMPKINS_PAIR, PUMPKINS_BIG } pumpkins_t; + +typedef struct { + int ghosts, eyes, bats; + pumpkins_t pumpkins; + bool scare; + int scare_first_min, scare_first_max, scare_min, scare_max; // seconds +} haunt_cfg_t; + typedef enum { SPOOK_HIDDEN, SPOOK_IN, SPOOK_SHOWN, SPOOK_OUT } spook_state_t; typedef struct { @@ -963,7 +978,10 @@ typedef struct { static haunt_ghost_t hghosts[HAUNT_GHOSTS]; static haunt_eyes_t heyes[HAUNT_EYES]; static flyer_t hbats[HAUNT_BATS]; +static int hbat_scale[HAUNT_BATS]; static pumpkin_t hpumpkins[2]; +static int num_hpumpkins; +static haunt_cfg_t hc; static spook_box_t haunt_drawn[2][HAUNT_SPRITES]; static int scare_timer, scare_frame; static uint32_t haunt_frame; @@ -976,12 +994,30 @@ static int rand_range(int lo, int hi) return lo + (int)(rng() % (uint32_t)(hi - lo + 1)); } +static bool boxes_overlap(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh) +{ + return ax < bx + bw && bx < ax + aw && ay < by + bh && by < ay + ah; +} + static void ghost_respawn(haunt_ghost_t *g) { g->scale = rand_range(H / 75, H / 46); // 8-13 at 600 lines const int gw = 16 * g->scale, gh = 17 * g->scale; - g->x = rand_range(0, W - gw); - g->y = rand_range(10, H * 55 / 100 - gh / 2); + // Try a few spots for one clear of the other ghosts (they still drift + // through each other later) + for (int tries = 0; tries < 12; tries++) { + g->x = rand_range(0, W - gw); + // Keep above the pumpkins when there are any + g->y = rand_range(10, hc.pumpkins ? H * 55 / 100 - gh / 2 : H - gh - 30); + bool clear = true; + for (int i = 0; i < hc.ghosts && clear; i++) { + const haunt_ghost_t *o = &hghosts[i]; + clear = o == g || o->scale == 0 || !boxes_overlap(g->x, g->y - 18, gw, gh + 36, o->x, o->y - 18, 16 * o->scale, 17 * o->scale + 36); + } + if (clear) { + break; + } + } g->vx = (rng() % 2 ? 1 : -1) * (0.3f + (rng() % 100) / 150.0f); g->phase = (rng() % 628) / 100.0f; g->state = SPOOK_HIDDEN; @@ -992,46 +1028,111 @@ static void eyes_respawn(haunt_eyes_t *e) { const uint8_t colours[] = {rgb332(7, 7, 0), rgb332(7, 1, 0), rgb332(3, 7, 0), rgb332(7, 4, 0)}; e->size = rand_range(H / 30, H / 13); - e->x = rand_range(e->size * 3, W - e->size * 3); - e->y = rand_range(e->size, H * 70 / 100); + // Eyes stay put, so find a spot clear of the other pairs + for (int tries = 0; tries < 20; tries++) { + e->x = rand_range(e->size * 3, W - e->size * 3); + e->y = rand_range(e->size, hc.pumpkins ? H * 70 / 100 : H - e->size); + bool clear = true; + for (int i = 0; i < hc.eyes && clear; i++) { + const haunt_eyes_t *o = &heyes[i]; + // Bounding boxes with a margin of half an eye + clear = o == e || o->size == 0 || !boxes_overlap(e->x - e->size * 3, e->y - e->size, e->size * 6, e->size * 2, o->x - o->size * 3, o->y - o->size, o->size * 6, o->size * 2); + } + if (clear) { + break; + } + } e->colour = colours[rng() % 4]; e->state = SPOOK_HIDDEN; e->timer = rand_range(SECONDS(1), SECONDS(8)); e->blink = 0; } -static void haunt_enter(const show_t *s) +static void haunt_start(const haunt_cfg_t *cfg) { + hc = *cfg; rng_state = esp_random(); memset(canvas, BLACK, W * H); - hpumpkins[0] = (pumpkin_t){W * 16 / 100, H * 82 / 100, W * 15 / 100, H * 15 / 100}; - hpumpkins[1] = (pumpkin_t){W * 85 / 100, H * 85 / 100, W * 12 / 100, H * 12 / 100}; - pumpkin_draw(&hpumpkins[0]); - pumpkin_draw(&hpumpkins[1]); + if (hc.pumpkins == PUMPKINS_PAIR) { + hpumpkins[0] = (pumpkin_t){W * 16 / 100, H * 82 / 100, W * 15 / 100, H * 15 / 100}; + hpumpkins[1] = (pumpkin_t){W * 85 / 100, H * 85 / 100, W * 12 / 100, H * 12 / 100}; + num_hpumpkins = 2; + } else if (hc.pumpkins == PUMPKINS_BIG) { + hpumpkins[0] = (pumpkin_t){W / 2, H * 56 / 100, W * 30 / 100, H * 36 / 100}; + num_hpumpkins = 1; + } else { + num_hpumpkins = 0; + } + for (int i = 0; i < num_hpumpkins; i++) { + pumpkin_draw(&hpumpkins[i]); + } - for (int i = 0; i < HAUNT_GHOSTS; i++) { + memset(hghosts, 0, sizeof(hghosts)); + memset(heyes, 0, sizeof(heyes)); + for (int i = 0; i < hc.ghosts; i++) { ghost_respawn(&hghosts[i]); } - hghosts[0].timer = SECONDS(0.5); // something appears straight away - for (int i = 0; i < HAUNT_EYES; i++) { + if (hc.ghosts) { + hghosts[0].timer = SECONDS(0.5); // something appears straight away + } + for (int i = 0; i < hc.eyes; i++) { eyes_respawn(&heyes[i]); } - for (int i = 0; i < HAUNT_BATS; i++) { + if (hc.eyes && !hc.ghosts) { + heyes[0].timer = SECONDS(0.5); + } + for (int i = 0; i < hc.bats; i++) { hbats[i] = (flyer_t){(float)(rng() % (W * 3 / 2)), 30 + rng() % (H / 2), 1.5f + (rng() % 100) / 60.0f, (rng() % 628) / 100.0f}; + // A swarm on its own gets a mix of near and far bats + hbat_scale[i] = hc.ghosts ? H / 110 : rand_range(H / 160, H / 60); + if (!hc.ghosts) { + hbats[i].y = rand_range(10, H - 6 * hbat_scale[i] - 30); + hbats[i].speed *= hbat_scale[i] / (float)(H / 110); // nearer is faster + } } memset(haunt_drawn, 0, sizeof(haunt_drawn)); - scare_timer = rand_range(SECONDS(25), SECONDS(50)); + scare_timer = rand_range(SECONDS(hc.scare_first_min), SECONDS(hc.scare_first_max)); scare_frame = -1; haunt_frame = 0; } +static void haunt_enter(const show_t *s) +{ + haunt_start(&(haunt_cfg_t){3, 4, 4, PUMPKINS_PAIR, true, 25, 50, 45, 90}); +} + +static void ghosts_enter(const show_t *s) +{ + haunt_start(&(haunt_cfg_t){.ghosts = 5}); +} + +static void eyes_enter(const show_t *s) +{ + haunt_start(&(haunt_cfg_t){.eyes = 8}); +} + +static void bats_enter(const show_t *s) +{ + haunt_start(&(haunt_cfg_t){.bats = 10}); +} + +static void pumpkin_enter(const show_t *s) +{ + haunt_start(&(haunt_cfg_t){.pumpkins = PUMPKINS_BIG}); +} + +static void scare_enter(const show_t *s) +{ + haunt_start(&(haunt_cfg_t){.scare = true, .scare_first_min = 2, .scare_first_max = 4, .scare_min = 10, .scare_max = 25}); +} + static void haunt_tick(void) { haunt_frame++; if (haunt_frame % 4 == 0) { const uint8_t glows[] = {rgb332(7, 7, 0), rgb332(7, 6, 0), rgb332(7, 5, 0), rgb332(7, 7, 1), rgb332(7, 6, 0)}; - for (int i = 0; i < 2; i++) { + for (int i = 0; i < num_hpumpkins; i++) { if (rng() % 3 == 0) { pumpkin_face(&hpumpkins[i], glows[rng() % sizeof(glows)]); int x, y, w, h; @@ -1072,7 +1173,7 @@ static bool haunt_draw(uint8_t *fb, int buf) int n = 0; // Eyes in the dark: open, blink now and then, close, move somewhere else - for (int i = 0; i < HAUNT_EYES; i++, n++) { + for (int i = 0; i < hc.eyes; i++, n++) { haunt_eyes_t *e = &heyes[i]; bool respawn; // The "fade" here is how open the eyes are @@ -1104,7 +1205,7 @@ static bool haunt_draw(uint8_t *fb, int buf) } // Ghosts fading in and out - for (int i = 0; i < HAUNT_GHOSTS; i++, n++) { + for (int i = 0; i < hc.ghosts; i++, n++) { haunt_ghost_t *g = &hghosts[i]; bool respawn; float bright = spook_step(&g->state, &g->timer, SECONDS(5), SECONDS(12), SECONDS(1), SECONDS(6), &respawn); @@ -1134,14 +1235,14 @@ static bool haunt_draw(uint8_t *fb, int buf) // Bats const uint8_t bat_colours[] = {rgb332(3, 0, 2), rgb332(7, 0, 0)}; - for (int i = 0; i < HAUNT_BATS; i++, n++) { + for (int i = 0; i < hc.bats; i++, n++) { flyer_t *b = &hbats[i]; b->x -= b->speed; b->phase += 0.07f; - const int scale = H / 110; + const int scale = hbat_scale[i]; if (b->x < -15 * scale) { b->x = W + rng() % 300; - b->y = 30 + rng() % (H / 2); + b->y = hc.ghosts ? 30 + rng() % (H / 2) : rand_range(10, H - 6 * scale - 30); } int x = (int)b->x, y = (int)(b->y + 22 * sinf(b->phase)); bool up = ((haunt_frame / 5) + i) & 1; @@ -1150,7 +1251,7 @@ static bool haunt_draw(uint8_t *fb, int buf) } // Now and then a ghost rushes at the audience - if (scare_frame < 0 && --scare_timer <= 0) { + if (hc.scare && scare_frame < 0 && --scare_timer <= 0) { scare_frame = 0; } if (scare_frame >= 0) { @@ -1164,7 +1265,7 @@ static bool haunt_draw(uint8_t *fb, int buf) drawn[n] = (spook_box_t){x, y, x + gw, y + gh}; if (++scare_frame > grow + hold) { scare_frame = -1; - scare_timer = rand_range(SECONDS(45), SECONDS(90)); + scare_timer = rand_range(SECONDS(hc.scare_min), SECONDS(hc.scare_max)); } } return true; @@ -1182,6 +1283,11 @@ static const content_ops_t ops[CONTENT_COUNT] = { [CONTENT_IMAGE] = {image_enter, NULL, NULL}, [CONTENT_HALLOWEEN] = {halloween_enter, halloween_tick, halloween_draw}, [CONTENT_HAUNT] = {haunt_enter, haunt_tick, haunt_draw}, + [CONTENT_GHOSTS] = {ghosts_enter, haunt_tick, haunt_draw}, + [CONTENT_EYES] = {eyes_enter, haunt_tick, haunt_draw}, + [CONTENT_BATS] = {bats_enter, haunt_tick, haunt_draw}, + [CONTENT_PUMPKIN] = {pumpkin_enter, haunt_tick, haunt_draw}, + [CONTENT_SCARE] = {scare_enter, haunt_tick, haunt_draw}, }; const content_ops_t *content_ops(content_id_t c)