This repository was archived by the owner on Dec 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add PowerUp class #5
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
125 changes: 125 additions & 0 deletions
125
src/main/java/com/github/codestorm/bounceverse/powerup/PowerUp.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
thnhmai06 marked this conversation as resolved.
Show resolved
Hide resolved
thnhmai06 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /** 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); | ||
thnhmai06 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
Comment on lines
+26
to
+51
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
thnhmai06 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.