Selaa lähdekoodia

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

tags/v0.7.0
Fen Dweller 7 vuotta sitten
vanhempi
commit
024345829b
2 muutettua tiedostoa jossa 28 lisäystä ja 17 poistoa
  1. +1
    -1
      game.js
  2. +27
    -16
      recursive-macro.js

+ 1
- 1
game.js Näytä tiedosto

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

var digested = container.sum();


+ 27
- 16
recursive-macro.js Näytä tiedosto

@@ -175,6 +175,32 @@ function defaultMass(thing) {
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) {
return function() {
@@ -224,6 +250,7 @@ function DefaultEntity() {
this.area = defaultArea;
this.mass = defaultMass;
this.sum_property = defaultSumProperty;
this.merge = defaultMerge;
return this;
}

@@ -260,22 +287,6 @@ function Container(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;
}



Loading…
Peruuta
Tallenna