'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 = { 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 defaultPlanet () { return createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 }); } /** * 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; } /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */ function missChance (planet) { return 0.60 - 0.55 * power(planet); } /** * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³), * launch force (N), and 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: Math.round(power(planet) * 100), 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, jumpHeight: jumpHeight, launchVelocity: launchVelocity, launchDeceleration: launchDeceleration, power: power, missChance: missChance, displayValues: displayValues }; if (typeof module !== 'undefined' && module.exports) { module.exports = Planet; } else if (typeof globalThis !== 'undefined') { globalThis.Planet = Planet; }