Skip to content

Commit

Permalink
Handle special cases for scene saving
Browse files Browse the repository at this point in the history
Wait until scene is loaded before saving or entering game mode. Also saving in game mode is not allowed.
  • Loading branch information
adamsol committed Aug 3, 2019
1 parent 89328ea commit 5c12d4a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
16 changes: 13 additions & 3 deletions src/core/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ function Game()

Game.prototype.initialize = function()
{
if (this.running || !scene.ready) {
return;
}
this.running = true;

if (EDITOR) {
this.original_scene = scene.export();
}
Expand Down Expand Up @@ -80,8 +85,6 @@ Game.prototype.initialize = function()
layout.openView(GameRendererView, parent);
}
}

this.running = true;
}

Game.prototype.update = function(dt)
Expand All @@ -102,6 +105,10 @@ Game.prototype.update = function(dt)

Game.prototype.stop = function()
{
if (!this.running) {
return;
}

let stack = null;
for (let game of layout.findViews(GameRendererView)) {
stack = game.getTabHeader();
Expand All @@ -112,7 +119,9 @@ Game.prototype.stop = function()
}
}

scene = Scene.import(this.original_scene);
scene = new Scene();
scene.import(this.original_scene);

for (let view of layout.findViews(SceneRendererView, SceneHierarchyView)) {
view.refresh();
}
Expand All @@ -128,6 +137,7 @@ Game.prototype.onKeyDown = function(event)
} else {
this.stop();
}
return;
}

if (!this.running) {
Expand Down
19 changes: 13 additions & 6 deletions src/core/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Scene.prototype.getSelection = function()

Scene.prototype.setSelection = function(ids)
{
if (!EDITOR) {
return;
}

this.selection.ids = ids;
let actors = this.getSelection();

Expand Down Expand Up @@ -90,17 +94,20 @@ Scene.prototype.export = function()
return json;
}

Scene.import = function(json)
Scene.prototype.import = async function(json)
{
let scene = new Scene();
this.ready = false;
if (json.name) {
scene.name = json.name;
this.name = json.name;
}
let promises = [];
for (let obj of json.children) {
Actor.import(obj, scene);
promises.push(Actor.import(obj, this));
}
scene.setSelection([]);
return scene;
this.setSelection([]);
return $.when(...promises).then(() => {
this.ready = true;
});
}

let scene = new Scene();
9 changes: 7 additions & 2 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const VERSION = '0.3.0';
const electron = require('electron');
const dialog = electron.remote.dialog;

let scene_path = null;
let scene_path;

async function newScene()
{
Expand All @@ -15,6 +15,7 @@ async function newScene()
for (let view of layout.findViews(SceneRendererView, SceneHierarchyView)) {
view.refresh();
}
scene.ready = true;
scene_path = null;
}

Expand All @@ -31,7 +32,8 @@ function loadScene(file_path)
fs.readFile(file_path, (error, content) => {
try {
let json = JSON.parse(content);
scene = Scene.import(json);
scene = new Scene();
scene.import(json);
scene_path = file_path;
for (let view of layout.findViews(SceneRendererView, SceneHierarchyView)) {
view.refresh();
Expand All @@ -46,6 +48,9 @@ function loadScene(file_path)

function saveScene(file_path)
{
if (!scene.ready || game.running) {
return;
}
if (!file_path) {
dialog.showSaveDialog({filters: [{name: 'Scene', extensions: ['scene']}]}, file => {
if (!file) {
Expand Down
22 changes: 8 additions & 14 deletions src/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,12 @@ function start()
game.initialize();
}

$(() => {
file_path = 'data/World.scene';
fs.readFile(file_path, (error, content) => {
try {
let json = JSON.parse(content);
let promises = [];
for (let obj of json.children) {
promises.push(Actor.import(obj, scene));
}
$.when(...promises).then(start);
} catch (error) {
console.error(file_path, error);
}
});
file_path = 'data/World.scene';
fs.readFile(file_path, (error, content) => {
try {
let json = JSON.parse(content);
scene.import(json).then(start);
} catch (error) {
console.error(file_path, error);
}
});

0 comments on commit 5c12d4a

Please sign in to comment.