-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimedEffect.java
More file actions
36 lines (32 loc) · 1.33 KB
/
Copy pathTimedEffect.java
File metadata and controls
36 lines (32 loc) · 1.33 KB
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This class represents a timed effect that applies an effect for a certain duration.
*
* @authors (Rupert & Raymond Van Niekerk)
* @student Numbers (222894237 & 221154469)
* @version (1.0)
*/
public class TimedEffect extends Actor {
private long startTime; // The time when the effect started
private int duration; // The duration of the effect in milliseconds
private Runnable effect; // The effect to apply immediately
private Runnable endEffect; // The effect to apply when the duration ends
public TimedEffect(int duration, Runnable effect, Runnable endEffect) {
// Constructor initializes the duration and the effects
this.duration = duration;
this.effect = effect;
this.endEffect = endEffect;
}
protected void addedToWorld(World world) {
// Called when the object is added to the world, start the timer and run the initial effect
startTime = System.currentTimeMillis();
effect.run();
}
public void act() {
// Called in every cycle, check if the duration has ended to run the end effect and remove the object
if (System.currentTimeMillis() - startTime >= duration) {
endEffect.run();
getWorld().removeObject(this);
}
}
}