- Enemy missile trails now drawn 3px thick via drawThickLine() with a bright head dot (white) that flashes against the trail color every couple of frames, like the Atari 2600 - eraseBombTrail() uses a thicker erase band than the draw (5px, 7px for wigging smart bombs) plus a 2px tip circle, so no trail residue is left behind when a missile dies or impacts
1271 lines
36 KiB
Arduino
1271 lines
36 KiB
Arduino
/**
|
|
* @file MissileCommand.ino
|
|
* @brief Classic Missile Command for the LilyGo T-Deck (ESP32-S3).
|
|
*
|
|
* Controls
|
|
* Trackball : move the aiming crosshair
|
|
* Trackball press : fire from the closest battery with ammo
|
|
* Screen tap : move crosshair + fire
|
|
* Keyboard A / S / D : fire from left / center / right battery
|
|
*
|
|
* Rendering: incremental drawing with TFT_eSPI (no full frame buffer).
|
|
* Missile trails are persistent lines, explosions are animated circles
|
|
* that repair the terrain underneath when they clear.
|
|
*
|
|
* Sound: I2S speaker via sound_engine (see sound_engine.cpp). Each wave
|
|
* opens with the original-style attack alarm (four falling tones), then
|
|
* layered sweeps/noise effects for launches, explosions and city hits.
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include <SPI.h>
|
|
#include <TFT_eSPI.h>
|
|
#include <Wire.h>
|
|
#include "utilities.h"
|
|
#include "TouchDrvGT911.hpp"
|
|
#include "sound_engine.h"
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Hardware
|
|
// ---------------------------------------------------------------------------
|
|
TFT_eSPI tft;
|
|
TouchDrvGT911 touch;
|
|
SoundEngine sound;
|
|
|
|
static bool touchAvail = false;
|
|
static bool kbAvail = false;
|
|
|
|
// Screen metrics (filled in during setup, rotation 1 -> 320x240)
|
|
static int scrW, scrH, groundY;
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Tunables
|
|
// ---------------------------------------------------------------------------
|
|
static const int FRAME_MS = 33; // ~30 fps
|
|
static const int CITY_COUNT = 6;
|
|
static const int AMMO_MAX = 10;
|
|
static const int MAX_BOMBS = 20;
|
|
static const int MAX_ROCKETS = 8;
|
|
static const int MAX_EXPL = 12;
|
|
static const int CROSS_STEP = 14; // px per trackball step
|
|
static const float ROCKET_SPEED = 6.0f; // px / frame
|
|
static const float ROCKET_RADIUS = 24.0f;
|
|
static const uint32_t BANNER_MS = 1500;
|
|
static const uint32_t BONUS_MS = 3400;
|
|
|
|
static const uint16_t EXPL_COLORS[3] = { TFT_WHITE, TFT_ORANGE, TFT_YELLOW };
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Entities
|
|
// ---------------------------------------------------------------------------
|
|
struct Bomb {
|
|
bool alive;
|
|
float x, y; // current tip position
|
|
float sx, sy; // origin
|
|
float tx, ty; // target
|
|
float dirx, diry; // unit direction
|
|
float speed; // px per frame
|
|
float wob; // wiggle phase (smart bombs)
|
|
bool smart;
|
|
};
|
|
|
|
struct Rocket {
|
|
bool alive;
|
|
float x, y;
|
|
float tx, ty;
|
|
float dirx, diry;
|
|
float speed;
|
|
};
|
|
|
|
struct Explosion {
|
|
bool alive;
|
|
float x, y;
|
|
float r, maxR, vel;
|
|
int hold;
|
|
};
|
|
|
|
static Bomb bombs[MAX_BOMBS];
|
|
static Rocket rockets[MAX_ROCKETS];
|
|
static Explosion expls[MAX_EXPL];
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Game state
|
|
// ---------------------------------------------------------------------------
|
|
enum GameState { ST_TITLE, ST_BANNER, ST_PLAYING, ST_BONUS, ST_GAMEOVER };
|
|
static GameState gameState = ST_TITLE;
|
|
static uint32_t stateT0 = 0;
|
|
static uint32_t frameCount = 0;
|
|
|
|
static int cityX[CITY_COUNT];
|
|
static bool cityAlive[CITY_COUNT];
|
|
static int BATX[3];
|
|
static bool batAlive[3];
|
|
static int batAmmo[3];
|
|
|
|
static int score = 0;
|
|
static int highScore = 0;
|
|
static int wave = 1;
|
|
|
|
static int bombsSpawned = 0;
|
|
static int waveQuota = 0;
|
|
static float bombSpeed = 0.5f; // px per frame
|
|
static uint32_t spawnIntervalMs = 1400;
|
|
static uint32_t lastSpawnMs = 0;
|
|
static int waveKills = 0;
|
|
static bool spawningStopped = false;
|
|
|
|
static struct {
|
|
int x, y; // current
|
|
int dx, dy; // last drawn position (erase here, NOT at px/py: several
|
|
// moves can happen per frame and would leave ghosts)
|
|
bool drawn;
|
|
bool moved;
|
|
} cross;
|
|
|
|
static uint32_t volOSDUntil = 0; // volume bar overlay timer
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Forward declarations
|
|
// ---------------------------------------------------------------------------
|
|
static void drawWorld();
|
|
static void drawHUD();
|
|
static void drawAmmoPyramid(int bat);
|
|
static void redrawGroundArea(float cx, float cy, float r);
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sound helpers
|
|
// ---------------------------------------------------------------------------
|
|
static void sfx(SndType t, uint16_t f0, uint16_t f1, uint16_t d, uint16_t a)
|
|
{
|
|
SndEvent e;
|
|
e.type = t;
|
|
e.freq = f0;
|
|
e.freq2 = f1;
|
|
e.dur = d;
|
|
e.amp = a;
|
|
sound.play(e);
|
|
}
|
|
|
|
static void sfxTone(uint16_t f, uint16_t d, uint16_t a) { sfx(SND_TONE, f, f, d, a); }
|
|
static void sfxSweep(uint16_t f0, uint16_t f1, uint16_t d, uint16_t a) { sfx(SND_SWEEP, f0, f1, d, a); }
|
|
static void sfxNoise(uint16_t d, uint16_t a) { sfx(SND_NOISE, 0, 0, d, a); }
|
|
|
|
static void sfxDeny() { sfxTone(220, 90, 8000); }
|
|
|
|
// Attack alarm, played once as a wave-start intro (like the original):
|
|
// four falling tones, then gameplay begins and the alarm is done.
|
|
static void sfxSirenIntro()
|
|
{
|
|
for (int i = 0; i < 4; i++) {
|
|
sfxSweep(760 - i * 40, 330, 280, 12000);
|
|
}
|
|
}
|
|
|
|
static void sfxFire()
|
|
{
|
|
sfxSweep(280, 1100, 70, 8000); // rising "pew"
|
|
sfxNoise(50, 9000); // launch puff
|
|
}
|
|
|
|
static void sfxKill() // enemy warhead destroyed
|
|
{
|
|
sfxNoise(150, 20000);
|
|
sfxSweep(500, 120, 150, 12000);
|
|
}
|
|
|
|
static void sfxBoom() // counter-missile explosion
|
|
{
|
|
sfxNoise(260, 24000);
|
|
sfxSweep(200, 55, 260, 16000); // deep rumble tail
|
|
}
|
|
|
|
static void sfxCityBoom()
|
|
{
|
|
sfxSweep(150, 40, 420, 22000); // very deep boom
|
|
sfxNoise(380, 24000);
|
|
}
|
|
|
|
static void sfxWaveClear()
|
|
{
|
|
sfxTone(800, 90, 9000);
|
|
sfxTone(1000, 90, 9000);
|
|
sfxTone(1300, 140, 9000);
|
|
}
|
|
|
|
static void sfxGameOver()
|
|
{
|
|
sfxSweep(600, 300, 220, 12000);
|
|
sfxSweep(450, 200, 220, 12000);
|
|
sfxSweep(300, 90, 420, 14000);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Layout / terrain drawing
|
|
// ---------------------------------------------------------------------------
|
|
static void layout()
|
|
{
|
|
BATX[0] = 18;
|
|
BATX[1] = scrW / 2;
|
|
BATX[2] = scrW - 18;
|
|
float frac[CITY_COUNT] = { 0.18f, 0.30f, 0.42f, 0.58f, 0.70f, 0.82f };
|
|
for (int i = 0; i < CITY_COUNT; i++) {
|
|
cityX[i] = (int)(scrW * frac[i]);
|
|
}
|
|
}
|
|
|
|
static void drawGround()
|
|
{
|
|
tft.fillRect(0, groundY, scrW, scrH - groundY, TFT_DARKGREEN);
|
|
tft.drawFastHLine(0, groundY, scrW, TFT_GREEN);
|
|
}
|
|
|
|
static void drawCity(int i)
|
|
{
|
|
int x = cityX[i];
|
|
if (cityAlive[i]) {
|
|
tft.fillRect(x - 10, groundY - 12, 7, 12, TFT_WHITE);
|
|
tft.fillRect(x - 2, groundY - 9, 5, 9, TFT_WHITE);
|
|
tft.fillRect(x + 4, groundY - 14, 6, 14, TFT_WHITE);
|
|
} else {
|
|
tft.fillRect(x - 10, groundY - 4, 20, 4, TFT_DARKGREY);
|
|
}
|
|
}
|
|
|
|
static void drawBattery(int i)
|
|
{
|
|
int x = BATX[i];
|
|
if (batAlive[i]) {
|
|
tft.fillTriangle(x - 5, groundY, x + 5, groundY, x, groundY - 7, TFT_BLUE);
|
|
} else {
|
|
tft.fillRect(x - 4, groundY - 3, 9, 3, TFT_DARKGREY);
|
|
}
|
|
drawAmmoPyramid(i);
|
|
}
|
|
|
|
// Classic pyramid of up-pointing triangles above each battery
|
|
static void drawAmmoPyramid(int bat)
|
|
{
|
|
int x = BATX[bat];
|
|
tft.fillRect(x - 12, groundY - 32, 25, 24, TFT_BLACK);
|
|
if (!batAlive[bat]) return;
|
|
|
|
int n = batAmmo[bat];
|
|
int drawn = 0;
|
|
for (int row = 0; row < 4 && drawn < n; row++) {
|
|
int count = 4 - row; // bottom row has 4
|
|
int rowY = groundY - 9 - row * 5; // base line of this row
|
|
for (int t = 0; t < count && drawn < n; t++, drawn++) {
|
|
int cx = x - (count - 1) * 3 + t * 6;
|
|
tft.fillTriangle(cx - 2, rowY, cx + 2, rowY, cx, rowY - 4, TFT_CYAN);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void stars(int n)
|
|
{
|
|
for (int i = 0; i < n; i++) {
|
|
int x = random(0, scrW);
|
|
int y = random(0, groundY - 50);
|
|
tft.drawPixel(x, y, random(10) < 8 ? TFT_DARKGREY : TFT_WHITE);
|
|
}
|
|
}
|
|
|
|
static void drawWorld()
|
|
{
|
|
tft.fillScreen(TFT_BLACK);
|
|
stars(70);
|
|
drawGround();
|
|
for (int i = 0; i < CITY_COUNT; i++) drawCity(i);
|
|
for (int i = 0; i < 3; i++) drawBattery(i);
|
|
drawHUD();
|
|
cross.drawn = false;
|
|
}
|
|
|
|
// Redraw the part of the terrain that an explosion circle just erased.
|
|
static void redrawGroundArea(float cx, float cy, float r)
|
|
{
|
|
(void)cy; // the strip is redrawn at ground level regardless of blast height
|
|
int x0 = (int)(cx - r) - 2;
|
|
int x1 = (int)(cx + r) + 2;
|
|
if (x0 < 0) x0 = 0;
|
|
if (x1 > scrW - 1) x1 = scrW - 1;
|
|
if (x1 < x0) return;
|
|
tft.fillRect(x0, groundY, x1 - x0 + 1, scrH - groundY, TFT_DARKGREEN);
|
|
tft.drawFastHLine(x0, groundY, x1 - x0 + 1, TFT_GREEN);
|
|
for (int i = 0; i < CITY_COUNT; i++) {
|
|
if (cityX[i] + 10 >= x0 && cityX[i] - 10 <= x1) drawCity(i);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
if (BATX[i] + 12 >= x0 && BATX[i] - 12 <= x1) drawBattery(i);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// HUD
|
|
// ---------------------------------------------------------------------------
|
|
static int scoreMult()
|
|
{
|
|
int m = 1 + (wave - 1) / 2;
|
|
return m > 6 ? 6 : m;
|
|
}
|
|
|
|
static void drawHUD()
|
|
{
|
|
tft.fillRect(0, 0, scrW, 16, TFT_BLACK);
|
|
tft.setTextFont(2);
|
|
tft.setTextColor(TFT_WHITE, TFT_BLACK);
|
|
tft.setTextDatum(TL_DATUM);
|
|
tft.drawString("WAVE " + String(wave), 4, 2);
|
|
tft.setTextDatum(TC_DATUM);
|
|
tft.drawString(String(score), scrW / 2, 2);
|
|
tft.setTextDatum(TR_DATUM);
|
|
tft.drawString("x" + String(scoreMult()) + " HI " + String(highScore), scrW - 4, 2);
|
|
tft.setTextDatum(MC_DATUM);
|
|
}
|
|
|
|
// Brief volume bar shown after U / V / M keypresses
|
|
static void drawVolumeOSD()
|
|
{
|
|
static bool shown = false;
|
|
bool want = millis() < volOSDUntil;
|
|
int bx = scrW - 108, by = 20;
|
|
if (want) {
|
|
shown = true;
|
|
tft.fillRect(bx, by, 104, 14, TFT_BLACK);
|
|
tft.drawRect(bx, by, 104, 14, TFT_DARKGREY);
|
|
int vol = sound.volume();
|
|
if (vol == 0) {
|
|
tft.setTextFont(2);
|
|
tft.setTextColor(TFT_RED, TFT_BLACK);
|
|
tft.setTextDatum(MC_DATUM);
|
|
tft.drawString("MUTE", bx + 52, by + 7);
|
|
} else {
|
|
for (int i = 0; i < SoundEngine::VOL_MAX; i++) {
|
|
if (i < vol) {
|
|
tft.fillRect(bx + 3 + i * 12, by + 3, 9, 8, TFT_GREEN);
|
|
}
|
|
}
|
|
}
|
|
} else if (shown) {
|
|
shown = false;
|
|
tft.fillRect(bx - 2, by - 2, 108, 18, TFT_BLACK);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Crosshair
|
|
// ---------------------------------------------------------------------------
|
|
static void drawCrossAt(int x, int y, uint16_t c)
|
|
{
|
|
tft.drawFastHLine(x - 5, y, 4, c);
|
|
tft.drawFastHLine(x + 1, y, 4, c);
|
|
tft.drawFastVLine(x, y - 5, 4, c);
|
|
tft.drawFastVLine(x, y + 1, 4, c);
|
|
}
|
|
|
|
static void renderCross()
|
|
{
|
|
if (!cross.drawn || cross.moved) {
|
|
if (cross.drawn) drawCrossAt(cross.dx, cross.dy, TFT_BLACK);
|
|
drawCrossAt(cross.x, cross.y, TFT_GREEN);
|
|
cross.dx = cross.x;
|
|
cross.dy = cross.y;
|
|
cross.drawn = true;
|
|
cross.moved = false;
|
|
}
|
|
}
|
|
|
|
static void clampCross()
|
|
{
|
|
if (cross.x < 2) cross.x = 2;
|
|
if (cross.x > scrW - 3) cross.x = scrW - 3;
|
|
if (cross.y < 18) cross.y = 18;
|
|
if (cross.y > groundY - 3) cross.y = groundY - 3;
|
|
}
|
|
|
|
static void moveCross(int dir)
|
|
{
|
|
if (gameState != ST_PLAYING && gameState != ST_BANNER) return;
|
|
switch (dir) {
|
|
case 0: cross.x += CROSS_STEP; break; // right
|
|
case 1: cross.y -= CROSS_STEP; break; // up
|
|
case 2: cross.x -= CROSS_STEP; break; // left
|
|
case 3: cross.y += CROSS_STEP; break; // down
|
|
}
|
|
clampCross();
|
|
cross.moved = true;
|
|
}
|
|
|
|
static void setCross(int x, int y)
|
|
{
|
|
if (gameState != ST_PLAYING && gameState != ST_BANNER) return;
|
|
cross.x = x;
|
|
cross.y = y;
|
|
clampCross();
|
|
cross.moved = true;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Entities
|
|
// ---------------------------------------------------------------------------
|
|
static void spawnExpl(float x, float y, float maxR, float vel)
|
|
{
|
|
for (int i = 0; i < MAX_EXPL; i++) {
|
|
if (!expls[i].alive) {
|
|
expls[i].alive = true;
|
|
expls[i].x = x;
|
|
expls[i].y = y;
|
|
expls[i].r = 0;
|
|
expls[i].maxR = maxR;
|
|
expls[i].vel = vel;
|
|
expls[i].hold = 0;
|
|
return;
|
|
}
|
|
}
|
|
// no free slot: replace the smallest one
|
|
int best = 0;
|
|
for (int i = 1; i < MAX_EXPL; i++) {
|
|
if (expls[i].r < expls[best].r) best = i;
|
|
}
|
|
expls[best].x = x;
|
|
expls[best].y = y;
|
|
expls[best].maxR = maxR;
|
|
expls[best].vel = vel;
|
|
expls[best].r = 0;
|
|
expls[best].hold = 0;
|
|
}
|
|
|
|
static int pickTargetX()
|
|
{
|
|
int cand[9];
|
|
int n = 0;
|
|
for (int i = 0; i < CITY_COUNT; i++) {
|
|
if (cityAlive[i]) cand[n++] = cityX[i];
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
if (batAlive[i]) cand[n++] = BATX[i];
|
|
}
|
|
if (n == 0) return random(10, scrW - 10);
|
|
int tx = cand[random(0, n)] + random(-6, 7);
|
|
if (tx < 4) tx = 4;
|
|
if (tx > scrW - 5) tx = scrW - 5;
|
|
return tx;
|
|
}
|
|
|
|
static void spawnBomb()
|
|
{
|
|
for (int i = 0; i < MAX_BOMBS; i++) {
|
|
Bomb &b = bombs[i];
|
|
if (b.alive) continue;
|
|
|
|
b.smart = (random(100) < (wave >= 3 ? (2 + wave > 30 ? 30 : 2 + wave) : 0));
|
|
b.sx = random(4, scrW - 4);
|
|
b.sy = 2;
|
|
b.tx = pickTargetX();
|
|
b.ty = groundY;
|
|
float dx = b.tx - b.sx;
|
|
float dy = b.ty - b.sy;
|
|
float len = sqrtf(dx * dx + dy * dy);
|
|
b.dirx = dx / len;
|
|
b.diry = dy / len;
|
|
b.speed = bombSpeed * (b.smart ? 1.25f : 1.0f);
|
|
b.x = b.sx;
|
|
b.y = b.sy;
|
|
b.wob = random(0, 100) * 0.06f;
|
|
b.alive = true;
|
|
bombsSpawned++;
|
|
return;
|
|
}
|
|
}
|
|
|
|
static bool anyBombAlive()
|
|
{
|
|
for (int i = 0; i < MAX_BOMBS; i++) if (bombs[i].alive) return true;
|
|
return false;
|
|
}
|
|
|
|
static bool anyRocketAlive()
|
|
{
|
|
for (int i = 0; i < MAX_ROCKETS; i++) if (rockets[i].alive) return true;
|
|
return false;
|
|
}
|
|
|
|
static bool anyExplAlive()
|
|
{
|
|
for (int i = 0; i < MAX_EXPL; i++) if (expls[i].alive) return true;
|
|
return false;
|
|
}
|
|
|
|
static bool waveDone()
|
|
{
|
|
return bombsSpawned >= waveQuota && !anyBombAlive() && !anyRocketAlive() && !anyExplAlive();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Firing
|
|
// ---------------------------------------------------------------------------
|
|
static void launch(int bat, int tx, int ty)
|
|
{
|
|
if (!batAlive[bat] || batAmmo[bat] <= 0) {
|
|
sfxDeny();
|
|
return;
|
|
}
|
|
for (int i = 0; i < MAX_ROCKETS; i++) {
|
|
Rocket &r = rockets[i];
|
|
if (r.alive) continue;
|
|
batAmmo[bat]--;
|
|
drawAmmoPyramid(bat);
|
|
|
|
r.x = BATX[bat];
|
|
r.y = groundY - 8;
|
|
r.tx = tx;
|
|
r.ty = ty;
|
|
float dx = tx - r.x;
|
|
float dy = ty - r.y;
|
|
float len = sqrtf(dx * dx + dy * dy);
|
|
if (len < 1) len = 1;
|
|
r.dirx = dx / len;
|
|
r.diry = dy / len;
|
|
r.speed = ROCKET_SPEED;
|
|
r.alive = true;
|
|
sfxFire();
|
|
return;
|
|
}
|
|
sfxDeny(); // no free rocket slot
|
|
}
|
|
|
|
static void fireAuto()
|
|
{
|
|
int best = -1;
|
|
int bestD = 1 << 30;
|
|
for (int i = 0; i < 3; i++) {
|
|
if (batAlive[i] && batAmmo[i] > 0) {
|
|
int d = abs(BATX[i] - cross.x);
|
|
if (d < bestD) {
|
|
bestD = d;
|
|
best = i;
|
|
}
|
|
}
|
|
}
|
|
if (best < 0) {
|
|
sfxDeny();
|
|
return;
|
|
}
|
|
launch(best, cross.x, cross.y);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Trail cleanup
|
|
// ---------------------------------------------------------------------------
|
|
// Thick line: `w` parallel lines offset perpendicular to the dominant axis.
|
|
// Missile trails are drawn 3px wide, so erase passes must be at least as
|
|
// wide or they leave residue.
|
|
static void drawThickLine(int x0, int y0, int x1, int y1, int w, uint16_t c)
|
|
{
|
|
int dx = (x1 > x0) ? x1 - x0 : x0 - x1;
|
|
int dy = (y1 > y0) ? y1 - y0 : y0 - y1;
|
|
if (dx >= dy) {
|
|
for (int i = 0; i < w; i++) {
|
|
int off = i - w / 2;
|
|
tft.drawLine(x0, y0 + off, x1, y1 + off, c);
|
|
}
|
|
} else {
|
|
for (int i = 0; i < w; i++) {
|
|
int off = i - w / 2;
|
|
tft.drawLine(x0 + off, y0, x1 + off, y1, c);
|
|
}
|
|
}
|
|
}
|
|
|
|
#define TRAIL_W 3 // enemy trail thickness (px)
|
|
|
|
// Does the segment (x0,y0)-(x1,y1) pass within `rad` of (px,py)?
|
|
static bool linePassesNear(int x0, int y0, int x1, int y1, int px, int py, int rad)
|
|
{
|
|
long dx = x1 - x0, dy = y1 - y0;
|
|
long len2 = dx * dx + dy * dy;
|
|
if (len2 == 0) {
|
|
long ddx = px - x0, ddy = py - y0;
|
|
return ddx * ddx + ddy * ddy <= (long)rad * rad;
|
|
}
|
|
long t = (long)(px - x0) * dx + (long)(py - y0) * dy;
|
|
if (t < 0) t = 0;
|
|
if (t > len2) t = len2;
|
|
int cx = x0 + (int)((long)t * dx / len2);
|
|
int cy = y0 + (int)((long)t * dy / len2);
|
|
long ddx = px - cx, ddy = py - cy;
|
|
return ddx * ddx + ddy * ddy <= (long)rad * rad;
|
|
}
|
|
|
|
// Enemy trails persist while the bomb falls (classic look, drawn TRAIL_W px
|
|
// thick with a 2px head dot), but vanish the moment the bomb is destroyed or
|
|
// reaches the ground, like the Atari 2600. The erase band is wider than the
|
|
// draw: smart bombs wiggle sideways off the straight path, so their erase
|
|
// must sweep a wider corridor.
|
|
static void eraseBombTrail(const Bomb &b)
|
|
{
|
|
drawThickLine((int)b.sx, (int)b.sy, (int)b.x, (int)b.y,
|
|
b.smart ? 7 : 5, TFT_BLACK);
|
|
tft.fillCircle((int)b.x, (int)b.y, 2, TFT_BLACK);
|
|
// repair anything the erase swept over
|
|
if (cross.drawn && linePassesNear((int)b.sx, (int)b.sy, (int)b.x, (int)b.y,
|
|
cross.dx, cross.dy, 8)) {
|
|
drawCrossAt(cross.dx, cross.dy, TFT_BLACK); // remove it now,
|
|
cross.drawn = false; // redraw next frame
|
|
}
|
|
redrawGroundArea(b.tx, (float)groundY, 14.0f); // target-end terrain
|
|
drawHUD(); // trails start inside the HUD band
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Updates
|
|
// ---------------------------------------------------------------------------
|
|
static void updateRockets()
|
|
{
|
|
for (int i = 0; i < MAX_ROCKETS; i++) {
|
|
Rocket &r = rockets[i];
|
|
if (!r.alive) continue;
|
|
|
|
// AA rockets have no trail (like the 2600): just a moving dot.
|
|
// Erase the previous dot, then draw the new position.
|
|
tft.fillCircle((int)r.x, (int)r.y, 2, TFT_BLACK);
|
|
|
|
float dx = r.tx - r.x;
|
|
float dy = r.ty - r.y;
|
|
float d = sqrtf(dx * dx + dy * dy);
|
|
if (d <= r.speed) {
|
|
r.alive = false;
|
|
spawnExpl(r.tx, r.ty, ROCKET_RADIUS, 1.6f);
|
|
sfxBoom();
|
|
} else {
|
|
r.x += r.dirx * r.speed;
|
|
r.y += r.diry * r.speed;
|
|
tft.fillCircle((int)r.x, (int)r.y, 2, TFT_CYAN);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void killCity(int i)
|
|
{
|
|
cityAlive[i] = false;
|
|
drawCity(i);
|
|
sfxCityBoom();
|
|
}
|
|
|
|
static void killBattery(int i)
|
|
{
|
|
batAlive[i] = false;
|
|
batAmmo[i] = 0;
|
|
drawBattery(i);
|
|
}
|
|
|
|
static void updateBombs()
|
|
{
|
|
for (int i = 0; i < MAX_BOMBS; i++) {
|
|
Bomb &b = bombs[i];
|
|
if (!b.alive) continue;
|
|
|
|
float edx = b.dirx;
|
|
float edy = b.diry;
|
|
if (b.smart) { // wiggle perpendicular to the flight path
|
|
b.wob += 0.55f;
|
|
float s = sinf(b.wob) * 0.4f;
|
|
edx = b.dirx - b.diry * s;
|
|
edy = b.diry + b.dirx * s;
|
|
float len = sqrtf(edx * edx + edy * edy);
|
|
edx /= len;
|
|
edy /= len;
|
|
}
|
|
|
|
// NOTE: keep float positions between frames; truncating to int and
|
|
// storing back would make slow missiles stall (0.5 px/frame rounds
|
|
// back to the same pixel forever).
|
|
float nxf = b.x + edx * b.speed;
|
|
float nyf = b.y + edy * b.speed;
|
|
uint16_t col = b.smart ? TFT_MAGENTA : TFT_RED;
|
|
|
|
// Restore the previous head dot to trail color (so the path stays a
|
|
// continuous 3px line), extend the trail, then draw the new head: a
|
|
// bright dot that flashes, like the 2600 version.
|
|
tft.fillCircle((int)b.x, (int)b.y, 2, col);
|
|
drawThickLine((int)b.x, (int)b.y, (int)nxf, (int)nyf, TRAIL_W, col);
|
|
uint16_t headCol = (frameCount % 4) < 2 ? TFT_WHITE : col;
|
|
tft.fillCircle((int)nxf, (int)nyf, 2, headCol);
|
|
b.x = nxf;
|
|
b.y = nyf;
|
|
|
|
// caught by an explosion?
|
|
for (int e = 0; e < MAX_EXPL; e++) {
|
|
Explosion &ex = expls[e];
|
|
if (!ex.alive || ex.r < 2) continue;
|
|
float ddx = b.x - ex.x;
|
|
float ddy = b.y - ex.y;
|
|
if (ddx * ddx + ddy * ddy < ex.r * ex.r) {
|
|
b.alive = false;
|
|
waveKills++;
|
|
score += 25 * scoreMult();
|
|
eraseBombTrail(b); // also refreshes the HUD with the new score
|
|
spawnExpl(b.x, b.y, 14.0f, 1.4f);
|
|
sfxKill();
|
|
break;
|
|
}
|
|
}
|
|
if (!b.alive) continue;
|
|
|
|
// reached the ground?
|
|
if (b.y >= b.ty) {
|
|
b.alive = false;
|
|
eraseBombTrail(b);
|
|
spawnExpl(b.tx, groundY - 4, 11.0f, 1.3f);
|
|
sfxBoom();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void updateExpls()
|
|
{
|
|
for (int i = 0; i < MAX_EXPL; i++) {
|
|
Explosion &e = expls[i];
|
|
if (!e.alive) continue;
|
|
|
|
float oldR = e.r;
|
|
if (e.hold > 0) {
|
|
e.hold--;
|
|
} else {
|
|
e.r += e.vel;
|
|
if (e.r >= e.maxR) {
|
|
e.r = e.maxR;
|
|
e.hold = 5;
|
|
e.vel = -1.6f;
|
|
} else if (e.r <= 0.5f && e.vel < 0) {
|
|
e.r = 0;
|
|
e.alive = false;
|
|
}
|
|
}
|
|
|
|
// erase old circle and repair the terrain it covered
|
|
if (oldR > 0.5f) {
|
|
tft.fillCircle((int)e.x, (int)e.y, (int)oldR, TFT_BLACK);
|
|
redrawGroundArea(e.x, e.y, oldR);
|
|
// if the crosshair was inside the erased circle, remove the
|
|
// remaining arms and redraw it next frame
|
|
float cdx = cross.dx - e.x;
|
|
float cdy = cross.dy - e.y;
|
|
if (cross.drawn && cdx * cdx + cdy * cdy < (oldR + 6) * (oldR + 6)) {
|
|
drawCrossAt(cross.dx, cross.dy, TFT_BLACK);
|
|
cross.drawn = false;
|
|
}
|
|
}
|
|
|
|
if (e.alive && e.r > 0.5f) {
|
|
// flickering fireball
|
|
tft.fillCircle((int)e.x, (int)e.y, (int)e.r, EXPL_COLORS[frameCount % 3]);
|
|
|
|
// burn cities and batteries caught inside
|
|
for (int c = 0; c < CITY_COUNT; c++) {
|
|
if (!cityAlive[c]) continue;
|
|
float dx = cityX[c] - e.x;
|
|
float dy = (groundY - 6) - e.y;
|
|
if (dx * dx + dy * dy < e.r * e.r) killCity(c);
|
|
}
|
|
for (int b = 0; b < 3; b++) {
|
|
if (!batAlive[b]) continue;
|
|
float dx = BATX[b] - e.x;
|
|
float dy = (groundY - 4) - e.y;
|
|
if (dx * dx + dy * dy < e.r * e.r) killBattery(b);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Waves / game flow
|
|
// ---------------------------------------------------------------------------
|
|
static void resetGame()
|
|
{
|
|
for (int i = 0; i < CITY_COUNT; i++) cityAlive[i] = true;
|
|
for (int i = 0; i < 3; i++) {
|
|
batAlive[i] = true;
|
|
batAmmo[i] = AMMO_MAX;
|
|
}
|
|
for (int i = 0; i < MAX_BOMBS; i++) bombs[i].alive = false;
|
|
for (int i = 0; i < MAX_ROCKETS; i++) rockets[i].alive = false;
|
|
for (int i = 0; i < MAX_EXPL; i++) expls[i].alive = false;
|
|
score = 0;
|
|
wave = 0;
|
|
cross.x = cross.dx = scrW / 2;
|
|
cross.y = cross.dy = scrH / 2;
|
|
}
|
|
|
|
static void startWave(int n)
|
|
{
|
|
wave = n;
|
|
for (int i = 0; i < 3; i++) {
|
|
if (batAlive[i]) batAmmo[i] = AMMO_MAX;
|
|
}
|
|
// every third wave one destroyed city is rebuilt
|
|
if (n > 1 && (n - 1) % 3 == 0) {
|
|
for (int i = 0; i < CITY_COUNT; i++) {
|
|
if (!cityAlive[i]) {
|
|
cityAlive[i] = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
waveQuota = 8 + 2 * n;
|
|
if (waveQuota > 24) waveQuota = 24;
|
|
bombSpeed = 0.5f + 0.12f * (n - 1);
|
|
if (bombSpeed > 2.4f) bombSpeed = 2.4f;
|
|
spawnIntervalMs = 1400 - 70 * n;
|
|
if (spawnIntervalMs < 500) spawnIntervalMs = 500;
|
|
bombsSpawned = 0;
|
|
waveKills = 0;
|
|
spawningStopped = false;
|
|
lastSpawnMs = millis();
|
|
|
|
drawWorld();
|
|
|
|
tft.setFreeFont(&FreeSansBold18pt7b);
|
|
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
|
|
tft.setTextDatum(MC_DATUM);
|
|
tft.drawString("WAVE " + String(n), scrW / 2, 70);
|
|
tft.setTextFont(2);
|
|
|
|
cross.drawn = false;
|
|
gameState = ST_BANNER;
|
|
stateT0 = millis();
|
|
|
|
// Attack alarm: four falling tones as a wave-start intro, like the
|
|
// original. By the time it ends, the banner clears and gameplay begins.
|
|
sfxSirenIntro();
|
|
}
|
|
|
|
static void eraseBanner()
|
|
{
|
|
tft.fillRect(0, 50, scrW, 40, TFT_BLACK);
|
|
stars(12);
|
|
cross.drawn = false;
|
|
}
|
|
|
|
static void enterBonus()
|
|
{
|
|
sfxWaveClear();
|
|
|
|
int aliveCities = 0;
|
|
for (int i = 0; i < CITY_COUNT; i++) if (cityAlive[i]) aliveCities++;
|
|
|
|
int ammoLeft = 0;
|
|
for (int i = 0; i < 3; i++) ammoLeft += batAmmo[i];
|
|
|
|
int mult = scoreMult();
|
|
int cityPts = aliveCities * 100 * mult;
|
|
int ammoPts = ammoLeft * 5 * mult;
|
|
score += cityPts + ammoPts;
|
|
if (score > highScore) highScore = score;
|
|
|
|
int px = 60, pw = scrW - 120, py = 64, ph = 108;
|
|
tft.fillRect(px, py, pw, ph, TFT_BLACK);
|
|
tft.drawRect(px, py, pw, ph, TFT_WHITE);
|
|
|
|
tft.setTextFont(2);
|
|
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
|
|
tft.setTextDatum(MC_DATUM);
|
|
tft.drawString("WAVE " + String(wave) + " CLEARED", scrW / 2, py + 12);
|
|
tft.setTextColor(TFT_WHITE, TFT_BLACK);
|
|
tft.drawString("MISSILES " + String(waveKills) + " x " + String(25 * mult), scrW / 2, py + 34);
|
|
tft.drawString("CITIES " + String(aliveCities) + " x " + String(100 * mult), scrW / 2, py + 52);
|
|
tft.drawString("AMMO " + String(ammoLeft) + " x " + String(5 * mult), scrW / 2, py + 70);
|
|
tft.setTextColor(TFT_GOLD, TFT_BLACK);
|
|
tft.drawString("SCORE " + String(score), scrW / 2, py + 90);
|
|
|
|
gameState = ST_BONUS;
|
|
stateT0 = millis();
|
|
}
|
|
|
|
static void enterGameOver()
|
|
{
|
|
sfxGameOver();
|
|
if (score > highScore) highScore = score;
|
|
|
|
tft.fillScreen(TFT_BLACK);
|
|
stars(40);
|
|
tft.setFreeFont(&FreeSansBold24pt7b);
|
|
tft.setTextColor(TFT_RED, TFT_BLACK);
|
|
tft.setTextDatum(MC_DATUM);
|
|
tft.drawString("THE END", scrW / 2, 80);
|
|
tft.setTextFont(2);
|
|
tft.setTextColor(TFT_WHITE, TFT_BLACK);
|
|
tft.drawString("FINAL SCORE " + String(score), scrW / 2, 130);
|
|
tft.setTextColor(TFT_GOLD, TFT_BLACK);
|
|
tft.drawString("HIGH SCORE " + String(highScore), scrW / 2, 152);
|
|
tft.setTextColor(TFT_DARKGREY, TFT_BLACK);
|
|
tft.drawString("press to continue", scrW / 2, 186);
|
|
|
|
gameState = ST_GAMEOVER;
|
|
stateT0 = millis();
|
|
}
|
|
|
|
static void drawTitle()
|
|
{
|
|
tft.fillScreen(TFT_BLACK);
|
|
stars(70);
|
|
drawGround();
|
|
for (int i = 0; i < CITY_COUNT; i++) {
|
|
cityAlive[i] = true;
|
|
drawCity(i);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
batAlive[i] = true;
|
|
batAmmo[i] = AMMO_MAX;
|
|
drawBattery(i);
|
|
}
|
|
|
|
tft.setTextDatum(MC_DATUM);
|
|
tft.setFreeFont(&FreeSansBold24pt7b);
|
|
tft.setTextColor(TFT_RED, TFT_BLACK);
|
|
tft.drawString("MISSILE", scrW / 2, 55);
|
|
tft.setTextColor(TFT_CYAN, TFT_BLACK);
|
|
tft.drawString("COMMAND", scrW / 2, 95);
|
|
|
|
tft.setTextFont(2);
|
|
tft.setTextColor(TFT_WHITE, TFT_BLACK);
|
|
tft.drawString("Trackball: aim press / tap: fire", scrW / 2, 136);
|
|
tft.drawString("A / S / D: battery U / V / M: volume", scrW / 2, 154);
|
|
tft.setTextColor(TFT_GOLD, TFT_BLACK);
|
|
if (highScore > 0) tft.drawString("HIGH " + String(highScore), scrW / 2, 178);
|
|
|
|
gameState = ST_TITLE;
|
|
stateT0 = millis();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Input
|
|
// ---------------------------------------------------------------------------
|
|
static void pollTrackball()
|
|
{
|
|
// Quadrant pins + center click, per UnitTest's mouse mapping:
|
|
// G02 -> right, G01 -> up, G04 -> left, G03 -> down, BOOT -> click
|
|
static const uint8_t tbPins[5] = {
|
|
BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03, BOARD_BOOT_PIN
|
|
};
|
|
static bool last[5];
|
|
bool cur[5];
|
|
for (int i = 0; i < 5; i++) {
|
|
cur[i] = digitalRead(tbPins[i]);
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
if (cur[i] != last[i]) moveCross(i);
|
|
}
|
|
|
|
bool pressed = (!cur[4] && last[4]); // falling edge = click
|
|
for (int i = 0; i < 5; i++) last[i] = cur[i];
|
|
|
|
if (!pressed) return;
|
|
|
|
switch (gameState) {
|
|
case ST_TITLE:
|
|
resetGame();
|
|
startWave(1);
|
|
break;
|
|
case ST_BONUS:
|
|
startWave(wave + 1);
|
|
break;
|
|
case ST_GAMEOVER:
|
|
drawTitle();
|
|
break;
|
|
case ST_PLAYING:
|
|
fireAuto();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void pollTouch()
|
|
{
|
|
if (!touchAvail) return;
|
|
static bool wasDown = false;
|
|
bool down = touch.isPressed();
|
|
if (!down) {
|
|
wasDown = false;
|
|
return;
|
|
}
|
|
if (wasDown) return; // only act on new taps
|
|
int16_t x[1], y[1];
|
|
if (touch.getPoint(x, y, 1) > 0) {
|
|
wasDown = true;
|
|
switch (gameState) {
|
|
case ST_TITLE:
|
|
resetGame();
|
|
startWave(1);
|
|
break;
|
|
case ST_BONUS:
|
|
startWave(wave + 1);
|
|
break;
|
|
case ST_GAMEOVER:
|
|
drawTitle();
|
|
break;
|
|
case ST_PLAYING:
|
|
case ST_BANNER:
|
|
setCross(x[0], y[0]);
|
|
fireAuto();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static void pollKeyboard()
|
|
{
|
|
static char lastKey = 0;
|
|
Wire.requestFrom((uint8_t)0x55, (uint8_t)1);
|
|
if (Wire.available() == 0) {
|
|
lastKey = 0; // idle: arm the next press (same key can repeat)
|
|
return;
|
|
}
|
|
char k = (char)Wire.read();
|
|
if (k == 0) {
|
|
lastKey = 0;
|
|
return;
|
|
}
|
|
if (k == lastKey) return;
|
|
lastKey = k;
|
|
kbAvail = true;
|
|
|
|
char c = tolower(k);
|
|
|
|
// volume keys work in every state
|
|
if (c == 'u') {
|
|
sound.volumeUp();
|
|
volOSDUntil = millis() + 1200;
|
|
return;
|
|
}
|
|
if (c == 'v') {
|
|
sound.volumeDown();
|
|
volOSDUntil = millis() + 1200;
|
|
return;
|
|
}
|
|
if (c == 'm') {
|
|
sound.toggleMute();
|
|
volOSDUntil = millis() + 1200;
|
|
return;
|
|
}
|
|
|
|
switch (gameState) {
|
|
case ST_TITLE:
|
|
resetGame();
|
|
startWave(1);
|
|
break;
|
|
case ST_BONUS:
|
|
startWave(wave + 1);
|
|
break;
|
|
case ST_GAMEOVER:
|
|
drawTitle();
|
|
break;
|
|
case ST_PLAYING:
|
|
if (c == 'a') launch(0, cross.x, cross.y);
|
|
else if (c == 's') launch(1, cross.x, cross.y);
|
|
else if (c == 'd') launch(2, cross.x, cross.y);
|
|
else if (c == ' ') fireAuto();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Per-state loop handlers
|
|
// ---------------------------------------------------------------------------
|
|
static void loopPlaying()
|
|
{
|
|
uint32_t now = millis();
|
|
|
|
if (spawningStopped) {
|
|
// no cities left: let the screen settle, then game over
|
|
if (!anyBombAlive() && !anyRocketAlive() && !anyExplAlive()) {
|
|
enterGameOver();
|
|
return;
|
|
}
|
|
} else {
|
|
if (bombsSpawned < waveQuota && now - lastSpawnMs >= spawnIntervalMs) {
|
|
lastSpawnMs = now;
|
|
int burst = 1 + random(0, 1 + wave / 4);
|
|
while (burst-- > 0 && bombsSpawned < waveQuota) spawnBomb();
|
|
}
|
|
}
|
|
|
|
updateRockets();
|
|
updateBombs();
|
|
updateExpls();
|
|
renderCross();
|
|
|
|
if (!spawningStopped) {
|
|
bool citiesLeft = false;
|
|
for (int i = 0; i < CITY_COUNT; i++) {
|
|
if (cityAlive[i]) citiesLeft = true;
|
|
}
|
|
if (!citiesLeft) spawningStopped = true;
|
|
}
|
|
|
|
if (waveDone()) enterBonus();
|
|
}
|
|
|
|
static void loopTitle()
|
|
{
|
|
// blinking start prompt
|
|
static bool shown = true;
|
|
static uint32_t lastBlink = 0;
|
|
uint32_t now = millis();
|
|
if (now - lastBlink >= 700) {
|
|
lastBlink = now;
|
|
shown = !shown;
|
|
tft.setTextFont(2);
|
|
tft.setTextColor(shown ? TFT_WHITE : TFT_BLACK, TFT_BLACK);
|
|
tft.setTextDatum(MC_DATUM);
|
|
tft.drawString("TAP OR PRESS TO START", scrW / 2, 200);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Init
|
|
// ---------------------------------------------------------------------------
|
|
// LilyGo T-Deck backlight has 16 levels (0..16)
|
|
static void setBrightness(uint8_t value)
|
|
{
|
|
static uint8_t level = 0;
|
|
static uint8_t steps = 16;
|
|
if (value == 0) {
|
|
digitalWrite(BOARD_BL_PIN, 0);
|
|
delay(3);
|
|
level = 0;
|
|
return;
|
|
}
|
|
if (level == 0) {
|
|
digitalWrite(BOARD_BL_PIN, 1);
|
|
level = steps;
|
|
delayMicroseconds(30);
|
|
}
|
|
int from = steps - level;
|
|
int to = steps - value;
|
|
int num = (steps + to - from) % steps;
|
|
for (int i = 0; i < num; i++) {
|
|
digitalWrite(BOARD_BL_PIN, 0);
|
|
digitalWrite(BOARD_BL_PIN, 1);
|
|
}
|
|
level = value;
|
|
}
|
|
|
|
static void hardwareInit()
|
|
{
|
|
Serial.begin(115200);
|
|
Serial.println("T-Deck Missile Command");
|
|
|
|
pinMode(BOARD_POWERON, OUTPUT);
|
|
digitalWrite(BOARD_POWERON, HIGH);
|
|
|
|
// CS lines high during init
|
|
pinMode(BOARD_SDCARD_CS, OUTPUT);
|
|
pinMode(RADIO_CS_PIN, OUTPUT);
|
|
pinMode(BOARD_TFT_CS, OUTPUT);
|
|
digitalWrite(BOARD_SDCARD_CS, HIGH);
|
|
digitalWrite(RADIO_CS_PIN, HIGH);
|
|
digitalWrite(BOARD_TFT_CS, HIGH);
|
|
|
|
pinMode(BOARD_SPI_MISO, INPUT_PULLUP);
|
|
SPI.begin(BOARD_SPI_SCK, BOARD_SPI_MISO, BOARD_SPI_MOSI);
|
|
|
|
tft.begin();
|
|
tft.setRotation(1);
|
|
tft.fillScreen(TFT_BLACK);
|
|
|
|
pinMode(BOARD_BL_PIN, OUTPUT);
|
|
for (int i = 0; i <= 16; ++i) {
|
|
setBrightness(i);
|
|
delay(20);
|
|
}
|
|
|
|
// I2C: touch + keyboard share the bus
|
|
Wire.begin(BOARD_I2C_SDA, BOARD_I2C_SCL);
|
|
|
|
touch.setPins(-1, BOARD_TOUCH_INT);
|
|
touchAvail = touch.begin(Wire);
|
|
if (touchAvail) {
|
|
touch.setMaxCoordinates(scrW, scrH);
|
|
touch.setSwapXY(true);
|
|
touch.setMirrorXY(false, true);
|
|
Serial.println("GT911 touch ready");
|
|
} else {
|
|
Serial.println("No GT911 touch found");
|
|
}
|
|
|
|
// Trackball pins (quadrants + center click)
|
|
const uint8_t tbPins[5] = {
|
|
BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03, BOARD_BOOT_PIN
|
|
};
|
|
for (int i = 0; i < 5; i++) {
|
|
pinMode(tbPins[i], INPUT_PULLUP);
|
|
}
|
|
|
|
randomSeed(esp_random());
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
hardwareInit();
|
|
|
|
scrW = tft.width();
|
|
scrH = tft.height();
|
|
groundY = scrH - 12;
|
|
layout();
|
|
|
|
if (!sound.begin()) {
|
|
Serial.println("Sound engine init failed (no I2S?)");
|
|
}
|
|
|
|
Serial.printf("Screen %dx%d, ground at %d\n", scrW, scrH, groundY);
|
|
drawTitle();
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
static uint32_t nextFrame = 0;
|
|
uint32_t now = millis();
|
|
if ((int32_t)(now - nextFrame) < 0) {
|
|
delay(1);
|
|
return;
|
|
}
|
|
nextFrame = now + FRAME_MS;
|
|
frameCount++;
|
|
|
|
pollTrackball();
|
|
pollTouch();
|
|
pollKeyboard();
|
|
|
|
switch (gameState) {
|
|
case ST_TITLE:
|
|
loopTitle();
|
|
break;
|
|
case ST_BANNER:
|
|
if (now - stateT0 > BANNER_MS) {
|
|
eraseBanner();
|
|
gameState = ST_PLAYING;
|
|
}
|
|
break;
|
|
case ST_PLAYING:
|
|
loopPlaying();
|
|
break;
|
|
case ST_BONUS:
|
|
if (now - stateT0 > BONUS_MS) {
|
|
startWave(wave + 1);
|
|
}
|
|
break;
|
|
case ST_GAMEOVER:
|
|
default:
|
|
break;
|
|
}
|
|
|
|
drawVolumeOSD();
|
|
}
|