humus-old/common/combat/damaging.trait.js
2019-05-06 12:13:47 -05:00

60 lines
1.2 KiB
JavaScript

import * as I from 'immutable';
import {Trait} from '@avocado/entity';
import {AFFINITY_PHYSICAL} from './constants';
export class Damaging extends Trait {
static defaultParams() {
return {
damagingSound: '',
damageSpecs: [],
};
}
static type() {
return 'damaging';
}
constructor(entity, params, state) {
super(entity, params, state);
this._collidingWith = [];
const damageSpecsJSON = this.params.damageSpecs;
this._damageSpecs = damageSpecsJSON.map((damageSpec) => {
return {
affinity: AFFINITY_PHYSICAL,
lock: 0.1,
power: 0,
variance: 0.2,
...damageSpec,
};
});
this._damagingSound = this.params.damagingSound;
}
get damageSpecs() {
return this._damageSpecs;
}
get damagingSound() {
return this._damagingSound;
}
tick(elapsed) {
if (AVOCADO_SERVER) {
const isCollidingWith = this.entity.isCollidingWith;
for (let i = 0; i < isCollidingWith.length; i++) {
const entity = isCollidingWith[i];
if (entity.is('vulnerable')) {
if (!entity.isInvulnerable) {
entity.takeDamageFrom(this.entity);
}
}
}
}
}
}