34 lines
1.3 KiB
Java
34 lines
1.3 KiB
Java
package com.example.systems;
|
|
|
|
import com.example.components.Attack;
|
|
import com.example.components.CombatLog;
|
|
import com.example.components.Health;
|
|
import com.example.components.HumanDefense;
|
|
import com.example.components.Stats;
|
|
import com.example.ecs.Entity;
|
|
import com.example.ecs.Registry;
|
|
import com.example.ecs.System;
|
|
|
|
public class DamageSystem extends System {
|
|
public DamageSystem(Registry registry) {
|
|
super(registry);
|
|
}
|
|
|
|
public void run() {
|
|
for (Entity attackEntity : registry.getWithComponents(Attack.class)) {
|
|
Attack attack = registry.getComponent(attackEntity, Attack.class);
|
|
Stats targetStats = registry.getComponent(attack.target(), Stats.class);
|
|
Health targetHealth = registry.getComponent(attack.target(), Health.class);
|
|
|
|
double dmgDealt = attack.damage() * (1 - targetStats.armor());
|
|
if (registry.hasComponent(attack.target(), HumanDefense.class)) {
|
|
HumanDefense defense = registry.getComponent(attack.target(), HumanDefense.class);
|
|
dmgDealt -= dmgDealt * defense.defense();
|
|
}
|
|
|
|
targetHealth.lowerHp(dmgDealt);
|
|
registry.addComponent(attackEntity, new CombatLog(attack.source(), attack.target(), attack.damage(), dmgDealt));
|
|
}
|
|
}
|
|
}
|