Skip to content

Commit

Permalink
Layer, Renderer, and Scene system revamps.
Browse files Browse the repository at this point in the history
  • Loading branch information
NotTimTam committed Sep 28, 2024
1 parent bf39341 commit 69643fd
Show file tree
Hide file tree
Showing 25 changed files with 229 additions and 164 deletions.
4 changes: 3 additions & 1 deletion behaviors/ScrollTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class ScrollTo extends Behavior {
},
scene: {
camera,
renderer: { width: screenWidth, height: screenHeight },
runtime: {
renderer: { width: screenWidth, height: screenHeight },
},
},
} = this;

Expand Down
4 changes: 1 addition & 3 deletions behaviors/TopDownMovement.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ class TopDownMovement extends Behavior {
height,
origin: [oX, oY],
},
scene: {
renderer: { layerManager },
},
scene: { layerManager },
} = this;

if (width <= 1 && height <= 1) {
Expand Down
6 changes: 4 additions & 2 deletions core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class Core {

this.scene = scene;

this.runtime = scene.runtime;

this.id = crypto.randomUUID();
}

get runtime() {
return this.scene.runtime;
}
}

export default Core;
57 changes: 33 additions & 24 deletions core/GameObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class GameObject extends Core {
* @param {Scene} scene The scene this Object is a part of.
* @param {number} x This entity's x-coordinate.
* @param {number} y This entity's y-coordinate.
*@param {string} layer The label of the layer to start the `Area` on.
*/
constructor(scene, x = 0, y = 0) {
constructor(scene, x = 0, y = 0, layer) {
super(scene);

if (typeof x !== "number")
Expand All @@ -32,6 +33,8 @@ class GameObject extends Core {
this.__rawRenderable = new Pixel({ value: "#", color: "magenta" });

this.behaviors = [];

if (layer) this.layer = layer;
}

/**
Expand Down Expand Up @@ -96,6 +99,17 @@ class GameObject extends Core {
return Math.round(this.__rawY);
}

/**
* Set the game object's y-coordinate.
*/
set y(n) {
if (typeof n !== "number")
throw new Error(
"Entity y-coordinate value must be of type 'number'."
);
this.__rawY = n;
}

/**
* Get the width of this `GameObject`'s renderable.
*/
Expand All @@ -117,30 +131,13 @@ class GameObject extends Core {
return (this.renderable && this.renderable.origin) || [0, 0];
}

/**
* Set the game object's y-coordinate.
*/
set y(n) {
if (typeof n !== "number")
throw new Error(
"Entity y-coordinate value must be of type 'number'."
);
this.__rawY = n;
}

/**
* Get the `GameObject`'s current layer.
*/
get layer() {
const {
scene: {
renderer: {
layerManager: { layers },
},
},
} = this;

return layers.find(({ gameObjects }) => gameObjects.includes(this));
return this.scene.layerManager.layers.find(({ gameObjects }) =>
gameObjects.includes(this)
);
}

/**
Expand Down Expand Up @@ -170,9 +167,7 @@ class GameObject extends Core {

const {
scene: {
renderer: {
layerManager: { layers },
},
layerManager: { layers },
},
} = this;

Expand Down Expand Up @@ -240,6 +235,20 @@ class GameObject extends Core {
? item !== this
: item.gameObject !== this
);

/**
* Delete this `GameObject`.
*
* **NOTE:** JavaScript has an automatic garbage collector, which means as long as an object is not referenced anywhere, it will be removed from memory.
* This method will remove references to the object from engine-created runtime objects. Custom objects or variables that reference this object must stop referencing it before it is fully removed from memory.
*
*
* At minimum, this functions behaviors and tick methods will stop when `GameObject.delete()` is executed. Unless they are called from somewhere other than its parent `Layer`.
*/
delete() {
if (this.layer) this.layer = undefined;
delete this;
}
}

export default GameObject;
17 changes: 16 additions & 1 deletion dist/bundle.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1592,8 +1592,9 @@ class GameObject extends Core {
* @param {Scene} scene The scene this Object is a part of.
* @param {number} x This entity's x-coordinate.
* @param {number} y This entity's y-coordinate.
* @param {string} layer The label of the layer to start the `GameObject` on.
*/
constructor(scene, x = 0, y = 0) {
constructor(scene, x = 0, y = 0, layer) {
super(scene);

if (typeof x !== "number")
Expand Down Expand Up @@ -1820,6 +1821,20 @@ class GameObject extends Core {
? item !== this
: item.gameObject !== this
);

/**
* Delete this `GameObject`.
*
* **NOTE:** JavaScript has an automatic garbage collector, which means as long as an object is not referenced anywhere, it will be removed from memory.
* This method will remove references to the object from engine-created runtime objects. Custom objects or variables that reference this object must stop referencing it before it is fully removed from memory.
*
*
* At minimum, this functions behaviors and tick methods will stop when `GameObject.delete()` is executed. Unless they are called from somewhere other than its parent `Layer`.
*/
delete() {
if (this.layer) this.layer = undefined;
delete this;
}
}

class Frame {
Expand Down
17 changes: 16 additions & 1 deletion dist/bundle.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1588,8 +1588,9 @@ class GameObject extends Core {
* @param {Scene} scene The scene this Object is a part of.
* @param {number} x This entity's x-coordinate.
* @param {number} y This entity's y-coordinate.
* @param {string} layer The label of the layer to start the `GameObject` on.
*/
constructor(scene, x = 0, y = 0) {
constructor(scene, x = 0, y = 0, layer) {
super(scene);

if (typeof x !== "number")
Expand Down Expand Up @@ -1816,6 +1817,20 @@ class GameObject extends Core {
? item !== this
: item.gameObject !== this
);

/**
* Delete this `GameObject`.
*
* **NOTE:** JavaScript has an automatic garbage collector, which means as long as an object is not referenced anywhere, it will be removed from memory.
* This method will remove references to the object from engine-created runtime objects. Custom objects or variables that reference this object must stop referencing it before it is fully removed from memory.
*
*
* At minimum, this functions behaviors and tick methods will stop when `GameObject.delete()` is executed. Unless they are called from somewhere other than its parent `Layer`.
*/
delete() {
if (this.layer) this.layer = undefined;
delete this;
}
}

class Frame {
Expand Down
10 changes: 5 additions & 5 deletions documentation/learn/RUNTIME.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ Initializes the game state using the provided configuration object.

### `paused`

A `boolean` that indicates whether or not this runtime is currently paused. If `true`, this `Runtime`'s `scene.renderer.layerManager.layers` do not run their object's `__onTick` methods.
A `boolean` that indicates whether or not this runtime is currently paused. If `true`, this `Runtime`'s `scene.layerManager.layers` do not run their object's `__onTick` methods.

---
### `renderer`

## Methods
An instance of `Renderer`.

### `get renderer()`
---

Get the renderer in the current scene.
## Methods

### `get webGLSupported()`

Expand Down
4 changes: 2 additions & 2 deletions documentation/learn/core/GAMEOBJECT.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ However, when extending the `GameObject` class, you can override the getter to r

```js
class MyObject extends GameObject() {
constructor(scene, x, y) {
super(scene, x, y);
constructor(scene, x, y, layer) {
super(scene, x, y, layer);

new Animate(scene, this, true, myAnimateConfigObject);
}
Expand Down
16 changes: 4 additions & 12 deletions documentation/learn/engine/CAMERA.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ The scene contains variable layers and compiles them into one frame to render to
## Constructor

```javascript
new Camera(renderer);
new Camera(scene);
```

### Arguments

- `renderer` &mdash; `Renderer` The `Renderer` this `Camera` is a part of.

---

## Properties

### `renderable`

Returns undefined.
- `scene` &mdash; `Scene` The `Scene` this `Camera` is a part of.

---

Expand All @@ -36,5 +28,5 @@ Check if a bounding box is on screen.
- `y` &mdash; `number` The y-coordinate to check.
- `width` &mdash; `number` The width to check.
- `height` &mdash; `number` The height to check.
- `parallaxX` &mdash; `number` Optional parallax x-value. (0-1)
- `parallaxY` &mdash; `number` Optional parallax y-value. (0-1)
- `parallaxX` &mdash; `number` Optional parallax x-value.
- `parallaxY` &mdash; `number` Optional parallax y-value.
4 changes: 2 additions & 2 deletions documentation/learn/engine/LAYERMANAGER.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ The layer manager contains variable layers and compiles them into one frame to r
## Constructor

```javascript
new LayerManager(renderer, layers);
new LayerManager(scene, layers);
```

### Arguments

- `renderer` &mdash; `Renderer` The main runtime's renderer object.
- `scene` &mdash; `Scene` The scene this layerManager is for.
- `layers` &mdash; `Array<Object>` The layer configuration objects.

---
Expand Down
5 changes: 2 additions & 3 deletions documentation/learn/engine/RENDERER.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ Handles rendering the game using **2D Context**.

## Constructor

### `constructor(scene, layers)`
### `constructor(runtime)`

Creates an instance of the `Renderer`.

- **Parameters**
- `scene` {Scene} - The scene this Object is a part of.
- `layers` {Array<Object>} - The layer configuration objects to pass to this `Renderer` instance's `LayerManager` instance.
- `runtime` {Runtime} - The current `Runtime` instance.

## Properties

Expand Down
8 changes: 4 additions & 4 deletions documentation/learn/engine/SCENE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ A scene is a level, screen, or world that can be loaded at any point during runt
- `config.layers[].label` &mdash; `string` The layer's label.
- `config.layers[].parallax` &mdash; `Array<number>` Optional parallax data, where the format is [integer, integer]. (`[1, 1]` is 100% parallax, `[0, 0]` is 0% parallax)
- `config.layers[].gameObjectConstructors` &mdash; `Array<function>` Optional callback functions that return `GameObject`s for this layer.
- `config.layers[].gameObjectConstructors[]` &mdash; `function` A callback function, passed this `Scene` as an argument, that return an instance of `GameObject`.
- `config.layers[].gameObjectConstructors[]` &mdash; `function` A callback function, passed this `Scene` as an argument, that return an instance of `GameObject`. This `GameObject` is automatically added to the constructed `Layer`.
- `config.onLoad` &mdash; `function` A callback (passed this `Scene` as an argument) that runs when the `Scene` has finished loading.
- `config.onTick` &mdash; `function` A callback (passed this `Scene` as an argument) that runs every frame that this `Scene` is loaded.

Expand All @@ -27,11 +27,11 @@ A scene is a level, screen, or world that can be loaded at any point during runt

### `layerManager`

Get the layer manager associated with this scene.
This scene's `LayerManager` instance.

#### Returns
### `camera`

- `LayerManager` The layer manager for the scene.
This scene's `Camera` instance.

---

Expand Down
Loading

0 comments on commit 69643fd

Please sign in to comment.