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.
- package/README.md +968 -0
- package/dist/types/imports/BitmapText.d.ts +79 -0
- package/dist/types/imports/Color.d.ts +17 -0
- package/dist/types/imports/Drawable.d.ts +157 -0
- package/dist/types/imports/Emerald.d.ts +73 -0
- package/dist/types/imports/FPSCounter.d.ts +19 -0
- package/dist/types/imports/GLUtils.d.ts +6 -0
- package/dist/types/imports/Instance.d.ts +87 -0
- package/dist/types/imports/InstancedTexture.d.ts +230 -0
- package/dist/types/imports/Physics.d.ts +139 -0
- package/dist/types/imports/Scene.d.ts +39 -0
- package/dist/types/imports/Shaders.d.ts +4 -0
- package/dist/types/imports/Shapes.d.ts +25 -0
- package/dist/types/imports/Storage.d.ts +46 -0
- package/dist/types/imports/Texture.d.ts +19 -0
- package/dist/types/imports/Time.d.ts +22 -0
- package/dist/types/imports/Transform.d.ts +41 -0
- package/dist/types/imports/components/BoxCollider.d.ts +41 -0
- package/dist/types/imports/components/BoxColliderDebug.d.ts +19 -0
- package/dist/types/imports/components/CircleCollider.d.ts +39 -0
- package/dist/types/imports/components/CircleColliderDebug.d.ts +19 -0
- package/dist/types/imports/components/Collider.d.ts +53 -0
- package/dist/types/imports/components/GameObject.d.ts +72 -0
- package/dist/types/imports/components/RigidBody.d.ts +104 -0
- package/dist/types/imports/lights/DirectionalLight.d.ts +26 -0
- package/dist/types/imports/lights/PointLight.d.ts +18 -0
- package/dist/types/imports/managers/AudioManager.d.ts +60 -0
- package/dist/types/imports/managers/CameraManager.d.ts +35 -0
- package/dist/types/imports/managers/EventManager.d.ts +187 -0
- package/dist/types/imports/managers/GLManager.d.ts +47 -0
- package/dist/types/imports/managers/IDManager.d.ts +21 -0
- package/dist/types/imports/managers/SceneManager.d.ts +22 -0
- package/dist/types/imports/particlesystem/Particle.d.ts +42 -0
- package/dist/types/imports/particlesystem/ParticleSettings.d.ts +49 -0
- package/dist/types/imports/particlesystem/Particles.d.ts +71 -0
- package/dist/types/index.d.ts +32 -0
- package/imports/BitmapText.js +245 -0
- package/imports/Color.js +18 -0
- package/imports/Drawable.js +550 -0
- package/imports/Emerald.js +459 -0
- package/imports/FPSCounter.js +43 -0
- package/imports/GLUtils.js +67 -0
- package/imports/Instance.js +187 -0
- package/imports/InstancedTexture.js +856 -0
- package/imports/Physics.js +242 -0
- package/imports/Scene.js +79 -0
- package/imports/Shaders.js +165 -0
- package/imports/Shapes.js +129 -0
- package/imports/Storage.js +63 -0
- package/imports/Texture.js +67 -0
- package/imports/Time.js +28 -0
- package/imports/Transform.js +59 -0
- package/imports/components/BoxCollider.js +67 -0
- package/imports/components/BoxColliderDebug.js +38 -0
- package/imports/components/CircleCollider.js +67 -0
- package/imports/components/CircleColliderDebug.js +36 -0
- package/imports/components/Collider.js +51 -0
- package/imports/components/GameObject.js +160 -0
- package/imports/components/RigidBody.js +169 -0
- package/imports/lights/DirectionalLight.js +68 -0
- package/imports/lights/PointLight.js +17 -0
- package/imports/managers/AudioManager.js +146 -0
- package/imports/managers/CameraManager.js +60 -0
- package/imports/managers/EventManager.js +479 -0
- package/imports/managers/GLManager.js +65 -0
- package/imports/managers/IDManager.js +32 -0
- package/imports/managers/SceneManager.js +27 -0
- package/imports/managers/ShaderManager.js +133 -0
- package/imports/particlesystem/Particle.js +75 -0
- package/imports/particlesystem/ParticleSettings.js +49 -0
- package/imports/particlesystem/Particles.js +226 -0
- package/index.js +67 -0
- package/package.json +38 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { initVertexBuffer } from "./GLUtils.js";
|
|
2
|
+
import Drawable from "./Drawable.js";
|
|
3
|
+
import GLManager from "./managers/GLManager.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @class Texture
|
|
7
|
+
* @description Represents a texture component
|
|
8
|
+
* @param {string} texturePath - The path to the texture
|
|
9
|
+
* @param {number} frameWidth - The width of the frame
|
|
10
|
+
* @param {number} frameHeight - The height of the frame
|
|
11
|
+
* @param {number} framesPerRow - The number of frames per row
|
|
12
|
+
* @param {number} totalFrames - The total number of frames
|
|
13
|
+
* @param {number} animationSpeed - The speed of the animation
|
|
14
|
+
* @param {boolean} autoPlay - Whether the animation should play automatically
|
|
15
|
+
* @param {boolean} pixelart - Whether the texture is a pixel art texture
|
|
16
|
+
* @param {boolean} useLighting - Whether the texture should react to lighting
|
|
17
|
+
*/
|
|
18
|
+
class Texture extends Drawable {
|
|
19
|
+
constructor(
|
|
20
|
+
texturePath = {},
|
|
21
|
+
frameWidth = 0,
|
|
22
|
+
frameHeight = 0,
|
|
23
|
+
framesPerRow = 1,
|
|
24
|
+
totalFrames = 1,
|
|
25
|
+
animationSpeed = 1000,
|
|
26
|
+
autoPlay = true,
|
|
27
|
+
pixelart = true,
|
|
28
|
+
useLighting = true
|
|
29
|
+
) {
|
|
30
|
+
const vertices = [1.0, 1.0, -1.0, 1.0, 1.0, -1.0, -1.0, -1.0];
|
|
31
|
+
const verticesBuffer = initVertexBuffer(GLManager.getGL(), vertices);
|
|
32
|
+
const textureCoordinates = [
|
|
33
|
+
1.0,
|
|
34
|
+
1.0, // Top-right
|
|
35
|
+
0.0,
|
|
36
|
+
1.0, // Top-left
|
|
37
|
+
1.0,
|
|
38
|
+
0.0, // Bottom-right
|
|
39
|
+
0.0,
|
|
40
|
+
0.0, // Bottom-left
|
|
41
|
+
];
|
|
42
|
+
const texCoordBuffer = initVertexBuffer(
|
|
43
|
+
GLManager.getGL(),
|
|
44
|
+
textureCoordinates
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
super(
|
|
48
|
+
GLManager.getGL(),
|
|
49
|
+
GLManager.getProgramInfo(),
|
|
50
|
+
verticesBuffer,
|
|
51
|
+
texCoordBuffer,
|
|
52
|
+
vertices,
|
|
53
|
+
true,
|
|
54
|
+
texturePath,
|
|
55
|
+
frameWidth,
|
|
56
|
+
frameHeight,
|
|
57
|
+
framesPerRow,
|
|
58
|
+
totalFrames,
|
|
59
|
+
animationSpeed,
|
|
60
|
+
autoPlay,
|
|
61
|
+
pixelart,
|
|
62
|
+
useLighting
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default Texture;
|
package/imports/Time.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @class Time
|
|
3
|
+
* @description Represents the time
|
|
4
|
+
* @param {number} deltaTime - The delta time
|
|
5
|
+
*/
|
|
6
|
+
class Time {
|
|
7
|
+
static deltaTime = 0;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @method getDeltaTime
|
|
11
|
+
* @description Returns the delta time
|
|
12
|
+
* @returns {number} - The delta time
|
|
13
|
+
*/
|
|
14
|
+
static getDeltaTime() {
|
|
15
|
+
return this.deltaTime;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @method setDeltaTime
|
|
20
|
+
* @description Sets the delta time
|
|
21
|
+
* @param {number} deltaTime - The delta time
|
|
22
|
+
*/
|
|
23
|
+
static setDeltaTime(deltaTime) {
|
|
24
|
+
this.deltaTime = deltaTime;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default Time;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @class Transform
|
|
3
|
+
* @description Represents a transform
|
|
4
|
+
* @param {Vector3} position - The position of the transform
|
|
5
|
+
* @param {number} rotation - The rotation of the transform
|
|
6
|
+
* @param {Vector2} scale - The scale of the transform
|
|
7
|
+
*/
|
|
8
|
+
class Transform {
|
|
9
|
+
constructor(position, rotation, scale) {
|
|
10
|
+
this.position = position;
|
|
11
|
+
this.rotation = rotation;
|
|
12
|
+
this.scale = scale;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @method equals
|
|
17
|
+
* @description Checks if the transform is equal to another transform
|
|
18
|
+
* @param {Transform} other - The other transform
|
|
19
|
+
* @returns {boolean} - True if the transform is equal to the other transform
|
|
20
|
+
*/
|
|
21
|
+
equals(other) {
|
|
22
|
+
if (!other) return false;
|
|
23
|
+
if (!(other instanceof Transform)) return false;
|
|
24
|
+
return (
|
|
25
|
+
this.position.equals(other.position) &&
|
|
26
|
+
this.rotation === other.rotation &&
|
|
27
|
+
this.scale.equals(other.scale)
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @method getPosition
|
|
33
|
+
* @description Returns the position of the transform
|
|
34
|
+
* @returns {Vector3} - The position of the transform
|
|
35
|
+
*/
|
|
36
|
+
getPosition() {
|
|
37
|
+
return this.position;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @method getRotation
|
|
42
|
+
* @description Returns the rotation of the transform
|
|
43
|
+
* @returns {number} - The rotation of the transform
|
|
44
|
+
*/
|
|
45
|
+
getRotation() {
|
|
46
|
+
return this.rotation;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @method getScale
|
|
51
|
+
* @description Returns the scale of the transform
|
|
52
|
+
* @returns {Vector2} - The scale of the transform
|
|
53
|
+
*/
|
|
54
|
+
getScale() {
|
|
55
|
+
return this.scale;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default Transform;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as planck from "planck";
|
|
2
|
+
import BoxColliderDebug from "./BoxColliderDebug.js";
|
|
3
|
+
import SceneManager from "../managers/SceneManager.js";
|
|
4
|
+
import Collider from "./Collider.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class BoxCollider
|
|
8
|
+
* @extends Collider
|
|
9
|
+
* @param {Rigidbody} rigidbody - The rigidbody to attach the collider to
|
|
10
|
+
* @param {Vector2} fixtureSize - The size of the collider
|
|
11
|
+
* @param {number} density - The density of the collider
|
|
12
|
+
* @param {number} friction - The friction of the collider
|
|
13
|
+
* @param {number} restitution - The restitution of the collider
|
|
14
|
+
* @param {boolean} isSensor - Whether the collider is a sensor
|
|
15
|
+
* @param {GameObject} parentObject - The parent object of the collider
|
|
16
|
+
*/
|
|
17
|
+
class BoxCollider extends Collider {
|
|
18
|
+
constructor(
|
|
19
|
+
rigidbody,
|
|
20
|
+
fixtureSize,
|
|
21
|
+
density,
|
|
22
|
+
friction,
|
|
23
|
+
restitution,
|
|
24
|
+
isSensor = false,
|
|
25
|
+
parentObject = null
|
|
26
|
+
) {
|
|
27
|
+
super(rigidbody, isSensor, parentObject);
|
|
28
|
+
this.collider = rigidbody
|
|
29
|
+
.getBody()
|
|
30
|
+
.createFixture(new planck.Box(fixtureSize.x, fixtureSize.y), {
|
|
31
|
+
density: density,
|
|
32
|
+
friction: friction,
|
|
33
|
+
restitution: restitution,
|
|
34
|
+
isSensor: isSensor,
|
|
35
|
+
});
|
|
36
|
+
this.fixtureSize = fixtureSize;
|
|
37
|
+
this.rigidbody.setCollider(this);
|
|
38
|
+
this.name = "BoxCollider" + this.id;
|
|
39
|
+
this.debugShape = new BoxColliderDebug(this.rigidbody);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @method showDebugShape
|
|
44
|
+
* @description Shows the debug shape of the collider
|
|
45
|
+
*/
|
|
46
|
+
showDebugShape() {
|
|
47
|
+
SceneManager.getScene()?.add(this.debugShape.gameObject);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @method hideDebugShape
|
|
52
|
+
* @description Hides the debug shape of the collider
|
|
53
|
+
*/
|
|
54
|
+
hideDebugShape() {
|
|
55
|
+
SceneManager.getScene().remove(this.debugShape.gameObject);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @method getFixtureSize
|
|
60
|
+
* @description Returns the size of the collider
|
|
61
|
+
*/
|
|
62
|
+
getFixtureSize() {
|
|
63
|
+
return this.fixtureSize;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default BoxCollider;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import Color from "../Color.js";
|
|
2
|
+
import { Vector2, Vector3 } from "../Physics.js";
|
|
3
|
+
import { Square2D } from "../Shapes.js";
|
|
4
|
+
import GameObject from "./GameObject.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class BoxColliderDebug
|
|
8
|
+
* @param {Rigidbody} rigidbody - The rigidbody to attach the collider to
|
|
9
|
+
* @param {Color} color - The color of the collider
|
|
10
|
+
*/
|
|
11
|
+
class BoxColliderDebug {
|
|
12
|
+
constructor(rigidbody, color = null) {
|
|
13
|
+
this.rigidbody = rigidbody;
|
|
14
|
+
this.physics = this.rigidbody.physics;
|
|
15
|
+
this.scale = this.physics.getScale();
|
|
16
|
+
this.color = color || new Color(255, 0, 0, 255);
|
|
17
|
+
this.gameObject = new GameObject(
|
|
18
|
+
"BoxColliderDebug",
|
|
19
|
+
new Vector3(
|
|
20
|
+
this.rigidbody.getBody().getPosition().x *
|
|
21
|
+
this.rigidbody.getPhysics().getScale(),
|
|
22
|
+
this.rigidbody.getBody().getPosition().y *
|
|
23
|
+
this.rigidbody.getPhysics().getScale(),
|
|
24
|
+
100
|
|
25
|
+
),
|
|
26
|
+
this.rigidbody.getAngle(),
|
|
27
|
+
new Vector2(
|
|
28
|
+
this.rigidbody.getCollider().getFixtureSize().x * this.scale,
|
|
29
|
+
this.rigidbody.getCollider().getFixtureSize().y * this.scale
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
this.gameObject.addComponent(new Square2D());
|
|
33
|
+
this.gameObject.getComponent(Square2D).setColor(this.color);
|
|
34
|
+
this.gameObject.setOpacity(0.3);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default BoxColliderDebug;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import * as planck from "planck";
|
|
2
|
+
import SceneManager from "../managers/SceneManager.js";
|
|
3
|
+
import CircleColliderDebug from "./CircleColliderDebug.js";
|
|
4
|
+
import Collider from "./Collider.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class CircleCollider
|
|
8
|
+
* @extends Collider
|
|
9
|
+
* @param {Rigidbody} rigidbody - The rigidbody to attach the collider to
|
|
10
|
+
* @param {number} radius - The radius of the collider
|
|
11
|
+
* @param {number} density - The density of the collider
|
|
12
|
+
* @param {number} friction - The friction of the collider
|
|
13
|
+
* @param {number} restitution - The restitution of the collider
|
|
14
|
+
* @param {boolean} isSensor - Whether the collider is a sensor
|
|
15
|
+
* @param {GameObject} parentObject - The parent object of the collider
|
|
16
|
+
*/
|
|
17
|
+
class CircleCollider extends Collider {
|
|
18
|
+
constructor(
|
|
19
|
+
rigidbody,
|
|
20
|
+
radius,
|
|
21
|
+
density,
|
|
22
|
+
friction,
|
|
23
|
+
restitution,
|
|
24
|
+
isSensor = false,
|
|
25
|
+
parentObject = null
|
|
26
|
+
) {
|
|
27
|
+
super(rigidbody, isSensor, parentObject);
|
|
28
|
+
this.collider = rigidbody
|
|
29
|
+
.getBody()
|
|
30
|
+
.createFixture(new planck.Circle(radius), {
|
|
31
|
+
density: density,
|
|
32
|
+
friction: friction,
|
|
33
|
+
restitution: restitution,
|
|
34
|
+
isSensor: isSensor,
|
|
35
|
+
});
|
|
36
|
+
this.radius = radius;
|
|
37
|
+
this.rigidbody.setCollider(this);
|
|
38
|
+
this.name = "CircleCollider" + this.id;
|
|
39
|
+
this.debugShape = new CircleColliderDebug(this.rigidbody);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @method showDebugShape
|
|
44
|
+
* @description Shows the debug shape of the collider
|
|
45
|
+
*/
|
|
46
|
+
showDebugShape() {
|
|
47
|
+
SceneManager.getScene()?.add(this.debugShape.gameObject);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @method hideDebugShape
|
|
52
|
+
* @description Hides the debug shape of the collider
|
|
53
|
+
*/
|
|
54
|
+
hideDebugShape() {
|
|
55
|
+
SceneManager.getScene()?.remove(this.debugShape.gameObject);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @method getRadius
|
|
60
|
+
* @description Returns the radius of the collider
|
|
61
|
+
*/
|
|
62
|
+
getRadius() {
|
|
63
|
+
return this.radius;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export default CircleCollider;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import Color from "../Color.js";
|
|
2
|
+
import { Vector2, Vector3 } from "../Physics.js";
|
|
3
|
+
import { Circle2D } from "../Shapes.js";
|
|
4
|
+
import GameObject from "./GameObject.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @class CircleColliderDebug
|
|
8
|
+
* @param {Rigidbody} rigidbody - The rigidbody to attach the collider to
|
|
9
|
+
* @param {Color} color - The color of the collider
|
|
10
|
+
*/
|
|
11
|
+
class CircleColliderDebug {
|
|
12
|
+
constructor(rigidbody, color = null) {
|
|
13
|
+
this.rigidbody = rigidbody;
|
|
14
|
+
this.physics = this.rigidbody.physics;
|
|
15
|
+
this.scale = this.physics.getScale();
|
|
16
|
+
this.color = color || new Color(255, 0, 0, 255);
|
|
17
|
+
this.gameObject = new GameObject(
|
|
18
|
+
"CircleColliderDebug",
|
|
19
|
+
new Vector3(
|
|
20
|
+
this.rigidbody.getBody().getPosition().x * this.scale,
|
|
21
|
+
this.rigidbody.getBody().getPosition().y * this.scale,
|
|
22
|
+
100
|
|
23
|
+
),
|
|
24
|
+
this.rigidbody.getAngle(),
|
|
25
|
+
new Vector2(
|
|
26
|
+
this.rigidbody.getCollider().getRadius() * this.scale,
|
|
27
|
+
this.rigidbody.getCollider().getRadius() * this.scale
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
this.gameObject.addComponent(new Circle2D());
|
|
31
|
+
this.gameObject.getComponent(Circle2D).setColor(this.color);
|
|
32
|
+
this.gameObject.setOpacity(0.3);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default CircleColliderDebug;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import IDManager from "../managers/IDManager.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @class Collider
|
|
5
|
+
* @param {Rigidbody} rigidbody - The rigidbody to attach the collider to
|
|
6
|
+
* @param {boolean} isSensor - Whether the collider is a sensor
|
|
7
|
+
* @param {GameObject} parentObject - The parent object of the collider
|
|
8
|
+
*/
|
|
9
|
+
class Collider {
|
|
10
|
+
constructor(rigidbody, isSensor = false, parentObject = null) {
|
|
11
|
+
this.rigidbody = rigidbody;
|
|
12
|
+
this.parentObject = parentObject;
|
|
13
|
+
this.isSensor = isSensor;
|
|
14
|
+
this.id = IDManager.generateUniqueID();
|
|
15
|
+
this.name = "Collider" + this.id;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @method getRigidBody
|
|
20
|
+
* @description Returns the rigidbody of the collider
|
|
21
|
+
*/
|
|
22
|
+
getRigidBody() {
|
|
23
|
+
return this.rigidbody;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @method getCollider
|
|
28
|
+
* @description Returns the collider
|
|
29
|
+
*/
|
|
30
|
+
getCollider() {
|
|
31
|
+
return this.collider;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @method setParent
|
|
36
|
+
* @description Sets the parent object of the collider
|
|
37
|
+
*/
|
|
38
|
+
setParent(parent) {
|
|
39
|
+
this.parentObject = parent;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @method getParent
|
|
44
|
+
* @description Returns the parent object of the collider
|
|
45
|
+
*/
|
|
46
|
+
getParent() {
|
|
47
|
+
return this.parentObject;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default Collider;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import IDManager from "../managers/IDManager.js";
|
|
2
|
+
import { Vector2, Vector3 } from "../Physics.js";
|
|
3
|
+
import Transform from "../Transform.js";
|
|
4
|
+
import RigidBody from "./RigidBody.js";
|
|
5
|
+
import Collider from "./Collider.js";
|
|
6
|
+
import Drawable from "../Drawable.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @class GameObject
|
|
10
|
+
* @param {string} name - The name of the game object
|
|
11
|
+
* @param {Vector3} position - The position of the game object
|
|
12
|
+
* @param {number} rotation - The rotation of the game object
|
|
13
|
+
* @param {Vector2} scale - The scale of the game object
|
|
14
|
+
*/
|
|
15
|
+
class GameObject {
|
|
16
|
+
constructor(
|
|
17
|
+
name,
|
|
18
|
+
position = new Vector3(0, 0, 0),
|
|
19
|
+
rotation = 0,
|
|
20
|
+
scale = new Vector2(1, 1)
|
|
21
|
+
) {
|
|
22
|
+
this.name = name;
|
|
23
|
+
this.components = [];
|
|
24
|
+
this.id = IDManager.generateUniqueID();
|
|
25
|
+
this.isActive = false;
|
|
26
|
+
this.transform = new Transform(position, rotation, scale);
|
|
27
|
+
this.opacity = 1.0; // 0..1 opacity multiplier for this GameObject
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @method addComponent
|
|
32
|
+
* @description Adds a component to the game object
|
|
33
|
+
* @param {Component} componentInstance - The component to add
|
|
34
|
+
*/
|
|
35
|
+
addComponent(componentInstance) {
|
|
36
|
+
const componentType = componentInstance.constructor;
|
|
37
|
+
if (this.components.some((comp) => comp instanceof componentType)) {
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Component ${componentType.name} already exists in ${this.name}`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
this.components.push(componentInstance);
|
|
43
|
+
componentInstance.setParent(this);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @method removeComponent
|
|
48
|
+
* @description Removes a component from the game object
|
|
49
|
+
* @param {Component} component - The component to remove
|
|
50
|
+
*/
|
|
51
|
+
removeComponent(component) {
|
|
52
|
+
if(!component || !this.components.includes(component)) {
|
|
53
|
+
throw new Error(`Component ${component.name} not found in ${this.name}`);
|
|
54
|
+
}
|
|
55
|
+
if (component instanceof RigidBody) {
|
|
56
|
+
component.destroy();
|
|
57
|
+
}
|
|
58
|
+
this.components = this.components.filter(
|
|
59
|
+
(comp) => comp.id !== component.id
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @method setIsActive
|
|
65
|
+
* @description Sets the active state of the game object
|
|
66
|
+
* @param {boolean} bool - The active state
|
|
67
|
+
*/
|
|
68
|
+
setIsActive(bool) {
|
|
69
|
+
this.isActive = bool;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @method setOpacity
|
|
74
|
+
* @description Sets the opacity of the game object (0..1)
|
|
75
|
+
* @param {number} value - The opacity value between 0 and 1
|
|
76
|
+
*/
|
|
77
|
+
setOpacity(value) {
|
|
78
|
+
const num = Number(value);
|
|
79
|
+
if (Number.isNaN(num)) return;
|
|
80
|
+
this.opacity = Math.max(0, Math.min(1, num));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @method getOpacity
|
|
85
|
+
* @description Gets the opacity of the game object
|
|
86
|
+
* @returns {number} - The opacity value between 0 and 1
|
|
87
|
+
*/
|
|
88
|
+
getOpacity() {
|
|
89
|
+
return this.opacity;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @method getComponent
|
|
94
|
+
* @description Returns a component from the game object
|
|
95
|
+
* @param {Component} componentType - The type of component to return
|
|
96
|
+
*/
|
|
97
|
+
getComponent(componentType) {
|
|
98
|
+
return this.components.find((comp) => comp instanceof componentType);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @method getRigidBodyAtPosition
|
|
103
|
+
* @description Returns the rigidbody at a position
|
|
104
|
+
* @param {Vector2} position - The position to check
|
|
105
|
+
*/
|
|
106
|
+
getRigidBodyAtPosition(position) {
|
|
107
|
+
for (let component of this.components) {
|
|
108
|
+
if (component instanceof RigidBody) {
|
|
109
|
+
if (
|
|
110
|
+
component.getPosition().x === position.x &&
|
|
111
|
+
component.getPosition().y === position.y
|
|
112
|
+
) {
|
|
113
|
+
return component;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @method draw
|
|
123
|
+
* @description Draws the game object
|
|
124
|
+
* @param {Matrix4} globalViewMatrix - The global view matrix
|
|
125
|
+
* @param {WebGLUniformLocation} uniformLocation - The uniform location
|
|
126
|
+
* @param {number} currentTime - The current time
|
|
127
|
+
*/
|
|
128
|
+
draw(globalViewMatrix, uniformLocation, currentTime) {
|
|
129
|
+
const drawable = this.getComponent(Drawable);
|
|
130
|
+
const rigidBody = this.getComponent(RigidBody);
|
|
131
|
+
const collider = this.getComponent(Collider);
|
|
132
|
+
if (rigidBody && rigidBody?.getType() === "dynamic") {
|
|
133
|
+
const updatedPos = rigidBody.getBody().getPosition();
|
|
134
|
+
const rigidBodyOffset = rigidBody.getOffset();
|
|
135
|
+
const physicsScale = rigidBody.getPhysics().getScale();
|
|
136
|
+
|
|
137
|
+
this.transform.position.x =
|
|
138
|
+
updatedPos.x * physicsScale - rigidBodyOffset.x;
|
|
139
|
+
this.transform.position.y =
|
|
140
|
+
updatedPos.y * physicsScale - rigidBodyOffset.y;
|
|
141
|
+
|
|
142
|
+
if (collider) {
|
|
143
|
+
collider.debugShape.gameObject.transform.position.x =
|
|
144
|
+
this.transform.position.x;
|
|
145
|
+
collider.debugShape.gameObject.transform.position.y =
|
|
146
|
+
this.transform.position.y;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (drawable) {
|
|
150
|
+
drawable.draw(
|
|
151
|
+
globalViewMatrix,
|
|
152
|
+
uniformLocation,
|
|
153
|
+
currentTime,
|
|
154
|
+
this.transform
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export default GameObject;
|