Post

Episode 4: Polymorphism - Adding Skills and Skill Effects

Goal: Implement the Skill system using inheritance and polymorphism, and leverage the Usable interface introduced in Episode 3.5

Episode 4: Polymorphism - Adding Skills and Skill Effects

1. Project Structure

sequenceDiagram
    participant Player
    participant Game
    participant Enemy

    Player->>Game: Start()
    Game->>Enemy: Spawn()
    Enemy-->>Game: Ready
    Game-->>Player: Begin Battle
classDiagram
    class Hero {
        +String name
        +int hp
        +void attack()
    }

    class Mage {
        +castSpell()
    }

    Hero <|-- Mage

Create the following folder layout:

1
2
3
4
5
6
7
8
9
10
11
src/
├── model/
│   ├── Skill.java      // New
│   └── Hero.java       // Updated to support skills

model/
├── Skill.java        ✅ abstract, implements Usable
├── Usable.java       ✅ interface for all usable actions
skill/
├── Fireball.java     ✅ concrete skill
├── HealSkill.java

2. Create Skill.java

Create the file: src/main/Main.java

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
package model;

public class Skill {
    private String name;
    private int power;
    private String description;

    public Skill(String name, int power, String description) {
        this.name = name;
        this.power = power;
        this.description = description;
    }

    public void use(Hero user, Hero target) {
        System.out.println(user.getName() + " uses " + name + " on " + target.getName() + "!");
        target.takeDamage(power);
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public int getPower() {
        return power;
    }
}

3. Add List<Skill> to Hero

Update Hero.java to support a list of skills:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.List;

public abstract class Hero {
    protected String name;
    protected int hp;
    protected int attack;
    protected List<Skill> skills;

    public Hero(String name, int hp, int attack) {
        this.name = name;
        this.hp = hp;
        this.attack = attack;
        this.skills = new ArrayList<>();
    }

    public void addSkill(Skill skill) {
        skills.add(skill);
    }

    public List<Skill> getSkills() {
        return skills;
    }

    public void useSkill(int index, Hero target) {
        if (index >= 0 && index < skills.size()) {
            skills.get(index).use(this, target);
        } else {
            System.out.println("Invalid skill index.");
        }
    }

    public void takeDamage(int damage) {
        this.hp -= damage;
        if (this.hp < 0) this.hp = 0;
        System.out.println(name + " now has " + hp + " HP.");
    }

    public String getName() {
        return name;
    }

    public int getHp() {
        return hp;
    }

    public abstract void attack(Hero target);
}

4. Update Warrior and Mage to add skills

Warrior.java

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Warrior extends Hero {

    public Warrior(String name) {
        super(name, 150, 20);
        addSkill(new Skill("Heavy Strike", 30, "Deals powerful melee damage"));
    }

    @Override
    public void attack(Hero target) {
        System.out.println(name + " swings sword at " + target.getName() + " for " + attack + " damage.");
        target.takeDamage(attack);
    }
}

Mage.java

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Mage extends Hero {

    public Mage(String name) {
        super(name, 100, 25);
        addSkill(new Skill("Fireball", 35, "Deals magic damage and burns"));
    }

    @Override
    public void attack(Hero target) {
        System.out.println(name + " launches magic at " + target.getName() + " for " + attack + " damage.");
        target.takeDamage(attack);
    }
}

5. Test in Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import model.*;

public class Main {
    public static void main(String[] args) {
        Hero garen = new Warrior("Garen");
        Hero ahri = new Mage("Ahri");

        garen.attack(ahri);
        ahri.attack(garen);

        garen.useSkill(0, ahri);
        ahri.useSkill(0, garen);
    }
}
This post is licensed under CC BY 4.0 by the author.