feat: spawnRaw and spawnRawAt
This commit is contained in:
parent
0a7c60e8b7
commit
99dc832866
|
@ -43,6 +43,21 @@ export class Spawner extends decorate(Trait) {
|
|||
this.spawnJSONs = this.params.spawns;
|
||||
}
|
||||
|
||||
augmentJSONWithPosition(json, position) {
|
||||
if (!json.traits) {
|
||||
json.traits = {};
|
||||
}
|
||||
if (!json.traits.positioned) {
|
||||
json.traits.positioned = {};
|
||||
}
|
||||
if (!json.traits.positioned.state) {
|
||||
json.traits.positioned.state = {};
|
||||
}
|
||||
json.traits.positioned.state.x = position[0];
|
||||
json.traits.positioned.state.y = position[1];
|
||||
return json;
|
||||
}
|
||||
|
||||
destinationEntityList() {
|
||||
if (
|
||||
this.entity.is('listed')
|
||||
|
@ -63,6 +78,9 @@ export class Spawner extends decorate(Trait) {
|
|||
if (this.maxSpawns <= this.children.length) {
|
||||
return false;
|
||||
}
|
||||
if (!this.destinationEntityList()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -105,40 +123,43 @@ export class Spawner extends decorate(Trait) {
|
|||
if (!spawnJSON) {
|
||||
return;
|
||||
}
|
||||
const list = this.destinationEntityList();
|
||||
if (!list) {
|
||||
return this.entity.spawnRaw(merge({}, spawnJSON, json));
|
||||
},
|
||||
|
||||
spawnAt: (key, position, json = {}) => {
|
||||
if (!this.maySpawn()) {
|
||||
return;
|
||||
}
|
||||
json = this.augmentJSONWithPosition(json, position);
|
||||
return this.entity.spawn(key, json);
|
||||
},
|
||||
|
||||
spawnRaw: (json) => {
|
||||
if (!this.maySpawn()) {
|
||||
return;
|
||||
}
|
||||
const mergedJSON = merge({}, spawnJSON, json);
|
||||
// Add null to children to prevent race.
|
||||
const childIndex = this.children.length;
|
||||
this.children.push(null);
|
||||
return Entity.loadOrInstance(mergedJSON).then((child) => {
|
||||
const list = this.destinationEntityList();
|
||||
return Entity.loadOrInstance(json).then((child) => {
|
||||
this.children[childIndex] = child;
|
||||
// Listen for destroy event.
|
||||
const listener = this.removeChild.bind(this, child);
|
||||
this.childrenListeners.set(child, listener);
|
||||
child.once('destroy', listener);
|
||||
// Add child to list.
|
||||
list.addEntity(child);
|
||||
return child;
|
||||
});
|
||||
},
|
||||
|
||||
spawnAt: (key, position, json = {}) => {
|
||||
if (true && !this.maySpawn()) {
|
||||
spawnRawAt: (position, json = {}) => {
|
||||
if (!this.maySpawn()) {
|
||||
return;
|
||||
}
|
||||
if (!json.traits) {
|
||||
json.traits = {};
|
||||
}
|
||||
if (!json.traits.positioned) {
|
||||
json.traits.positioned = {};
|
||||
}
|
||||
if (!json.traits.positioned.state) {
|
||||
json.traits.positioned.state = {};
|
||||
}
|
||||
json.traits.positioned.state.x = position[0];
|
||||
json.traits.positioned.state.y = position[1];
|
||||
return this.entity.spawn(key, json);
|
||||
json = this.augmentJSONWithPosition(json, position);
|
||||
return this.entity.spawnRaw(json);
|
||||
},
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user