@zaisma/effects 1.0.0 → 1.1.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/batches/chunks.js +45 -45
- package/batches/marks.js +15 -15
- package/batches/ribbons.js +33 -33
- package/batches/shared.js +1 -1
- package/batches/sprites.js +47 -47
- package/batches/warps.js +18 -18
- package/budget.js +17 -17
- package/debris.js +33 -33
- package/emit.js +35 -35
- package/flashes.js +7 -7
- package/format.js +39 -39
- package/index.d.ts +5 -5
- package/index.js +75 -73
- package/looks.js +3 -3
- package/motion.js +23 -23
- package/package.json +1 -1
- package/partners.js +30 -30
- package/piece.json +3 -3
- package/piece.json.js +3 -3
- package/shapes.js +5 -5
- package/sync.js +113 -113
- package/tables.js +20 -20
- package/weather.js +17 -17
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
|
|
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
|
|
26
|
-
const
|
|
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
|
|
39
|
+
let keptShareNumber = 1;
|
|
40
40
|
if (!isNeverThinned) {
|
|
41
|
-
|
|
42
|
-
if (distanceM > FULL_COUNT_DISTANCE_M)
|
|
43
|
-
|
|
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 *
|
|
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)
|
|
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
|
|
61
|
+
let fillShareNumber = 1;
|
|
62
62
|
return {
|
|
63
63
|
get fillShare() {
|
|
64
|
-
return
|
|
64
|
+
return fillShareNumber;
|
|
65
65
|
},
|
|
66
66
|
/** Answers the share now. */
|
|
67
|
-
|
|
67
|
+
keptShareOf(coveredScreenCount, qualityLevel) {
|
|
68
68
|
const limit = QUALITY_BUDGETS[qualityLevel].fillScreenCount;
|
|
69
|
-
if (coveredScreenCount > limit)
|
|
70
|
-
else if (coveredScreenCount < limit * 0.7)
|
|
71
|
-
return
|
|
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
|
-
|
|
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
|
|
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
|
|
26
|
+
let capacityCount = 0;
|
|
27
27
|
/** Every place in the slab, a chunk or null. */
|
|
28
28
|
let held = [];
|
|
29
|
-
let
|
|
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
|
-
|
|
38
|
-
slab =
|
|
39
|
-
if (slab)
|
|
40
|
-
else
|
|
41
|
-
held = new Array(
|
|
42
|
-
|
|
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 (
|
|
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
|
|
59
|
-
|
|
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[
|
|
92
|
-
held[
|
|
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
|
|
119
|
-
if (
|
|
120
|
-
const alongX = chunk.velocityX -
|
|
121
|
-
const alongY = chunk.velocityY -
|
|
122
|
-
const alongZ = chunk.velocityZ -
|
|
123
|
-
const
|
|
124
|
-
chunk.velocityX = alongX *
|
|
125
|
-
chunk.velocityY = alongY *
|
|
126
|
-
chunk.velocityZ = alongZ *
|
|
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 (
|
|
141
|
-
leftoverSec = Math.min(leftoverSec + frameSec, STEP_SEC *
|
|
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
|
|
146
|
-
const chunk = held[
|
|
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[
|
|
149
|
+
held[slotIndex] = null;
|
|
150
150
|
chunk.play.debrisCount -= 1;
|
|
151
|
-
batch.rewrite(slab,
|
|
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
|
|
157
|
-
batch.rewrite(slab,
|
|
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
|
|
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
|
|
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
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
into.x = Math.cos(
|
|
52
|
-
into.y =
|
|
53
|
-
into.z = Math.sin(
|
|
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
|
|
74
|
-
offset.x *=
|
|
75
|
-
offset.y *=
|
|
76
|
-
offset.z *=
|
|
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
|
|
80
|
-
const
|
|
81
|
-
offset.x = Math.cos(
|
|
82
|
-
offset.z = Math.sin(
|
|
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
|
|
102
|
-
const
|
|
103
|
-
way.x = (offset.x /
|
|
104
|
-
way.y = (offset.y /
|
|
105
|
-
way.z = (offset.z /
|
|
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
|
|
110
|
-
const
|
|
111
|
-
const
|
|
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
|
|
118
|
-
sideX /=
|
|
119
|
-
sideZ /=
|
|
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
|
|
124
|
-
const
|
|
125
|
-
const x = way.x *
|
|
126
|
-
const y = way.y *
|
|
127
|
-
const z = way.z *
|
|
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
|
|
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 *
|
|
138
|
-
into.velocityY = way.y *
|
|
139
|
-
into.velocityZ = way.z *
|
|
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
|
|
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)
|
|
53
|
-
return
|
|
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
|
|
65
|
-
if (
|
|
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 =
|
|
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 = (
|
|
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,
|
|
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
|
|
207
|
-
if (!Array.isArray(
|
|
208
|
-
for (let index = 0; index <
|
|
209
|
-
const found = each(
|
|
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(
|
|
216
|
+
return numberProblem(fieldValue, "");
|
|
217
217
|
case "integer":
|
|
218
|
-
return numberProblem(
|
|
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
|
|
220
|
+
return fieldValue === null ? null : numberProblem(fieldValue, "");
|
|
221
221
|
case "boolean":
|
|
222
|
-
return typeof
|
|
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(
|
|
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
|
|
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 (
|
|
229
|
-
if (!Array.isArray(
|
|
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(
|
|
232
|
+
return colorProblem(fieldValue, "");
|
|
233
233
|
case "text":
|
|
234
|
-
return typeof
|
|
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
|
|
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 = (
|
|
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
|
|
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", `${
|
|
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],
|
|
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", `${
|
|
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],
|
|
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,
|
|
338
|
-
let
|
|
339
|
-
for (const
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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
|
|
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
|
|
350
|
+
let stateNumber = seedNumber >>> 0;
|
|
351
351
|
return function next() {
|
|
352
|
-
|
|
353
|
-
let
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return ((
|
|
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
|
|
363
|
-
const index = Math.min(Math.floor(
|
|
364
|
-
return keys[index] + (keys[index + 1] - keys[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: {
|
|
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: {
|
|
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
|
|
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,
|
|
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,
|
|
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. */
|