BlueGit

projects-timfalken-com meteors

Files

meteors / src / planet.js

planet.js

planet.js 8.7 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
108function planetFromData (data)
109{
110 var planet = createPlanet({
111 name: data.name,
112 people: data.people,
113 jumpsToClear: data.jumpsToClear,
114 fallTime: data.fallTime,
115 riseTime: data.riseTime,
116 colors: data.colors
117 });
118 planet.id = data.id;
119 return planet;
120}
121
122function allPlanets ()
123{
124 var raw = loadCatalog().planets || [];
125 var out = [];
126 for (var i = 0; i < raw.length; i++)
127 { out.push(planetFromData(raw[i])); }
128 if (out.length === 0)
129 { out.push(createPlanet({ name: 'Terra', people: 'Terrans', jumpsToClear: 2, fallTime: 3, riseTime: 1.5 })); out[0].id = 'terra'; }
130 return out;
131}
132
133function planetById (id)
134{
135 var all = allPlanets();
136 for (var i = 0; i < all.length; i++)
137 {
138 if (all[i].id === id)
139 { return all[i]; }
140 }
141 return defaultPlanet();
142}
143
144function defaultPlanet ()
145{
146 var all = allPlanets();
147 for (var i = 0; i < all.length; i++)
148 {
149 if (all[i].id === 'terra')
150 { return all[i]; }
151 }
152 return all[0];
153}
154
155function formatStats (planet)
156{
157 var d = displayValues(planet);
158 return 'g ' + d.gravity.toFixed(2)
159 + ' d ' + d.density.toFixed(2)
160 + ' F ' + Math.round(d.newtons) + ' N'
161 + ' Power ' + d.power + '%';
162}
163
164function spriteKey (planet, colorIndex)
165{
166 var colors = planet && planet.colors;
167 var c = colors && colors[colorIndex];
168 if (!c)
169 { return 'gray'; }
170 return (planet.id || 'planet') + '-' + (c.name || String(colorIndex));
171}
172
173/**
174 * Jump height in tiles for a match line.
175 * @param {object} planet
176 * @param {number} matchLength number of blocks in the line (>= 3)
177 * @param {boolean} vertical true for a vertical line
178 */
179function jumpHeight (planet, matchLength, vertical)
180{
181 var extra = Math.max(0, (matchLength | 0) - 3);
182 var multiplier = (vertical ? 1.2 : 1.0) + 0.05 * extra;
183 return planet.baseJumpHeight * multiplier;
184}
185
186/** Initial upward speed (tiles/s) so a jump of `height` peaks in riseTime. */
187function launchVelocity (planet, height)
188{
189 if (height <= 0)
190 { return 0; }
191 return (2 * height) / planet.riseTime;
192}
193
194/** Constant deceleration (tiles/s^2) paired with launchVelocity. */
195function launchDeceleration (planet, height)
196{
197 if (height <= 0)
198 { return 0; }
199 return (2 * height) / (planet.riseTime * planet.riseTime);
200}
201
202/**
203 * Difficulty power in 0..1. Higher is harder (and, in stage 2, a lower
204 * chance that a shot-off block misses the opponent).
205 */
206function power (planet)
207{
208 var gravity = (planet.jumpsToClear - 1) / 3;
209 var air = (5 - planet.fallTime) / 4;
210 var mass = (3 - planet.riseTime) / 2.5;
211 return (gravity + air + mass) / 3;
212}
213
214/** Maximum miss chance, used by the gameplay formula and the displayed Power. */
215var MAX_MISS_CHANCE = 0.60;
216var MIN_MISS_CHANCE = 0.05;
217
218/** Stage-2 miss chance, 0.60 at power 0 down to 0.05 at power 1. */
219function missChance (planet)
220{
221 return MAX_MISS_CHANCE - (MAX_MISS_CHANCE - MIN_MISS_CHANCE) * power(planet);
222}
223
224/**
225 * Displayed Power, as a 0–100 percentage. This is the complement of the
226 * miss chance against the maximum (60%): 0% miss → 100% Power, 60% miss
227 * → 0% Power. The gameplay power() and missChance() formulas are unchanged.
228 */
229function displayPower (planet)
230{
231 return Math.round((1 - missChance(planet) / MAX_MISS_CHANCE) * 100);
232}
233
234/**
235 * Numeric HUD values: a gravity-like constant (m/s²), air density (kg/m³),
236 * launch force (N), and displayed Power as a percentage. The numbers do
237 * not have to explain themselves.
238 */
239function displayValues (planet)
240{
241 var gravity = 9.81 * planet.jumpsToClear / 2;
242 var density = 0.25 + ((5 - planet.fallTime) / 4) * 1.60;
243 var newtons = 40 + ((planet.riseTime - 0.5) / 2.5) * 160;
244 return {
245 gravity: gravity,
246 density: density,
247 newtons: newtons,
248 power: displayPower(planet),
249 missChance: missChance(planet)
250 };
251}
252
253var Planet = {
254 FIELD_WIDTH: FIELD_WIDTH,
255 FIELD_HEIGHT: FIELD_HEIGHT,
256 JUMP_HEIGHT_SCALE: JUMP_HEIGHT_SCALE,
257 DESCENT_SPEED_SCALE: DESCENT_SPEED_SCALE,
258 COLOR_COUNT: 5,
259 SETTLE_TIME: 3,
260 SPAWN_INTERVAL: 1,
261 JUMP_VERTICAL_BONUS: 1.2,
262 JUMP_EXTRA_BLOCK: 0.05,
263 createPlanet: createPlanet,
264 defaultPlanet: defaultPlanet,
265 defaultColors: defaultColors,
266 allPlanets: allPlanets,
267 planetById: planetById,
268 planetFromData: planetFromData,
269 formatStats: formatStats,
270 spriteKey: spriteKey,
271 jumpHeight: jumpHeight,
272 launchVelocity: launchVelocity,
273 launchDeceleration: launchDeceleration,
274 MAX_MISS_CHANCE: MAX_MISS_CHANCE,
275 MIN_MISS_CHANCE: MIN_MISS_CHANCE,
276 power: power,
277 missChance: missChance,
278 displayPower: displayPower,
279 displayValues: displayValues
280};
281
282if (typeof module !== 'undefined' && module.exports)
283{ module.exports = Planet; }
284else if (typeof globalThis !== 'undefined')
285{ globalThis.Planet = Planet; }