Explorar el Código

Merging is now done all the way! Should function correctly for arbitrarily nested objects

tags/v0.7.0
Fen Dweller hace 8 años
padre
commit
024345829b
Se han modificado 2 ficheros con 28 adiciones y 17 borrados
  1. +1
    -1
      game.js
  2. +27
    -16
      recursive-macro.js

+ 1
- 1
game.js Ver fichero

@@ -229,7 +229,7 @@ function doDigest(containerName)
var toDigest = digestType.shift(); var toDigest = digestType.shift();
if (toDigest.name != "Container") if (toDigest.name != "Container")
toDigest = new Container([toDigest]); toDigest = new Container([toDigest]);
container.merge(toDigest);
container = container.merge(toDigest);
} }


var digested = container.sum(); var digested = container.sum();


+ 27
- 16
recursive-macro.js Ver fichero

@@ -175,6 +175,32 @@ function defaultMass(thing) {
return masses[thing.name]; return masses[thing.name];
} }


function defaultMerge(thing) {
return function(container) {
var newCount = this.count + container.count;

var newThing = new things[thing.name](newCount);
newThing.contents = {};

for (var key in this.contents) {
if (this.contents.hasOwnProperty(key)) {
newThing.contents[key] = this.contents[key];
}
}
for (var key in container.contents) {
if (container.contents.hasOwnProperty(key)) {
if (this.contents.hasOwnProperty(key)) {
newThing.contents[key] = this.contents[key].merge(container.contents[key]);
} else {;
newThing.contents[key] = container.contents[key];
}
}
}

return newThing;
}
}


function defaultSum(thing) { function defaultSum(thing) {
return function() { return function() {
@@ -224,6 +250,7 @@ function DefaultEntity() {
this.area = defaultArea; this.area = defaultArea;
this.mass = defaultMass; this.mass = defaultMass;
this.sum_property = defaultSumProperty; this.sum_property = defaultSumProperty;
this.merge = defaultMerge;
return this; return this;
} }


@@ -260,22 +287,6 @@ function Container(contents = []) {
return describe_all(this.contents) return describe_all(this.contents)
} }


// put another container into this one

this.merge = function(container) {
for (var key in container.contents) {
if (container.contents.hasOwnProperty(key)) {
if (this.contents.hasOwnProperty(key)) {
this.count += container.contents[key].count;
this.contents[key] = new things[container.contents[key].name](container.contents[key].count + this.contents[key].count);
} else {
this.count += container.contents[key].count;
this.contents[key] = new things[container.contents[key].name](container.contents[key].count);
}
}
}
}

return this; return this;
} }




Cargando…
Cancelar
Guardar