Skip to content

Commit

Permalink
scene states for camera
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyMakesThings committed Oct 9, 2024
1 parent 893fd25 commit bbd3d4f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
52 changes: 52 additions & 0 deletions src/engine/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class Camera extends EventEmitter {
* Interpolation data used by tw-interpolate.
*/
this.interpolationData = null;

/**
* Maps of various "scene states" for each scene in the runtime.
* @type {Object.<string, *>}
*/
this.sceneStates = {};

this.saveSceneState(this.runtime.scene);
}

/**
Expand Down Expand Up @@ -108,6 +116,50 @@ class Camera extends EventEmitter {
this.enabled = enabled;
}

/**
* Saves all current values to a scene state.
* @param {string} scene - the name of the scene.
* @returns {*} the scene state.
*/
saveSceneState (scene) {
if (!scene || !this.runtime.scenes[scene]) {

Check failure on line 125 in src/engine/camera.js

View workflow job for this annotation

GitHub Actions / build

Multiple spaces found before '||'
scene = this.runtime.scene;
}

this.sceneStates[scene] = {
x: this.x,
y: this.y,
direction: this.direction,
zoom: this.zoom,
enabled: this.enabled,

Check failure on line 134 in src/engine/camera.js

View workflow job for this annotation

GitHub Actions / build

Unexpected trailing comma
}

Check failure on line 135 in src/engine/camera.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
}

/**
* Loads all scene state data into the camera.
* @param {string} scene - the name of the scene.
* @returns {*}

Check failure on line 141 in src/engine/camera.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed
*/
loadSceneState (scene) {
if (!scene) {
scene = this.runtime.scene;
}

if (!this.runtime.scenes[scene]) return;

if (!this.sceneStates[scene]) {
// If we're here, the scene does exist but
// for whatever reason the sprite doesn't
// have a state for it.
this.saveSceneState(scene);
return;
}

Object.assign(this, this.sceneStates[scene]);

this.emitCameraUpdate();
}

/**
* Tell the renderer to update the rendered camera state.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3578,9 +3578,12 @@ class Runtime extends EventEmitter {

for (let i = 0; i < this.targets.length; i++) {
const target = this.targets[i];
target.saveSpriteSceneState(oldScene);
target.loadSpriteSceneState(scene);
target.saveSceneState(oldScene);
target.loadSceneState(scene);
}

this.camera.saveSceneState(oldScene);
this.camera.loadSceneState(scene);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/serialization/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1573,8 +1573,6 @@ const deserialize = async function (json, runtime, zip, isSingleSprite) {
}
}

console.log(json);

if (json.scene) {
runtime.scene = json.scene;
runtime.scenes = json.scenes;
Expand Down
10 changes: 5 additions & 5 deletions src/sprites/rendered-target.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class RenderedTarget extends Target {

this.interpolationData = null;

this.saveSpriteSceneState(this.runtime.scene);
this.saveSceneState(this.runtime.scene);
}

/**
Expand Down Expand Up @@ -1088,7 +1088,7 @@ class RenderedTarget extends Target {
* @param {string} scene - the name of the scene.
* @returns {*} the scene state.
*/
saveSpriteSceneState (scene) {
saveSceneState (scene) {
if (!scene || !this.runtime.scenes[scene]) {
scene = this.runtime.scene;
}
Expand All @@ -1104,11 +1104,11 @@ class RenderedTarget extends Target {
}

/**
* Loads all scene data into current sprite.
* Loads all scene state data into the sprite.
* @param {string} scene - the name of the scene.
* @returns {*}
*/
loadSpriteSceneState (scene) {
loadSceneState (scene) {
if (!scene) {
scene = this.runtime.scene;
}
Expand All @@ -1119,7 +1119,7 @@ class RenderedTarget extends Target {
// If we're here, the scene does exist but
// for whatever reason the sprite doesn't
// have a state for it.
this.saveSpriteSceneState(scene);
this.saveSceneState(scene);
return;
}

Expand Down

0 comments on commit bbd3d4f

Please sign in to comment.