2019-05-17 05:37:29 -05:00
|
|
|
import {compose, merge} from '@avocado/core';
|
2019-04-21 01:30:03 -05:00
|
|
|
import {Vector} from '@avocado/math';
|
|
|
|
|
|
|
|
import {StateProperty, Trait} from '../trait';
|
|
|
|
import {Entity} from '..';
|
|
|
|
|
|
|
|
const decorate = compose(
|
|
|
|
StateProperty('isSpawning', {
|
|
|
|
track: true,
|
|
|
|
}),
|
|
|
|
StateProperty('maxSpawns'),
|
|
|
|
);
|
|
|
|
|
|
|
|
export class Spawner extends decorate(Trait) {
|
|
|
|
|
|
|
|
static defaultParams() {
|
|
|
|
return {
|
|
|
|
spawns: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultState() {
|
|
|
|
return {
|
|
|
|
isSpawning: true,
|
|
|
|
maxSpawns: Infinity,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-05 04:26:35 -05:00
|
|
|
static type() {
|
|
|
|
return 'spawner';
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:24:02 -05:00
|
|
|
destroy() {
|
|
|
|
this.children = [];
|
|
|
|
}
|
|
|
|
|
2019-05-06 04:03:43 -05:00
|
|
|
constructor(entity, params, state) {
|
|
|
|
super(entity, params, state);
|
2019-04-21 01:30:03 -05:00
|
|
|
this.children = [];
|
2019-05-04 14:06:47 -05:00
|
|
|
this.spawnJSONs = this.params.spawns;
|
2019-04-21 01:30:03 -05:00
|
|
|
}
|
|
|
|
|
2019-05-30 09:11:17 -05:00
|
|
|
destinationEntityList() {
|
2019-09-08 03:20:15 -05:00
|
|
|
if (
|
|
|
|
this.entity.is('listed')
|
|
|
|
&& this.entity.list
|
|
|
|
) {
|
2019-05-30 09:11:17 -05:00
|
|
|
return this.entity.list;
|
|
|
|
}
|
2019-09-08 03:20:15 -05:00
|
|
|
if (
|
|
|
|
this.entity.wielder
|
|
|
|
&& this.entity.wielder.is('listed')
|
|
|
|
&& this.entity.wielder.list
|
|
|
|
) {
|
2019-05-30 09:11:17 -05:00
|
|
|
return this.entity.wielder.list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:30:03 -05:00
|
|
|
listeners() {
|
|
|
|
return {
|
|
|
|
|
|
|
|
dying: () => {
|
|
|
|
this.isSpawning = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
methods() {
|
|
|
|
return {
|
|
|
|
|
|
|
|
killAllChildren: () => {
|
|
|
|
for (let i = 0; i < this.children.length; i++) {
|
2019-05-14 05:58:04 -05:00
|
|
|
this.children[i].destroyGently();
|
2019-04-21 01:30:03 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
spawn: (key, json = {}) => {
|
2019-04-21 14:57:35 -05:00
|
|
|
if (this.maxSpawns <= this.children.length) {
|
2019-04-21 01:30:03 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const spawnJSON = this.spawnJSONs[key];
|
|
|
|
if (!spawnJSON) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-30 09:11:17 -05:00
|
|
|
const list = this.destinationEntityList();
|
|
|
|
if (!list) {
|
|
|
|
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((spawn) => {
|
|
|
|
this.children[childIndex] = spawn;
|
2019-05-17 05:37:29 -05:00
|
|
|
spawn.once('destroy', () => {
|
|
|
|
const index = this.children.indexOf(spawn);
|
|
|
|
this.children.splice(index, 1);
|
|
|
|
})
|
|
|
|
list.addEntity(spawn);
|
2019-06-05 20:16:34 -05:00
|
|
|
return spawn;
|
2019-05-17 05:37:29 -05:00
|
|
|
});
|
2019-04-21 01:30:03 -05:00
|
|
|
},
|
|
|
|
|
2019-05-30 09:11:17 -05:00
|
|
|
spawnAt: (key, position, json = {}) => {
|
|
|
|
if (this.maxSpawns <= this.children.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!json.traits) {
|
|
|
|
json.traits = {};
|
|
|
|
}
|
|
|
|
if (!json.traits.positioned) {
|
|
|
|
json.traits.positioned = {};
|
|
|
|
}
|
|
|
|
if (!json.traits.positioned.state) {
|
|
|
|
json.traits.positioned.state = {};
|
|
|
|
}
|
2019-09-30 01:36:02 -05:00
|
|
|
json.traits.positioned.state.x = position[0];
|
|
|
|
json.traits.positioned.state.y = position[1];
|
2019-05-30 09:11:17 -05:00
|
|
|
return this.entity.spawn(key, json);
|
|
|
|
},
|
|
|
|
|
2019-04-21 01:30:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|