A GameObject is something in the game, such as a sign, an enemy or a pickup. A component holds one piece of its state, such as position or visibility. A system processes objects with the components it needs. The engine runs the built-in rendering and physics systems; you call your own system from an update callback.
Run the demo · View the source
Select Move sign. The board and marker move together. Select Toggle marker. The sign stays visible while the marker disappears.
Keep the <head> from the first scene. Replace the contents of <body> with the following:
<div id="game" style="width: 360px; max-width: 100%; height: 220px"></div>
<button id="move">Move sign</button>
<button id="toggle">Toggle marker</button>
<p id="status" role="status"></p>
Save this as main.js. Follow pin from its creation, through pin.addComponent(marker), to the call to markers.update(Engine.objects) on each frame.
import { Circle, Component, Engine, Group, Rectangle, System, Vector2, VisibilityComponent } from 'dino-ge';
class Marker extends Component {
visible = true;
}
class MarkerSystem extends System {
update(objects) {
for (const object of objects) {
const marker = object.getComponent(Marker);
if (marker) object.getComponent(VisibilityComponent).visible = marker.visible;
}
}
}
const markers = new MarkerSystem();
const status = document.querySelector('#status');
new Engine({
onLoad() {
const sign = new Group('sign');
sign.transform.position.set(32, 70);
const board = new Rectangle({ position: new Vector2(0, 0), width: 84, height: 40, colour: '#578281' });
const pin = new Circle({ position: new Vector2(35, -22), radius: 8, colour: '#ffc46f', zIndex: 1 });
sign.transform.addChild(board.transform);
sign.transform.addChild(pin.transform);
const marker = new Marker();
pin.addComponent(marker);
sign.on('toggle-marker', () => {
marker.visible = !marker.visible;
status.textContent = marker.visible ? 'Marker visible' : 'Marker hidden';
});
document.querySelector('#toggle').addEventListener('click', () => sign.emit('toggle-marker'));
document.querySelector('#move').addEventListener('click', () => {
sign.transform.position.x = sign.transform.position.x === 32 ? 122 : 32;
status.textContent = `Sign world x: ${sign.transform.position.x}`;
});
},
update() {
markers.update(Engine.objects);
},
}, { containerId: 'game', width: '100%', height: '100%', backgroundColour: '#183035' });
This example has no active scene, so built-in objects register in Engine.objects. If you use a scene, pass its objects set to the system instead. The system ignores objects without Marker and copies the marker’s state to the circle’s built-in VisibilityComponent.
The HTML button listens for a browser click and calls sign.emit('toggle-marker'). The sign’s sign.on('toggle-marker', ...) handler changes the component’s data, which the system applies on the next update. To remove a listener with object.off(), pass the same function you registered. The static Engine.on(), Engine.emit() and Engine.off() methods handle game-wide events.
The sign is a Group, a non-drawing object with a transform. sign.transform.addChild(pin.transform) makes the pin’s position relative to the sign. The pin’s local position stays the same when the sign moves. Its world position changes. Positions, scales and offsets use Vector2. Rotation is in radians.
Tags help identify objects, bounds describe their extent and visibility determines whether they draw. Built-in objects configure the components they need, so you can begin with Sprite, Rectangle or Circle and add capabilities as needed.
Change the board’s new Vector2(0, 0) to new Vector2(10, 0) and reload. The board moves 10 pixels to the right of the sign’s origin, but the pin’s local offset stays the same.
GameObject · Group · Component · System · Transform · Vector2