-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7d045d
commit c984aa4
Showing
10 changed files
with
124 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
25 changes: 25 additions & 0 deletions
25
src/main/java/pink/zak/minestom/towerdefence/model/prediction/DamagePredictable.java
This file contains 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,25 @@ | ||
package pink.zak.minestom.towerdefence.model.prediction; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface DamagePredictable { | ||
|
||
@NotNull DamagePredictionHandler damagePredictionHandler(); | ||
|
||
default @NotNull Prediction addDamagePrediction(float damage) { | ||
return this.damagePredictionHandler().addPrediction(damage); | ||
} | ||
|
||
default @NotNull Prediction addDamagePrediction(long duration, float damage) { | ||
return this.damagePredictionHandler().addPrediction(duration, damage); | ||
} | ||
|
||
/** | ||
* Whether it is predicted this mob will die soon due to a damage hold from a tower. | ||
* | ||
* @return whether this mob is predicted to die soon. | ||
*/ | ||
default boolean isPredictedDead() { | ||
return this.damagePredictionHandler().isPredictedDead(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/pink/zak/minestom/towerdefence/model/prediction/DamagePredictionHandler.java
This file contains 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,59 @@ | ||
package pink.zak.minestom.towerdefence.model.prediction; | ||
|
||
import com.google.common.util.concurrent.AtomicDouble; | ||
import net.minestom.server.MinecraftServer; | ||
import net.minestom.server.timer.SchedulerManager; | ||
import net.minestom.server.timer.Task; | ||
import net.minestom.server.utils.time.TimeUnit; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Supplier; | ||
|
||
// NOTE: These preds will unnecessarily stack up. We need to remove them when the projectile hits. | ||
public final class DamagePredictionHandler { | ||
private static final SchedulerManager SCHEDULER_MANAGER = MinecraftServer.getSchedulerManager(); | ||
|
||
private final @NotNull Set<Task> cleanupTasks = ConcurrentHashMap.newKeySet(); | ||
private final @NotNull Supplier<Float> healthSupplier; | ||
|
||
private final AtomicDouble counter = new AtomicDouble(0); | ||
|
||
public DamagePredictionHandler(@NotNull Supplier<Float> healthSupplier) { | ||
this.healthSupplier = healthSupplier; | ||
} | ||
|
||
public Prediction addPrediction(float damage) { | ||
this.counter.addAndGet(damage); | ||
return new Prediction(damage, null, this); | ||
} | ||
|
||
public Prediction addPrediction(long duration, float damage) { | ||
this.counter.addAndGet(damage); | ||
Task task = SCHEDULER_MANAGER.buildTask(() -> this.counter.addAndGet(-damage)) | ||
.delay(duration, TimeUnit.MILLISECOND).schedule(); | ||
this.cleanupTasks.add(task); | ||
|
||
return new Prediction(damage, task, this); | ||
} | ||
|
||
void removePrediction(@NotNull Prediction prediction) { | ||
Task task = prediction.task(); | ||
if (task != null) { | ||
task.cancel(); | ||
this.cleanupTasks.remove(task); | ||
} | ||
|
||
this.counter.addAndGet(-prediction.damage()); | ||
} | ||
|
||
public boolean isPredictedDead() { | ||
return this.counter.get() >= this.healthSupplier.get(); | ||
} | ||
|
||
public void destroy() { | ||
this.cleanupTasks.forEach(Task::cancel); | ||
this.cleanupTasks.clear(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/pink/zak/minestom/towerdefence/model/prediction/Prediction.java
This file contains 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,12 @@ | ||
package pink.zak.minestom.towerdefence.model.prediction; | ||
|
||
import net.minestom.server.timer.Task; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public record Prediction(double damage, @Nullable Task task, @NotNull DamagePredictionHandler damagePredictable) { | ||
|
||
public void destroy() { | ||
this.damagePredictable.removePrediction(this); | ||
} | ||
} |
This file contains 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