@zaisma/effects 0.0.0-stage → 1.0.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.
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
@@ -0,0 +1,23 @@
1
+ {
2
+ "effect_format_version_number": 1,
3
+ "name": "warp_ripple",
4
+ "summary": "A ring that bends the picture as it spreads, with nothing else drawn.",
5
+ "family": "warps",
6
+ "duration_sec": 0.6,
7
+ "elements": [
8
+ {
9
+ "element_kind": "warp",
10
+ "warp_kind": "ripple",
11
+ "life_sec": 0.55,
12
+ "radius_over_life_m": [
13
+ 0.3,
14
+ 7
15
+ ],
16
+ "strength_over_life_pct": [
17
+ 100,
18
+ 0
19
+ ],
20
+ "thickness_pct": 28
21
+ }
22
+ ]
23
+ }
@@ -0,0 +1,69 @@
1
+ {
2
+ "effect_format_version_number": 1,
3
+ "name": "water_ripple",
4
+ "summary": "Rings spreading on still water, nothing else: a drop, a step, a fish.",
5
+ "family": "water",
6
+ "importance": "low",
7
+ "duration_sec": 1.3,
8
+ "elements": [
9
+ {
10
+ "element_kind": "particles",
11
+ "material": "water",
12
+ "emit": {
13
+ "burst_count": 1
14
+ },
15
+ "move": {
16
+ "speed_m_per_sec": 0
17
+ },
18
+ "fade": {
19
+ "life_sec": 1.1,
20
+ "life_variation_pct": 0,
21
+ "size_over_life_m": [
22
+ 0.1,
23
+ 1.5
24
+ ],
25
+ "size_variation_pct": 0,
26
+ "colors_over_life": [
27
+ "#eef6ff"
28
+ ],
29
+ "opacity_over_life_pct": [
30
+ 70,
31
+ 35,
32
+ 0
33
+ ],
34
+ "facing_kind": "ground"
35
+ },
36
+ "shape": "ring"
37
+ },
38
+ {
39
+ "element_kind": "particles",
40
+ "material": "water",
41
+ "emit": {
42
+ "burst_count": 1
43
+ },
44
+ "move": {
45
+ "speed_m_per_sec": 0
46
+ },
47
+ "fade": {
48
+ "life_sec": 1,
49
+ "life_variation_pct": 0,
50
+ "size_over_life_m": [
51
+ 0.1,
52
+ 1.1
53
+ ],
54
+ "size_variation_pct": 0,
55
+ "colors_over_life": [
56
+ "#e0f0ff"
57
+ ],
58
+ "opacity_over_life_pct": [
59
+ 70,
60
+ 35,
61
+ 0
62
+ ],
63
+ "facing_kind": "ground"
64
+ },
65
+ "shape": "ring",
66
+ "start_delay_sec": 0.25
67
+ }
68
+ ]
69
+ }
@@ -0,0 +1,95 @@
1
+ {
2
+ "effect_format_version_number": 1,
3
+ "name": "waterfall_mist",
4
+ "summary": "The foot of a waterfall: rolling white mist and drops kicked up.",
5
+ "family": "water",
6
+ "is_looping": true,
7
+ "duration_sec": 1,
8
+ "elements": [
9
+ {
10
+ "element_kind": "particles",
11
+ "material": "mist",
12
+ "emit": {
13
+ "per_sec": 10,
14
+ "from_kind": "box",
15
+ "from_size_m": [
16
+ 3,
17
+ 0.3,
18
+ 1
19
+ ]
20
+ },
21
+ "move": {
22
+ "direction_kind": "up",
23
+ "spread_deg": 70,
24
+ "speed_m_per_sec": 1.6,
25
+ "speed_variation_pct": 50,
26
+ "drag_per_sec": 1,
27
+ "wind_pct": 100,
28
+ "turbulence_m": 0.5,
29
+ "turbulence_per_sec": 0.5
30
+ },
31
+ "fade": {
32
+ "life_sec": 2.6,
33
+ "life_variation_pct": 30,
34
+ "size_over_life_m": [
35
+ 0.8,
36
+ 2.2,
37
+ 3
38
+ ],
39
+ "size_variation_pct": 30,
40
+ "colors_over_life": [
41
+ "#eef2f6",
42
+ "#ffffff"
43
+ ],
44
+ "opacity_over_life_pct": [
45
+ 0,
46
+ 42,
47
+ 20,
48
+ 0
49
+ ],
50
+ "spin_deg_per_sec": 15
51
+ }
52
+ },
53
+ {
54
+ "element_kind": "particles",
55
+ "material": "water",
56
+ "emit": {
57
+ "burst_count": 0,
58
+ "per_sec": 60,
59
+ "from_kind": "box",
60
+ "from_size_m": [
61
+ 3,
62
+ 0.1,
63
+ 0.8
64
+ ]
65
+ },
66
+ "move": {
67
+ "direction_kind": "up",
68
+ "spread_deg": 55,
69
+ "speed_m_per_sec": 3.2,
70
+ "speed_variation_pct": 55,
71
+ "gravity_pct": 100
72
+ },
73
+ "fade": {
74
+ "life_sec": 0.7,
75
+ "life_variation_pct": 35,
76
+ "size_over_life_m": [
77
+ 0.05,
78
+ 0.035
79
+ ],
80
+ "size_variation_pct": 45,
81
+ "colors_over_life": [
82
+ "#e4f2ff",
83
+ "#b0d4f0"
84
+ ],
85
+ "opacity_over_life_pct": [
86
+ 90,
87
+ 90,
88
+ 0
89
+ ],
90
+ "facing_kind": "velocity",
91
+ "streak_duration_sec": 0.04
92
+ }
93
+ }
94
+ ]
95
+ }
package/looks.js ADDED
@@ -0,0 +1,162 @@
1
+ // A look: how each material is drawn. An effect file says what happens (smoke rises, sparks fly); the look in use
2
+ // says what smoke and sparks look like. Four come with the piece, and a game defines its own from any of them.
3
+ // No engine here.
4
+ import { MATERIAL_NAMES, SHAPE_NAMES } from "./format.js";
5
+
6
+ /** The low-poly shapes a material can be drawn as, in place of a flat sprite. */
7
+ export const CHUNK_SHAPE_NAMES = ["tetra", "octa", "icosa", "cube", "shard", "plate"];
8
+ /** The moving pictures a material can be drawn with: sixteen frames played over a particle's life. */
9
+ export const FILM_NAMES = ["smoke_film", "fire_film"];
10
+
11
+ /**
12
+ * What a look says of one material:
13
+ * - shapeName: the sprite's shape. filmName: a moving picture in its place, or null.
14
+ * - chunkShapeName: drawn as a low-poly chunk of this shape instead of a sprite, or null.
15
+ * - addPct: 0 covers what's behind it (smoke), 100 adds its light to it (fire, sparks).
16
+ * - litPct: how much the scene's light colours it; the rest shows its own colour whatever the light.
17
+ * - roundPct: how much it's shaded as a ball, bright toward the sun.
18
+ * - hardPct: 0 is a soft edge that fades out, 100 a crisp cut-out edge.
19
+ * - toneStepCount: 0 shades smoothly; 2 to 4 shades in that many flat tones.
20
+ * - outlinePct: a dark line round its edge.
21
+ * - lightenPct: how far its colours lift toward white, for a look that draws see-through things solid.
22
+ * - corePct: how much the shape's core lightens toward white: a flame's hot middle.
23
+ * - countPct, sizePct, glowPct, opacityPct: against what the effect file says.
24
+ */
25
+ export const LOOK_STYLE_FIELDS = {
26
+ shapeName: { valueKind: "choice", choices: SHAPE_NAMES },
27
+ filmName: { valueKind: "choice_or_null", choices: FILM_NAMES },
28
+ chunkShapeName: { valueKind: "choice_or_null", choices: CHUNK_SHAPE_NAMES },
29
+ addPct: { valueKind: "number", minimumNumber: 0, maximumNumber: 100 },
30
+ litPct: { valueKind: "number", minimumNumber: 0, maximumNumber: 100 },
31
+ roundPct: { valueKind: "number", minimumNumber: 0, maximumNumber: 100 },
32
+ hardPct: { valueKind: "number", minimumNumber: 0, maximumNumber: 100 },
33
+ toneStepCount: { valueKind: "integer", minimumNumber: 0, maximumNumber: 4 },
34
+ outlinePct: { valueKind: "number", minimumNumber: 0, maximumNumber: 100 },
35
+ corePct: { valueKind: "number", minimumNumber: 0, maximumNumber: 100 },
36
+ lightenPct: { valueKind: "number", minimumNumber: 0, maximumNumber: 100 },
37
+ countPct: { valueKind: "number", minimumNumber: 5, maximumNumber: 400 },
38
+ sizePct: { valueKind: "number", minimumNumber: 10, maximumNumber: 1000 },
39
+ glowPct: { valueKind: "number", minimumNumber: 0, maximumNumber: 1000 },
40
+ opacityPct: { valueKind: "number", minimumNumber: 0, maximumNumber: 400 },
41
+ };
42
+
43
+ const style = (shapeName, fields = {}) => ({ shapeName, filmName: null, chunkShapeName: null, addPct: 0, litPct: 0, roundPct: 0, hardPct: 0, toneStepCount: 0, outlinePct: 0, corePct: 0, lightenPct: 0, countPct: 100, sizePct: 100, glowPct: 100, opacityPct: 100, ...fields });
44
+
45
+ // Bright cartoon: few big shapes with crisp edges and flat tones, like drawn effects. Nothing is see-through mush.
46
+ const cartoon = {
47
+ fire: style("flame", { hardPct: 100, toneStepCount: 3, corePct: 35, countPct: 60, sizePct: 125, glowPct: 70, opacityPct: 140 }),
48
+ smoke: style("puff", { hardPct: 100, toneStepCount: 2, litPct: 35, lightenPct: 22, countPct: 45, sizePct: 130, opacityPct: 200 }),
49
+ spark: style("diamond", { hardPct: 100, addPct: 100, corePct: 60, countPct: 60, sizePct: 150, glowPct: 40 }),
50
+ ember: style("disc", { hardPct: 100, addPct: 100, countPct: 60, sizePct: 130, glowPct: 40 }),
51
+ dust: style("puff", { hardPct: 100, toneStepCount: 2, litPct: 45, lightenPct: 10, countPct: 50, sizePct: 135, opacityPct: 200 }),
52
+ water: style("drop", { hardPct: 100, toneStepCount: 2, litPct: 30, corePct: 60, countPct: 60, sizePct: 140, opacityPct: 150 }),
53
+ mist: style("soft", { litPct: 40, countPct: 70, opacityPct: 90 }),
54
+ magic: style("star", { hardPct: 100, addPct: 100, corePct: 70, countPct: 65, sizePct: 140, glowPct: 45 }),
55
+ glow: style("soft", { addPct: 80, glowPct: 60 }),
56
+ energy: style("streak", { hardPct: 100, addPct: 100, corePct: 70, countPct: 70, sizePct: 130, glowPct: 45 }),
57
+ snow: style("disc", { hardPct: 100, litPct: 40, sizePct: 120 }),
58
+ leaf: style("leaf", { hardPct: 100, toneStepCount: 2, litPct: 50, sizePct: 120 }),
59
+ stone: style("shard", { hardPct: 100, toneStepCount: 2, litPct: 60, chunkShapeName: "cube" }),
60
+ wood: style("shard", { hardPct: 100, toneStepCount: 2, litPct: 60, chunkShapeName: "shard" }),
61
+ metal: style("shard", { hardPct: 100, toneStepCount: 2, litPct: 60, chunkShapeName: "plate" }),
62
+ glass: style("shard", { hardPct: 100, addPct: 40, litPct: 40, chunkShapeName: "plate" }),
63
+ };
64
+
65
+ // Soft and glowing: everything is light. Soft edges, bright cores that bloom, smoke as a faint veil.
66
+ const glowing = {
67
+ fire: style("flame", { addPct: 70, corePct: 45, glowPct: 88, sizePct: 120, opacityPct: 85 }),
68
+ smoke: style("soft", { addPct: 25, litPct: 20, lightenPct: 12, opacityPct: 75, sizePct: 120 }),
69
+ spark: style("streak", { addPct: 100, glowPct: 150 }),
70
+ ember: style("soft", { addPct: 100, glowPct: 150, sizePct: 120 }),
71
+ dust: style("soft", { addPct: 15, litPct: 40, opacityPct: 65 }),
72
+ water: style("soft", { addPct: 70, litPct: 20, glowPct: 120, opacityPct: 80 }),
73
+ mist: style("soft", { addPct: 20, litPct: 40, opacityPct: 80 }),
74
+ magic: style("star", { addPct: 100, corePct: 40, glowPct: 150, sizePct: 110 }),
75
+ glow: style("soft", { addPct: 80, glowPct: 120 }),
76
+ energy: style("streak", { addPct: 100, corePct: 40, glowPct: 160 }),
77
+ snow: style("soft", { addPct: 40, litPct: 50, glowPct: 120 }),
78
+ leaf: style("leaf", { litPct: 60, hardPct: 40 }),
79
+ stone: style("shard", { litPct: 80, hardPct: 60, chunkShapeName: "cube" }),
80
+ wood: style("shard", { litPct: 80, hardPct: 60, chunkShapeName: "shard" }),
81
+ metal: style("shard", { litPct: 70, hardPct: 60, chunkShapeName: "plate" }),
82
+ glass: style("shard", { addPct: 60, litPct: 40, hardPct: 60, glowPct: 140, chunkShapeName: "plate" }),
83
+ };
84
+
85
+ // Faceted low-poly: particles are little solids with flat faces that catch the light, never see-through. What must
86
+ // stay flat (a ring spreading on the ground, a shape an effect names) is drawn crisp, in flat tones.
87
+ const solid = (shapeName, chunkShapeName, fields = {}) => style(shapeName, { chunkShapeName, hardPct: 100, toneStepCount: 2, ...fields });
88
+ const faceted = {
89
+ fire: style("angular_flame", { hardPct: 100, addPct: 30, corePct: 45, glowPct: 95, countPct: 70, sizePct: 95 }),
90
+ smoke: solid("puff", "icosa", { litPct: 100, lightenPct: 34, countPct: 60, sizePct: 55 }),
91
+ spark: solid("diamond", "shard", { addPct: 100, glowPct: 60, countPct: 70, sizePct: 70 }),
92
+ ember: solid("disc", "tetra", { addPct: 100, glowPct: 60, countPct: 70, sizePct: 70 }),
93
+ dust: solid("puff", "icosa", { litPct: 100, lightenPct: 15, countPct: 45, sizePct: 70 }),
94
+ water: solid("drop", "octa", { litPct: 85, countPct: 60, sizePct: 70 }),
95
+ mist: style("soft", { litPct: 50, opacityPct: 80 }),
96
+ magic: solid("diamond", "octa", { addPct: 100, glowPct: 65, countPct: 65, sizePct: 70 }),
97
+ glow: style("soft", { addPct: 80, glowPct: 90 }),
98
+ energy: solid("streak", "shard", { addPct: 100, glowPct: 70, countPct: 70, sizePct: 70 }),
99
+ snow: solid("diamond", "octa", { litPct: 90, sizePct: 70 }),
100
+ leaf: solid("leaf", "plate", { litPct: 100, sizePct: 90 }),
101
+ stone: solid("shard", "cube", { litPct: 100 }),
102
+ wood: solid("shard", "shard", { litPct: 100 }),
103
+ metal: solid("shard", "plate", { litPct: 100 }),
104
+ glass: solid("shard", "plate", { litPct: 70, glowPct: 130 }),
105
+ };
106
+
107
+ // Realistic: textured, lit by the scene, soft where it thins, with more and smaller particles.
108
+ const realistic = {
109
+ fire: style("flame", { filmName: "fire_film", addPct: 60, glowPct: 115, countPct: 120, sizePct: 150, opacityPct: 75 }),
110
+ smoke: style("puff", { filmName: "smoke_film", litPct: 100, roundPct: 70, lightenPct: 14, countPct: 120, opacityPct: 85 }),
111
+ spark: style("streak", { addPct: 100, glowPct: 150, countPct: 140, sizePct: 70 }),
112
+ ember: style("soft", { addPct: 100, glowPct: 140, countPct: 130, sizePct: 70 }),
113
+ dust: style("puff", { filmName: "smoke_film", litPct: 100, roundPct: 50, countPct: 120, opacityPct: 70 }),
114
+ water: style("drop", { litPct: 80, roundPct: 60, addPct: 25, countPct: 140, sizePct: 80, opacityPct: 85 }),
115
+ mist: style("soft", { litPct: 100, roundPct: 30, opacityPct: 70 }),
116
+ magic: style("wisp", { addPct: 100, corePct: 30, glowPct: 130, countPct: 120 }),
117
+ glow: style("soft", { addPct: 80, glowPct: 110 }),
118
+ energy: style("streak", { addPct: 100, corePct: 30, glowPct: 150, countPct: 120 }),
119
+ snow: style("soft", { litPct: 100, sizePct: 80, hardPct: 30 }),
120
+ leaf: style("leaf", { litPct: 100, hardPct: 80 }),
121
+ stone: style("shard", { litPct: 100, hardPct: 80, chunkShapeName: "cube" }),
122
+ wood: style("shard", { litPct: 100, hardPct: 80, chunkShapeName: "shard" }),
123
+ metal: style("shard", { litPct: 100, hardPct: 80, chunkShapeName: "plate" }),
124
+ glass: style("shard", { litPct: 60, addPct: 40, hardPct: 80, chunkShapeName: "plate" }),
125
+ };
126
+
127
+ /** The looks that come with the piece: each says how every material is drawn. */
128
+ export const LOOKS = { cartoon, glowing, faceted, realistic };
129
+
130
+ /**
131
+ * What's wrong with a look of a game's own, as { message, field, fix }, or null. A look is { basedOnLookName,
132
+ * materials: { smoke: { hardPct: 100, ... }, ... } }: only what differs from the look it's based on.
133
+ */
134
+ export function ownLookProblem(look) {
135
+ if (!look || typeof look !== "object" || Array.isArray(look)) return { message: "The look isn't an object.", field: "look", fix: 'Give { basedOnLookName: "cartoon", materials: { smoke: { hardPct: 60 } } }.' };
136
+ for (const field of Object.keys(look)) {
137
+ if (field !== "basedOnLookName" && field !== "materials") return { message: `A look has no field "${field}".`, field, fix: "Give only basedOnLookName and materials." };
138
+ }
139
+ if (!(look.basedOnLookName in LOOKS)) return { message: "basedOnLookName isn't one of the piece's looks.", field: "basedOnLookName", fix: `Give one of: ${Object.keys(LOOKS).join(", ")}.` };
140
+ const materials = look.materials ?? {};
141
+ if (typeof materials !== "object" || Array.isArray(materials)) return { message: "materials isn't an object.", field: "materials", fix: "Give an object with a material's name for each one that differs." };
142
+ for (const [materialName, changes] of Object.entries(materials)) {
143
+ if (!MATERIAL_NAMES.includes(materialName)) return { message: `"${materialName}" isn't a material.`, field: `materials.${materialName}`, fix: `Give any of: ${MATERIAL_NAMES.join(", ")}.` };
144
+ if (!changes || typeof changes !== "object") return { message: `materials.${materialName} isn't an object.`, field: `materials.${materialName}`, fix: "Give the fields that differ." };
145
+ for (const [field, value] of Object.entries(changes)) {
146
+ const statement = LOOK_STYLE_FIELDS[field];
147
+ const path = `materials.${materialName}.${field}`;
148
+ if (!statement) return { message: `${path} isn't a field of a material's style.`, field: path, fix: `Give only: ${Object.keys(LOOK_STYLE_FIELDS).join(", ")}.` };
149
+ const isChoice = statement.valueKind === "choice" || statement.valueKind === "choice_or_null";
150
+ if (isChoice && !(statement.choices.includes(value) || (value === null && statement.valueKind === "choice_or_null"))) return { message: `${path} isn't one of its choices.`, field: path, fix: `Give one of: ${statement.choices.join(", ")}${statement.valueKind === "choice_or_null" ? ", or null" : ""}.` };
151
+ if (!isChoice && (typeof value !== "number" || !(value >= statement.minimumNumber && value <= statement.maximumNumber) || (statement.valueKind === "integer" && !Number.isInteger(value)))) return { message: `${path} isn't a number from ${statement.minimumNumber} to ${statement.maximumNumber}.`, field: path, fix: `Give a number from ${statement.minimumNumber} to ${statement.maximumNumber}.` };
152
+ }
153
+ }
154
+ return null;
155
+ }
156
+
157
+ /** Every material's style in a look of a game's own that passed ownLookProblem. */
158
+ export function resolveOwnLook(look) {
159
+ const resolved = {};
160
+ for (const materialName of MATERIAL_NAMES) resolved[materialName] = { ...LOOKS[look.basedOnLookName][materialName], ...(look.materials?.[materialName] ?? {}) };
161
+ return resolved;
162
+ }
package/motion.js ADDED
@@ -0,0 +1,145 @@
1
+ // Where a particle is, from the time alone. A particle is written once, as it's born (where, how fast, when, its
2
+ // seed), and its place at any later moment is worked out exactly from its age: no step every frame, so thousands
3
+ // cost the page nothing while they fly, and the same birth gives the same flight on every player's screen.
4
+ //
5
+ // The same formulas twice: in JavaScript (the check, the bouncing chunks' first throw, anything that must know where
6
+ // a particle is) and as shader text (what draws them). test/node/piece-effects.test.ts holds the JavaScript to a
7
+ // stepped simulation; keep the two in step by hand, line for line.
8
+
9
+ export const GRAVITY_M_PER_SEC2 = 9.81;
10
+ /** How quickly the wind takes hold of a particle with no drag of its own, per second. */
11
+ export const WIND_GRIP_PER_SEC = 1;
12
+
13
+ /**
14
+ * A particle's place at an age. birth: { x, y, z, velocityX, velocityY, velocityZ, lifeSec, seedNumber (0 to 1) }.
15
+ * motion: { dragPerSec, gravityMPerSec2 (down), windShare (0 to 4), turbulenceM, turbulencePerSec, swirlRadPerSec,
16
+ * pullShare (-1 to 1) }. around: { centerX, centerY, centerZ, axisX, axisY, axisZ, windX, windY, windZ }, the effect's
17
+ * place and direction and the wind in metres a second. Writes into `into` ({ x, y, z }) and answers it.
18
+ */
19
+ export function particlePlaceAt(birth, ageSec, motion, around, into = { x: 0, y: 0, z: 0 }) {
20
+ const drag = motion.dragPerSec;
21
+ const travelSec = drag > 1e-4 ? (1 - Math.exp(-drag * ageSec)) / drag : ageSec;
22
+ let x = birth.x + birth.velocityX * travelSec;
23
+ let y = birth.y + birth.velocityY * travelSec;
24
+ let z = birth.z + birth.velocityZ * travelSec;
25
+
26
+ if (motion.turbulenceM > 0) {
27
+ const turn = ageSec * motion.turbulencePerSec;
28
+ const easeIn = Math.min(turn, 1);
29
+ const phase = birth.seedNumber * 6.2831853;
30
+ const reach = motion.turbulenceM * easeIn * 0.5;
31
+ x += reach * (Math.sin(turn * 2.1 + phase * 3) + Math.sin(turn * 1.3 + phase * 7));
32
+ y += reach * (Math.sin(turn * 1.7 + phase * 5) + Math.sin(turn * 2.9 + phase * 11));
33
+ z += reach * (Math.sin(turn * 1.9 + phase * 13) + Math.sin(turn * 1.1 + phase * 17));
34
+ }
35
+
36
+ if (motion.swirlRadPerSec !== 0) {
37
+ // Rodrigues' rotation of the offset from the centre, round the axis.
38
+ const angle = motion.swirlRadPerSec * ageSec;
39
+ const cos = Math.cos(angle);
40
+ const sin = Math.sin(angle);
41
+ const offsetX = x - around.centerX;
42
+ const offsetY = y - around.centerY;
43
+ const offsetZ = z - around.centerZ;
44
+ const along = (offsetX * around.axisX + offsetY * around.axisY + offsetZ * around.axisZ) * (1 - cos);
45
+ const crossX = around.axisY * offsetZ - around.axisZ * offsetY;
46
+ const crossY = around.axisZ * offsetX - around.axisX * offsetZ;
47
+ const crossZ = around.axisX * offsetY - around.axisY * offsetX;
48
+ x = around.centerX + offsetX * cos + crossX * sin + around.axisX * along;
49
+ y = around.centerY + offsetY * cos + crossY * sin + around.axisY * along;
50
+ z = around.centerZ + offsetZ * cos + crossZ * sin + around.axisZ * along;
51
+ }
52
+
53
+ if (motion.pullShare !== 0) {
54
+ const lifeShare = Math.min(ageSec / birth.lifeSec, 1);
55
+ // Toward the centre: all the way there as life ends. Away from it: up to twice as far.
56
+ const keep = motion.pullShare > 0 ? 1 - motion.pullShare * lifeShare * lifeShare : 1 - motion.pullShare * lifeShare;
57
+ x = around.centerX + (x - around.centerX) * keep;
58
+ y = around.centerY + (y - around.centerY) * keep;
59
+ z = around.centerZ + (z - around.centerZ) * keep;
60
+ }
61
+
62
+ // Gravity and the wind act in the world, after everything the effect itself does.
63
+ y -= motion.gravityMPerSec2 * (drag > 1e-4 ? (ageSec - travelSec) / drag : 0.5 * ageSec * ageSec);
64
+ if (motion.windShare > 0) {
65
+ const grip = drag + WIND_GRIP_PER_SEC;
66
+ const carriedSec = (ageSec - (1 - Math.exp(-grip * ageSec)) / grip) * motion.windShare;
67
+ x += around.windX * carriedSec;
68
+ y += around.windY * carriedSec;
69
+ z += around.windZ * carriedSec;
70
+ }
71
+ into.x = x;
72
+ into.y = y;
73
+ into.z = z;
74
+ return into;
75
+ }
76
+
77
+ /**
78
+ * The same as shader text. zaismaLocalPlace is everything the effect itself does (in the effect's own space for
79
+ * attached particles, the world otherwise); zaismaWorldDrift is gravity and the wind, added in the world.
80
+ * motionA: drag, gravity (m/s2 down), wind share, turbulence (m). motionB: turbulence per sec, swirl (rad/s), pull share.
81
+ */
82
+ export const MOTION_GLSL = /* glsl */ `
83
+ float zaismaTravelSec(float drag, float age) {
84
+ return drag > 1e-4 ? (1.0 - exp(-drag * age)) / drag : age;
85
+ }
86
+ vec3 zaismaLocalPlace(vec3 birthPlace, vec3 birthVelocity, float age, float life, float seed, vec4 motionA, vec4 motionB, vec3 center, vec3 axis) {
87
+ vec3 place = birthPlace + birthVelocity * zaismaTravelSec(motionA.x, age);
88
+ if (motionA.w > 0.0) {
89
+ float turn = age * motionB.x;
90
+ float phase = seed * 6.2831853;
91
+ place += motionA.w * min(turn, 1.0) * 0.5 * vec3(
92
+ sin(turn * 2.1 + phase * 3.0) + sin(turn * 1.3 + phase * 7.0),
93
+ sin(turn * 1.7 + phase * 5.0) + sin(turn * 2.9 + phase * 11.0),
94
+ sin(turn * 1.9 + phase * 13.0) + sin(turn * 1.1 + phase * 17.0));
95
+ }
96
+ if (motionB.y != 0.0) {
97
+ float angle = motionB.y * age;
98
+ float cosAngle = cos(angle);
99
+ vec3 offset = place - center;
100
+ place = center + offset * cosAngle + cross(axis, offset) * sin(angle) + axis * dot(axis, offset) * (1.0 - cosAngle);
101
+ }
102
+ if (motionB.z != 0.0) {
103
+ float lifeShare = min(age / life, 1.0);
104
+ float keep = motionB.z > 0.0 ? 1.0 - motionB.z * lifeShare * lifeShare : 1.0 - motionB.z * lifeShare;
105
+ place = center + (place - center) * keep;
106
+ }
107
+ return place;
108
+ }
109
+ vec3 zaismaWorldDrift(float age, vec4 motionA, vec3 wind) {
110
+ float drag = motionA.x;
111
+ vec3 drift = vec3(0.0, -motionA.y * (drag > 1e-4 ? (age - zaismaTravelSec(drag, age)) / drag : 0.5 * age * age), 0.0);
112
+ if (motionA.z > 0.0) {
113
+ float grip = drag + ${WIND_GRIP_PER_SEC.toFixed(1)};
114
+ drift += wind * (age - (1.0 - exp(-grip * age)) / grip) * motionA.z;
115
+ }
116
+ return drift;
117
+ }
118
+ vec3 zaismaTurn(vec4 turn, vec3 place) {
119
+ return place + 2.0 * cross(turn.xyz, cross(turn.xyz, place) + turn.w * place);
120
+ }
121
+ /** A value read off up to four keys spread evenly over a life. */
122
+ float zaismaOverLife(vec4 keys, float keyCount, float lifeShare) {
123
+ float place = clamp(lifeShare, 0.0, 1.0) * (keyCount - 1.0);
124
+ float index = min(floor(place), keyCount - 2.0);
125
+ float from = index < 0.5 ? keys.x : index < 1.5 ? keys.y : keys.z;
126
+ float to = index < 0.5 ? keys.y : index < 1.5 ? keys.z : keys.w;
127
+ return mix(from, to, place - index);
128
+ }
129
+ float zaismaHash(float seed, float salt) {
130
+ return fract(sin(seed * 91.3458 + salt * 47.9898) * 43758.5453);
131
+ }
132
+ `;
133
+
134
+ /** The wind at a moment, in metres a second: its steady speed along its direction, swelling and easing with gusts. */
135
+ export function windAt(weather, timeSec, into = { x: 0, y: 0, z: 0 }) {
136
+ const directionRad = (weather.windDirectionDeg * Math.PI) / 180;
137
+ // Two slow swells that never line up, so gusts don't repeat.
138
+ const gust = 1 + (weather.windGustPct / 100) * 0.5 * (Math.sin(timeSec * 0.83) + Math.sin(timeSec * 0.31 + 1.7));
139
+ const speed = weather.windSpeedMPerSec * Math.max(gust, 0);
140
+ into.x = Math.cos(directionRad) * speed;
141
+ into.y = 0;
142
+ // Counterclockwise seen from above, with y up: 90 degrees blows toward -z.
143
+ into.z = -Math.sin(directionRad) * speed;
144
+ return into;
145
+ }
package/online.d.ts ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * @zaisma/effects, online: the same effect on every player's screen, particle for particle, at the same moment.
3
+ * Its own file, so a game played alone never loads the online core.
4
+ *
5
+ * @example
6
+ * import { effectsKind } from "./pieces/@zaisma/effects/online.js";
7
+ * const shared = net.use(effectsKind());
8
+ * shared.attach(effects); // createEffects' answer
9
+ * shared.play("explosion_large", { positionM }); // here and for everyone
10
+ * const fire = shared.start("campfire", { positionM }); // kept: a late joiner sees it too
11
+ * fire.stop();
12
+ */
13
+ import type { Kind } from "../net/index.js";
14
+ import type { EffectPlay, Effects, PlayAt } from "./index.js";
15
+
16
+ export { PieceError } from "../piece/index.js";
17
+
18
+ /** Where and how a shared effect plays: as play's, without follow (an object exists in one game only), seedNumber and startedAgoSec (the room's clock sets them). */
19
+ export type SharedPlayAt = Omit<PlayAt, "follow" | "seedNumber" | "startedAgoSec"> & { positionM: NonNullable<PlayAt["positionM"]> };
20
+
21
+ /** The room's weather: only what changes. */
22
+ export interface SharedWeather {
23
+ rainPct?: number;
24
+ snowPct?: number;
25
+ fogPct?: number;
26
+ windSpeedMPerSec?: number;
27
+ windDirectionDeg?: number;
28
+ windGustPct?: number;
29
+ }
30
+
31
+ export interface EffectsKindOptions {
32
+ /** The effects a player may share, 1 to 400 names: anything else is dropped. Any name when left out. */
33
+ effectNames?: string[];
34
+ /** How many effects a second one player may share, 1 to 200; the rest are dropped. Default 20. */
35
+ maximumPlaysPerSec?: number;
36
+ }
37
+
38
+ /** An effect the room keeps playing. */
39
+ export interface KeptEffect {
40
+ readonly keptId: number;
41
+ readonly isEnded: boolean;
42
+ /** Stops it for everyone. */
43
+ stop(): void;
44
+ }
45
+
46
+ /** What net.use answers. Its calls throw PieceError "effects_not_attached", "unknown_effect", "bad_play", "unknown_argument", "bad_weather", "too_many_kept_effects". */
47
+ export interface SharedEffects {
48
+ /** Hands over the game's effects: from now on shared effects and the room's weather show in them. Throws PieceError "bad_argument". */
49
+ attach(effects: Effects): void;
50
+ /** Plays an effect here and in every other player's game, the same in each. Answers this game's play. */
51
+ play(effectName: string, at: SharedPlayAt): EffectPlay | null;
52
+ /** Starts an effect that keeps playing for everyone, a late joiner too, until its stop(). A room keeps at most MAXIMUM_KEPT_COUNT. */
53
+ start(effectName: string, at: SharedPlayAt): KeptEffect;
54
+ /** On the relay, by the host: the weather for the whole room, kept for late joiners. With a game server, set it there. Answers false where this game isn't in charge. */
55
+ setWeather(values: SharedWeather): boolean;
56
+ /** How many effects the room keeps playing now. */
57
+ readonly keptCount: number;
58
+ }
59
+
60
+ /** What the game server's own code calls, reached as kind.onServer(state, room). */
61
+ export interface ServerEffects {
62
+ /** Plays an effect in every player's game: an explosion the server decided. */
63
+ play(effectName: string, at: SharedPlayAt): void;
64
+ /** Starts an effect that keeps playing for everyone. Answers its keptId, for stop. Throws PieceError "too_many_kept_effects". */
65
+ start(effectName: string, at: SharedPlayAt): number;
66
+ /** Stops an effect that keeps playing. Answers whether it was playing. */
67
+ stop(keptId: number): boolean;
68
+ /** The weather for the whole room, kept for late joiners. */
69
+ setWeather(values: SharedWeather): void;
70
+ readonly keptCount: number;
71
+ }
72
+
73
+ export type EffectsKind = Kind<SharedEffects, ServerEffects>;
74
+
75
+ /** The effects' kind of sync, for net.use. Give the same options in the game and on its game server. Throws PieceError "bad_argument", "unknown_argument". */
76
+ export declare function effectsKind(given?: EffectsKindOptions): EffectsKind;
77
+
78
+ /** How many effects a room keeps playing at once: 64. */
79
+ export declare const MAXIMUM_KEPT_COUNT: number;
package/online.js ADDED
@@ -0,0 +1,32 @@
1
+ // @zaisma/effects, online: the same effect on every player's screen. Its own file, so a game played alone never
2
+ // loads the online core.
3
+ //
4
+ // import { effectsKind } from "@zaisma/effects/online.js";
5
+ // const shared = net.use(effectsKind());
6
+ // shared.attach(effects); // createEffects' answer
7
+ // shared.play("explosion_large", { positionM }); // here and for everyone, the same in each game
8
+ // const fire = shared.start("campfire", { positionM }); // keeps playing, a late joiner sees it too
9
+ // fire.stop();
10
+ //
11
+ // With a game server, give its defineNetServer the same kind from "@zaisma/effects/server.js".
12
+ import { defineKind } from "../net/index.js";
13
+ import { effectsKindFrom } from "./sync.js";
14
+
15
+ const kinds = new Map();
16
+
17
+ /**
18
+ * The effects' kind of sync, for net.use. given: { effectNames, maximumPlaysPerSec }, the same in the game and on its
19
+ * game server. The same options answer the same kind. Throws PieceError "bad_argument", "unknown_argument".
20
+ */
21
+ export function effectsKind(given) {
22
+ const key = JSON.stringify([given?.effectNames ?? null, given?.maximumPlaysPerSec ?? null]);
23
+ if (given?.beforePlay !== undefined || !kinds.has(key)) {
24
+ const kind = effectsKindFrom(defineKind, given);
25
+ if (given?.beforePlay !== undefined) return kind;
26
+ kinds.set(key, kind);
27
+ }
28
+ return kinds.get(key);
29
+ }
30
+
31
+ export { MAXIMUM_KEPT_COUNT } from "./sync.js";
32
+ export { PieceError } from "../piece/index.js";