humus-old/common/damage/damaging.trait.js

66 lines
1.2 KiB
JavaScript
Raw Normal View History

import * as I from 'immutable';
import {Trait} from '@avocado/entity';
export class Damaging extends Trait {
static defaultParams() {
return {
affinities: [],
lock: 0.1,
power: 0,
variance: 0.2,
};
}
initialize() {
this._affinities = this.params.get('affinities').toJS();
this._collidingWith = [];
this._lock = this.params.get('lock');
this._power = this.params.get('power');
this._variance = this.params.get('variance');
}
get affinities() {
return this._affinities;
}
get lock() {
return this._lock;
}
get power() {
return this._power;
}
get variance() {
return this._variance;
}
listeners() {
return {
collisionEnd: (other) => {
const index = this._collidingWith.indexOf(other);
this._collidingWith.splice(index, 1);
},
collisionStart: (other) => {
this._collidingWith.push(other);
},
};
}
tick(elapsed) {
for (let i = 0; i < this._collidingWith.length; ++i) {
const entity = this._collidingWith[i];
if (entity.is('vulnerable')) {
entity.takeDamageFrom(this.entity);
}
}
}
}