Scenes#
Scenes represents a self-contained game world.
A scene groups the entities, systems and assets required for a particular part of the game and manages their runtime. This can be a menu or a level within your game.
Scenes own their runtime state independently. Entities and systems belonging to one scene do not automatically carry over to another. This keeps each scene self-contained and allows GameKit to initialize and dispose its resources as a single unit.
Creating a Scene#
Create a scene using the createScene function as shown below:
import { createScene } from "gamekit-engine/core/scene";
const scene = createScene({
name: "my-scene",
assets: [
{ src: "player.png", type: "image" },
{ src: "hit.mp3", type: "audio" },
],
entities: {
"player": player,
"eneny0" enemy
},
systems: [
damageController,
enemyAI,
/* ... */
],
});
Let's go through the above step-by-step:
- We've created a scene called "my-scene"
- The scene declares two assets that should be downloaded and cached when it starts
- The scene starts with a player and enemy entities labeled "player" and "enemy0" respectively
- The scene uses the
damageControllerandenemyAIsystems
Below is a table of all createScene options:
| Argument | Desription | Required |
|---|---|---|
name |
Name of the system | Yes |
entities |
Map of entities the scene starts with | Yes |
systems |
List of systems which operate in the scene | Yes |
assets |
List of asset files to download and cache when the scene starts | No |
ui |
User interface object of the scene | No |
Entity Management#
During runtime, entities can be dynamically added to the scene. Likewise, entities can also be destroyed and removed from the scene.
Use addEntity and removeEntity to add and remove entities respectively
import { addEntity, removeEntity } from "gamekit-engine/core/scene";
const spawnSystem = createSystem({
name: "spawn-system",
update() {
if (/* Some condition is met */) {
addEntity(/* Entity instance */);
}
}
});
const cleanupSystem = createSystem({
name: "cleanup-system",
update({ entities }) {
for (const entity of entities) {
if (/* Some condition is met */) {
removeEntity(entity);
}
}
}
});
Asset Management#
Certain components require asset files to operate. The $sprite component requires a path to an
image file, while the $audioSource component required a path to an audio file.
import { $sprite, $audioSource } from "gamekit-engine/core/prefabs";
const sprite = $sprite({ src: "path-to-image-file" })
const audioSource = $audioSource({ src: "path-to-audio-file" })
Asset paths are always relative to the
src/assetsproject directory.
These asset paths must be declared with the scene. During initialization, the scene downloads and caches all asset files. I prefer this approach over loading assets on-demand.
Since GameKit is a web engine all browser-supported image and audio formats work out of the box.
Task Scheduling#
You can schedule an arbitrary task to execute at the end of the current frame or after some time has
passed with the scheduleTask function. This functions a lot like setTimeout, but only executes
during the scene's flush phase.
import { scheduleTask } from "gamekit-engine/core/scene";
const timeout1 = scheduleTask(() => { /* Do something */ }); // Executes at the end of the current frame
const timeout2 = scheduleTask(2000, () => { /* Do something */ }); // Executes after 2s
To cancel a scheduled task before it executes, use the cancelTimeout function.
import { cancelTimeout } from "gamekit-engine/utils/timeout";
const timeout = scheduleTask(2000, () => {
});
cancelTimeout(timeout);
Transitions#
Transitions animate a value from 0 to 1 and allow you to perform an action based on its current value.
Create a transition using the beginTransition function as shown below:
import { beginTransition } from "gamekit-engine/core/scene";
import { beginTransition } from "gamekit-engine/utils/transition";
const transition = beginTransition(2000, {
onvalue: (val) => { /* Perform action */ }
});
In the above example, we created a transition that completes in 2s.
Below is a table of all beginTransition options:
| Argument | Desription | Required |
|---|---|---|
mode |
Determines how the transition behaves when it reaches its end. Possible values are none, restart, reverse. Defaults to none |
No |
curve |
Custom function which changes how the transition animates its value. Possible functions are LINEAR, EASE_IN_SINE, EASE_OUT_SINE, EASE_IN_OUT_SINE, EASE_IN_QUAD, EASE_OUT_QUAD, EASE_IN_OUT_QUAD, EASE_IN_CUBIC, EASE_OUT_CUBIC, EASE_IN_OUT_CUBIC, EASE_IN_EXPO, EASE_OUT_EXPO, EASE_IN_OUT_EXPO, EASE_IN_BACK, EASE_OUT_BACK, EASE_IN_OUT_BACK, EASE_IN_ELASTIC, EASE_OUT_ELASTIC, EASE_IN_OUT_ELASTIC, EASE_OUT_BOUNCE, EASE_IN_BOUNCE, EASE_IN_OUT_BOUNCE. Defaults to LINEAR |
No |
onvalue |
Called with the transition value anytime it changes | No |
onstart |
Called when the transition starts | No |
onpause |
Called when the transition is paused | No |
onstop |
Called when the transition is stopped | No |
onrestart |
Called when the transition restarts | No |
onreverse |
Called when the transition is reversed | No |
Next Steps#
In the next guide, we'll explore GameKit's input system.