2019-04-19 02:54:59 -05:00
|
|
|
import * as I from 'immutable';
|
|
|
|
|
2019-06-05 23:37:36 -05:00
|
|
|
import {compose} from '@avocado/core';
|
|
|
|
import {StateProperty, Trait} from '@avocado/entity';
|
2019-04-19 02:54:59 -05:00
|
|
|
|
2019-04-28 22:35:20 -05:00
|
|
|
import {AFFINITY_PHYSICAL} from './constants';
|
|
|
|
|
2019-06-05 23:37:36 -05:00
|
|
|
const decorate = compose(
|
|
|
|
StateProperty('isDamaging'),
|
|
|
|
);
|
|
|
|
|
|
|
|
export class Damaging extends decorate(Trait) {
|
2019-04-19 02:54:59 -05:00
|
|
|
|
|
|
|
static defaultParams() {
|
|
|
|
return {
|
2019-04-20 16:09:34 -05:00
|
|
|
damagingSound: '',
|
2019-04-19 12:16:28 -05:00
|
|
|
damageSpecs: [],
|
2019-04-19 02:54:59 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-05 23:37:36 -05:00
|
|
|
static defaultState() {
|
|
|
|
return {
|
|
|
|
isDamaging: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-05 04:26:23 -05:00
|
|
|
static type() {
|
|
|
|
return 'damaging';
|
|
|
|
}
|
|
|
|
|
2019-05-06 04:04:03 -05:00
|
|
|
constructor(entity, params, state) {
|
|
|
|
super(entity, params, state);
|
2019-04-19 02:54:59 -05:00
|
|
|
this._collidingWith = [];
|
2019-05-26 11:59:39 -05:00
|
|
|
this._doesNotDamage = [];
|
2019-05-04 14:07:16 -05:00
|
|
|
const damageSpecsJSON = this.params.damageSpecs;
|
2019-04-19 12:16:28 -05:00
|
|
|
this._damageSpecs = damageSpecsJSON.map((damageSpec) => {
|
|
|
|
return {
|
2019-04-28 22:35:20 -05:00
|
|
|
affinity: AFFINITY_PHYSICAL,
|
2019-04-19 12:16:28 -05:00
|
|
|
lock: 0.1,
|
|
|
|
power: 0,
|
|
|
|
variance: 0.2,
|
|
|
|
...damageSpec,
|
|
|
|
};
|
|
|
|
});
|
2019-05-04 14:07:16 -05:00
|
|
|
this._damagingSound = this.params.damagingSound;
|
2019-04-19 02:54:59 -05:00
|
|
|
}
|
|
|
|
|
2019-05-26 11:59:39 -05:00
|
|
|
doesNotDamageEntity(entity) {
|
2019-06-05 23:37:36 -05:00
|
|
|
if (!this.entity.isDamaging) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-26 11:59:39 -05:00
|
|
|
return -1 !== this._doesNotDamage.indexOf(entity);
|
|
|
|
}
|
|
|
|
|
2019-04-19 12:16:28 -05:00
|
|
|
get damageSpecs() {
|
|
|
|
return this._damageSpecs;
|
2019-04-19 02:54:59 -05:00
|
|
|
}
|
|
|
|
|
2019-04-20 16:09:34 -05:00
|
|
|
get damagingSound() {
|
|
|
|
return this._damagingSound;
|
|
|
|
}
|
|
|
|
|
2019-06-01 15:06:10 -05:00
|
|
|
tryDamagingEntity(entity) {
|
|
|
|
if (this.doesNotDamageEntity(entity)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (entity.is('vulnerable')) {
|
|
|
|
if (!entity.isInvulnerable) {
|
|
|
|
entity.takeDamageFrom(this.entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners() {
|
|
|
|
const listeners = {};
|
|
|
|
if (AVOCADO_SERVER) {
|
|
|
|
listeners.collisionStart = (other) => {
|
|
|
|
this.tryDamagingEntity(other);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return listeners;
|
|
|
|
}
|
|
|
|
|
2019-05-26 11:59:39 -05:00
|
|
|
methods() {
|
|
|
|
return {
|
|
|
|
|
|
|
|
setDoesDamage: (entity) => {
|
|
|
|
const index = this._doesNotDamage.indexOf(entity);
|
|
|
|
if (-1 !== index) {
|
|
|
|
this._doesNotDamage.splice(index, 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setDoesNotDamage: (entity) => {
|
|
|
|
if (-1 === this._doesNotDamage.indexOf(entity)) {
|
|
|
|
this._doesNotDamage.push(entity);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-19 02:54:59 -05:00
|
|
|
tick(elapsed) {
|
2019-04-22 21:03:16 -05:00
|
|
|
if (AVOCADO_SERVER) {
|
2019-06-01 15:06:10 -05:00
|
|
|
if (this.entity.is('collider')) {
|
|
|
|
const isCollidingWith = this.entity.isCollidingWith;
|
|
|
|
for (let i = 0; i < isCollidingWith.length; i++) {
|
|
|
|
this.tryDamagingEntity(isCollidingWith[i]);
|
2019-04-22 21:03:16 -05:00
|
|
|
}
|
2019-04-19 02:54:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|