49 lines
1.1 KiB
JavaScript
49 lines
1.1 KiB
JavaScript
import Component from '@/ecs/component.js';
|
|
|
|
export const DamageTypes = {
|
|
PAIN: 0,
|
|
HEALING: 1,
|
|
MANA: 2,
|
|
};
|
|
|
|
export default class Vulnerable extends Component {
|
|
mergeDiff(original, update) {
|
|
const merged = {};
|
|
if (original.damage || update.damage) {
|
|
merged.damage = {
|
|
...original.damage,
|
|
...update.damage,
|
|
}
|
|
}
|
|
return merged;
|
|
}
|
|
instanceFromSchema() {
|
|
const Component = this;
|
|
return class VulnerableInstance extends super.instanceFromSchema() {
|
|
id = 0;
|
|
Types = DamageTypes;
|
|
damage(specification) {
|
|
if (this.isInvulnerable) {
|
|
return;
|
|
}
|
|
const {Alive} = Component.ecs.get(this.entity);
|
|
if (Alive) {
|
|
switch (specification.type) {
|
|
case DamageTypes.PAIN: {
|
|
Alive.acceptDamage(specification.amount);
|
|
}
|
|
}
|
|
}
|
|
Component.markChange(
|
|
this.entity,
|
|
'damage',
|
|
{[`${this.entity}-${this.id++}`]: specification},
|
|
);
|
|
}
|
|
};
|
|
}
|
|
static properties = {
|
|
isInvulnerable: {type: 'uint8'},
|
|
};
|
|
}
|