Episode 7: Status Effects – Stun, Burn, Heal
Goal: Implement a flexible StatusEffect system using inheritance and polymorphism, allowing heroes to gain and process effects during battle.
Episode 7: Status Effects – Stun, Burn, Heal
1. Project Structure
1
2
3
4
5
6
src/
└── effect/
├── StatusEffect.java // Abstract base class
├── BurnEffect.java // Deals damage over time
├── StunEffect.java // Prevents acting
└── HealEffect.java // Restores HP
2. StatusEffect.java – Abstract Base Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package effect;
import model.Hero;
public abstract class StatusEffect {
protected int duration;
public StatusEffect(int duration) {
this.duration = duration;
}
public boolean isExpired() {
return duration <= 0;
}
public void tick(Hero target) {
if (!isExpired()) {
apply(target);
duration--;
}
}
public abstract void apply(Hero target);
public abstract String getName();
}
3. BurnEffect.java – Damage over Time
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package effect;
import model.Hero;
public class BurnEffect extends StatusEffect {
private int burnDamage;
public BurnEffect(int duration, int burnDamage) {
super(duration);
this.burnDamage = burnDamage;
}
@Override
public void apply(Hero target) {
System.out.println(target.getName() + " is burning! Takes " + burnDamage + " damage.");
target.takeDamage(burnDamage);
}
@Override
public String getName() {
return "Burn";
}
}
4. StunEffect.java – Skips Turn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package effect;
import model.Hero;
public class StunEffect extends StatusEffect {
public StunEffect(int duration) {
super(duration);
}
@Override
public void apply(Hero target) {
System.out.println(target.getName() + " is stunned and cannot act!");
// We'll check this flag in Main or Hero logic later
}
@Override
public String getName() {
return "Stun";
}
}
5. HealEffect.java – Heals HP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package effect;
import model.Hero;
public class HealEffect extends StatusEffect {
private int healAmount;
public HealEffect(int duration, int healAmount) {
super(duration);
this.healAmount = healAmount;
}
@Override
public void apply(Hero target) {
System.out.println(target.getName() + " is healed for " + healAmount + " HP.");
target.heal(healAmount);
}
@Override
public String getName() {
return "Heal";
}
}
6. Update Hero.java to Support Effects
Update your Hero class to store and process status effects:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import effect.StatusEffect;
import java.util.*;
public abstract class Hero {
// ... existing fields
protected List<StatusEffect> effects = new ArrayList<>();
public void addEffect(StatusEffect effect) {
effects.add(effect);
System.out.println(name + " is affected by " + effect.getName());
}
public void processEffects() {
Iterator<StatusEffect> it = effects.iterator();
while (it.hasNext()) {
StatusEffect effect = it.next();
effect.tick(this);
if (effect.isExpired()) {
System.out.println(effect.getName() + " has worn off.");
it.remove();
}
}
}
public void heal(int amount) {
this.hp += amount;
System.out.println(name + " now has " + hp + " HP.");
}
// existing attack/takeDamage/etc...
}
7. Example Test in Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import effect.*;
import model.*;
public class Main {
public static void main(String[] args) {
Hero garen = new Warrior("Garen");
Hero ahri = new Mage("Ahri");
ahri.addEffect(new BurnEffect(3, 5));
garen.addEffect(new HealEffect(2, 10));
for (int i = 1; i <= 3; i++) {
System.out.println("=== Turn " + i + " ===");
garen.processEffects();
ahri.processEffects();
garen.attack(ahri);
ahri.attack(garen);
}
}
}
This post is licensed under CC BY 4.0 by the author.