html/core/engine.js in gamefic-sdk-1.5.0 vs html/core/engine.js in gamefic-sdk-1.6.0
- old
+ new
@@ -1,75 +1,30 @@
var Gamefic = (function() {
+ var updateCallbacks = [];
var startCallbacks = [];
- var inputCallbacks = [];
- var finishCallbacks = [];
- var responseCallbacks = {};
- var lastInput = null;
- var lastPrompt = null;
- var getResponse = function(withOutput) {
- var r = {
- output: (withOutput ? Opal.GameficOpal.$static_user().$flush() : null),
- //state: Opal.GameficOpal.$static_plot().$scenes().$fetch(Opal.GameficOpal.$static_character().$scene()).$type(),
- state: Opal.GameficOpal.$static_character().$scene().$type(),
- prompt: lastPrompt,
- input: lastInput,
- testing: (Opal.GameficOpal.$static_character().$queue().$length() > 0)
- }
- return r;
- }
- var doReady = function(response) {
- startCallbacks.forEach(function(callback) {
- callback(response);
- });
- }
- var handle = function(response) {
- var handler = responseCallbacks[response.state] || responseCallbacks['Active'];
- handler(response);
- }
return {
start: function() {
- Opal.GameficOpal.$load_scripts();
- Opal.GameficOpal.$static_plot().$introduce(Opal.GameficOpal.$static_character());
- lastPrompt = Opal.GameficOpal.$static_character().$prompt();
- this.update('');
+ startCallbacks.forEach((callback) => {
+ callback();
+ });
+ Opal.gvars.engine.$run();
},
- update: function(input) {
- if (input != null) {
- Opal.GameficOpal.$static_character().$queue().$push(input);
- }
- lastInput = input;
- var response = getResponse(false);
- inputCallbacks.forEach(function(callback) {
- callback(response);
+ update: function(response) {
+ console.log('Called the Gamefic update');
+ var state = JSON.parse(response);
+ console.log('Output: ' + state.output);
+ updateCallbacks.forEach(function(callback) {
+ callback(state);
});
- Opal.GameficOpal.$static_plot().$update();
- Opal.GameficOpal.$static_plot().$ready();
- lastPrompt = Opal.GameficOpal.$static_character().$prompt();
- response = getResponse(true);
- var updateResponse = response;
- doReady(response);
- lastPrompt = Opal.GameficOpal.$static_character().$prompt();
- response = getResponse(true);
- response.output = updateResponse.output + response.output;
- handle(response);
- finishCallbacks.forEach(function(callback) {
- callback(response);
- });
- if (Opal.GameficOpal.$static_character().$queue().$length() > 0) {
- setTimeout("Gamefic.update();", 1);
- }
},
+ receive: function(input) {
+ Opal.gvars.engine.$receive(input);
+ },
onStart: function(callback) {
startCallbacks.push(callback);
},
- onInput: function(callback) {
- inputCallbacks.push(callback);
- },
- onFinish: function(callback) {
- finishCallbacks.push(callback);
- },
- handleResponse: function(state, callback) {
- responseCallbacks[state] = callback;
+ onUpdate: function(callback) {
+ updateCallbacks.push(callback);
},
save: function(filename, data) {
var json = Opal.JSON.$generate(data);
localStorage.setItem(filename, json);
Opal.GameficOpal.$static_character().$tell('Game saved.');