GameTimer.java

1
package io.github.some_example_name;
2
3
import com.badlogic.gdx.Gdx;
4
import com.badlogic.gdx.audio.Sound;
5
import com.badlogic.gdx.scenes.scene2d.ui.Label;
6
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
7
import com.badlogic.gdx.scenes.scene2d.ui.Table;
8
9
/**
10
 * <code>GameTimer</code> implements timer during gameplay, which makes a sound
11
 * when it reaches 0.
12
 * <p>
13
 * It contains a sprite to display to the user graphically
14
 * how much time is left.
15
 *
16
 * @since 2025-11-04 19:28:26
17
 */
18
public class GameTimer {
19
20
    private static final float DEFAULT_START_SECONDS = 300f;
21
    private final GameTimerLogic logic;
22
    private Label timerLabel;
23
    private final Sound timeUpSound;
24
    private boolean soundPlayed = false;
25
26
    /**
27
     * Constructs a new <code>GameTimer</code> instance compatible with GameScreen.
28
     */
29
    public GameTimer(Skin skin, Table table, MyGame game) {
30
        this(skin, table, DEFAULT_START_SECONDS);
31
    }
32
33
    /**
34
     * Constructs a new <code>GameTimer</code> instance using an explicit start time.
35
     */
36
    public GameTimer(Skin skin, Table table, float startSeconds) {
37
        this.logic = new GameTimerLogic(startSeconds);
38
39 1 1. <init> : removed call to io/github/some_example_name/GameTimer::instantiateLabel → NO_COVERAGE
        instantiateLabel(skin, table);
40
41
        this.timeUpSound = Gdx.audio.newSound(
42
            Gdx.files.internal("ding.wav")
43
        );
44
    }
45
46
    /**
47
     * Decrements timer by given value or to 0 if new value will be
48
     * negative.
49
     */
50
    public void decrementTimer(float delta) {
51 1 1. decrementTimer : removed call to io/github/some_example_name/GameTimerLogic::decrement → NO_COVERAGE
        logic.decrement(delta);
52 1 1. decrementTimer : removed call to io/github/some_example_name/GameTimer::updateLabel → NO_COVERAGE
        updateLabel();
53
54 1 1. decrementTimer : negated conditional → NO_COVERAGE
        if (logic.isTimeUp()) {
55 1 1. decrementTimer : removed call to io/github/some_example_name/GameTimer::onTimeUp → NO_COVERAGE
            onTimeUp();
56
        }
57
    }
58
59
    /**
60
     * Handles timer reaching zero.
61
     */
62
    public void onTimeUp() {
63 1 1. onTimeUp : negated conditional → NO_COVERAGE
        if (!soundPlayed) {
64
            timeUpSound.play();
65
            soundPlayed = true;
66
        }
67
    }
68
69
    /**
70
     * Returns time left in seconds.
71
     */
72
    public float getTimeLeft() {
73 1 1. getTimeLeft : replaced float return with 0.0f for io/github/some_example_name/GameTimer::getTimeLeft → NO_COVERAGE
        return logic.getTimeLeft();
74
    }
75
76
    /**
77
     * Returns timer label for rendering.
78
     */
79
    public Label getTimerLabel() {
80 1 1. getTimerLabel : replaced return value with null for io/github/some_example_name/GameTimer::getTimerLabel → NO_COVERAGE
        return this.timerLabel;
81
    }
82
83
    /**
84
     * Return time formatted in mm:ss for time left.
85
     */
86
    @Override
87
    public String toString() {
88
        float timeLeft = logic.getTimeLeft();
89 1 1. toString : Replaced float division with multiplication → NO_COVERAGE
        long minutes = (long) Math.floor(timeLeft / 60);
90 2 1. toString : Replaced long subtraction with addition → NO_COVERAGE
2. toString : Replaced long multiplication with division → NO_COVERAGE
        long seconds = (long) Math.floor(timeLeft) - (minutes * 60);
91 1 1. toString : replaced return value with "" for io/github/some_example_name/GameTimer::toString → NO_COVERAGE
        return String.format("%02d : %02d", minutes, seconds);
92
    }
93
94
    /**
95
     * Helper method to create and add label to given table.
96
     */
97
    private void instantiateLabel(Skin skin, Table table) {
98
        this.timerLabel = new Label("", skin);
99 1 1. instantiateLabel : removed call to com/badlogic/gdx/scenes/scene2d/ui/Cell::row → NO_COVERAGE
        table.add(this.timerLabel).row();
100 1 1. instantiateLabel : removed call to io/github/some_example_name/GameTimer::updateLabel → NO_COVERAGE
        updateLabel();
101
    }
102
103
    /**
104
     * Updates timer label text based on remaining time.
105
     */
106
    private void updateLabel() {
107 1 1. updateLabel : removed call to com/badlogic/gdx/scenes/scene2d/ui/Label::setText → NO_COVERAGE
        timerLabel.setText(toString());
108
    }
109
110
    /**
111
     * Adds bonus time to the timer.
112
     *
113
     * @param seconds amount of time to add
114
     */
115
    public void addTime(float seconds) {
116 1 1. addTime : removed call to io/github/some_example_name/GameTimerLogic::addTime → NO_COVERAGE
        logic.addTime(seconds);
117 1 1. addTime : removed call to io/github/some_example_name/GameTimer::updateLabel → NO_COVERAGE
        updateLabel();
118
    }
119
120
}

Mutations

39

1.1
Location : <init>
Killed by : none
removed call to io/github/some_example_name/GameTimer::instantiateLabel → NO_COVERAGE

51

1.1
Location : decrementTimer
Killed by : none
removed call to io/github/some_example_name/GameTimerLogic::decrement → NO_COVERAGE

52

1.1
Location : decrementTimer
Killed by : none
removed call to io/github/some_example_name/GameTimer::updateLabel → NO_COVERAGE

54

1.1
Location : decrementTimer
Killed by : none
negated conditional → NO_COVERAGE

55

1.1
Location : decrementTimer
Killed by : none
removed call to io/github/some_example_name/GameTimer::onTimeUp → NO_COVERAGE

63

1.1
Location : onTimeUp
Killed by : none
negated conditional → NO_COVERAGE

73

1.1
Location : getTimeLeft
Killed by : none
replaced float return with 0.0f for io/github/some_example_name/GameTimer::getTimeLeft → NO_COVERAGE

80

1.1
Location : getTimerLabel
Killed by : none
replaced return value with null for io/github/some_example_name/GameTimer::getTimerLabel → NO_COVERAGE

89

1.1
Location : toString
Killed by : none
Replaced float division with multiplication → NO_COVERAGE

90

1.1
Location : toString
Killed by : none
Replaced long subtraction with addition → NO_COVERAGE

2.2
Location : toString
Killed by : none
Replaced long multiplication with division → NO_COVERAGE

91

1.1
Location : toString
Killed by : none
replaced return value with "" for io/github/some_example_name/GameTimer::toString → NO_COVERAGE

99

1.1
Location : instantiateLabel
Killed by : none
removed call to com/badlogic/gdx/scenes/scene2d/ui/Cell::row → NO_COVERAGE

100

1.1
Location : instantiateLabel
Killed by : none
removed call to io/github/some_example_name/GameTimer::updateLabel → NO_COVERAGE

107

1.1
Location : updateLabel
Killed by : none
removed call to com/badlogic/gdx/scenes/scene2d/ui/Label::setText → NO_COVERAGE

116

1.1
Location : addTime
Killed by : none
removed call to io/github/some_example_name/GameTimerLogic::addTime → NO_COVERAGE

117

1.1
Location : addTime
Killed by : none
removed call to io/github/some_example_name/GameTimer::updateLabel → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.15.0