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'),
|
|
|
|
);
|
|
|
|
|
2020-06-15 17:26:20 -05:00
|
|
|
export default class Spawner extends decorate(Trait) {
|
2019-04-21 01:30:03 -05:00
|
|
|
|
2020-06-23 11:19:59 -05:00
|
|
|
static behaviorTypes() {
|
2020-06-19 15:31:35 -05:00
|
|
|
return {
|
2020-06-19 22:01:45 -05:00
|
|
|
killAllChildren: {
|
|
|
|
type: 'void',
|
|
|
|
label: 'Kill all spawned children',
|
|
|
|
},
|
2020-06-19 15:31:35 -05:00
|
|
|
spawn: {
|
2020-06-20 07:36:04 -05:00
|
|
|
cycle: true,
|
2020-06-25 09:53:49 -05:00
|
|
|
type: 'void|entity',
|
2020-06-19 15:31:35 -05:00
|
|
|
label: 'Spawn $1 with $2 extensions.',
|
|
|
|
args: [
|
|
|
|
['key', {
|
|
|
|
type: 'string',
|
2020-06-25 04:58:09 -05:00
|
|
|
options: (entity) => this.optionsForSpawn(entity),
|
2020-06-19 15:31:35 -05:00
|
|
|
}],
|
|
|
|
['json', {
|
|
|
|
type: 'object',
|
|
|
|
}],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
spawnAt: {
|
2020-06-20 07:36:04 -05:00
|
|
|
cycle: true,
|
2020-06-25 09:53:49 -05:00
|
|
|
type: 'void|entity',
|
2020-06-19 15:31:35 -05:00
|
|
|
label: 'Spawn $1 as $2 with $3 extensions.',
|
|
|
|
args: [
|
|
|
|
['key', {
|
|
|
|
type: 'string',
|
2020-06-25 04:58:09 -05:00
|
|
|
options: (entity) => this.optionsForSpawn(entity),
|
2020-06-19 15:31:35 -05:00
|
|
|
}],
|
|
|
|
['position', {
|
|
|
|
type: 'vector',
|
|
|
|
}],
|
|
|
|
['json', {
|
|
|
|
type: 'object',
|
|
|
|
}],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
spawnRaw: {
|
2020-06-20 07:36:04 -05:00
|
|
|
cycle: true,
|
2020-06-25 09:53:49 -05:00
|
|
|
type: 'void|entity',
|
2020-06-19 15:31:35 -05:00
|
|
|
label: 'Spawn $1.',
|
|
|
|
args: [
|
|
|
|
['json', {
|
|
|
|
type: 'object',
|
|
|
|
}],
|
|
|
|
]
|
|
|
|
},
|
|
|
|
spawnRawAt: {
|
2020-06-20 07:36:04 -05:00
|
|
|
cycle: true,
|
2020-06-25 09:53:49 -05:00
|
|
|
type: 'void|entity',
|
2020-06-19 15:31:35 -05:00
|
|
|
label: 'Spawn $1 at $2.',
|
|
|
|
args: [
|
|
|
|
['position', {
|
|
|
|
type: 'vector',
|
|
|
|
}],
|
|
|
|
['json', {
|
|
|
|
type: 'object',
|
|
|
|
}],
|
|
|
|
]
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:30:03 -05:00
|
|
|
static defaultParams() {
|
|
|
|
return {
|
|
|
|
spawns: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-19 22:01:45 -05:00
|
|
|
static describeParams() {
|
|
|
|
return {
|
|
|
|
spawns: {
|
|
|
|
type: 'object',
|
|
|
|
label: 'Entities that may be spawned',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:30:03 -05:00
|
|
|
static defaultState() {
|
|
|
|
return {
|
|
|
|
isSpawning: true,
|
|
|
|
maxSpawns: Infinity,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-19 22:01:45 -05:00
|
|
|
static describeState() {
|
|
|
|
return {
|
|
|
|
isSpawning: {
|
|
|
|
type: 'bool',
|
|
|
|
label: 'Is spawning',
|
|
|
|
},
|
|
|
|
maxSpawns: {
|
|
|
|
type: 'number',
|
|
|
|
label: 'Maximum concurrent spawns',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:58:09 -05:00
|
|
|
static optionsForSpawn(entity) {
|
2020-06-28 12:47:09 -05:00
|
|
|
if (!entity) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-06-25 04:58:09 -05:00
|
|
|
return Object.keys(entity.traitInstance('spawner').params.spawns)
|
|
|
|
.reduce((r, key) => ({...r, [key]: key}), {});
|
|
|
|
}
|
|
|
|
|
2019-05-05 04:26:35 -05:00
|
|
|
static type() {
|
|
|
|
return 'spawner';
|
|
|
|
}
|
|
|
|
|
2019-05-03 02:24:02 -05:00
|
|
|
destroy() {
|
2019-10-08 02:29:24 -05:00
|
|
|
while (this.children.length > 0) {
|
2019-10-09 03:46:19 -05:00
|
|
|
const child = this.children.pop();
|
|
|
|
if (child) {
|
|
|
|
this.removeChild(child);
|
|
|
|
}
|
2019-10-08 02:29:24 -05:00
|
|
|
}
|
2019-05-03 02:24:02 -05:00
|
|
|
}
|
|
|
|
|
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-10-08 02:29:24 -05:00
|
|
|
this.childrenListeners = new Map();
|
2019-05-04 14:06:47 -05:00
|
|
|
this.spawnJSONs = this.params.spawns;
|
2019-04-21 01:30:03 -05:00
|
|
|
}
|
|
|
|
|
2019-10-08 03:24:20 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-10-01 20:37:08 -05:00
|
|
|
maySpawn() {
|
|
|
|
if (this.maxSpawns <= this.children.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-08 03:24:20 -05:00
|
|
|
if (!this.destinationEntityList()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-01 20:37:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-08 02:29:24 -05:00
|
|
|
removeChild(child) {
|
|
|
|
const index = this.children.indexOf(child);
|
|
|
|
if (-1 !== index) {
|
|
|
|
this.children.splice(index, 1);
|
|
|
|
const listener = this.childrenListeners.get(child);
|
|
|
|
child.off('destroy', listener);
|
|
|
|
this.childrenListeners.delete(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:30:03 -05:00
|
|
|
listeners() {
|
|
|
|
return {
|
|
|
|
|
2019-10-10 01:27:02 -05:00
|
|
|
isDyingChanged: (_, isDying) => {
|
|
|
|
this.isSpawning = !isDying;
|
2019-04-21 01:30:03 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
methods() {
|
|
|
|
return {
|
|
|
|
|
|
|
|
killAllChildren: () => {
|
2019-10-01 01:46:13 -05:00
|
|
|
// Juggle children since this may cause splices and mess up the array.
|
|
|
|
const children = this.children.slice(0);
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
children[i].destroyGently();
|
2019-04-21 01:30:03 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
spawn: (key, json = {}) => {
|
2019-10-01 20:37:08 -05:00
|
|
|
if (!this.maySpawn()) {
|
2019-04-21 01:30:03 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const spawnJSON = this.spawnJSONs[key];
|
|
|
|
if (!spawnJSON) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-08 03:24:20 -05:00
|
|
|
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()) {
|
2019-05-30 09:11:17 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Add null to children to prevent race.
|
|
|
|
const childIndex = this.children.length;
|
|
|
|
this.children.push(null);
|
2019-10-08 03:24:20 -05:00
|
|
|
const list = this.destinationEntityList();
|
|
|
|
return Entity.loadOrInstance(json).then((child) => {
|
2019-10-08 02:29:24 -05:00
|
|
|
this.children[childIndex] = child;
|
2019-10-08 03:24:20 -05:00
|
|
|
// Listen for destroy event.
|
2019-10-08 02:29:24 -05:00
|
|
|
const listener = this.removeChild.bind(this, child);
|
|
|
|
this.childrenListeners.set(child, listener);
|
|
|
|
child.once('destroy', listener);
|
2019-10-08 03:24:20 -05:00
|
|
|
// Add child to list.
|
2019-10-08 02:29:24 -05:00
|
|
|
list.addEntity(child);
|
|
|
|
return child;
|
2019-05-17 05:37:29 -05:00
|
|
|
});
|
2019-04-21 01:30:03 -05:00
|
|
|
},
|
|
|
|
|
2019-10-08 03:24:20 -05:00
|
|
|
spawnRawAt: (position, json = {}) => {
|
|
|
|
if (!this.maySpawn()) {
|
2019-05-30 09:11:17 -05:00
|
|
|
return;
|
|
|
|
}
|
2019-10-08 03:24:20 -05:00
|
|
|
json = this.augmentJSONWithPosition(json, position);
|
|
|
|
return this.entity.spawnRaw(json);
|
2019-05-30 09:11:17 -05:00
|
|
|
},
|
|
|
|
|
2019-04-21 01:30:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|