BlueGit

projects-timfalken-com meteors

Files

meteors / src / planet.js

planet.js

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