emeraldengine 2.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 (73) hide show
  1. package/README.md +968 -0
  2. package/dist/types/imports/BitmapText.d.ts +79 -0
  3. package/dist/types/imports/Color.d.ts +17 -0
  4. package/dist/types/imports/Drawable.d.ts +157 -0
  5. package/dist/types/imports/Emerald.d.ts +73 -0
  6. package/dist/types/imports/FPSCounter.d.ts +19 -0
  7. package/dist/types/imports/GLUtils.d.ts +6 -0
  8. package/dist/types/imports/Instance.d.ts +87 -0
  9. package/dist/types/imports/InstancedTexture.d.ts +230 -0
  10. package/dist/types/imports/Physics.d.ts +139 -0
  11. package/dist/types/imports/Scene.d.ts +39 -0
  12. package/dist/types/imports/Shaders.d.ts +4 -0
  13. package/dist/types/imports/Shapes.d.ts +25 -0
  14. package/dist/types/imports/Storage.d.ts +46 -0
  15. package/dist/types/imports/Texture.d.ts +19 -0
  16. package/dist/types/imports/Time.d.ts +22 -0
  17. package/dist/types/imports/Transform.d.ts +41 -0
  18. package/dist/types/imports/components/BoxCollider.d.ts +41 -0
  19. package/dist/types/imports/components/BoxColliderDebug.d.ts +19 -0
  20. package/dist/types/imports/components/CircleCollider.d.ts +39 -0
  21. package/dist/types/imports/components/CircleColliderDebug.d.ts +19 -0
  22. package/dist/types/imports/components/Collider.d.ts +53 -0
  23. package/dist/types/imports/components/GameObject.d.ts +72 -0
  24. package/dist/types/imports/components/RigidBody.d.ts +104 -0
  25. package/dist/types/imports/lights/DirectionalLight.d.ts +26 -0
  26. package/dist/types/imports/lights/PointLight.d.ts +18 -0
  27. package/dist/types/imports/managers/AudioManager.d.ts +60 -0
  28. package/dist/types/imports/managers/CameraManager.d.ts +35 -0
  29. package/dist/types/imports/managers/EventManager.d.ts +187 -0
  30. package/dist/types/imports/managers/GLManager.d.ts +47 -0
  31. package/dist/types/imports/managers/IDManager.d.ts +21 -0
  32. package/dist/types/imports/managers/SceneManager.d.ts +22 -0
  33. package/dist/types/imports/particlesystem/Particle.d.ts +42 -0
  34. package/dist/types/imports/particlesystem/ParticleSettings.d.ts +49 -0
  35. package/dist/types/imports/particlesystem/Particles.d.ts +71 -0
  36. package/dist/types/index.d.ts +32 -0
  37. package/imports/BitmapText.js +245 -0
  38. package/imports/Color.js +18 -0
  39. package/imports/Drawable.js +550 -0
  40. package/imports/Emerald.js +459 -0
  41. package/imports/FPSCounter.js +43 -0
  42. package/imports/GLUtils.js +67 -0
  43. package/imports/Instance.js +187 -0
  44. package/imports/InstancedTexture.js +856 -0
  45. package/imports/Physics.js +242 -0
  46. package/imports/Scene.js +79 -0
  47. package/imports/Shaders.js +165 -0
  48. package/imports/Shapes.js +129 -0
  49. package/imports/Storage.js +63 -0
  50. package/imports/Texture.js +67 -0
  51. package/imports/Time.js +28 -0
  52. package/imports/Transform.js +59 -0
  53. package/imports/components/BoxCollider.js +67 -0
  54. package/imports/components/BoxColliderDebug.js +38 -0
  55. package/imports/components/CircleCollider.js +67 -0
  56. package/imports/components/CircleColliderDebug.js +36 -0
  57. package/imports/components/Collider.js +51 -0
  58. package/imports/components/GameObject.js +160 -0
  59. package/imports/components/RigidBody.js +169 -0
  60. package/imports/lights/DirectionalLight.js +68 -0
  61. package/imports/lights/PointLight.js +17 -0
  62. package/imports/managers/AudioManager.js +146 -0
  63. package/imports/managers/CameraManager.js +60 -0
  64. package/imports/managers/EventManager.js +479 -0
  65. package/imports/managers/GLManager.js +65 -0
  66. package/imports/managers/IDManager.js +32 -0
  67. package/imports/managers/SceneManager.js +27 -0
  68. package/imports/managers/ShaderManager.js +133 -0
  69. package/imports/particlesystem/Particle.js +75 -0
  70. package/imports/particlesystem/ParticleSettings.js +49 -0
  71. package/imports/particlesystem/Particles.js +226 -0
  72. package/index.js +67 -0
  73. package/package.json +38 -0
@@ -0,0 +1,169 @@
1
+ import * as planck from "planck";
2
+ import IDManager from "../managers/IDManager.js";
3
+ import { Vector2 } from "../Physics.js";
4
+ import Collider from "./Collider.js";
5
+
6
+ /**
7
+ * @class RigidBody
8
+ * @param {Physics} physics - The physics engine
9
+ * @param {string | "static" | "dynamic" | "kinematic"} type - The type of rigidbody
10
+ * @param {Vector2} position - The position of the rigidbody
11
+ * @param {boolean} fixedRotation - Whether the rigidbody is fixed rotation
12
+ * @param {GameObject} parentObject - The parent object of the rigidbody
13
+ * @param {Vector2} offset - The offset of the rigidbody
14
+ */
15
+ class RigidBody {
16
+ constructor(
17
+ physics,
18
+ type,
19
+ position,
20
+ fixedRotation,
21
+ parentObject = null,
22
+ offset = new Vector2(0, 0)
23
+ ) {
24
+ this.physics = physics;
25
+ this.offset = offset;
26
+ this.type = type;
27
+ this.body = physics.world.createBody({
28
+ type: type,
29
+ position: new planck.Vec2(
30
+ (position.x + offset.x) / physics.scale,
31
+ (position.y + offset.y) / physics.scale
32
+ ),
33
+ fixedRotation: fixedRotation,
34
+ });
35
+ this.parentObject = parentObject;
36
+ this.position = position;
37
+ this.collider = null;
38
+ this.id = IDManager.generateUniqueID();
39
+ this.name = "RigidBody" + this.id;
40
+ }
41
+
42
+ /**
43
+ * @method updatePosition
44
+ * @description Updates the position of the rigidbody
45
+ * @param {Vector2} position - The new position
46
+ */
47
+ updatePosition(position) {
48
+ this.body.setPosition(
49
+ new planck.Vec2(
50
+ (position.x + this.offset.x) / this.physics.scale,
51
+ (position.y + this.offset.y) / this.physics.scale
52
+ )
53
+ );
54
+ }
55
+
56
+ /**
57
+ * @method getOffset
58
+ * @description Returns the offset of the rigidbody
59
+ * @returns {Vector2} - The offset of the rigidbody
60
+ */
61
+ getOffset() {
62
+ return this.offset;
63
+ }
64
+
65
+ /**
66
+ * @method setCollider
67
+ * @description Sets the collider of the rigidbody
68
+ * @param {Collider} collider - The collider to set
69
+ */
70
+ setCollider(collider) {
71
+ if (collider instanceof Collider) {
72
+ this.collider = collider;
73
+ }
74
+ }
75
+
76
+ /**
77
+ * @method setRotation
78
+ * @description Sets the rotation of the rigidbody
79
+ * @param {number} rotation - The new rotation
80
+ */
81
+ setRotation(rotation) {
82
+ this.body.setAngle(rotation);
83
+ }
84
+
85
+ /**
86
+ * @method getPosition
87
+ * @description Returns the position of the rigidbody
88
+ * @returns {Vector2} - The position of the rigidbody
89
+ */
90
+ getPosition() {
91
+ return this.position;
92
+ }
93
+
94
+ /**
95
+ * @method getAngle
96
+ * @description Returns the angle of the rigidbody
97
+ * @returns {number} - The angle of the rigidbody
98
+ */
99
+ getAngle() {
100
+ return this.body.getAngle();
101
+ }
102
+
103
+ /**
104
+ * @method getCollider
105
+ * @description Returns the collider of the rigidbody
106
+ * @returns {Collider} - The collider of the rigidbody
107
+ */
108
+ getCollider() {
109
+ return this.collider;
110
+ }
111
+
112
+ /**
113
+ * @method getBody
114
+ * @description Returns the body of the rigidbody
115
+ * @returns {planck.Body} - The body of the rigidbody
116
+ */
117
+ getBody() {
118
+ return this.body;
119
+ }
120
+
121
+ /**
122
+ * @method getPhysics
123
+ * @description Returns the physics engine of the rigidbody
124
+ * @returns {Physics} - The physics engine of the rigidbody
125
+ */
126
+ getPhysics() {
127
+ return this.physics;
128
+ }
129
+
130
+ /**
131
+ * @method destroy
132
+ * @description Destroys the rigidbody
133
+ */
134
+ destroy() {
135
+ this.physics.world.destroyBody(this.body);
136
+ }
137
+
138
+ /**
139
+ * @method detachCollider
140
+ * @description Detaches a collider from the rigidbody
141
+ * @param {Collider} collider - The collider to detach
142
+ */
143
+ detachCollider(collider) {
144
+ if (collider && collider.getCollider()) {
145
+ this.body.destroyFixture(collider.getCollider());
146
+ this.collider = null;
147
+ }
148
+ }
149
+
150
+ /**
151
+ * @method setParent
152
+ * @description Sets the parent object of the rigidbody
153
+ * @param {GameObject} parent - The parent object to set
154
+ */
155
+ setParent(parent) {
156
+ this.parentObject = parent;
157
+ }
158
+
159
+ /**
160
+ * @method getType
161
+ * @description Returns the type of the rigidbody
162
+ * @returns {string | "static" | "dynamic" | "kinematic"} - The type of the rigidbody
163
+ */
164
+ getType() {
165
+ return this.type;
166
+ }
167
+ }
168
+
169
+ export default RigidBody;
@@ -0,0 +1,68 @@
1
+ import { Vector2 } from "../Physics.js";
2
+
3
+ /**
4
+ * @class DirectionalLight
5
+ * @param {Vector2} position - The position of the light
6
+ * @param {Vector2} direction - The direction of the light
7
+ * @param {Color} color - The color of the light
8
+ * @param {number} intensity - The intensity of the light
9
+ * @param {number} width - The width of the light
10
+ */
11
+ class DirectionalLight {
12
+ constructor(position, direction, color, intensity, width) {
13
+ this.position = position; // Vector2 - origin point of the light
14
+ // Normalize the direction vector
15
+ const length = Math.sqrt(
16
+ direction.x * direction.x + direction.y * direction.y
17
+ );
18
+ this.direction = new Vector2(direction.x / length, direction.y / length); // Vector2 - normalized direction vector
19
+ this.color = color; // Color
20
+ this.intensity = intensity; // Number
21
+ this.width = width; // Number - width of the light beam
22
+ }
23
+
24
+ /**
25
+ * Calculate the light intensity at a given point
26
+ * @param {Vector2} point - The point to calculate intensity for
27
+ * @returns {number} - Light intensity (0-1)
28
+ */
29
+ getIntensityAtPoint(point) {
30
+
31
+ // Calculate distance from light position to point
32
+ const dx = point.x - this.position.x;
33
+ const dy = point.y - this.position.y;
34
+ const distance = Math.sqrt(dx * dx + dy * dy);
35
+
36
+ // Calculate the projection of the point onto the light direction
37
+ const lightDirNormalized = new Vector2(this.direction.x, this.direction.y);
38
+ const toPoint = new Vector2(dx, dy);
39
+ const projection =
40
+ toPoint.x * lightDirNormalized.x + toPoint.y * lightDirNormalized.y;
41
+
42
+ // If point is behind the light, no illumination
43
+ if (projection < 0) {
44
+ return 0;
45
+ }
46
+
47
+ // Calculate perpendicular distance from the light beam center
48
+ const perpDistance = Math.abs(
49
+ toPoint.x * lightDirNormalized.y - toPoint.y * lightDirNormalized.x
50
+ );
51
+
52
+ // If point is outside the light beam width, no illumination
53
+ if (perpDistance > this.width / 2) {
54
+ return 0;
55
+ }
56
+
57
+ // Calculate intensity based on distance within the beam
58
+ const widthFactor = 1 - perpDistance / (this.width / 2);
59
+
60
+ // Optional: Add distance attenuation
61
+ const maxDistance = 1000; // Maximum effective distance
62
+ const distanceFactor = Math.max(0, 1 - distance / maxDistance);
63
+
64
+ return this.intensity * widthFactor * distanceFactor;
65
+ }
66
+ }
67
+
68
+ export default DirectionalLight;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @class PointLight
3
+ * @param {Vector2} position - The position of the light
4
+ * @param {Color} color - The color of the light
5
+ * @param {number} intensity - The intensity of the light
6
+ * @param {number} radius - The radius of the light
7
+ */
8
+ class PointLight {
9
+ constructor(position, color, intensity, radius) {
10
+ this.position = position; // Vector2
11
+ this.color = color; // Color
12
+ this.intensity = intensity; // Number
13
+ this.radius = radius; // Number
14
+ }
15
+ }
16
+
17
+ export default PointLight;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * @class AudioManager
3
+ * @description Manages the audio for the game
4
+ */
5
+ class AudioManager {
6
+ constructor() {
7
+ this.sounds = new Map();
8
+ }
9
+
10
+ /**
11
+ * @method add
12
+ * @description Adds an audio file to the manager
13
+ * @param {string} path - The path to the audio file
14
+ * @param {string} name - The name of the audio file
15
+ */
16
+ add(path, name) {
17
+ if (!this.sounds.has(name)) {
18
+ const audio = new Audio(path);
19
+ this.sounds.set(name, {
20
+ audio,
21
+ id: Math.random().toString(36).substring(7),
22
+ playPromise: null,
23
+ });
24
+ return true;
25
+ }
26
+ return false;
27
+ }
28
+
29
+ /**
30
+ * @method remove
31
+ * @description Removes an audio file from the manager
32
+ * @param {string} name - The name of the audio file
33
+ */
34
+ remove(name) {
35
+ return this.sounds.delete(name);
36
+ }
37
+
38
+ /**
39
+ * @method play
40
+ * @description Plays an audio file
41
+ * @param {string} name - The name of the audio file
42
+ */
43
+ async play(name) {
44
+ const sound = this.sounds.get(name);
45
+ if (sound) {
46
+ try {
47
+ // Wait for any existing play promise to resolve before starting a new one
48
+ if (sound.playPromise) {
49
+ await sound.playPromise;
50
+ }
51
+
52
+ sound.audio.pause();
53
+ sound.audio.currentTime = 0;
54
+
55
+ // Store the play promise to handle future conflicts
56
+ sound.playPromise = sound.audio.play();
57
+ await sound.playPromise;
58
+
59
+ // Clear the promise once it's resolved
60
+ sound.playPromise = null;
61
+ return true;
62
+ } catch (error) {
63
+ // Handle the interrupt error gracefully
64
+ console.warn(`Audio play interrupted for ${name}:`, error);
65
+ sound.playPromise = null;
66
+ return false;
67
+ }
68
+ }
69
+ return false;
70
+ }
71
+
72
+ /**
73
+ * @method stop
74
+ * @description Stops an audio file
75
+ * @param {string} name - The name of the audio file
76
+ */
77
+ async stop(name) {
78
+ const sound = this.sounds.get(name);
79
+ if (sound) {
80
+ try {
81
+ // Wait for any existing play promise to resolve before stopping
82
+ if (sound.playPromise) {
83
+ await sound.playPromise;
84
+ }
85
+
86
+ sound.audio.pause();
87
+ sound.audio.currentTime = 0;
88
+ sound.playPromise = null;
89
+ return true;
90
+ } catch (error) {
91
+ // Handle any errors gracefully
92
+ console.warn(`Audio stop interrupted for ${name}:`, error);
93
+ sound.audio.pause();
94
+ sound.audio.currentTime = 0;
95
+ sound.playPromise = null;
96
+ return false;
97
+ }
98
+ }
99
+ return false;
100
+ }
101
+
102
+ /**
103
+ * @method stopAll
104
+ * @description Stops all audio files
105
+ */
106
+ async stopAll() {
107
+ const stopPromises = Array.from(this.sounds.values()).map(async (sound) => {
108
+ try {
109
+ if (sound.playPromise) {
110
+ await sound.playPromise;
111
+ }
112
+ sound.audio.pause();
113
+ sound.audio.currentTime = 0;
114
+ sound.playPromise = null;
115
+ } catch (error) {
116
+ console.warn("Audio stop interrupted:", error);
117
+ sound.audio.pause();
118
+ sound.audio.currentTime = 0;
119
+ sound.playPromise = null;
120
+ }
121
+ });
122
+
123
+ await Promise.all(stopPromises);
124
+ }
125
+
126
+ /**
127
+ * @method playExclusive
128
+ * @description Plays an audio file after stopping all other audio files
129
+ * @param {string} name - The name of the audio file
130
+ */
131
+ async playExclusive(name) {
132
+ await this.stopAll();
133
+ return this.play(name);
134
+ }
135
+
136
+ /**
137
+ * @method getSound
138
+ * @description Returns an audio file from the manager
139
+ * @param {string} name - The name of the audio file
140
+ */
141
+ getSound(name) {
142
+ return this.sounds.get(name);
143
+ }
144
+ }
145
+
146
+ export default AudioManager;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @class CameraManager
3
+ * @description Manages the camera for the game
4
+ */
5
+ class CameraManager {
6
+ static camera = null;
7
+ static lastPosition = null;
8
+
9
+ /**
10
+ * @method setLastPosition
11
+ * @description Sets the last position of the camera
12
+ * @param {Vector2} position - The position to set
13
+ */
14
+ static setLastPosition(position) {
15
+ this.lastPosition = position;
16
+ }
17
+
18
+ /**
19
+ * @method getLastPosition
20
+ * @description Returns the last position of the camera
21
+ * @returns {Vector2} - The last position of the camera
22
+ */
23
+ static getLastPosition() {
24
+ if (this.lastPosition) {
25
+ return this.lastPosition;
26
+ } else {
27
+ console.warn("Last position is not set.");
28
+ return null;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * @method setCamera
34
+ * @description Sets the camera for the game
35
+ * @param {Camera} camera - The camera to set
36
+ */
37
+ static setCamera(camera) {
38
+ if (camera) {
39
+ this.camera = camera;
40
+ } else {
41
+ console.warn("Attempted to set camera to null or undefined.");
42
+ }
43
+ }
44
+
45
+ /**
46
+ * @method getCamera
47
+ * @description Returns the camera for the game
48
+ * @returns {Camera} - The camera for the game
49
+ */
50
+ static getCamera() {
51
+ if (this.camera) {
52
+ return this.camera;
53
+ } else {
54
+ console.warn("Camera is not set.");
55
+ return null;
56
+ }
57
+ }
58
+ }
59
+
60
+ export default CameraManager;