@zaisma/effects 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/budget.js CHANGED
@@ -17,13 +17,13 @@ export const QUALITY_BUDGETS = {
17
17
  };
18
18
 
19
19
  /** The share of the particle budget solid chunks may take: each is up to sixty corners, where a sprite is four. */
20
- export const CHUNK_BUDGET_SHARE = 0.3;
20
+ export const CHUNK_BUDGET_SHARE_NUMBER = 0.3;
21
21
  /** The share of what's free that one emitter may take, by its effect's importance: the rest is kept for what comes next. */
22
22
  export const IMPORTANCE_FREE_SHARE = { low: 0.4, normal: 0.7, high: 1 };
23
23
  /** Beyond this far from the camera an effect draws fewer particles, down to a quarter. */
24
24
  export const FULL_COUNT_DISTANCE_M = 60;
25
- const MINIMUM_DISTANCE_SHARE = 0.25;
26
- const MINIMUM_FILL_SHARE = 0.25;
25
+ const MINIMUM_DISTANCE_SHARE_NUMBER = 0.25;
26
+ const MINIMUM_FILL_SHARE_NUMBER = 0.25;
27
27
 
28
28
  /** How many particles a level's budget holds, with the game's own scale on it. */
29
29
  export function particleBudget(qualityLevel, particleBudgetPct) {
@@ -36,21 +36,21 @@ export function particleBudget(qualityLevel, particleBudgetPct) {
36
36
  * A burst of three or fewer with no flow is never thinned: one missing of three is seen.
37
37
  */
38
38
  export function planEmitter({ wantedCount, isNeverThinned, qualityLevel, importance, distanceM, freeCount, longestFreeCount, lookCountShare, fillShare }) {
39
- let keptShare = 1;
39
+ let keptShareNumber = 1;
40
40
  if (!isNeverThinned) {
41
- keptShare = Math.min(lookCountShare, 1) * (QUALITY_BUDGETS[qualityLevel].keptPct / 100);
42
- if (distanceM > FULL_COUNT_DISTANCE_M) keptShare *= Math.max(FULL_COUNT_DISTANCE_M / distanceM, MINIMUM_DISTANCE_SHARE);
43
- keptShare *= fillShare;
41
+ keptShareNumber = Math.min(lookCountShare, 1) * (QUALITY_BUDGETS[qualityLevel].keptPct / 100);
42
+ if (distanceM > FULL_COUNT_DISTANCE_M) keptShareNumber *= Math.max(FULL_COUNT_DISTANCE_M / distanceM, MINIMUM_DISTANCE_SHARE_NUMBER);
43
+ keptShareNumber *= fillShare;
44
44
  }
45
45
  // A little room over the count, since lives differ: the ring takes the oldest when it's short.
46
- let slotCount = Math.ceil(wantedCount * keptShare * 1.15) + 2;
46
+ let slotCount = Math.ceil(wantedCount * keptShareNumber * 1.15) + 2;
47
47
  const allowedCount = Math.min(Math.floor(freeCount * IMPORTANCE_FREE_SHARE[importance]), longestFreeCount);
48
48
  if (slotCount > allowedCount) {
49
49
  if (allowedCount < 1) return { slotCount: 0, keptShare: 0 };
50
- if (!isNeverThinned) keptShare *= allowedCount / slotCount;
50
+ if (!isNeverThinned) keptShareNumber *= allowedCount / slotCount;
51
51
  slotCount = allowedCount;
52
52
  }
53
- return { slotCount, keptShare };
53
+ return { slotCount, keptShare: keptShareNumber };
54
54
  }
55
55
 
56
56
  /**
@@ -58,17 +58,17 @@ export function planEmitter({ wantedCount, isNeverThinned, qualityLevel, importa
58
58
  * level's limit, new particles of big see-through effects are drawn thinner, and it lets go as the screen clears.
59
59
  */
60
60
  export function createFillGuard() {
61
- let fillShare = 1;
61
+ let fillShareNumber = 1;
62
62
  return {
63
63
  get fillShare() {
64
- return fillShare;
64
+ return fillShareNumber;
65
65
  },
66
66
  /** Answers the share now. */
67
- count(coveredScreenCount, qualityLevel) {
67
+ keptShareOf(coveredScreenCount, qualityLevel) {
68
68
  const limit = QUALITY_BUDGETS[qualityLevel].fillScreenCount;
69
- if (coveredScreenCount > limit) fillShare = Math.max(fillShare * 0.94, MINIMUM_FILL_SHARE);
70
- else if (coveredScreenCount < limit * 0.7) fillShare = Math.min(fillShare * 1.02, 1);
71
- return fillShare;
69
+ if (coveredScreenCount > limit) fillShareNumber = Math.max(fillShareNumber * 0.94, MINIMUM_FILL_SHARE_NUMBER);
70
+ else if (coveredScreenCount < limit * 0.7) fillShareNumber = Math.min(fillShareNumber * 1.02, 1);
71
+ return fillShareNumber;
72
72
  },
73
73
  };
74
74
  }
@@ -85,7 +85,7 @@ export function createFrameWatcher() {
85
85
  let totalMs = 0;
86
86
  let frameCount = 0;
87
87
  return {
88
- count(nowMs) {
88
+ countFrame(nowMs) {
89
89
  if (lastFrameMs !== null) {
90
90
  const durationMs = nowMs - lastFrameMs;
91
91
  if (durationMs > 0 && durationMs < MAXIMUM_COUNTED_FRAME_DURATION_MS) {
package/debris.js CHANGED
@@ -10,7 +10,7 @@ import { makeParticle } from "./emit.js";
10
10
  import { GRAVITY_M_PER_SEC2 } from "./motion.js";
11
11
 
12
12
  const STEP_SEC = 1 / 60;
13
- const MAXIMUM_STEPS_PER_FRAME = 4;
13
+ const MAXIMUM_STEP_COUNT_PER_FRAME = 4;
14
14
  const REST_SPEED_M_PER_SEC = 0.35;
15
15
 
16
16
  const born = { x: 0, y: 0, z: 0, velocityX: 0, velocityY: 0, velocityZ: 0, lifeSec: 0, seedNumber: 0 };
@@ -23,10 +23,10 @@ const born = { x: 0, y: 0, z: 0, velocityX: 0, velocityY: 0, velocityZ: 0, lifeS
23
23
  */
24
24
  export function createDebris({ chunks, findSurface, onLanded }) {
25
25
  let slab = null;
26
- let capacity = 0;
26
+ let capacityCount = 0;
27
27
  /** Every place in the slab, a chunk or null. */
28
28
  let held = [];
29
- let nextOffset = 0;
29
+ let nextSlotIndex = 0;
30
30
  let liveCount = 0;
31
31
  let leftoverSec = 0;
32
32
  const met = { x: 0, y: 0, z: 0, normalX: 0, normalY: 1, normalZ: 0 };
@@ -34,12 +34,12 @@ export function createDebris({ chunks, findSurface, onLanded }) {
34
34
  function setCapacity(newCapacity) {
35
35
  if (slab) chunks().giveSlab(slab);
36
36
  for (const chunk of held) if (chunk) chunk.play.debrisCount -= 1;
37
- capacity = newCapacity;
38
- slab = capacity > 0 ? chunks().takeSlab(capacity, null) : null;
39
- if (slab) capacity = slab.capacity;
40
- else capacity = 0;
41
- held = new Array(capacity).fill(null);
42
- nextOffset = 0;
37
+ capacityCount = newCapacity;
38
+ slab = capacityCount > 0 ? chunks().takeSlab(capacityCount, null) : null;
39
+ if (slab) capacityCount = slab.capacity;
40
+ else capacityCount = 0;
41
+ held = new Array(capacityCount).fill(null);
42
+ nextSlotIndex = 0;
43
43
  liveCount = 0;
44
44
  }
45
45
 
@@ -49,14 +49,14 @@ export function createDebris({ chunks, findSurface, onLanded }) {
49
49
  * spinRadPerSec, elementRows (one per colour), nowSec }. Answers how many were thrown.
50
50
  */
51
51
  function throwChunks(thrower) {
52
- if (capacity === 0) return 0;
52
+ if (capacityCount === 0) return 0;
53
53
  const { play, emitter } = thrower;
54
54
  let thrownCount = 0;
55
55
  for (let number = 0; number < thrower.count; number += 1) {
56
56
  makeParticle(emitter, thrower.playSeedNumber, number, born);
57
57
  const next = randomFrom(hashNumbers(thrower.playSeedNumber, emitter.elementIndex, number, 11));
58
- const offset = nextOffset;
59
- nextOffset = (nextOffset + 1) % capacity;
58
+ const slotIndex = nextSlotIndex;
59
+ nextSlotIndex = (nextSlotIndex + 1) % capacityCount;
60
60
  const scale = play.scaleShare;
61
61
  // The play's turn takes its own space (+y its direction) to the world's.
62
62
  const place = play.turnPoint(born.x * scale, born.y * scale, born.z * scale);
@@ -65,7 +65,7 @@ export function createDebris({ chunks, findSurface, onLanded }) {
65
65
  const z = play.place.z + place.z;
66
66
  const velocity = play.turnPoint(born.velocityX * scale, born.velocityY * scale, born.velocityZ * scale);
67
67
  const chunk = {
68
- offset,
68
+ offset: slotIndex,
69
69
  play,
70
70
  x,
71
71
  y,
@@ -88,8 +88,8 @@ export function createDebris({ chunks, findSurface, onLanded }) {
88
88
  hasLanded: false,
89
89
  isWritten: false,
90
90
  };
91
- if (held[offset]) held[offset].play.debrisCount -= 1;
92
- held[offset] = chunk;
91
+ if (held[slotIndex]) held[slotIndex].play.debrisCount -= 1;
92
+ held[slotIndex] = chunk;
93
93
  play.debrisCount += 1;
94
94
  thrownCount += 1;
95
95
  }
@@ -115,15 +115,15 @@ export function createDebris({ chunks, findSurface, onLanded }) {
115
115
  chunk.x = met.x + met.normalX * chunk.halfSizeM;
116
116
  chunk.y = met.y + met.normalY * chunk.halfSizeM;
117
117
  chunk.z = met.z + met.normalZ * chunk.halfSizeM;
118
- const intoSpeed = chunk.velocityX * met.normalX + chunk.velocityY * met.normalY + chunk.velocityZ * met.normalZ;
119
- if (intoSpeed < 0) {
120
- const alongX = chunk.velocityX - intoSpeed * met.normalX;
121
- const alongY = chunk.velocityY - intoSpeed * met.normalY;
122
- const alongZ = chunk.velocityZ - intoSpeed * met.normalZ;
123
- const kept = 1 - chunk.gripShare * 0.6;
124
- chunk.velocityX = alongX * kept - intoSpeed * chunk.bounceShare * met.normalX;
125
- chunk.velocityY = alongY * kept - intoSpeed * chunk.bounceShare * met.normalY;
126
- chunk.velocityZ = alongZ * kept - intoSpeed * chunk.bounceShare * met.normalZ;
118
+ const intoSpeedMPerSec = chunk.velocityX * met.normalX + chunk.velocityY * met.normalY + chunk.velocityZ * met.normalZ;
119
+ if (intoSpeedMPerSec < 0) {
120
+ const alongX = chunk.velocityX - intoSpeedMPerSec * met.normalX;
121
+ const alongY = chunk.velocityY - intoSpeedMPerSec * met.normalY;
122
+ const alongZ = chunk.velocityZ - intoSpeedMPerSec * met.normalZ;
123
+ const keptShareNumber = 1 - chunk.gripShare * 0.6;
124
+ chunk.velocityX = alongX * keptShareNumber - intoSpeedMPerSec * chunk.bounceShare * met.normalX;
125
+ chunk.velocityY = alongY * keptShareNumber - intoSpeedMPerSec * chunk.bounceShare * met.normalY;
126
+ chunk.velocityZ = alongZ * keptShareNumber - intoSpeedMPerSec * chunk.bounceShare * met.normalZ;
127
127
  chunk.spinRadPerSec *= 0.6;
128
128
  }
129
129
  if (!chunk.hasLanded) {
@@ -137,24 +137,24 @@ export function createDebris({ chunks, findSurface, onLanded }) {
137
137
  /** Steps what's moving and writes it. toBatchSec turns a time on the effects' clock into the batch's own. */
138
138
  function update(nowSec, frameSec, toBatchSec) {
139
139
  liveCount = 0;
140
- if (capacity === 0) return;
141
- leftoverSec = Math.min(leftoverSec + frameSec, STEP_SEC * MAXIMUM_STEPS_PER_FRAME);
140
+ if (capacityCount === 0) return;
141
+ leftoverSec = Math.min(leftoverSec + frameSec, STEP_SEC * MAXIMUM_STEP_COUNT_PER_FRAME);
142
142
  const stepCount = Math.floor(leftoverSec / STEP_SEC);
143
143
  leftoverSec -= stepCount * STEP_SEC;
144
144
  const batch = chunks();
145
- for (let offset = 0; offset < capacity; offset += 1) {
146
- const chunk = held[offset];
145
+ for (let slotIndex = 0; slotIndex < capacityCount; slotIndex += 1) {
146
+ const chunk = held[slotIndex];
147
147
  if (!chunk) continue;
148
148
  if (nowSec - chunk.birthSec >= chunk.lifeSec || chunk.play.isEnded) {
149
- held[offset] = null;
149
+ held[slotIndex] = null;
150
150
  chunk.play.debrisCount -= 1;
151
- batch.rewrite(slab, offset, 0, 0, 0, -1e9, 1, 0, 0, 0, 0);
151
+ batch.rewrite(slab, slotIndex, 0, 0, 0, -1e9, 1, 0, 0, 0, 0);
152
152
  continue;
153
153
  }
154
154
  liveCount += 1;
155
155
  if (chunk.isResting && chunk.isWritten) continue;
156
- if (!chunk.isResting && nowSec >= chunk.birthSec) for (let count = 0; count < stepCount && !chunk.isResting; count += 1) step(chunk);
157
- batch.rewrite(slab, offset, chunk.x, chunk.y, chunk.z, toBatchSec(chunk.birthSec), chunk.lifeSec, chunk.seedNumber, chunk.elementRow, chunk.play.row, chunk.angleRad);
156
+ if (!chunk.isResting && nowSec >= chunk.birthSec) for (let stepIndex = 0; stepIndex < stepCount && !chunk.isResting; stepIndex += 1) step(chunk);
157
+ batch.rewrite(slab, slotIndex, chunk.x, chunk.y, chunk.z, toBatchSec(chunk.birthSec), chunk.lifeSec, chunk.seedNumber, chunk.elementRow, chunk.play.row, chunk.angleRad);
158
158
  chunk.isWritten = true;
159
159
  }
160
160
  }
@@ -167,7 +167,7 @@ export function createDebris({ chunks, findSurface, onLanded }) {
167
167
  return liveCount;
168
168
  },
169
169
  get capacity() {
170
- return capacity;
170
+ return capacityCount;
171
171
  },
172
172
  };
173
173
  }
package/emit.js CHANGED
@@ -4,7 +4,7 @@
4
4
  // draws some of the same ones, and a player who joins late works out exactly the ones still alive. No engine here.
5
5
  import { hashNumbers, randomFrom } from "./format.js";
6
6
 
7
- const TWO_PI = Math.PI * 2;
7
+ const FULL_TURN_RAD = Math.PI * 2;
8
8
 
9
9
  /**
10
10
  * How many particles one run of an emitter gives off: its burst and what its flow adds while it emits.
@@ -45,12 +45,12 @@ export function isParticleKept(playSeedNumber, elementIndex, particleNumber, kep
45
45
  }
46
46
 
47
47
  function randomDirection(next, into) {
48
- const height = next() * 2 - 1;
49
- const angle = next() * TWO_PI;
50
- const flat = Math.sqrt(1 - height * height);
51
- into.x = Math.cos(angle) * flat;
52
- into.y = height;
53
- into.z = Math.sin(angle) * flat;
48
+ const heightNumber = next() * 2 - 1;
49
+ const angleRad = next() * FULL_TURN_RAD;
50
+ const flatNumber = Math.sqrt(1 - heightNumber * heightNumber);
51
+ into.x = Math.cos(angleRad) * flatNumber;
52
+ into.y = heightNumber;
53
+ into.z = Math.sin(angleRad) * flatNumber;
54
54
  }
55
55
 
56
56
  const offset = { x: 0, y: 0, z: 0 };
@@ -70,16 +70,16 @@ export function makeParticle(emitter, playSeedNumber, particleNumber, into) {
70
70
  let hasOffset = false;
71
71
  if (emitter.fromKind === "sphere" && emitter.fromRadiusM > 0) {
72
72
  randomDirection(next, offset);
73
- const far = emitter.fromRadiusM * (emitter.isFromEdge ? 1 : Math.cbrt(next()));
74
- offset.x *= far;
75
- offset.y *= far;
76
- offset.z *= far;
73
+ const farM = emitter.fromRadiusM * (emitter.isFromEdge ? 1 : Math.cbrt(next()));
74
+ offset.x *= farM;
75
+ offset.y *= farM;
76
+ offset.z *= farM;
77
77
  hasOffset = true;
78
78
  } else if (emitter.fromKind === "ring" && emitter.fromRadiusM > 0) {
79
- const angle = next() * TWO_PI;
80
- const far = emitter.fromRadiusM * (emitter.isFromEdge ? 1 : Math.sqrt(next()));
81
- offset.x = Math.cos(angle) * far;
82
- offset.z = Math.sin(angle) * far;
79
+ const angleRad = next() * FULL_TURN_RAD;
80
+ const farM = emitter.fromRadiusM * (emitter.isFromEdge ? 1 : Math.sqrt(next()));
81
+ offset.x = Math.cos(angleRad) * farM;
82
+ offset.z = Math.sin(angleRad) * farM;
83
83
  hasOffset = true;
84
84
  } else if (emitter.fromKind === "box") {
85
85
  offset.x = (next() - 0.5) * emitter.fromSizeM[0];
@@ -98,45 +98,45 @@ export function makeParticle(emitter, playSeedNumber, particleNumber, into) {
98
98
  // From nowhere in particular, outward and inward have no meaning: it sets off any way.
99
99
  randomDirection(next, way);
100
100
  } else {
101
- const far = Math.hypot(offset.x, offset.y, offset.z) || 1;
102
- const sign = emitter.directionKind === "inward" ? -1 : 1;
103
- way.x = (offset.x / far) * sign;
104
- way.y = (offset.y / far) * sign;
105
- way.z = (offset.z / far) * sign;
101
+ const farM = Math.hypot(offset.x, offset.y, offset.z) || 1;
102
+ const signNumber = emitter.directionKind === "inward" ? -1 : 1;
103
+ way.x = (offset.x / farM) * signNumber;
104
+ way.y = (offset.y / farM) * signNumber;
105
+ way.z = (offset.z / farM) * signNumber;
106
106
  }
107
107
  if (emitter.spreadRad > 0 && (emitter.directionKind === "along" || emitter.directionKind === "up" || hasOffset)) {
108
108
  // Anywhere inside a cone round its direction, evenly over the cone's cap.
109
- const cosAway = 1 - next() * (1 - Math.cos(emitter.spreadRad));
110
- const sinAway = Math.sqrt(Math.max(1 - cosAway * cosAway, 0));
111
- const round = next() * TWO_PI;
109
+ const cosAwayNumber = 1 - next() * (1 - Math.cos(emitter.spreadRad));
110
+ const sinAwayNumber = Math.sqrt(Math.max(1 - cosAwayNumber * cosAwayNumber, 0));
111
+ const roundRad = next() * FULL_TURN_RAD;
112
112
  // Two directions square to the way.
113
113
  const isSteep = Math.abs(way.y) > 0.99;
114
114
  let sideX = isSteep ? 1 : -way.z;
115
115
  let sideY = 0;
116
116
  let sideZ = isSteep ? 0 : way.x;
117
- const sideLength = Math.hypot(sideX, sideY, sideZ);
118
- sideX /= sideLength;
119
- sideZ /= sideLength;
117
+ const sideLengthNumber = Math.hypot(sideX, sideY, sideZ);
118
+ sideX /= sideLengthNumber;
119
+ sideZ /= sideLengthNumber;
120
120
  const otherX = way.y * sideZ - way.z * sideY;
121
121
  const otherY = way.z * sideX - way.x * sideZ;
122
122
  const otherZ = way.x * sideY - way.y * sideX;
123
- const cosRound = Math.cos(round) * sinAway;
124
- const sinRound = Math.sin(round) * sinAway;
125
- const x = way.x * cosAway + sideX * cosRound + otherX * sinRound;
126
- const y = way.y * cosAway + sideY * cosRound + otherY * sinRound;
127
- const z = way.z * cosAway + sideZ * cosRound + otherZ * sinRound;
123
+ const cosRoundNumber = Math.cos(roundRad) * sinAwayNumber;
124
+ const sinRoundNumber = Math.sin(roundRad) * sinAwayNumber;
125
+ const x = way.x * cosAwayNumber + sideX * cosRoundNumber + otherX * sinRoundNumber;
126
+ const y = way.y * cosAwayNumber + sideY * cosRoundNumber + otherY * sinRoundNumber;
127
+ const z = way.z * cosAwayNumber + sideZ * cosRoundNumber + otherZ * sinRoundNumber;
128
128
  way.x = x;
129
129
  way.y = y;
130
130
  way.z = z;
131
131
  }
132
132
 
133
- const speed = emitter.speedMPerSec * (1 + emitter.speedVariationShare * (next() * 2 - 1));
133
+ const speedMPerSec = emitter.speedMPerSec * (1 + emitter.speedVariationShare * (next() * 2 - 1));
134
134
  into.x = offset.x;
135
135
  into.y = offset.y;
136
136
  into.z = offset.z;
137
- into.velocityX = way.x * speed;
138
- into.velocityY = way.y * speed;
139
- into.velocityZ = way.z * speed;
137
+ into.velocityX = way.x * speedMPerSec;
138
+ into.velocityY = way.y * speedMPerSec;
139
+ into.velocityZ = way.z * speedMPerSec;
140
140
  into.lifeSec = emitter.lifeSec * (1 + emitter.lifeVariationShare * (next() * 2 - 1));
141
141
  into.seedNumber = next();
142
142
  return into;
package/flashes.js CHANGED
@@ -47,10 +47,10 @@ export function createFlashes({ scene, sharedUniforms }) {
47
47
  sinceSec %= flash.loopDurationSec;
48
48
  }
49
49
  if (sinceSec >= flash.durationSec) return 0;
50
- let strength = flash.strengthLux * (valueOverLife(flash.strengthKeys, (sinceSec / flash.durationSec) * 100) / 100) * flash.play.strengthShare * flash.play.scaleShare;
50
+ let strengthLux = flash.strengthLux * (valueOverLife(flash.strengthKeys, (sinceSec / flash.durationSec) * 100) / 100) * flash.play.strengthShare * flash.play.scaleShare;
51
51
  // Two flickers that never line up, like a flame.
52
- if (flash.flickerShare > 0) strength *= 1 - flash.flickerShare * 0.5 * (1 + Math.sin(nowSec * 23.7 + flash.startSec * 5) * Math.sin(nowSec * 9.1 + flash.startSec));
53
- return strength;
52
+ if (flash.flickerShare > 0) strengthLux *= 1 - flash.flickerShare * 0.5 * (1 + Math.sin(nowSec * 23.7 + flash.startSec * 5) * Math.sin(nowSec * 9.1 + flash.startSec));
53
+ return strengthLux;
54
54
  }
55
55
 
56
56
  function update(nowSec, cameraPosition, isEnabled) {
@@ -61,16 +61,16 @@ export function createFlashes({ scene, sharedUniforms }) {
61
61
  flashes.delete(flash);
62
62
  continue;
63
63
  }
64
- const strength = isEnabled ? strengthNow(flash, nowSec) : 0;
65
- if (strength <= 0) continue;
64
+ const strengthLux = isEnabled ? strengthNow(flash, nowSec) : 0;
65
+ if (strengthLux <= 0) continue;
66
66
  place.copy(flash.offset).multiplyScalar(flash.play.scaleShare).add(flash.play.place);
67
67
  const radiusM = flash.radiusM * flash.play.scaleShare;
68
- flash.nowStrength = strength;
68
+ flash.nowStrength = strengthLux;
69
69
  flash.nowRadiusM = radiusM;
70
70
  flash.nowX = place.x;
71
71
  flash.nowY = place.y;
72
72
  flash.nowZ = place.z;
73
- flash.score = (strength * radiusM * radiusM) / Math.max(place.distanceToSquared(cameraPosition), 1);
73
+ flash.score = (strengthLux * radiusM * radiusM) / Math.max(place.distanceToSquared(cameraPosition), 1);
74
74
  ranked.push(flash);
75
75
  }
76
76
  ranked.sort((first, second) => second.score - first.score);
package/format.js CHANGED
@@ -194,7 +194,7 @@ function problem(code, message, field, fix) {
194
194
  return { code, message, field, fix };
195
195
  }
196
196
 
197
- function valueProblem(statement, value, field) {
197
+ function valueProblem(statement, fieldValue, field) {
198
198
  const says = (message, fix) => problem("bad_effect_field", `${field} ${message}`, field, fix);
199
199
  const numberProblem = (one, where) => {
200
200
  if (typeof one !== "number" || !Number.isFinite(one)) return says(`${where}isn't a number.`, `Give a number from ${statement.minimumNumber} to ${statement.maximumNumber}.`);
@@ -203,37 +203,37 @@ function valueProblem(statement, value, field) {
203
203
  };
204
204
  const colorProblem = (one, where) => (typeof one === "string" && COLOR_PATTERN.test(one) ? null : says(`${where}isn't a colour.`, 'Give a colour as six hex digits in lowercase, like "#ff7a1a".'));
205
205
  const listProblem = (each) => {
206
- const count = `${statement.minimumCount === statement.maximumCount ? statement.minimumCount : `${statement.minimumCount} to ${statement.maximumCount}`}`;
207
- if (!Array.isArray(value) || value.length < statement.minimumCount || value.length > statement.maximumCount) return says(`isn't a list of ${count}.`, `Give a list of ${count} values.`);
208
- for (let index = 0; index < value.length; index += 1) {
209
- const found = each(value[index], `has an entry (${index}) that `);
206
+ const countText = `${statement.minimumCount === statement.maximumCount ? statement.minimumCount : `${statement.minimumCount} to ${statement.maximumCount}`}`;
207
+ if (!Array.isArray(fieldValue) || fieldValue.length < statement.minimumCount || fieldValue.length > statement.maximumCount) return says(`isn't a list of ${countText}.`, `Give a list of ${countText} values.`);
208
+ for (let index = 0; index < fieldValue.length; index += 1) {
209
+ const found = each(fieldValue[index], `has an entry (${index}) that `);
210
210
  if (found) return found;
211
211
  }
212
212
  return null;
213
213
  };
214
214
  switch (statement.valueKind) {
215
215
  case "number":
216
- return numberProblem(value, "");
216
+ return numberProblem(fieldValue, "");
217
217
  case "integer":
218
- return numberProblem(value, "") ?? (Number.isInteger(value) ? null : says("isn't a whole number.", "Give a whole number."));
218
+ return numberProblem(fieldValue, "") ?? (Number.isInteger(fieldValue) ? null : says("isn't a whole number.", "Give a whole number."));
219
219
  case "number_or_null":
220
- return value === null ? null : numberProblem(value, "");
220
+ return fieldValue === null ? null : numberProblem(fieldValue, "");
221
221
  case "boolean":
222
- return typeof value === "boolean" ? null : says("isn't true or false.", "Give true or false.");
222
+ return typeof fieldValue === "boolean" ? null : says("isn't true or false.", "Give true or false.");
223
223
  case "choice":
224
- return statement.choices.includes(value) ? null : says(`is ${JSON.stringify(value)}, which isn't one of its choices.`, `Give one of: ${statement.choices.join(", ")}.`);
224
+ return statement.choices.includes(fieldValue) ? null : says(`is ${JSON.stringify(fieldValue)}, which isn't one of its choices.`, `Give one of: ${statement.choices.join(", ")}.`);
225
225
  case "choice_or_null":
226
- return value === null || statement.choices.includes(value) ? null : says(`is ${JSON.stringify(value)}, which isn't one of its choices.`, `Give null, or one of: ${statement.choices.join(", ")}.`);
226
+ return fieldValue === null || statement.choices.includes(fieldValue) ? null : says(`is ${JSON.stringify(fieldValue)}, which isn't one of its choices.`, `Give null, or one of: ${statement.choices.join(", ")}.`);
227
227
  case "choices_or_null":
228
- if (value === null) return null;
229
- if (!Array.isArray(value) || value.length === 0 || value.some((one) => !statement.choices.includes(one))) return says("isn't null or a list of its choices.", `Give null, or a list of any of: ${statement.choices.join(", ")}.`);
228
+ if (fieldValue === null) return null;
229
+ if (!Array.isArray(fieldValue) || fieldValue.length === 0 || fieldValue.some((one) => !statement.choices.includes(one))) return says("isn't null or a list of its choices.", `Give null, or a list of any of: ${statement.choices.join(", ")}.`);
230
230
  return null;
231
231
  case "color":
232
- return colorProblem(value, "");
232
+ return colorProblem(fieldValue, "");
233
233
  case "text":
234
- return typeof value === "string" && value.length <= statement.maximumCharacterCount ? null : says(`isn't text of up to ${statement.maximumCharacterCount} characters.`, `Give text of up to ${statement.maximumCharacterCount} characters.`);
234
+ return typeof fieldValue === "string" && fieldValue.length <= statement.maximumCharacterCount ? null : says(`isn't text of up to ${statement.maximumCharacterCount} characters.`, `Give text of up to ${statement.maximumCharacterCount} characters.`);
235
235
  case "text_or_null":
236
- return value === null || (typeof value === "string" && value.length > 0 && value.length <= statement.maximumCharacterCount) ? null : says(`isn't null or text of up to ${statement.maximumCharacterCount} characters.`, `Give null, or text of up to ${statement.maximumCharacterCount} characters.`);
236
+ return fieldValue === null || (typeof fieldValue === "string" && fieldValue.length > 0 && fieldValue.length <= statement.maximumCharacterCount) ? null : says(`isn't null or text of up to ${statement.maximumCharacterCount} characters.`, `Give null, or text of up to ${statement.maximumCharacterCount} characters.`);
237
237
  case "numbers":
238
238
  return listProblem(numberProblem);
239
239
  case "colors":
@@ -244,27 +244,27 @@ function valueProblem(statement, value, field) {
244
244
  }
245
245
 
246
246
  const isGroup = (statement) => statement.valueKind === undefined;
247
- const isPlainObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
247
+ const isPlainObject = (candidate) => candidate !== null && typeof candidate === "object" && !Array.isArray(candidate);
248
248
 
249
249
  function checkFields(statements, given, path, problems) {
250
250
  if (!isPlainObject(given)) {
251
251
  problems.push(problem("bad_effect_field", `${path || "The effect"} isn't an object.`, path || null, "Give an object with its fields."));
252
252
  return;
253
253
  }
254
- const at = (name) => (path ? `${path}.${name}` : name);
254
+ const pathTo = (name) => (path ? `${path}.${name}` : name);
255
255
  for (const name of Object.keys(given)) {
256
- if (!(name in statements)) problems.push(problem("unknown_effect_field", `${at(name)} isn't a field.`, at(name), `Give only: ${Object.keys(statements).join(", ")}.`));
256
+ if (!(name in statements)) problems.push(problem("unknown_effect_field", `${pathTo(name)} isn't a field.`, pathTo(name), `Give only: ${Object.keys(statements).join(", ")}.`));
257
257
  }
258
258
  for (const [name, statement] of Object.entries(statements)) {
259
259
  if (isGroup(statement)) {
260
- if (given[name] !== undefined) checkFields(statement, given[name], at(name), problems);
260
+ if (given[name] !== undefined) checkFields(statement, given[name], pathTo(name), problems);
261
261
  continue;
262
262
  }
263
263
  if (given[name] === undefined) {
264
- if (statement.isRequired) problems.push(problem("missing_effect_field", `${at(name)} is missing.`, at(name), `Add ${name}: ${statement.summary}`));
264
+ if (statement.isRequired) problems.push(problem("missing_effect_field", `${pathTo(name)} is missing.`, pathTo(name), `Add ${name}: ${statement.summary}`));
265
265
  continue;
266
266
  }
267
- const found = valueProblem(statement, given[name], at(name));
267
+ const found = valueProblem(statement, given[name], pathTo(name));
268
268
  if (found) problems.push(found);
269
269
  }
270
270
  }
@@ -334,32 +334,32 @@ export function listElementFields(elementKind) {
334
334
  // ---- Numbers every side agrees on ----
335
335
 
336
336
  /** A whole-number hash of up to four whole numbers: the same on every device. */
337
- export function hashNumbers(first, second = 0, third = 0, fourth = 0) {
338
- let hash = (first | 0) ^ 0x9e3779b9;
339
- for (const one of [second, third, fourth]) {
340
- hash = Math.imul(hash ^ (one | 0), 0x85ebca6b);
341
- hash ^= hash >>> 13;
342
- hash = Math.imul(hash, 0xc2b2ae35);
343
- hash ^= hash >>> 16;
337
+ export function hashNumbers(first, secondNumber = 0, thirdNumber = 0, fourthNumber = 0) {
338
+ let hashNumber = (first | 0) ^ 0x9e3779b9;
339
+ for (const partNumber of [secondNumber, thirdNumber, fourthNumber]) {
340
+ hashNumber = Math.imul(hashNumber ^ (partNumber | 0), 0x85ebca6b);
341
+ hashNumber ^= hashNumber >>> 13;
342
+ hashNumber = Math.imul(hashNumber, 0xc2b2ae35);
343
+ hashNumber ^= hashNumber >>> 16;
344
344
  }
345
- return hash >>> 0;
345
+ return hashNumber >>> 0;
346
346
  }
347
347
 
348
348
  /** Random numbers from 0 up to 1 that follow from a seed: the same seed gives the same numbers on every device. */
349
349
  export function randomFrom(seedNumber) {
350
- let state = seedNumber >>> 0;
350
+ let stateNumber = seedNumber >>> 0;
351
351
  return function next() {
352
- state = (state + 0x6d2b79f5) >>> 0;
353
- let mixed = state;
354
- mixed = Math.imul(mixed ^ (mixed >>> 15), mixed | 1);
355
- mixed ^= mixed + Math.imul(mixed ^ (mixed >>> 7), mixed | 61);
356
- return ((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296;
352
+ stateNumber = (stateNumber + 0x6d2b79f5) >>> 0;
353
+ let mixedNumber = stateNumber;
354
+ mixedNumber = Math.imul(mixedNumber ^ (mixedNumber >>> 15), mixedNumber | 1);
355
+ mixedNumber ^= mixedNumber + Math.imul(mixedNumber ^ (mixedNumber >>> 7), mixedNumber | 61);
356
+ return ((mixedNumber ^ (mixedNumber >>> 14)) >>> 0) / 4294967296;
357
357
  };
358
358
  }
359
359
 
360
360
  /** A value read off 2 to 4 keys spread evenly over 0 to 1. */
361
361
  export function valueOverLife(keys, sharePct) {
362
- const place = Math.min(Math.max(sharePct / 100, 0), 1) * (keys.length - 1);
363
- const index = Math.min(Math.floor(place), keys.length - 2);
364
- return keys[index] + (keys[index + 1] - keys[index]) * (place - index);
362
+ const placeNumber = Math.min(Math.max(sharePct / 100, 0), 1) * (keys.length - 1);
363
+ const index = Math.min(Math.floor(placeNumber), keys.length - 2);
364
+ return keys[index] + (keys[index + 1] - keys[index]) * (placeNumber - index);
365
365
  }
package/index.d.ts CHANGED
@@ -100,9 +100,9 @@ export interface EffectsHooks {
100
100
  /** Asked before every effect plays. Answer any field changed (another effectName, a tint), null to stop it, or nothing to let it play as it is. */
101
101
  beforeEffect: (proposal: EffectProposal) => Partial<Omit<EffectProposal, "positionM" | "reason">> | null | undefined | void;
102
102
  /** Told when an effect starts: where a game plays the sound that goes with it. */
103
- effectStarted: (started: { playId: number; effectName: string; positionM: Point; scalePct: number; strengthPct: number; surfaceKind: SurfaceKind | null; reason: PlayReason }) => void;
103
+ effectStarted: (started: { playNumber: number; effectName: string; positionM: Point; scalePct: number; strengthPct: number; surfaceKind: SurfaceKind | null; reason: PlayReason }) => void;
104
104
  /** Told when an effect's last particle is gone, or it was stopped. */
105
- effectEnded: (ended: { playId: number; effectName: string }) => void;
105
+ effectEnded: (ended: { playNumber: number; effectName: string }) => void;
106
106
  /** Asked what the surface is where a free effect or an impact lands, starting from the piece's guess. Answer a surface kind from the game's own level. */
107
107
  surfaceKindAt: (surfaceKind: SurfaceKind, where: { positionM: Point; reason: PlayReason; bodyId: string | null }) => SurfaceKind | undefined | void;
108
108
  /** Told when an effect shakes the camera, already faded by its distance: for a game with a camera of its own. */
@@ -176,7 +176,7 @@ export interface PlayAt {
176
176
  /** One effect playing. */
177
177
  export interface EffectPlay {
178
178
  /** 0 when nothing played: beforeEffect stopped it, the budget was spent, or it was over before it began. */
179
- readonly playId: number;
179
+ readonly playNumber: number;
180
180
  readonly effectName: string | null;
181
181
  readonly isEnded: boolean;
182
182
  /** Stops it: nothing new is given off, and what's in the air finishes. isImmediate takes everything away at once. */
@@ -290,9 +290,9 @@ export interface Effects {
290
290
  /** Every effect loaded. */
291
291
  list(): { name: string; family: FamilyName; summary: string; importance: "low" | "normal" | "high"; isLooping: boolean }[];
292
292
  /** Plays an effect. A looping one plays until its stop(). Throws PieceError "unknown_effect", "bad_play", "unknown_argument". */
293
- play(effectName: string, at: PlayAt): EffectPlay;
293
+ play(effectName: string, playAt: PlayAt): EffectPlay;
294
294
  /** What the online kind calls for an effect another player's game or the game server played: as play, told with the reason shared. */
295
- playShared(effectName: string, at: PlayAt): EffectPlay;
295
+ playShared(effectName: string, playAt: PlayAt): EffectPlay;
296
296
  /** Stops everything playing. isImmediate takes what's on screen away at once. */
297
297
  stopAll(stopOptions?: { isImmediate?: boolean }): void;
298
298
  /** Takes every mark off the level: a level starting over. */