Skip to content
This repository was archived by the owner on Dec 24, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/google-java-format.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.1.0'
id 'com.diffplug.spotless' version "8.0.0"
}

Expand All @@ -24,6 +25,11 @@ test {
useJUnitPlatform()
}

javafx {
version = "25"
modules = [ 'javafx.base' ]
}

spotless {
format 'misc', {
target '*.gradle', '.gitattributes', '.gitignore'
Expand Down
Empty file.
125 changes: 125 additions & 0 deletions src/main/java/com/github/codestorm/bounceverse/powerup/PowerUp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.github.codestorm.bounceverse.powerup;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.time.TimerAction;
import javafx.util.Duration;
import org.jetbrains.annotations.NotNull;

/**
*
*
* <h1><b>PowerUp</b></h1>
*
* PowerUps provide special effects and abilities to playable objects. <i>They are designed to
* update based on events (lazy-update).</i>
*
* <p><i>You need to extend this class to create another class that can provide effects though
* {@code apply()} and {@code doApply()} method.</i>
*/
public abstract class PowerUp {
private TimerAction waitAction;
private TimerAction activeAction;
private double startAt;
private double endAt;

/** Update {@code waitAction} when {@code startAt} is changed. */
private void updateWaitAction() {
final var gameTimer = FXGL.getGameTimer();
final var currentTime = gameTimer.getNow();
// when not executed
if (currentTime < startAt) {
if (waitAction != null) {
waitAction.expire();
}
final var timeLeft = Duration.millis(startAt - currentTime);
waitAction = gameTimer.runOnceAfter(this::doApply, timeLeft);
}
}

/** Update {@code activeAction} when {@code endAt} is changed. */
private void updateActiveAction() {
final var gameTimer = FXGL.getGameTimer();
final var currentTime = gameTimer.getNow();
// when not executed
if (currentTime < endAt) {
if (activeAction != null) {
activeAction.expire();
}
final var timeLeft = Duration.millis(endAt - currentTime);
activeAction = gameTimer.runOnceAfter(this::doUnapply, timeLeft);
}
}
Comment on lines +26 to +51
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need situation for startAt >= currentTime, and checking if startAt >= endAt


/** Wrapper for {@code apply()} method. */
protected void doApply() {
apply();
}

/** Wrapper for {@code unapply()} method. */
protected void doUnapply() {
unapply();
}

/**
* Apply PowerUp effects.
*
* <p>Automatically used when power up becomes active.
*/
protected abstract void apply();

/**
* Unapply PowerUp effects.
*
* <p>Automatically used when power up ends.
*/
protected abstract void unapply();

public double getStartAt() {
return startAt;
}

public double getEndAt() {
return endAt;
}

public void setStartAt(double startAt) {
this.startAt = startAt;
updateWaitAction();
}

public void setEndAt(double endAt) {
this.endAt = endAt;
updateActiveAction();
}

/**
* Create PowerUp object.
*
* @param startAt Time that PowerUp start
* @param endAt Time that PowerUp end
*/
public PowerUp(double startAt, double endAt) {
setStartAt(startAt);
setEndAt(endAt);
}

/**
* Create PowerUp object.
*
* @param duration Duration
* @param after Duration to wait before active
*/
public PowerUp(@NotNull Duration duration, @NotNull Duration after) {
final var currentTime = FXGL.getGameTimer().getNow();
this(currentTime + after.toMillis(), currentTime + after.toMillis() + duration.toMillis());
}

/**
* Create PowerUp object and active.
*
* @param duration Duration
*/
public PowerUp(@NotNull Duration duration) {
this(duration, Duration.ZERO);
}
}