silphius/app/ecs/components/vulnerable.js
2024-07-27 12:31:52 -05:00

39 lines
908 B
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 (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) {
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.id++]: specification});
}
};
}
}