BlueGit

projects-timfalken-com meteors

Files

meteors / src / planet.js

planet.js

planet.js 10.3 KiB Download Raw
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 JUMP_HEIGHT_SCALE * FIELD_HEIGHT
12 * / jumpsToClear tiles (JUMP_HEIGHT_SCALE is 1.5 since t1119: the
13 * minimum jump is 50% higher). At gravity 1 that is 19.5 tiles, so
14 * one jump from the floor clears the 13-tile field with room to spare;
15 * at 4 you still need more than one jump, but each is 50% taller.
16 * Air (fallTime): 1 s (hard, fast fall) .. 5 s (easy, slow fall).
17 * Spawn, unsupported and cargo blocks fall at FIELD_HEIGHT / fallTime
18 * tiles/s. After a jump peaks, a flying cluster floats down at
19 * DESCENT_SPEED_SCALE * that speed (0.5 since t1122 / t1124: only the
20 * cluster float-down is halved). Relative differences between planets
21 * (1 / fallTime) are unchanged. Upward launch is not affected.
22 * Mass (riseTime): 0.5 s (hard, fast jump) .. 3 s (easy, slow jump).
23 * Time to reach the apex, independent of jump height.
24 *
25 * Jump-height multipliers (never explained in the game):
26 * A vertical match of 3 is 120 % of a horizontal row of 3.
27 * Each extra block beyond 3 adds 5 %.
28 *
29 * Power is the average of the three difficulty axes (0 easy .. 1 hard)
30 * and is shown as a percentage. Miss chance (stage 2) goes from 60 % at
31 * power 0 to 5 % at power 1.
32 */
33
34var FIELD_WIDTH = 10;
35var FIELD_HEIGHT = 13;
36/** t1119: every match jump (the gravity-applied minimum and the
37 * vertical / extra-block bonuses derived from it) is 50% higher. */
38var JUMP_HEIGHT_SCALE = 1.5;
39/** t1122 / t1124: only a flying cluster's post-apex float-down
40 * (terminal speed and the apex ease into it) is halved. Spawn fall,
41 * unsupported / released singles and dropped cargo use the full
42 * fallSpeed. Upward launch is unchanged. */
43var DESCENT_SPEED_SCALE = 0.5;
44
45
46function clamp (value, lo, hi)
47{
48 return Math.max(lo, Math.min(hi, value));
49}
50
51function defaultColors ()
52{
53 return [
54 { id: 0, name: 'ember', hex: '#e85d3a', shape: 'triangle', image: 'images/block-ember.png' },
55 { id: 1, name: 'aqua', hex: '#3db8e8', shape: 'circle', image: 'images/block-aqua.png' },
56 { id: 2, name: 'leaf', hex: '#4cbf4c', shape: 'square', image: 'images/block-leaf.png' },
57 { id: 3, name: 'sun', hex: '#f0c53a', shape: 'diamond', image: 'images/block-sun.png' },
58 { id: 4, name: 'orchid', hex: '#c45ad4', shape: 'star', image: 'images/block-orchid.png' }
59 ];
60}
61
62function createPlanet (opts)
63{
64 opts = opts || {};
65 var jumpsToClear = clamp(opts.jumpsToClear == null ? 2 : opts.jumpsToClear, 1, 4);
66 var fallTime = clamp(opts.fallTime == null ? 3 : opts.fallTime, 1, 5);
67 var riseTime = clamp(opts.riseTime == null ? 1.5 : opts.riseTime, 0.5, 3);
68 var colors = opts.colors || defaultColors();
69 var planet = {
70 id: opts.id || null,
71 name: opts.name || 'Terra',
72 people: opts.people || 'Terrans',
73 jumpsToClear: jumpsToClear,
74 fallTime: fallTime,
75 riseTime: riseTime,
76 colors: colors,
77 baseJumpHeight: JUMP_HEIGHT_SCALE * FIELD_HEIGHT / jumpsToClear,
78 // Single-block fall (spawn, unsupported, dropped cargo).
79 fallSpeed: FIELD_HEIGHT / fallTime,
80 // Time to ease from vy=0 at the apex to terminal speed.
81 // Shorter when air resistance is low (small fallTime).
82 apexEaseTime: 0.15 * fallTime,
83 fallAccel: (FIELD_HEIGHT / fallTime) / (0.15 * fallTime),
84 // t1122 / t1124: flying-cluster float-down only. Same ease
85 // duration; accel scales with the halved terminal speed.
86 clusterFallSpeed: DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime,
87 clusterFallAccel: (DESCENT_SPEED_SCALE * FIELD_HEIGHT / fallTime) / (0.15 * fallTime),
88 // Visual landing bounce: faster at low air resistance. Stage-3 params.
89 bounceDuration: 0.12 * fallTime,
90 bounceSquash: 0.16
91 };
92 return planet;
93}
94
95function loadCatalog ()
96{
97 if (typeof PLANET_CATALOG !== 'undefined' && PLANET_CATALOG && PLANET_CATALOG.planets)
98 { return PLANET_CATALOG; }
99 if (typeof require !== 'undefined')
100 {
101 try
102 { return require('../server/lib/planets.json'); }
103 catch (e) {}
104 }
105 return { planets: [] };
106}
107
108/** Player-owned custom planets (from the profile) and any remembered
109 * snapshots (opponents in a lobby), not part of the official catalog. */
110var _customPlanets = [];
111var _remembered = Object.create(null);
112
113function setCustomPlanets (list)
114{
115 _customPlanets = [];
116 if (!list || !list.length)
117 { return; }
118 for (var i = 0; i < list.length; i++)
119 { _customPlanets.push(planetFromData(list[i])); }
120}
121
122function rememberPlanet (data)
123{
124 if (!data || !data.id)
125 { return null; }
126 var pl = planetFromData(data);
127 _remembered[pl.id] = pl;
128 return pl;
129}
130
131function forgetRemembered ()
132{
133 _remembered = Object.create(null);
134}
135
136function planetFromData (data)
137{
138 var planet = createPlanet({
139 name: data.name,
140 people: data.people,
141 jumpsToClear: data.jumpsToClear,
142 fallTime: data.fallTime,
143 riseTime: data.riseTime,
144 colors: data.colors
145 });
146 planet.id = data.id;
147 if (data.custom)
148 { planet.custom = true; }
149 if (data.owner_id)
150 { planet.owner_id = data.owner_id; }
151 return planet;
152}
153
154function allPlanets ()
155{
156 var raw = loadCatalog().planets || [];
157 var out = [];
158 for (var i = 0; i < raw.length; i++)
159 { out.push(planetFromData(raw[i])); }
160 if (out.length === 0)
161 { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; }
162 for (var c = 0; c < _customPlanets.length; c++)
163 { out.push(_customPlanets[c]); }
164 return out;
165}
166
167function planetById (id)
168{
169 var all = allPlanets();
170 for (var i = 0; i < all.length; i++)
171 {
172 if (all[i].id === id)
173 { return all[i]; }
174 }
175 if (id && _remembered[id])
176 { return _remembered[id]; }
177 return defaultPlanet();
178}
179
180function officialPlanets ()
181{
182 var raw = loadCatalog().planets || [];
183 var out = [];
184 for (var i = 0; i < raw.length; i++)
185 { out.push(planetFromData(raw[i])); }
186 return out;
187}
188
189function customPlanets ()
190{
191 return _customPlanets.slice();
192}
193
194function pickerPlanets ()
195{
196 return officialPlanets().concat(_customPlanets);
197}
198
199function defaultPlanet ()
200{
201 var all = allPlanets();
202 for (var i = 0; i < all.length; i++)
203 {
204 if (all[i].id === 'terra')
205 { return all[i]; }
206 }
207 return all[0];
208}
209
210function formatStats (planet)
211{
212 var d = displayValues(planet);
213 return 'g ' + d.gravity.toFixed(2)
214 + ' d ' + d.density.toFixed(2)
215 + ' F ' + Math.round(d.newtons) + ' N'
216 + ' Power ' + d.power + '%';
217}
218
219function spriteKey (planet, colorIndex)
220{
221 var colors = planet && planet.colors;
222 var c = colors && colors[colorIndex];
223 if (!c)
224 { return 'gray'; }
225 // Custom planets (and any color that names a registry block) share
226 // the preloaded Blocks.images() sprite, so late-loaded customs and
227 // opponent snapshots do not need a second preload.
228 if (c.block)
229 { return 'block:' + c.block; }
230 return (planet.id || 'planet') + '-' + (c.name || String(colorIndex));
231}
232
233/**
234 * Jump height in tiles for a match line.
235 * @param {object} planet
236 * @param {number} matchLength number of blocks in the line (>= 3)
237 * @param {boolean} vertical true for a vertical line
238 */
239function jumpHeight (planet, matchLength, vertical)
240{
241 var extra = Math.max(0, (matchLength | 0) - 3);
242 var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra;
243 return planet.baseJumpHeight * multiplier;
244}
245
246/** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */
247function launchVelocity (planet, height)
248{
249 if (height <= 0)
250 { return 0; }
251 return (2 * height) / planet.riseTime;
252}
253
254/** Constant deceleration (tiles/s^2) paired with launchVelocity. */
255function launchDeceleration (planet, height)
256{
257 if (height <= 0)
258 { return 0; }
259 return (2 * height) / (planet.riseTime * planet.riseTime);
260}
261
262/**
263 * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower
264 * chance that a shot-off block misses the opponent).
265 */
266function power (planet)
267{
268 var gravity = (planet.jumpsToClear - 1) / 3;
269 var air = (5 - planet.fallTime) / 4;
270 var mass = (3 - planet.riseTime) / 2.5;
271 return (gravity + air + mass) / 3;
272}
273
274/** Maximum miss chance, used by the gameplay formula and the displayed Power. */
275var MAX_MISS_CHANCE = 0.60;
276var MIN_MISS_CHANCE = 0.05;
277
278/** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */
279function missChance (planet)
280{
281 return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet);
282}
283
284/**
285 * Displayed Power, as a 0–100 percentage. This is the complement of the
286 * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss
287 * → 0% Power. The gameplay power() and missChance() formulas are unchanged.
288 */
289function displayPower (planet)
290{
291 return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100);
292}
293
294/**
295 * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),
296 * launch force (N), and displayed Power as a percentage. The numbers do
297 * not have to explain themselves.
298 */
299function displayValues (planet)
300{
301 var gravity = 9.81 * planet.jumpsToClear / 2;
302 var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60;
303 var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160;
304 return {
305 gravity: gravity,
306 density: density,
307 newtons: newtons,
308 power: displayPower(planet),
309 missChance: missChance(planet)
310 };
311}
312
313var Planet = {
314 FIELD_WIDTH: FIELD_WIDTH,
315 FIELD_HEIGHT: FIELD_HEIGHT,
316 JUMP_HEIGHT_SCALE: JUMP_HEIGHT_SCALE,
317 DESCENT_SPEED_SCALE: DESCENT_SPEED_SCALE,
318 COLOR_COUNT: 5,
319 SETTLE_TIME: 3,
320 SPAWN_INTERVAL: 1,
321 JUMP_VERTICAL_BONUS: 1.2,
322 JUMP_EXTRA_BLOCK: 0.05,
323 createPlanet: createPlanet,
324 defaultPlanet: defaultPlanet,
325 defaultColors: defaultColors,
326 allPlanets: allPlanets,
327 officialPlanets: officialPlanets,
328 customPlanets: customPlanets,
329 pickerPlanets: pickerPlanets,
330 setCustomPlanets: setCustomPlanets,
331 rememberPlanet: rememberPlanet,
332 forgetRemembered: forgetRemembered,
333 planetById: planetById,
334 planetFromData: planetFromData,
335 formatStats: formatStats,
336 spriteKey: spriteKey,
337 jumpHeight: jumpHeight,
338 launchVelocity: launchVelocity,
339 launchDeceleration: launchDeceleration,
340 MAX_MISS_CHANCE: MAX_MISS_CHANCE,
341 MIN_MISS_CHANCE: MIN_MISS_CHANCE,
342 power: power,
343 missChance: missChance,
344 displayPower: displayPower,
345 displayValues: displayValues
346};
347
348if (typeof module !== 'undefined' && module.exports)
349{ module.exports = Planet; }
350else if (typeof globalThis !== 'undefined')
351{ globalThis.Planet = Planet; }