Post

Episode 9: Turn System & Stun Logic

Goal: Integrate StunEffect into the turn system so it actually prevents actions when active.

Episode 9: Turn System & Stun Logic

1. Add isStunned() to Hero.java

Update the Hero class to detect if stunned:

1
2
3
4
5
6
7
8
public boolean isStunned() {
    for (StatusEffect effect : effects) {
        if (effect instanceof effect.StunEffect && !effect.isExpired()) {
            return true;
        }
    }
    return false;
}

2. Modify Turn Logic in Main.java

Update the PvE loop in Main.java to check for isStunned() before acting:

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
while (player.getHp() > 0 && bot.getHp() > 0) {
    System.out.println("\n=== Player Turn ===");
    player.processEffects();

    if (player.isStunned()) {
        System.out.println(player.getName() + " is stunned and skips this turn.");
    } else {
        System.out.println(player.getName() + ": " + player.getHp() + " HP");
        System.out.println(bot.getName() + ": " + bot.getHp() + " HP");

        System.out.println("1. Basic Attack");
        for (int i = 0; i < player.getSkills().size(); i++) {
            System.out.println((i + 2) + ". Skill: " + player.getSkills().get(i).getName());
        }

        System.out.print("Your choice: ");
        int action = scanner.nextInt();

        if (action == 1) {
            player.attack(bot);
        } else {
            int skillIndex = action - 2;
            player.useSkill(skillIndex, bot);
        }
    }

    if (bot.getHp() <= 0) break;

    System.out.println("\n=== Bot Turn ===");
    bot.processEffects();

    if (bot.isStunned()) {
        System.out.println(bot.getName() + " is stunned and skips this turn.");
    } else {
        if (random.nextBoolean() || bot.getSkills().isEmpty()) {
            bot.attack(player);
        } else {
            int skillIndex = random.nextInt(bot.getSkills().size());
            bot.useSkill(skillIndex, player);
        }
    }
}

This post is licensed under CC BY 4.0 by the author.