'use strict'; /** * View and input for Meteors. The simulation lives in GameCore; this file * only draws it, scales it to the viewport, and translates pointer events * into beginDrag / moveDrag / endDrag. Ready for a later multiplayer view * that reuses the same core. */ var IMAGES = [ { name: 'ember', url: 'images/block-ember.png' }, { name: 'aqua', url: 'images/block-aqua.png' }, { name: 'leaf', url: 'images/block-leaf.png' }, { name: 'sun', url: 'images/block-sun.png' }, { name: 'orchid', url: 'images/block-orchid.png' }, { name: 'gray', url: 'images/block-gray.png' }, { name: 'locked', url: 'images/block-locked.png' }, { name: 'flame', url: 'images/flame.png', subImgTotal: 4, perRow: 4 }, { name: 'cell', url: 'images/cell.png' }, { name: 'pixel', url: 'images/pixel.png' } ]; var COLOR_SPRITES = ['ember', 'aqua', 'leaf', 'sun', 'orchid']; var GAME_OVER_DELAY = 2; var game = new Game(); var app = { mode: 'menu', core: null, scene: null, manager: null, planet: null, lastScore: 0, tile: 48, fieldX: 0, fieldY: 0, fieldW: 0, fieldH: 0, hudH: 56, sprites: Object.create(null), flames: Object.create(null), cells: [], pointerId: null, gameOverShown: false }; window.onload = function () { var play = document.getElementById('btn-play'); play.addEventListener('click', onPlay); play.addEventListener('pointerup', function (e) { // A touch-friendly path: some mobile browsers swallow the click // after a slightly sloppy tap. Ignore the following click. if (e.pointerType === 'touch' || e.pointerType === 'pen') { e.preventDefault(); onPlay(); } }); game.preloadImagesThenStart(IMAGES, function () { if (game.images.flame && game.images.flame.addAnimationCycle) { game.images.flame.addAnimationCycle('burn', [0, 1, 2, 3], true); } }); }; function onPlay () { if (app.mode === 'playing') { return; } startPlay(); } function startPlay () { hideEl('menu'); hideEl('game-over'); showEl('hud'); app.planet = (typeof Planet !== 'undefined' ? Planet.defaultPlanet() : PlanetMod.defaultPlanet()); app.core = new GameCore(app.planet, Math.random); app.mode = 'playing'; app.gameOverShown = false; app.sprites = Object.create(null); app.flames = Object.create(null); app.cells = []; app.pointerId = null; layout(); app.scene = new Scene(app.fieldX, app.fieldY, app.fieldW, app.fieldH); app.scene.backgroundColor = { r: 12, g: 16, b: 32 }; app.scene.renderBackground = true; game.loadScene(app.scene); buildCells(); app.manager = app.scene.addObject(makeManager()); bindPointer(app.scene.canvas); refreshHud(); } function returnToMenu () { unbindPointer(); if (app.scene) { app.scene.destroy(); app.scene = null; } app.manager = null; app.core = null; app.mode = 'menu'; app.sprites = Object.create(null); app.flames = Object.create(null); app.cells = []; hideEl('hud'); hideEl('game-over'); showEl('menu'); var last = document.getElementById('last-score'); if (app.lastScore > 0) { last.hidden = false; last.querySelector('strong').textContent = String(app.lastScore); } } function buildCells () { if (!app.scene) { return; } var img = game.images.cell; for (var row = 0; row < 13; row++) { for (var col = 0; col < 10; col++) { var obj = new GameObject(0, 0, img, 0); placeTile(obj, col, row); obj.renderer.alpha = 0.9; app.scene.addObject(obj); app.cells.push(obj); } } } function placeTile (obj, col, y) { var t = app.tile; obj.transform.position.x = col * t + t / 2; obj.transform.position.y = app.fieldH - (y + 0.5) * t; obj.transform.size = { x: t, y: t }; } function layout () { var vw = window.innerWidth || 320; var vh = window.innerHeight || 480; var safeTop = 0; try { // env(safe-area) is a CSS thing; keep a conservative HUD height. safeTop = 0; } catch (e) { /* ignore */ } app.hudH = Math.max(56, Math.round(vh * 0.09) + safeTop); var margin = Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); var availW = Math.max(80, vw - margin * 2); var availH = Math.max(80, vh - app.hudH - margin); app.tile = Math.max(14, Math.floor(Math.min(availW / 10, availH / 13))); app.fieldW = app.tile * 10; app.fieldH = app.tile * 13; app.fieldX = Math.floor((vw - app.fieldW) / 2); app.fieldY = app.hudH + Math.floor((vh - app.hudH - app.fieldH - margin / 2) / 2); if (app.fieldY < app.hudH) { app.fieldY = app.hudH; } if (app.scene) { app.scene.position.x = app.fieldX; app.scene.position.y = app.fieldY; app.scene.size.x = app.fieldW; app.scene.size.y = app.fieldH; } } function spriteFor (block) { if (block.state === 'locked') { return game.images.locked; } if (block.color < 0) { return game.images.gray; } return game.images[COLOR_SPRITES[block.color]] || game.images.gray; } function syncSprites () { if (!app.core || !app.scene) { return; } var seen = Object.create(null); var blocks = app.core.blocks; for (var i = 0; i < blocks.length; i++) { var block = blocks[i]; seen[block.id] = true; var obj = app.sprites[block.id]; if (!obj) { obj = new GameObject(0, 0, spriteFor(block), 2); app.scene.addObject(obj); app.sprites[block.id] = obj; } obj.renderer.sprite = spriteFor(block); placeTile(obj, block.col, block.y); obj.renderer.visible = true; obj.renderer.alpha = 1; if (block.flame) { var flame = app.flames[block.id]; if (!flame) { flame = new GameObject(0, 0, game.images.flame, 1); flame.renderer.setAnimation('burn', 0, 0.08); app.scene.addObject(flame); app.flames[block.id] = flame; } flame.renderer.visible = true; var t = app.tile; flame.transform.position.x = block.col * t + t / 2; flame.transform.position.y = app.fieldH - block.y * t + t * 0.15; flame.transform.size = { x: t * 0.85, y: t * 0.85 }; } else if (app.flames[block.id]) { app.flames[block.id].renderer.visible = false; } } var ids = Object.keys(app.sprites); for (var i = 0; i < ids.length; i++) { if (!seen[ids[i]]) { app.sprites[ids[i]].destroy(); delete app.sprites[ids[i]]; if (app.flames[ids[i]]) { app.flames[ids[i]].destroy(); delete app.flames[ids[i]]; } } } } function refreshHud () { if (!app.core) { return; } document.getElementById('score').textContent = String(app.core.score); var planet = app.planet; document.getElementById('planet-name').textContent = planet.name + ' ยท ' + planet.people; var d = Planet.displayValues(planet); document.getElementById('planet-stats').textContent = 'g ' + d.gravity.toFixed(2) + ' d ' + d.density.toFixed(2) + ' F ' + Math.round(d.newtons) + ' N Power ' + d.power + '%'; } function makeManager () { var obj = new GameObject(0, 0, game.images.pixel, 0); obj.renderer.visible = false; var base = obj.update.bind(obj); obj.update = function (dt) { base(dt); tickView(dt); }; return obj; } function tickView (dt) { if (app.mode !== 'playing' || !app.core) { return; } layout(); for (var i = 0; i < app.cells.length; i++) { var col = i % 10; var row = Math.floor(i / 10); placeTile(app.cells[i], col, row); } app.core.tick(dt); syncSprites(); refreshHud(); if (app.core.state === 'gameover' && !app.gameOverShown) { app.gameOverShown = true; app.lastScore = app.core.score; document.getElementById('go-score').textContent = String(app.lastScore); showEl('game-over'); } if (app.core.state === 'gameover' && app.core.gameOverTime >= GAME_OVER_DELAY) { returnToMenu(); } } /* ---------- pointer input (mouse + touch) --------------------------- */ var boundCanvas = null; function bindPointer (canvas) { unbindPointer(); boundCanvas = canvas; canvas.addEventListener('pointerdown', onPointerDown); canvas.addEventListener('pointermove', onPointerMove); canvas.addEventListener('pointerup', onPointerUp); canvas.addEventListener('pointercancel', onPointerUp); canvas.addEventListener('lostpointercapture', onPointerUp); canvas.style.touchAction = 'none'; canvas.style.cursor = 'pointer'; } function unbindPointer () { if (!boundCanvas) { return; } boundCanvas.removeEventListener('pointerdown', onPointerDown); boundCanvas.removeEventListener('pointermove', onPointerMove); boundCanvas.removeEventListener('pointerup', onPointerUp); boundCanvas.removeEventListener('pointercancel', onPointerUp); boundCanvas.removeEventListener('lostpointercapture', onPointerUp); boundCanvas = null; app.pointerId = null; } function fieldFromEvent (e) { var canvas = app.scene && app.scene.canvas; if (!canvas) { return null; } var rect = canvas.getBoundingClientRect(); if (rect.width <= 0 || rect.height <= 0) { return null; } var x = (e.clientX - rect.left) * (canvas.width / rect.width); var y = (e.clientY - rect.top) * (canvas.height / rect.height); var col = Math.floor(x / app.tile); var fieldY = (canvas.height - y) / app.tile; return { col: col, y: fieldY }; } function onPointerDown (e) { if (!app.core || app.core.state !== 'playing') { return; } if (app.pointerId != null) { return; } e.preventDefault(); app.pointerId = e.pointerId; try { boundCanvas.setPointerCapture(e.pointerId); } catch (err) { /* ignore */ } var pos = fieldFromEvent(e); if (pos) { app.core.beginDrag(pos.col, pos.y); } } function onPointerMove (e) { if (app.pointerId == null || e.pointerId !== app.pointerId) { return; } if (!app.core || !app.core.drag) { return; } e.preventDefault(); var pos = fieldFromEvent(e); if (pos) { app.core.moveDrag(pos.y); } } function onPointerUp (e) { if (app.pointerId == null) { return; } if (e && e.pointerId != null && e.pointerId !== app.pointerId) { return; } if (e) { e.preventDefault(); } app.pointerId = null; if (app.core) { app.core.endDrag(); } } function hideEl (id) { document.getElementById(id).hidden = true; } function showEl (id) { document.getElementById(id).hidden = false; } window.addEventListener('resize', function () { if (app.mode === 'playing') { layout(); } }); window.addEventListener('orientationchange', function () { setTimeout(function () { if (app.mode === 'playing') { layout(); } }, 120); });