silphius/app/ecs/components/vulnerable.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-07-26 10:36:59 -05:00
import Component from '@/ecs/component.js';
2024-07-27 10:52:49 -05:00
export const DamageTypes = {
PAIN: 0,
HEALING: 1,
MANA: 2,
};
2024-07-26 10:36:59 -05:00
export default class Vulnerable extends Component {
2024-07-26 18:05:24 -05:00
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;
2024-07-27 10:52:49 -05:00
Types = DamageTypes;
2024-07-26 18:05:24 -05:00
damage(specification) {
2024-07-30 17:21:06 -05:00
if (this.isInvulnerable) {
return;
}
2024-07-27 12:31:52 -05:00
const {Alive} = Component.ecs.get(this.entity);
if (Alive) {
switch (specification.type) {
case DamageTypes.PAIN: {
Alive.acceptDamage(specification.amount);
}
}
}
2024-07-30 17:06:04 -05:00
Component.markChange(
this.entity,
'damage',
{[`${this.entity}-${this.id++}`]: specification},
);
2024-07-26 18:05:24 -05:00
}
};
}
2024-07-30 17:21:06 -05:00
static properties = {
isInvulnerable: {type: 'uint8'},
};
2024-07-26 10:36:59 -05:00
}