emeraldengine 2.2.0 → 3.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 (162) hide show
  1. package/README.md +2359 -968
  2. package/dist/types/index.d.ts +72 -32
  3. package/dist/types/src/Animator.d.ts +50 -0
  4. package/dist/types/{BitmapText.d.ts → src/BitmapText.d.ts} +19 -21
  5. package/dist/types/src/Camera.d.ts +122 -0
  6. package/dist/types/src/CameraController.d.ts +107 -0
  7. package/dist/types/src/CanvasText.d.ts +91 -0
  8. package/dist/types/src/CollisionLayers.d.ts +58 -0
  9. package/dist/types/{Color.d.ts → src/Color.d.ts} +5 -6
  10. package/dist/types/src/Coroutine.d.ts +111 -0
  11. package/dist/types/src/DebugOverlay.d.ts +86 -0
  12. package/dist/types/src/Drawable.d.ts +271 -0
  13. package/dist/types/src/Easing.d.ts +22 -0
  14. package/dist/types/src/Emerald.d.ts +420 -0
  15. package/dist/types/src/EmeraldDB.d.ts +159 -0
  16. package/dist/types/{FPSCounter.d.ts → src/FPSCounter.d.ts} +1 -3
  17. package/dist/types/src/GLUtils.d.ts +4 -0
  18. package/dist/types/{Instance.d.ts → src/Instance.d.ts} +37 -15
  19. package/dist/types/{InstancedTexture.d.ts → src/InstancedTexture.d.ts} +60 -23
  20. package/dist/types/src/Interpolator.d.ts +66 -0
  21. package/dist/types/src/Material.d.ts +87 -0
  22. package/dist/types/src/MathUtils.d.ts +80 -0
  23. package/dist/types/src/ParticleEmitter.d.ts +131 -0
  24. package/dist/types/{Physics.d.ts → src/Physics.d.ts} +88 -20
  25. package/dist/types/src/Pool.d.ts +53 -0
  26. package/dist/types/src/PostEffects.d.ts +68 -0
  27. package/dist/types/src/PostProcessor.d.ts +124 -0
  28. package/dist/types/src/RenderTarget.d.ts +56 -0
  29. package/dist/types/src/Scene.d.ts +62 -0
  30. package/dist/types/src/ScreenEffects.d.ts +111 -0
  31. package/dist/types/src/Serializer.d.ts +86 -0
  32. package/dist/types/src/Shaders.d.ts +2 -0
  33. package/dist/types/{Shapes.d.ts → src/Shapes.d.ts} +4 -6
  34. package/dist/types/src/SpatialGrid.d.ts +50 -0
  35. package/dist/types/src/SpriteBatch.d.ts +89 -0
  36. package/dist/types/src/StateMachine.d.ts +59 -0
  37. package/dist/types/src/Storage.d.ts +89 -0
  38. package/dist/types/{Texture.d.ts → src/Texture.d.ts} +3 -4
  39. package/dist/types/src/TextureAtlas.d.ts +55 -0
  40. package/dist/types/src/Tilemap.d.ts +91 -0
  41. package/dist/types/src/Time.d.ts +53 -0
  42. package/dist/types/src/Timer.d.ts +54 -0
  43. package/dist/types/src/Transform.d.ts +94 -0
  44. package/dist/types/src/Tween.d.ts +81 -0
  45. package/dist/types/src/UI.d.ts +121 -0
  46. package/dist/types/src/components/Behaviour.d.ts +71 -0
  47. package/dist/types/{components → src/components}/BoxCollider.d.ts +14 -14
  48. package/dist/types/src/components/BoxColliderDebug.d.ts +15 -0
  49. package/dist/types/{components → src/components}/CircleCollider.d.ts +14 -12
  50. package/dist/types/src/components/CircleColliderDebug.d.ts +15 -0
  51. package/dist/types/src/components/Collider.d.ts +96 -0
  52. package/dist/types/src/components/GameObject.d.ts +135 -0
  53. package/dist/types/src/components/RigidBody.d.ts +183 -0
  54. package/dist/types/src/importers/Aseprite.d.ts +79 -0
  55. package/dist/types/src/importers/TiledMap.d.ts +62 -0
  56. package/dist/types/{lights → src/lights}/DirectionalLight.d.ts +7 -9
  57. package/dist/types/{lights → src/lights}/PointLight.d.ts +6 -9
  58. package/dist/types/src/managers/AssetManager.d.ts +116 -0
  59. package/dist/types/src/managers/AudioManager.d.ts +259 -0
  60. package/dist/types/{managers → src/managers}/CameraManager.d.ts +8 -8
  61. package/dist/types/{managers → src/managers}/EventManager.d.ts +46 -32
  62. package/dist/types/src/managers/GLManager.d.ts +84 -0
  63. package/dist/types/src/managers/GLState.d.ts +37 -0
  64. package/dist/types/src/managers/IDManager.d.ts +31 -0
  65. package/dist/types/src/managers/InputManager.d.ts +290 -0
  66. package/dist/types/src/managers/NetworkManager.d.ts +93 -0
  67. package/dist/types/src/managers/RenderStats.d.ts +34 -0
  68. package/dist/types/src/managers/SceneManager.d.ts +44 -0
  69. package/dist/types/src/managers/ShaderManager.d.ts +55 -0
  70. package/dist/types/src/managers/TextureManager.d.ts +102 -0
  71. package/dist/types/src/particlesystem/Particle.d.ts +64 -0
  72. package/dist/types/{particlesystem → src/particlesystem}/ParticleSettings.d.ts +32 -24
  73. package/dist/types/{particlesystem → src/particlesystem}/Particles.d.ts +20 -12
  74. package/index.js +72 -0
  75. package/package.json +74 -60
  76. package/src/Animator.js +95 -0
  77. package/src/BitmapText.js +6 -5
  78. package/src/Camera.js +183 -0
  79. package/src/CameraController.js +192 -0
  80. package/src/CanvasText.js +281 -0
  81. package/src/CollisionLayers.js +86 -0
  82. package/src/Color.js +18 -18
  83. package/src/Coroutine.js +259 -0
  84. package/src/DebugOverlay.js +246 -0
  85. package/src/Drawable.js +842 -555
  86. package/src/Easing.js +57 -0
  87. package/src/Emerald.js +1150 -459
  88. package/src/EmeraldDB.js +328 -0
  89. package/src/FPSCounter.js +43 -43
  90. package/src/GLUtils.js +60 -67
  91. package/src/Instance.js +41 -5
  92. package/src/InstancedTexture.js +251 -118
  93. package/src/Interpolator.js +124 -0
  94. package/src/Material.js +202 -0
  95. package/src/MathUtils.js +133 -0
  96. package/src/ParticleEmitter.js +284 -0
  97. package/src/Physics.js +186 -5
  98. package/src/Pool.js +85 -0
  99. package/src/PostEffects.js +296 -0
  100. package/src/PostProcessor.js +304 -0
  101. package/src/RenderTarget.js +134 -0
  102. package/src/Scene.js +115 -83
  103. package/src/ScreenEffects.js +266 -0
  104. package/src/Serializer.js +131 -0
  105. package/src/Shaders.js +150 -165
  106. package/src/Shapes.js +118 -129
  107. package/src/SpatialGrid.js +111 -0
  108. package/src/SpriteBatch.js +299 -0
  109. package/src/StateMachine.js +82 -0
  110. package/src/Storage.js +175 -47
  111. package/src/Texture.js +58 -67
  112. package/src/TextureAtlas.js +96 -0
  113. package/src/Tilemap.js +274 -0
  114. package/src/Time.js +51 -6
  115. package/src/Timer.js +99 -0
  116. package/src/Transform.js +100 -7
  117. package/src/Tween.js +160 -0
  118. package/src/UI.js +394 -0
  119. package/src/components/Behaviour.js +90 -0
  120. package/src/components/BoxCollider.js +22 -3
  121. package/src/components/CircleCollider.js +19 -3
  122. package/src/components/CircleColliderDebug.js +24 -24
  123. package/src/components/Collider.js +140 -34
  124. package/src/components/GameObject.js +130 -21
  125. package/src/components/RigidBody.js +123 -2
  126. package/src/importers/Aseprite.js +142 -0
  127. package/src/importers/TiledMap.js +158 -0
  128. package/src/lights/DirectionalLight.js +6 -15
  129. package/src/lights/PointLight.js +4 -4
  130. package/src/managers/AssetManager.js +239 -0
  131. package/src/managers/AudioManager.js +565 -146
  132. package/src/managers/EventManager.js +488 -477
  133. package/src/managers/GLManager.js +57 -0
  134. package/src/managers/GLState.js +70 -0
  135. package/src/managers/IDManager.js +24 -2
  136. package/src/managers/InputManager.js +779 -0
  137. package/src/managers/NetworkManager.js +178 -0
  138. package/src/managers/RenderStats.js +34 -0
  139. package/src/managers/SceneManager.js +30 -0
  140. package/src/managers/ShaderManager.js +0 -2
  141. package/src/managers/TextureManager.js +218 -0
  142. package/src/particlesystem/Particle.js +82 -7
  143. package/src/particlesystem/ParticleSettings.js +21 -3
  144. package/src/particlesystem/Particles.js +80 -31
  145. package/dist/types/Drawable.d.ts +0 -157
  146. package/dist/types/Emerald.d.ts +0 -73
  147. package/dist/types/GLUtils.d.ts +0 -6
  148. package/dist/types/Scene.d.ts +0 -39
  149. package/dist/types/Shaders.d.ts +0 -4
  150. package/dist/types/Storage.d.ts +0 -46
  151. package/dist/types/Time.d.ts +0 -22
  152. package/dist/types/Transform.d.ts +0 -41
  153. package/dist/types/components/BoxColliderDebug.d.ts +0 -19
  154. package/dist/types/components/CircleColliderDebug.d.ts +0 -19
  155. package/dist/types/components/Collider.d.ts +0 -53
  156. package/dist/types/components/GameObject.d.ts +0 -72
  157. package/dist/types/components/RigidBody.d.ts +0 -104
  158. package/dist/types/managers/AudioManager.d.ts +0 -60
  159. package/dist/types/managers/GLManager.d.ts +0 -47
  160. package/dist/types/managers/IDManager.d.ts +0 -21
  161. package/dist/types/managers/SceneManager.d.ts +0 -22
  162. package/dist/types/particlesystem/Particle.d.ts +0 -42
@@ -56,10 +56,67 @@ class GLManager {
56
56
  static getCanvas() {
57
57
  return GLManager.canvas;
58
58
  }
59
+
60
+ /**
61
+ * @method registerRestorable
62
+ * @description Registers an object holding GL resources (buffers, textures,
63
+ * programs) for re-creation after a WebGL context loss. The object must
64
+ * implement `_restoreGL()`. Drawables register themselves automatically and
65
+ * unregister on dispose().
66
+ * @param {Object} obj - An object with a _restoreGL() method
67
+ */
68
+ static registerRestorable(obj) {
69
+ GLManager.restorables.add(obj);
70
+ }
71
+
72
+ /**
73
+ * @method unregisterRestorable
74
+ * @description Removes an object from the context-restore registry.
75
+ */
76
+ static unregisterRestorable(obj) {
77
+ GLManager.restorables.delete(obj);
78
+ }
79
+
80
+ /**
81
+ * @method restoreAll
82
+ * @description Calls _restoreGL() on every registered object. Invoked by
83
+ * Emerald after the context is restored and the default shaders/textures
84
+ * have been rebuilt.
85
+ */
86
+ static restoreAll() {
87
+ for (const obj of GLManager.restorables) {
88
+ try {
89
+ obj._restoreGL();
90
+ } catch (e) {
91
+ console.error("[GLManager] > restore failed for", obj, e);
92
+ }
93
+ }
94
+ }
95
+
96
+ /**
97
+ * @method setProjection
98
+ * @description Stores the current camera's projection matrix so custom
99
+ * Materials (which use their own shader program) can upload it.
100
+ * @param {Float32Array} matrix - The projection matrix
101
+ */
102
+ static setProjection(matrix) {
103
+ GLManager.projection = matrix;
104
+ }
105
+
106
+ /**
107
+ * @method getProjection
108
+ * @description Returns the last projection matrix set by the renderer.
109
+ * @returns {Float32Array}
110
+ */
111
+ static getProjection() {
112
+ return GLManager.projection;
113
+ }
59
114
  }
60
115
 
61
116
  GLManager.gl = null;
62
117
  GLManager.programInfo = null;
63
118
  GLManager.canvas = null;
119
+ GLManager.projection = null;
120
+ GLManager.restorables = new Set();
64
121
 
65
122
  export default GLManager;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * @class GLState
3
+ * @description Tiny redundant-uniform filter. The renderer sets the same handful
4
+ * of per-object uniforms (useTexture, useInstances, useLighting, color, opacity)
5
+ * for every object every frame; most calls repeat the previous value. These
6
+ * helpers skip the GL call when the value is unchanged. Keyed by uniform
7
+ * location, which is program-specific, so the cache is safe across objects.
8
+ */
9
+ class GLState {
10
+ /**
11
+ * @method reset
12
+ * @description Clears the cache. Called at the start of each frame so the
13
+ * first object always re-establishes state.
14
+ */
15
+ static reset() {
16
+ GLState.ints.clear();
17
+ GLState.floats.clear();
18
+ GLState.vec4s.clear();
19
+ }
20
+
21
+ /**
22
+ * @method uniform1i
23
+ * @description Cached gl.uniform1i (accepts booleans, coerced like WebGL does).
24
+ */
25
+ static uniform1i(gl, location, value) {
26
+ if (location == null) return;
27
+ const v = value === true ? 1 : value === false ? 0 : value;
28
+ if (GLState.ints.get(location) !== v) {
29
+ gl.uniform1i(location, v);
30
+ GLState.ints.set(location, v);
31
+ }
32
+ }
33
+
34
+ /**
35
+ * @method uniform1f
36
+ * @description Cached gl.uniform1f.
37
+ */
38
+ static uniform1f(gl, location, value) {
39
+ if (location == null) return;
40
+ if (GLState.floats.get(location) !== value) {
41
+ gl.uniform1f(location, value);
42
+ GLState.floats.set(location, value);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * @method uniform4fv
48
+ * @description Cached gl.uniform4fv (compares the four components).
49
+ */
50
+ static uniform4fv(gl, location, value) {
51
+ if (location == null) return;
52
+ const cached = GLState.vec4s.get(location);
53
+ if (
54
+ !cached ||
55
+ cached[0] !== value[0] ||
56
+ cached[1] !== value[1] ||
57
+ cached[2] !== value[2] ||
58
+ cached[3] !== value[3]
59
+ ) {
60
+ gl.uniform4fv(location, value);
61
+ GLState.vec4s.set(location, [value[0], value[1], value[2], value[3]]);
62
+ }
63
+ }
64
+ }
65
+
66
+ GLState.ints = new Map();
67
+ GLState.floats = new Map();
68
+ GLState.vec4s = new Map();
69
+
70
+ export default GLState;
@@ -5,11 +5,22 @@
5
5
  class IDManager {
6
6
  /**
7
7
  * @method generateID
8
- * @description Generates a random ID
8
+ * @description Generates a random ID, preferring the collision-resistant
9
+ * crypto.randomUUID when available and falling back to a random string.
9
10
  * @returns {string} - The random ID
10
11
  */
11
12
  static generateID() {
12
- return Math.random().toString(36).substring(2, 10);
13
+ if (
14
+ typeof globalThis !== "undefined" &&
15
+ globalThis.crypto &&
16
+ typeof globalThis.crypto.randomUUID === "function"
17
+ ) {
18
+ return globalThis.crypto.randomUUID();
19
+ }
20
+ return (
21
+ Math.random().toString(36).substring(2, 10) +
22
+ Math.random().toString(36).substring(2, 10)
23
+ );
13
24
  }
14
25
 
15
26
  /**
@@ -25,6 +36,17 @@ class IDManager {
25
36
  IDManager.existingIDs.add(id);
26
37
  return id;
27
38
  }
39
+
40
+ /**
41
+ * @method release
42
+ * @description Releases a previously-generated ID so the global registry
43
+ * doesn't grow without bound. Call this when an object/instance is destroyed
44
+ * (e.g. particles removed from an InstancedTexture).
45
+ * @param {string} id - The ID to release
46
+ */
47
+ static release(id) {
48
+ if (id != null) IDManager.existingIDs.delete(id);
49
+ }
28
50
  }
29
51
 
30
52
  IDManager.existingIDs = new Set();