@zaisma/effects 0.0.0-stage → 1.0.1

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.
Files changed (123) hide show
  1. package/EFFECT-FILES.md +301 -0
  2. package/LICENSE.txt +93 -0
  3. package/README.md +63 -2
  4. package/batches/chunks.js +294 -0
  5. package/batches/marks.js +160 -0
  6. package/batches/ribbons.js +233 -0
  7. package/batches/shared.js +155 -0
  8. package/batches/sprites.js +447 -0
  9. package/batches/warps.js +222 -0
  10. package/budget.js +115 -0
  11. package/debris.js +173 -0
  12. package/emit.js +143 -0
  13. package/flashes.js +113 -0
  14. package/format.js +365 -0
  15. package/index.d.ts +373 -0
  16. package/index.js +1168 -0
  17. package/library/bubbles_rising.json +47 -0
  18. package/library/bullet_impact.json +588 -0
  19. package/library/campfire.json +235 -0
  20. package/library/candle_flame.json +100 -0
  21. package/library/confetti.json +165 -0
  22. package/library/dash_burst.json +73 -0
  23. package/library/debris_glass.json +96 -0
  24. package/library/debris_metal.json +106 -0
  25. package/library/debris_stone.json +106 -0
  26. package/library/debris_wood.json +101 -0
  27. package/library/dust_motes.json +51 -0
  28. package/library/dust_puff.json +49 -0
  29. package/library/electric_sparks.json +175 -0
  30. package/library/embers_drift.json +55 -0
  31. package/library/exhaust_puff.json +62 -0
  32. package/library/explosion_air.json +232 -0
  33. package/library/explosion_large.json +373 -0
  34. package/library/explosion_magic.json +266 -0
  35. package/library/explosion_medium.json +289 -0
  36. package/library/explosion_small.json +289 -0
  37. package/library/explosion_water.json +177 -0
  38. package/library/falling_leaves.json +54 -0
  39. package/library/fire_burst.json +185 -0
  40. package/library/fire_large.json +279 -0
  41. package/library/fireball.json +288 -0
  42. package/library/fireflies.json +51 -0
  43. package/library/firework_burst.json +127 -0
  44. package/library/footstep.json +270 -0
  45. package/library/fountain_spray.json +80 -0
  46. package/library/frost_burst.json +202 -0
  47. package/library/ground_mist.json +50 -0
  48. package/library/ground_slam.json +122 -0
  49. package/library/heat_haze.json +24 -0
  50. package/library/hit_blade.json +150 -0
  51. package/library/hit_block.json +157 -0
  52. package/library/hit_critical.json +293 -0
  53. package/library/hit_heavy.json +211 -0
  54. package/library/hit_light.json +122 -0
  55. package/library/hit_magic.json +114 -0
  56. package/library/impact.json +417 -0
  57. package/library/impact_heavy.json +279 -0
  58. package/library/index.json +464 -0
  59. package/library/jump_puff.json +46 -0
  60. package/library/landing_heavy.json +105 -0
  61. package/library/landing_puff.json +75 -0
  62. package/library/level_up.json +164 -0
  63. package/library/lightning_arc.json +90 -0
  64. package/library/lightning_strike.json +387 -0
  65. package/library/magic_aura.json +127 -0
  66. package/library/magic_bolt.json +107 -0
  67. package/library/magic_bolt_impact.json +181 -0
  68. package/library/magic_cast.json +115 -0
  69. package/library/magic_charge.json +91 -0
  70. package/library/magic_heal.json +123 -0
  71. package/library/magic_shield.json +89 -0
  72. package/library/magic_teleport.json +177 -0
  73. package/library/mark_bullet_hole.json +20 -0
  74. package/library/mark_crack.json +18 -0
  75. package/library/mark_footprint.json +20 -0
  76. package/library/mark_frost.json +18 -0
  77. package/library/mark_puddle.json +18 -0
  78. package/library/mark_scorch.json +18 -0
  79. package/library/mark_slash.json +18 -0
  80. package/library/mark_splat.json +18 -0
  81. package/library/pickup_collect.json +117 -0
  82. package/library/pickup_glow.json +76 -0
  83. package/library/poison_cloud.json +107 -0
  84. package/library/portal.json +328 -0
  85. package/library/shockwave_air.json +71 -0
  86. package/library/shockwave_ground.json +95 -0
  87. package/library/slide_dust.json +48 -0
  88. package/library/smoke_column.json +93 -0
  89. package/library/smoke_puff.json +48 -0
  90. package/library/smoke_screen.json +87 -0
  91. package/library/sparks_burst.json +85 -0
  92. package/library/sparks_fountain.json +93 -0
  93. package/library/sparks_shower.json +89 -0
  94. package/library/splash_large.json +181 -0
  95. package/library/splash_small.json +111 -0
  96. package/library/steam_vent.json +64 -0
  97. package/library/summon_circle.json +494 -0
  98. package/library/swim_splash.json +77 -0
  99. package/library/torch_flame.json +219 -0
  100. package/library/trail_blade.json +39 -0
  101. package/library/trail_blade_fire.json +90 -0
  102. package/library/trail_dash.json +80 -0
  103. package/library/trail_magic.json +94 -0
  104. package/library/trail_projectile.json +31 -0
  105. package/library/trail_smoke.json +125 -0
  106. package/library/warp_pulse.json +22 -0
  107. package/library/warp_ripple.json +23 -0
  108. package/library/water_ripple.json +69 -0
  109. package/library/waterfall_mist.json +95 -0
  110. package/looks.js +162 -0
  111. package/motion.js +145 -0
  112. package/online.d.ts +79 -0
  113. package/online.js +32 -0
  114. package/package.json +69 -4
  115. package/partners.js +255 -0
  116. package/piece.json +415 -0
  117. package/piece.json.js +416 -0
  118. package/server.d.ts +55 -0
  119. package/server.js +26 -0
  120. package/shapes.js +269 -0
  121. package/sync.js +528 -0
  122. package/tables.js +109 -0
  123. package/weather.js +289 -0
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@zaisma/effects",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.1",
4
+ "description": "Spectacle that holds a phone's frame rate, for three.js: thousands of lit particles, hits, explosions, magic, trails, screen warps, marks on surfaces, debris that bounces and weather, in the look a game picks. An effect is a small file an AI writes; 92 come with it. Flashes light the scene, the character and physics bring effects free, and online every player sees the same effect at once.",
5
+ "license": "PolyForm-Shield-1.0.0",
6
+ "homepage": "https://zaisma.com",
7
+ "bugs": "https://zaisma.com/forum",
8
+ "keywords": [
9
+ "zaisma",
10
+ "zaisma-piece",
11
+ "games",
12
+ "browser-games",
13
+ "three",
14
+ "particles",
15
+ "effects",
16
+ "vfx",
17
+ "weather",
18
+ "multiplayer"
19
+ ],
20
+ "type": "module",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./index.d.ts",
24
+ "default": "./index.js"
25
+ },
26
+ "./online.js": {
27
+ "types": "./online.d.ts",
28
+ "default": "./online.js"
29
+ },
30
+ "./server.js": {
31
+ "types": "./server.d.ts",
32
+ "default": "./server.js"
33
+ },
34
+ "./format.js": {
35
+ "types": "./index.d.ts",
36
+ "default": "./format.js"
37
+ },
38
+ "./piece.json": "./piece.json",
39
+ "./library/*.json": "./library/*.json"
40
+ },
41
+ "files": [
42
+ "EFFECT-FILES.md",
43
+ "LICENSE.txt",
44
+ "README.md",
45
+ "batches",
46
+ "budget.js",
47
+ "debris.js",
48
+ "emit.js",
49
+ "flashes.js",
50
+ "format.js",
51
+ "index.d.ts",
52
+ "index.js",
53
+ "library",
54
+ "looks.js",
55
+ "motion.js",
56
+ "online.d.ts",
57
+ "online.js",
58
+ "partners.js",
59
+ "piece.json",
60
+ "piece.json.js",
61
+ "server.d.ts",
62
+ "server.js",
63
+ "shapes.js",
64
+ "sync.js",
65
+ "tables.js",
66
+ "weather.js"
67
+ ],
68
+ "publishConfig": {
69
+ "access": "public"
70
+ }
71
+ }
package/partners.js ADDED
@@ -0,0 +1,255 @@
1
+ // The other pieces' hookups: the effects a game gets with no code of its own. A character's footfalls, jumps and
2
+ // landings (@zaisma/character's view), a physics body's knocks (@zaisma/physics), and the camera piece shaken by
3
+ // effects that shake. A game hands over what it has, createEffects({ character, physics, camera }), and each hookup
4
+ // reads only what that piece states, so an older or newer version of it still works: what it doesn't tell is left out.
5
+ //
6
+ // Every free effect goes through before_effect with its reason, where a game swaps or refuses it, and each kind has
7
+ // its setting to turn it off. Nothing here is sent online: every player's game sees the same footfall and the same
8
+ // knock, and plays its own.
9
+ import { SURFACE_KINDS } from "./format.js";
10
+
11
+ /** The library's effects the hookups play, loaded when a game gives a piece that needs them. */
12
+ const CHARACTER_EFFECT_NAMES = ["footstep", "jump_puff", "landing_puff", "landing_heavy", "splash_small", "splash_large"];
13
+ const PHYSICS_EFFECT_NAMES = ["impact", "impact_heavy"];
14
+
15
+ // Past these, a free effect is too far to see and isn't played.
16
+ const FOOTSTEP_REACH_M = 45;
17
+ const LANDING_REACH_M = 90;
18
+ const IMPACT_REACH_M = 110;
19
+ // A landing slower than this is a step down, never a puff; faster than the second, a heavy one.
20
+ const LANDING_SPEED_M_PER_SEC = 2.5;
21
+ const HEAVY_LANDING_SPEED_M_PER_SEC = 11;
22
+ const IMPACT_SPEED_M_PER_SEC = 1.5;
23
+ const HEAVY_IMPACT_SPEED_M_PER_SEC = 9;
24
+ // A pile settling knocks many times a second: one body's knocks are this far apart, and a frame plays this many.
25
+ const IMPACT_GAP_SEC = 0.18;
26
+ const MAXIMUM_IMPACTS_PER_FRAME = 4;
27
+
28
+ const clamp = (value, low, high) => Math.min(Math.max(value, low), high);
29
+ const isPoint = (value) => value !== null && typeof value === "object" && Number.isFinite(value.x) && Number.isFinite(value.y) && Number.isFinite(value.z);
30
+ const hookNamesOf = (piece) => new Set(typeof piece?.hooks?.list === "function" ? piece.hooks.list().map((hook) => hook.name) : []);
31
+
32
+ /**
33
+ * Connects what the game gave. Answers { update(nowSec, frameSec, camera), dispose() }.
34
+ * given: createEffects' own arguments; shakeNow: the piece's shake this frame ({ strengthPct, offsetM }).
35
+ */
36
+ export function connectPartners({ effects, given, settings, startPlay, findSurface, ask, hasHandlers, shakeNow }) {
37
+ const stops = [];
38
+ const cameraPlace = { x: 0, y: 0, z: 0, isKnown: false };
39
+ const surface = { x: 0, y: 0, z: 0, normalX: 0, normalY: 1, normalZ: 0, materialName: null };
40
+ let nowSec = 0;
41
+
42
+ const isWithin = (positionM, reachM) => {
43
+ if (!cameraPlace.isKnown) return true;
44
+ const x = positionM.x - cameraPlace.x;
45
+ const y = positionM.y - cameraPlace.y;
46
+ const z = positionM.z - cameraPlace.z;
47
+ return x * x + y * y + z * z < reachM * reachM;
48
+ };
49
+
50
+ /** What a place is made of: the level's own word when it's one of the piece's, else stone, then the game's say. */
51
+ function surfaceKindOf(materialName, positionM, reason, bodyId) {
52
+ const guess = SURFACE_KINDS.includes(materialName) ? materialName : "stone";
53
+ if (!hasHandlers("surfaceKindAt")) return guess;
54
+ const answer = ask("surfaceKindAt", guess, { positionM: { x: positionM.x, y: positionM.y, z: positionM.z }, reason, bodyId });
55
+ return SURFACE_KINDS.includes(answer) ? answer : guess;
56
+ }
57
+
58
+ /** Plays a free effect once its file is here; before that, nothing: the first footfalls of a game go without. */
59
+ function playFree(effectName, at, reason) {
60
+ if (!effects.has(effectName)) return;
61
+ startPlay(effectName, at, reason);
62
+ }
63
+
64
+ function loadFree(names) {
65
+ const missing = names.filter((name) => !effects.has(name));
66
+ if (missing.length === 0) return;
67
+ effects.load(missing).catch((error) => console.warn(`@zaisma/effects: the free effects (${missing.join(", ")}) couldn't be loaded, so they don't play.`, error));
68
+ }
69
+
70
+ // ---- The character ----
71
+ /** One footfall, jump or landing: { stepKind, positionM, speedMPerSec, fallSpeedMPerSec, materialName }. */
72
+ function onFootstep(step) {
73
+ if (!step || !isPoint(step.positionM)) return;
74
+ const { positionM } = step;
75
+ if (step.stepKind === "step") {
76
+ if (!settings.get("footstepEffectsEnabled") || !isWithin(positionM, FOOTSTEP_REACH_M)) return;
77
+ const speed = Number.isFinite(step.speedMPerSec) ? step.speedMPerSec : 3;
78
+ playFree("footstep", { positionM, scalePct: clamp(70 + speed * 9, 70, 150), strengthPct: clamp(45 + speed * 12, 50, 100), surfaceKind: surfaceKindOf(step.materialName ?? null, positionM, "footstep", null) }, "footstep");
79
+ return;
80
+ }
81
+ if (!settings.get("landingEffectsEnabled") || !isWithin(positionM, LANDING_REACH_M)) return;
82
+ if (step.stepKind === "jump") {
83
+ playFree("jump_puff", { positionM, surfaceKind: surfaceKindOf(step.materialName ?? null, positionM, "jump", null) }, "jump");
84
+ return;
85
+ }
86
+ if (step.stepKind !== "land") return;
87
+ const fallSpeed = Number.isFinite(step.fallSpeedMPerSec) ? step.fallSpeedMPerSec : 0;
88
+ if (fallSpeed < LANDING_SPEED_M_PER_SEC) return;
89
+ const surfaceKind = surfaceKindOf(step.materialName ?? null, positionM, "landing", null);
90
+ const isHeavy = fallSpeed >= HEAVY_LANDING_SPEED_M_PER_SEC;
91
+ if (surfaceKind === "water") playFree(isHeavy ? "splash_large" : "splash_small", { positionM, scalePct: clamp(60 + fallSpeed * 8, 80, 200), surfaceKind }, "landing");
92
+ else if (isHeavy) playFree("landing_heavy", { positionM, scalePct: clamp(40 + fallSpeed * 5, 90, 190), surfaceKind }, "landing");
93
+ else playFree("landing_puff", { positionM, scalePct: clamp(50 + fallSpeed * 9, 70, 150), surfaceKind }, "landing");
94
+ }
95
+
96
+ // A character view that doesn't tell its footfalls yet: the models it made are watched instead, and a footfall is
97
+ // worked out from how each one moves. Rougher than the view's own (it can't see the feet), and the same effects.
98
+ /** characterId -> { object, x, y, z, hasPlace, fallSpeed, groundSec, strideM, isLeftNext } */
99
+ const watched = new Map();
100
+ const step = { stepKind: "step", positionM: { x: 0, y: 0, z: 0 }, speedMPerSec: 0, fallSpeedMPerSec: 0, materialName: null };
101
+ function tellWatched(stepKind, x, y, z, speed, fallSpeed) {
102
+ // The ground under it, and what it's made of.
103
+ if (!findSurface(x, y + 0.4, z, 0, -1, 0, 1.2, surface)) return;
104
+ step.stepKind = stepKind;
105
+ step.positionM.x = surface.x;
106
+ step.positionM.y = surface.y;
107
+ step.positionM.z = surface.z;
108
+ step.speedMPerSec = speed;
109
+ step.fallSpeedMPerSec = fallSpeed;
110
+ step.materialName = surface.materialName;
111
+ onFootstep(step);
112
+ }
113
+ function watchModels(frameSec) {
114
+ for (const [characterId, seen] of watched) {
115
+ const { object } = seen;
116
+ if (!object.parent) {
117
+ watched.delete(characterId);
118
+ continue;
119
+ }
120
+ const { x, y, z } = object.position;
121
+ const acrossM = Math.hypot(x - seen.x, z - seen.z);
122
+ const riseSpeed = (y - seen.y) / frameSec;
123
+ // A first frame, or a jump of metres in one (a respawn): start again from here.
124
+ if (!seen.hasPlace || acrossM > 3 || Math.abs(y - seen.y) > 3) {
125
+ seen.hasPlace = true;
126
+ seen.fallSpeed = 0;
127
+ seen.groundSec = 0;
128
+ seen.strideM = 0;
129
+ } else if (riseSpeed < -3.5) {
130
+ seen.fallSpeed = Math.max(seen.fallSpeed, -riseSpeed);
131
+ seen.groundSec = 0;
132
+ } else if (riseSpeed > 3.5) {
133
+ if (seen.groundSec > 0.1) tellWatched("jump", seen.x, seen.y, seen.z, 0, 0);
134
+ seen.groundSec = 0;
135
+ } else if (Math.abs(riseSpeed) < 3) {
136
+ if (seen.fallSpeed > 0) tellWatched("land", x, y, z, 0, seen.fallSpeed);
137
+ seen.fallSpeed = 0;
138
+ seen.groundSec += frameSec;
139
+ const speed = acrossM / frameSec;
140
+ if (speed > 0.6) {
141
+ seen.strideM += acrossM;
142
+ // A stride lengthens with speed: a walk's is short, a sprint's long.
143
+ if (seen.strideM >= clamp(0.5 + speed * 0.13, 0.55, 1.5)) {
144
+ seen.strideM = 0;
145
+ // The foot lands a little to one side of the way it's going.
146
+ const side = (seen.isLeftNext ? 0.11 : -0.11) / Math.max(acrossM, 1e-6);
147
+ seen.isLeftNext = !seen.isLeftNext;
148
+ tellWatched("step", x - (z - seen.z) * side, y, z + (x - seen.x) * side, speed, 0);
149
+ }
150
+ } else seen.strideM = 0;
151
+ }
152
+ seen.x = x;
153
+ seen.y = y;
154
+ seen.z = z;
155
+ }
156
+ }
157
+
158
+ const { character } = given;
159
+ if (character) {
160
+ const names = hookNamesOf(character);
161
+ if (names.has("footstep")) stops.push(character.hooks.on("footstep", onFootstep));
162
+ else if (names.has("modelMade")) {
163
+ const watch = (characterId, object) => {
164
+ if (object?.isObject3D && typeof characterId === "string") watched.set(characterId, { object, x: 0, y: 0, z: 0, hasPlace: false, fallSpeed: 0, groundSec: 0, strideM: 0, isLeftNext: true });
165
+ };
166
+ stops.push(character.hooks.on("modelMade", (made) => watch(made?.characterId, made?.model?.object)));
167
+ // Made before the effects were: the player's own character, at least, is watched from now.
168
+ if (typeof character.modelOf === "function" && typeof character.ownCharacterId === "string") watch(character.ownCharacterId, character.modelOf(character.ownCharacterId)?.object);
169
+ }
170
+ if (names.has("footstep") || names.has("modelMade")) loadFree(CHARACTER_EFFECT_NAMES);
171
+ }
172
+
173
+ // ---- Physics ----
174
+ /** bodyId -> when its last knock played. */
175
+ const knockedSec = new Map();
176
+ let impactsThisFrame = 0;
177
+ const { physics } = given;
178
+ if (physics && hookNamesOf(physics).has("bodyHit")) {
179
+ stops.push(
180
+ physics.hooks.on("bodyHit", (hit) => {
181
+ if (!hit || !settings.get("impactEffectsEnabled") || impactsThisFrame >= MAXIMUM_IMPACTS_PER_FRAME) return;
182
+ const speed = hit.speedMPerSec;
183
+ if (!(speed >= IMPACT_SPEED_M_PER_SEC)) return;
184
+ const bodyId = hit.bodyId ?? hit.otherBodyId ?? null;
185
+ if (bodyId !== null && nowSec - (knockedSec.get(bodyId) ?? -Infinity) < IMPACT_GAP_SEC) return;
186
+ const at = {};
187
+ if (isPoint(hit.positionM)) at.positionM = hit.positionM;
188
+ else {
189
+ // A physics that tells no place: the body's own, dropped to what's under it.
190
+ const middle = bodyId !== null && typeof physics.bodyState === "function" ? physics.bodyState(bodyId)?.positionM : null;
191
+ if (!isPoint(middle)) return;
192
+ if (findSurface(middle.x, middle.y, middle.z, 0, -1, 0, 2.5, surface)) {
193
+ at.positionM = { x: surface.x, y: surface.y, z: surface.z };
194
+ at.normal = { x: surface.normalX, y: surface.normalY, z: surface.normalZ };
195
+ } else at.positionM = { x: middle.x, y: middle.y, z: middle.z };
196
+ }
197
+ if (!isWithin(at.positionM, IMPACT_REACH_M)) return;
198
+ // Heavy things land hard at a lower speed: a crate at 9 m/s, a car at 4.
199
+ const massKg = Number.isFinite(hit.massKg) ? hit.massKg : 30;
200
+ const force = speed * clamp(Math.cbrt(massKg / 30), 0.6, 2.5);
201
+ const isHeavy = force >= HEAVY_IMPACT_SPEED_M_PER_SEC;
202
+ at.scalePct = isHeavy ? clamp(50 + force * 6, 90, 240) : clamp(35 + force * 12, 50, 150);
203
+ // What it landed on says what flies up; a physics that tells neither is stone until the game says.
204
+ at.surfaceKind = surfaceKindOf(hit.otherMaterialName ?? hit.materialName ?? surface.materialName ?? null, at.positionM, "body_hit", bodyId);
205
+ if (bodyId !== null) knockedSec.set(bodyId, nowSec);
206
+ impactsThisFrame += 1;
207
+ playFree(isHeavy ? "impact_heavy" : "impact", at, "body_hit");
208
+ }),
209
+ );
210
+ loadFree(PHYSICS_EFFECT_NAMES);
211
+ }
212
+
213
+ // ---- The camera piece ----
214
+ // Its before_place decision takes the shake: the place it was about to be, moved by this frame's shake.
215
+ const cameraPiece = given.camera ?? character?.camera ?? null;
216
+ if (cameraPiece && hookNamesOf(cameraPiece).has("beforePlace")) {
217
+ stops.push(
218
+ cameraPiece.hooks.on("beforePlace", (place) => {
219
+ if (shakeNow.strengthPct <= 0 || !place || !isPoint(place.positionM) || !isPoint(place.lookingAtM)) return undefined;
220
+ const { offsetM } = shakeNow;
221
+ // What it looks at moves less than where it stands, so the picture turns a little as well as shifting.
222
+ return {
223
+ ...place,
224
+ positionM: { x: place.positionM.x + offsetM.x, y: place.positionM.y + offsetM.y, z: place.positionM.z + offsetM.z },
225
+ lookingAtM: { x: place.lookingAtM.x + offsetM.x * 0.4, y: place.lookingAtM.y + offsetM.y * 0.4, z: place.lookingAtM.z + offsetM.z * 0.4 },
226
+ };
227
+ }),
228
+ );
229
+ }
230
+
231
+ return {
232
+ /** Every frame, before the effects are drawn. camera: the three.js camera the game draws with. */
233
+ update(sec, frameSec, camera) {
234
+ nowSec = sec;
235
+ impactsThisFrame = 0;
236
+ if (camera?.isCamera) {
237
+ const place = camera.matrixWorld.elements;
238
+ cameraPlace.x = place[12];
239
+ cameraPlace.y = place[13];
240
+ cameraPlace.z = place[14];
241
+ cameraPlace.isKnown = true;
242
+ }
243
+ if (watched.size > 0 && frameSec > 1e-4) watchModels(frameSec);
244
+ if (knockedSec.size > 256) {
245
+ for (const [bodyId, knockSec] of knockedSec) if (sec - knockSec > 2) knockedSec.delete(bodyId);
246
+ }
247
+ },
248
+ dispose() {
249
+ for (const stop of stops) stop();
250
+ stops.length = 0;
251
+ watched.clear();
252
+ knockedSec.clear();
253
+ },
254
+ };
255
+ }