| 1 | 'use strict'; |
| 2 | |
| 3 | /** |
| 4 | * Planet physics and visuals for Meteors. |
| 5 | * |
| 6 | * Stage 1 uses a single default planet. Stage 3 lets the player pick a |
| 7 | * planet; the numbers and jump-height rules are already applied here so |
| 8 | * that later planets only need new data, not new physics code. |
| 9 | * |
| 10 | * Gravity (jumpsToClear): 1 (easy, high jump) .. 4 (hard, short jump). |
| 11 | * A horizontal match of 3 jumps FIELD_HEIGHT / jumpsToClear tiles — |
| 12 | * at 1, one jump from the floor clears the field; at 4, you need four. |
| 13 | * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall). |
| 14 | * After a jump peaks, blocks float down at FIELD_HEIGHT / fallTime tiles/s. |
| 15 | * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump). |
| 16 | * Time to reach the apex, independent of jump height. |
| 17 | * |
| 18 | * Jump-height multipliers (never explained in the game): |
| 19 | * A vertical match of 3 is 120 % of a horizontal row of 3. |
| 20 | * Each extra block beyond 3 adds 5 %. |
| 21 | * |
| 22 | * Power is the average of the three difficulty axes (0 easy .. 1 hard) |
| 23 | * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at |
| 24 | * power 0 to 5 % at power 1. |
| 25 | */ |
| 26 | |
| 27 | var FIELD_WIDTH = 10; |
| 28 | var FIELD_HEIGHT = 13; |
| 29 | |
| 30 | function clamp (value, lo, hi) |
| 31 | { |
| 32 | return Math.max(lo, Math.min(hi, value)); |
| 33 | } |
| 34 | |
| 35 | function defaultColors () |
| 36 | { |
| 37 | return [ |
| 38 | { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' }, |
| 39 | { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' }, |
| 40 | { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' }, |
| 41 | { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' }, |
| 42 | { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' } |
| 43 | ]; |
| 44 | } |
| 45 | |
| 46 | function createPlanet (opts) |
| 47 | { |
| 48 | opts = opts || {}; |
| 49 | var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4); |
| 50 | var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5); |
| 51 | var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3); |
| 52 | var colors = opts.colors || defaultColors(); |
| 53 | var planet = { |
| 54 | id: opts.id || null, |
| 55 | name: opts.name || 'Terra', |
| 56 | people: opts.people || 'Terrans', |
| 57 | jumpsToClear: jumpsToClear, |
| 58 | fallTime: fallTime, |
| 59 | riseTime: riseTime, |
| 60 | colors: colors, |
| 61 | baseJumpHeight: FIELD_HEIGHT / jumpsToClear, |
| 62 | fallSpeed: FIELD_HEIGHT / fallTime, |
| 63 | // Time to ease from vy=0 at the apex to terminal fallSpeed. |
| 64 | // Shorter when air resistance is low (small fallTime). |
| 65 | apexEaseTime: 0.15 * fallTime, |
| 66 | fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime), |
| 67 | // Visual landing bounce: faster at low air resistance. Stage-3 params. |
| 68 | bounceDuration: 0.12 * fallTime, |
| 69 | bounceSquash: 0.16 |
| 70 | }; |
| 71 | return planet; |
| 72 | } |
| 73 | |
| 74 | function loadCatalog () |
| 75 | { |
| 76 | if (typeof PLANET_CATALOG !== 'undefined' && PLANET_CATALOG && PLANET_CATALOG.planets) |
| 77 | { return PLANET_CATALOG; } |
| 78 | if (typeof require !== 'undefined') |
| 79 | { |
| 80 | try |
| 81 | { return require('../server/lib/planets.json'); } |
| 82 | catch (e) {} |
| 83 | } |
| 84 | return { planets: [] }; |
| 85 | } |
| 86 | |
| 87 | function planetFromData (data) |
| 88 | { |
| 89 | var planet = createPlanet({ |
| 90 | name: data.name, |
| 91 | people: data.people, |
| 92 | jumpsToClear: data.jumpsToClear, |
| 93 | fallTime: data.fallTime, |
| 94 | riseTime: data.riseTime, |
| 95 | colors: data.colors |
| 96 | }); |
| 97 | planet.id = data.id; |
| 98 | return planet; |
| 99 | } |
| 100 | |
| 101 | function allPlanets () |
| 102 | { |
| 103 | var raw = loadCatalog().planets || []; |
| 104 | var out = []; |
| 105 | for (var i = 0; i < raw.length; i++) |
| 106 | { out.push(planetFromData(raw[i])); } |
| 107 | if (out.length === 0) |
| 108 | { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; } |
| 109 | return out; |
| 110 | } |
| 111 | |
| 112 | function planetById (id) |
| 113 | { |
| 114 | var all = allPlanets(); |
| 115 | for (var i = 0; i < all.length; i++) |
| 116 | { |
| 117 | if (all[i].id === id) |
| 118 | { return all[i]; } |
| 119 | } |
| 120 | return defaultPlanet(); |
| 121 | } |
| 122 | |
| 123 | function defaultPlanet () |
| 124 | { |
| 125 | var all = allPlanets(); |
| 126 | for (var i = 0; i < all.length; i++) |
| 127 | { |
| 128 | if (all[i].id === 'terra') |
| 129 | { return all[i]; } |
| 130 | } |
| 131 | return all[0]; |
| 132 | } |
| 133 | |
| 134 | function formatStats (planet) |
| 135 | { |
| 136 | var d = displayValues(planet); |
| 137 | return 'g ' + d.gravity.toFixed(2) |
| 138 | + ' d ' + d.density.toFixed(2) |
| 139 | + ' F ' + Math.round(d.newtons) + ' N' |
| 140 | + ' Power ' + d.power + '%'; |
| 141 | } |
| 142 | |
| 143 | function spriteKey (planet, colorIndex) |
| 144 | { |
| 145 | var colors = planet && planet.colors; |
| 146 | var c = colors && colors[colorIndex]; |
| 147 | if (!c) |
| 148 | { return 'gray'; } |
| 149 | return (planet.id || 'planet') + '-' + (c.name || String(colorIndex)); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Jump height in tiles for a match line. |
| 154 | * @param {object} planet |
| 155 | * @param {number} matchLength number of blocks in the line (>= 3) |
| 156 | * @param {boolean} vertical true for a vertical line |
| 157 | */ |
| 158 | function jumpHeight (planet, matchLength, vertical) |
| 159 | { |
| 160 | var extra = Math.max(0, (matchLength | 0) - 3); |
| 161 | var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra; |
| 162 | return planet.baseJumpHeight * multiplier; |
| 163 | } |
| 164 | |
| 165 | /** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */ |
| 166 | function launchVelocity (planet, height) |
| 167 | { |
| 168 | if (height <= 0) |
| 169 | { return 0; } |
| 170 | return (2 * height) / planet.riseTime; |
| 171 | } |
| 172 | |
| 173 | /** Constant deceleration (tiles/s^2) paired with launchVelocity. */ |
| 174 | function launchDeceleration (planet, height) |
| 175 | { |
| 176 | if (height <= 0) |
| 177 | { return 0; } |
| 178 | return (2 * height) / (planet.riseTime * planet.riseTime); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower |
| 183 | * chance that a shot-off block misses the opponent). |
| 184 | */ |
| 185 | function power (planet) |
| 186 | { |
| 187 | var gravity = (planet.jumpsToClear - 1) / 3; |
| 188 | var air = (5 - planet.fallTime) / 4; |
| 189 | var mass = (3 - planet.riseTime) / 2.5; |
| 190 | return (gravity + air + mass) / 3; |
| 191 | } |
| 192 | |
| 193 | /** Maximum miss chance, used by the gameplay formula and the displayed Power. */ |
| 194 | var MAX_MISS_CHANCE = 0.60; |
| 195 | var MIN_MISS_CHANCE = 0.05; |
| 196 | |
| 197 | /** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */ |
| 198 | function missChance (planet) |
| 199 | { |
| 200 | return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Displayed Power, as a 0–100 percentage. This is the complement of the |
| 205 | * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss |
| 206 | * → 0% Power. The gameplay power() and missChance() formulas are unchanged. |
| 207 | */ |
| 208 | function displayPower (planet) |
| 209 | { |
| 210 | return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³), |
| 215 | * launch force (N), and displayed Power as a percentage. The numbers do |
| 216 | * not have to explain themselves. |
| 217 | */ |
| 218 | function displayValues (planet) |
| 219 | { |
| 220 | var gravity = 9.81 * planet.jumpsToClear / 2; |
| 221 | var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60; |
| 222 | var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160; |
| 223 | return { |
| 224 | gravity: gravity, |
| 225 | density: density, |
| 226 | newtons: newtons, |
| 227 | power: displayPower(planet), |
| 228 | missChance: missChance(planet) |
| 229 | }; |
| 230 | } |
| 231 | |
| 232 | var Planet = { |
| 233 | FIELD_WIDTH: FIELD_WIDTH, |
| 234 | FIELD_HEIGHT: FIELD_HEIGHT, |
| 235 | COLOR_COUNT: 5, |
| 236 | SETTLE_TIME: 3, |
| 237 | SPAWN_INTERVAL: 1, |
| 238 | JUMP_VERTICAL_BONUS: 1.2, |
| 239 | JUMP_EXTRA_BLOCK: 0.05, |
| 240 | createPlanet: createPlanet, |
| 241 | defaultPlanet: defaultPlanet, |
| 242 | defaultColors: defaultColors, |
| 243 | allPlanets: allPlanets, |
| 244 | planetById: planetById, |
| 245 | planetFromData: planetFromData, |
| 246 | formatStats: formatStats, |
| 247 | spriteKey: spriteKey, |
| 248 | jumpHeight: jumpHeight, |
| 249 | launchVelocity: launchVelocity, |
| 250 | launchDeceleration: launchDeceleration, |
| 251 | MAX_MISS_CHANCE: MAX_MISS_CHANCE, |
| 252 | MIN_MISS_CHANCE: MIN_MISS_CHANCE, |
| 253 | power: power, |
| 254 | missChance: missChance, |
| 255 | displayPower: displayPower, |
| 256 | displayValues: displayValues |
| 257 | }; |
| 258 | |
| 259 | if (typeof module !== 'undefined' && module.exports) |
| 260 | { module.exports = Planet; } |
| 261 | else if (typeof globalThis !== 'undefined') |
| 262 | { globalThis.Planet = Planet; } |
projects-timfalken-com meteors