'use strict'; /** * Planet physics and visuals for Meteors. * * Stage 1 uses a single default planet. Stage 3 lets the player pick a * planet; the numbers and jump-height rules are already applied here so * that later planets only need new data, not new physics code. * * Gravity (jumpsToClear): 1 (easy, high jump) .. 4 (hard, short jump). * A horizontal match of 3 jumps FIELD_HEIGHT / jumpsToClear tiles — * at 1, one jump from the floor clears the field; at 4, you need four. * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall). * After a jump peaks, blocks float down at FIELD_HEIGHT / fallTime tiles/s. * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump). * Time to reach the apex, independent of jump height. * * Jump-height multipliers (never explained in the game): * A vertical match of 3 is 120 % of a horizontal row of 3. * Each extra block beyond 3 adds 5 %. * * Power is the average of the three difficulty axes (0 easy .. 1 hard) * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at * power 0 to 5 % at power 1. */ var FIELD_WIDTH = 10; var FIELD_HEIGHT = 13; function clamp (value, lo, hi) { return Math.max(lo, Math.min(hi, value)); } function defaultColors () { return [ { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' }, { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' }, { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' }, { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' }, { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' } ]; } function createPlanet (opts) { opts = opts || {}; var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4); var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5); var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3); var colors = opts.colors || defaultColors(); var planet = { id: opts.id || null, name: opts.name || 'Terra', people: opts.people || 'Terrans', jumpsToClear: jumpsToClear, fallTime: fallTime, riseTime: riseTime, colors: colors, baseJumpHeight: FIELD_HEIGHT / jumpsToClear, fallSpeed: FIELD_HEIGHT / fallTime, // Time to ease from vy=0 at the apex to terminal fallSpeed. // Shorter when air resistance is low (small fallTime). apexEaseTime: 0.15 * fallTime, fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime), // Visual landing bounce: faster at low air resistance. Stage-3 params. bounceDuration: 0.12 * fallTime, bounceSquash: 0.16 }; return planet; } function loadCatalog () { if (typeof PLANET_CATALOG !== 'undefined' && PLANET_CATALOG && PLANET_CATALOG.planets) { return PLANET_CATALOG; } if (typeof require !== 'undefined') { try { return require('../server/lib/planets.json'); } catch (e) {} } return { planets: [] }; } function planetFromData (data) { var planet = createPlanet({ name: data.name, people: data.people, jumpsToClear: data.jumpsToClear, fallTime: data.fallTime, riseTime: data.riseTime, colors: data.colors }); planet.id = data.id; return planet; } function allPlanets () { var raw = loadCatalog().planets || []; var out = []; for (var i = 0; i < raw.length; i++) { out.push(planetFromData(raw[i])); } if (out.length === 0) { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; } return out; } function planetById (id) { var all = allPlanets(); for (var i = 0; i < all.length; i++) { if (all[i].id === id) { return all[i]; } } return defaultPlanet(); } function defaultPlanet () { var all = allPlanets(); for (var i = 0; i < all.length; i++) { if (all[i].id === 'terra') { return all[i]; } } return all[0]; } function formatStats (planet) { var d = displayValues(planet); return 'g ' + d.gravity.toFixed(2) + ' d ' + d.density.toFixed(2) + ' F ' + Math.round(d.newtons) + ' N' + ' Power ' + d.power + '%'; } function spriteKey (planet, colorIndex) { var colors = planet && planet.colors; var c = colors && colors[colorIndex]; if (!c) { return 'gray'; } return (planet.id || 'planet') + '-' + (c.name || String(colorIndex)); } /** * Jump height in tiles for a match line. * @param {object} planet * @param {number} matchLength number of blocks in the line (>= 3) * @param {boolean} vertical true for a vertical line */ function jumpHeight (planet, matchLength, vertical) { var extra = Math.max(0, (matchLength | 0) - 3); var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra; return planet.baseJumpHeight * multiplier; } /** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */ function launchVelocity (planet, height) { if (height <= 0) { return 0; } return (2 * height) / planet.riseTime; } /** Constant deceleration (tiles/s^2) paired with launchVelocity. */ function launchDeceleration (planet, height) { if (height <= 0) { return 0; } return (2 * height) / (planet.riseTime * planet.riseTime); } /** * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower * chance that a shot-off block misses the opponent). */ function power (planet) { var gravity = (planet.jumpsToClear - 1) / 3; var air = (5 - planet.fallTime) / 4; var mass = (3 - planet.riseTime) / 2.5; return (gravity + air + mass) / 3; } /** Maximum miss chance, used by the gameplay formula and the displayed Power. */ var MAX_MISS_CHANCE = 0.60; var MIN_MISS_CHANCE = 0.05; /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */ function missChance (planet) { return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet); } /** * Displayed Power, as a 0–100 percentage. This is the complement of the * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss * → 0% Power. The gameplay power() and missChance() formulas are unchanged. */ function displayPower (planet) { return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100); } /** * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³), * launch force (N), and displayed Power as a percentage. The numbers do * not have to explain themselves. */ function displayValues (planet) { var gravity = 9.81 * planet.jumpsToClear / 2; var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60; var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160; return { gravity: gravity, density: density, newtons: newtons, power: displayPower(planet), missChance: missChance(planet) }; } var Planet = { FIELD_WIDTH: FIELD_WIDTH, FIELD_HEIGHT: FIELD_HEIGHT, COLOR_COUNT: 5, SETTLE_TIME: 3, SPAWN_INTERVAL: 1, JUMP_VERTICAL_BONUS: 1.2, JUMP_EXTRA_BLOCK: 0.05, createPlanet: createPlanet, defaultPlanet: defaultPlanet, defaultColors: defaultColors, allPlanets: allPlanets, planetById: planetById, planetFromData: planetFromData, formatStats: formatStats, spriteKey: spriteKey, jumpHeight: jumpHeight, launchVelocity: launchVelocity, launchDeceleration: launchDeceleration, MAX_MISS_CHANCE: MAX_MISS_CHANCE, MIN_MISS_CHANCE: MIN_MISS_CHANCE, power: power, missChance: missChance, displayPower: displayPower, displayValues: displayValues }; if (typeof module !== 'undefined' && module.exports) { module.exports = Planet; } else if (typeof globalThis !== 'undefined') { globalThis.Planet = Planet; }