// title: MorsePit // author: He4eT@oddsquat.org // desc: Defeat endless waves of enemies using Morse code. // site: https://github.com/He4eT/MorsePit // license: MIT License // version: 0.1 // script: js function TIC() { gameStages[currentStage]() } /* Stages */ /** @type Record void> */ const gameStages = { mainMenu, gameover, gameplay, } /** @type {keyof typeof gameStages} */ let currentStage = 'mainMenu' /* State */ /** * @typedef {{ x: number, y: number }} Point * * @typedef {{ * top: number, * right: number, * bottom: number, * left: number, * }} Bounds */ /** * @typedef {{ * screenPosition: Point, * bounds: Bounds, * spriteHalfSize: number, * wave: number, * }} Arena */ /** @type {Arena} */ let arena = { screenPosition: { x: 7, y: 7, }, bounds: { top: 0, right: 225, bottom: 89, left: 0, }, spriteHalfSize: 3, wave: 0, } /** * @typedef {{ * sprite: number, * speed: number, * position: Point, * }} Player */ /** @type {Player} */ let player = { sprite: 64, speed: 1, position: { x: 0, y: 0, }, } const enemyTypes = ['point', 'fidget', 'bounce', 'zombie'] /** * @typedef {{ * letter: string, * type: keyof typeof enemyBehaviors, * dangerZone: number, * positions: Point[], * }} Enemy */ /** @type {Enemy[]} */ let enemies = [] /** * @typedef {{ * type: 'laser' | 'nuke' | 'verticalLine' | 'horizontalLine' | 'detection', * from: Point, * to: Point, * frames: number[], * }} Effect */ /** @type {Effect[]} */ let effects = [] /* Main Menu */ function mainMenu() { if ([BTN_A, BTN_B, BTN_X, BTN_Y].map(btn).some(Boolean)) { currentStage = 'gameplay' } cls(0) const title = 'Morse Pit' print(title, 12, 12, 3, false, 2) const instruction = 'Press any key to start' print(instruction, 12, 30, 4) } /* Gameover */ function gameover() { cls(0) drawEnemies() drawPlayer() const title = 'Game Over' print(title, 12, 12, 10, false, 2) } /* Gameplay */ function gameplay() { checkColisions() handleMoves() handleMorse() spawn() moveEnemies() drawInterface() drawArena() drawFX() drawEnemies() drawPlayer() } function handleMoves() { player.speed = btn(BTN_A) ? 2 : 1 let dx = 0 if (btn(BTN_L)) dx -= 1 if (btn(BTN_R)) dx += 1 let dy = 0 if (btn(BTN_U)) dy -= 1 if (btn(BTN_D)) dy += 1 const norm = player.speed / ([dx, dy].every((d) => d !== 0) ? Math.SQRT2 : 1) const { bounds } = arena player.position.x = Math.max( bounds.left, Math.min(bounds.right, player.position.x + dx * norm), ) player.position.y = Math.max( bounds.top, Math.min(bounds.bottom, player.position.y + dy * norm), ) } function handleMorse() { player.sprite = btn(BTN_B) ? 65 : 64 if (btnp(BTN_B, 100, 100)) { destroyEnemiesByLetter('e') } } function destroyEnemiesByLetter(letter) { const destructionEffects = [ ['laser', [1, 2, 3, 4, 7, 7, 7, 6, 5, 4, 3, 2, 1]], ['nuke', [7, 6, 5, 4, 3, 2]], ['verticalLine', [4, 5, 6, 7, 7, 6, 5, 4]], ['horizontalLine', [4, 5, 6, 7, 7, 6, 5, 4]], ] enemies .filter((enemy) => enemy.letter === letter) .forEach((enemy) => { const [type, frames] = destructionEffects[rnd(0, 3)] effects.unshift({ type, frames, from: player.position, to: enemy.positions[0], }) }) enemies = enemies.filter((enemy) => enemy.letter !== letter) } /* Enemies */ function spawn() { if (enemies.length > 0) { return } arena.wave += 1 const enemyCount = 1 + Math.floor(arena.wave / 2) const getType = (wave) => { if (wave <= 2) return 'point' const enemyTypes = Object.keys(enemyBehaviors) return enemyTypes[rnd(0, enemyTypes.length - 1)] } const getDangerZone = (type) => { if (type === 'zombie') return 6 return 8 } const getSpawnPosition = () => { const minDistance = 50 let x, y, distance do { x = rnd(arena.bounds.left, arena.bounds.right) y = rnd(arena.bounds.top, arena.bounds.bottom) distance = Math.hypot(x - player.position.x, y - player.position.y) } while (distance < minDistance) return { x, y } } enemies = Array(enemyCount) .fill() .map(() => { const type = getType(arena.wave) return { type, letter: 'e', positions: [getSpawnPosition(), getSpawnPosition()], dangerZone: getDangerZone(type), } }) enemies.forEach((enemy) => { effects.unshift({ type: 'detection', to: enemy.positions[0], frames: Array(10).fill(4), }) }) } const enemyBehaviors = { point: () => {}, fidget: (enemy) => {}, bounce: (enemy) => { const speed = 1 const current = enemy.positions[0] const previous = enemy.positions[1] const d = getDirection(previous, current) let dx = d.x * speed let dy = d.y * speed let newX = current.x + dx let newY = current.y + dy if (newX < arena.bounds.left || newX > arena.bounds.right) { dx = -dx newX = current.x + dx } if (newY < arena.bounds.top || newY > arena.bounds.bottom) { dy = -dy newY = current.y + dy } enemy.positions = [ { x: newX, y: newY }, { x: current.x, y: current.y }, ] }, zombie: (enemy) => { const speed = 0.5 const current = enemy.positions[0] const target = player.position const d = getDirection(current, target) enemy.positions = [ { x: current.x + d.x * speed, y: current.y + d.y * speed, }, ] }, } function moveEnemies() { enemies.forEach((enemy) => enemyBehaviors[enemy.type](enemy)) } function checkColisions() { if ( enemies .map((enemy) => [ enemy, Math.hypot( player.position.x - enemy.positions[0].x, player.position.y - enemy.positions[0].y, ), ]) .some(([enemy, distance]) => distance < enemy.dangerZone) ) { currentStage = 'gameover' } } /* Draw */ function arenaToScreen({ x, y }) { return { x: x + arena.screenPosition.x, y: y + arena.screenPosition.y, } } function drawSprite(spriteIndex, x, y) { const colorkey = 0 const center = arenaToScreen({ x, y }) spr( spriteIndex, center.x - arena.spriteHalfSize, center.y - arena.spriteHalfSize, colorkey, ) } function drawInterface() { cls(0) } function drawArena() { map(0, 0, 30, 15) } const effectHandlers = { laser: ({ from, to, frames }) => { const color = frames.shift() line(from.x, from.y, to.x, to.y, color) circ(from.x, from.y, frames.length / 3, color) circ(to.x, to.y, frames.length / 2, color) circb(to.x, to.y, frames.length, color + 3) }, nuke: ({ to, frames }) => { const color = frames.shift() circ(to.x, to.y, Math.pow(frames.length, 5), color) }, verticalLine: ({ to, frames }) => { const color = frames.shift() rect(0, to.y - frames.length, SCREEN_W, frames.length * 2, color) }, horizontalLine: ({ to, frames }) => { const color = frames.shift() rect(to.x - frames.length, 0, frames.length * 2, SCREEN_W, color) }, detection: ({ to, frames }) => { const color = frames.shift() const w = arena.spriteHalfSize const d = frames.length + 2 * w const corners = [ [+1, +1], [+1, -1], [-1, +1], [-1, -1], ] corners.forEach(([dx, dy]) => { const x = to.x + dx * d const y = to.y + dy * d line(x, y, x - dx * w, y, color) line(x, y, x, y - dy * w, color) }) }, } function drawFX() { effects .map((effect) => ({ ...effect, from: arenaToScreen(effect.from ?? {}), to: arenaToScreen(effect.to ?? {}), })) .forEach((effect) => effectHandlers[effect.type](effect), ) effects = effects.filter(({ frames }) => frames.length > 0) } function drawEnemies() { enemies .map((enemy) => [80, enemy.positions[0].x, enemy.positions[0].y]) .forEach((spriteData) => drawSprite(...spriteData)) } function drawPlayer() { drawSprite(player.sprite, player.position.x, player.position.y) } /* Utils */ function rnd(from, to) { return Math.floor(Math.random() * (to - from + 1)) + from } function getDirection(from, to) { const dx = to.x - from.x const dy = to.y - from.y const distance = Math.hypot(dx, dy) || 1 return { x: dx / distance, y: dy / distance, } } /* Constants */ /* Screen */ const SCREEN_W = 240 const SCREEN_H = 136 /* Buttons */ const [BTN_U, BTN_D, BTN_L, BTN_R, BTN_A, BTN_B, BTN_X, BTN_Y] = [ ...Array(8).keys(), ] // // 001:1111110012222100123321001233210012222100111111000000000000000000 // 002:1111111112222221123333211233332112222221111111110000000000000000 // 016:0000000000000000000000000000000000000000000001110000012200000123 // 017:0000000000000000000000000000000000000000111111112222222233333333 // 018:0000000000000000000000000000000000000000111000002210000032100000 // 032:0000012300000123000001230000012300000123000001230000012300000123 // 033:2111111211111111111111111111111111111111111111111111111121111112 // 034:3210000032100000321000003210000032100000321000003210000032100000 // 048:0000012300000122000001110000000000000000000000000000000000000000 // 049:3333333322222222111111110000000000000000000000000000000000000000 // 050:3210000022100000111000000000000000000000000000000000000000000000 // 064:8800088080000080008880000088800000888000800000808800088000000000 // 065:0000000008808800080008000008000008000800088088000000000000000000 // 080:aaaaaaa0aaaaaaa0aa000aa0aa000aa0aa000aa0aaaaaaa0aaaaaaa000000000 // // // 000:011111111111111111111111111111111111111111111111111111111121000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 001:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 002:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 003:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 004:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 005:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 006:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 007:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 008:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 009:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 010:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 011:021212121212121212121212121212121212121212121212121212121222000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 012:031313131313131313131313131313131313131313131313131313131323000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // // // 000:00000000ffffffff00000000ffffffff // 001:0123456789abcdeffedcba9876543210 // 002:0123456789abcdef0123456789abcdef // // // 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000 // // // 000:100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // // // 000:000000002b36073642586e75657b8383949693a1a1ffffffb58900cb4b16dc322fd336826c71c4268bd22aa198859900 //