
/* Dasca, an idle game by @CylonicRaider (github.com/CylonicRaider/dasca)
 * Game implementation */

'use strict';

/* *** Action ***
 * A serializable (and reifiable) function wrapper, usable as a Scheduler
 * task. */

/* Construct a new action */
function DascaAction(name, cb) {
  if (cb == null && DascaAction.cache[name])
    cb = DascaAction.cache[name].cb;
  this.name = name;
  this.cb = cb;
}

DascaAction.prototype = {
  /* OOP helper */
  constructor: DascaAction
};

/* Cache of actions
 * Although the term is not exactly correct, I couldn't think of a better
 * one. */
DascaAction.cache = {};

/* Add an action to the cache */
DascaAction.addAction = function(name, cb) {
  DascaAction.cache[name] = new DascaAction(name, cb);
};

/* Serialization hook */
DascaAction.__save__ = function(action) {
  return {name: action.name};
}

/* Deserialization hook */
DascaAction.__restore__ = function(data) {
  var ret = new DascaAction(data.name);
  if (ret.cb == null) throw new Error("Unrecognized action!");
  return ret;
}

/* *** Initialization *** */

function init() {
  // TODO: Actual layer swapping code
  document.getElementById("titlescreen").classList.add("selected");
}

/* Install load handler */
window.addEventListener("load", init);

@@ -1031,16 +1031,1568 @@
 ret;%0A%7D%0A%0A
+/* *** Variables ***%0A * Each variable has a name and a (mutable) value; when the value is changed%0A * via the setter method, callbacks registered for the variable (statically)%0A * are invoked, each with %22this%22 set to the variable, and the name, the new,%0A * and the old value as arguments.%0A * Callbacks are registered statically to aid serialization. */%0A%0A/* Construct a new variable */%0Afunction Variable(name, value) %7B%0A  this.name = name;%0A  this.value = value;%0A%7D%0A%0AVariable.prototype = %7B%0A  /* Get the value of the variable */%0A  get: function() %7B%0A    return this.value;%0A  %7D,%0A%0A  /* Set the value of the variable%0A   * If the value changes, callbacks are invoked. */%0A  set: function(value) %7B%0A    if (value === this.value) return;%0A    var oldVal = this.value;%0A    this.value = value;%0A    var cbs = callbacks%5Bthis.name%5D;%0A    if (cbs) %7B%0A      for (var i = 0; i %3C cbs.length; i++)%0A        cbs%5Bi%5D.call(this, this.name, value, oldVal);%0A    %7D%0A  %7D,%0A%0A  /* OOP noise */%0A  constructor: Variable%0A%7D%0A%0A/* Mapping of variable names to values */%0AVariable.callbacks = %7B%7D;%0A%0A/* Register the given callback for the given variable name */%0AVariable.addCallback = function(name, cb) %7B%0A  if (! Variable.callbacks%5Bname%5D)%0A    Variable.callbacks%5Bname%5D = %5B%5D;%0A  if (Variable.callbacks%5Bname%5D.indexOf(cb) == -1)%0A    Variable.callbacks%5Bname%5D.push(cb);%0A%7D;%0A%0A/* Deregister the given callback */%0AVariable.removeCallback = function(name, cb) %7B%0A  if (! Variable.callbacks%5Bname%5D) return;%0A  var idx = Variable.callbacks%5Bname%5D.indexOf(cb);%0A  if (idx != -1) Variable.callbacks%5Bname%5D.splice(idx, 1);%0A%7D;%0A%0A
 /* *** I

Implement variables