Skip to content

Commit

Permalink
add sidebar to track game progression
Browse files Browse the repository at this point in the history
  • Loading branch information
Quarri6343 committed Nov 24, 2022
1 parent ddc42d3 commit 6506371
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/main/java/quarri6343/unredstone/common/ProgressionSidebar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package quarri6343.unredstone.common;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;
import quarri6343.unredstone.UnRedstone;
import quarri6343.unredstone.common.data.URData;
import quarri6343.unredstone.common.data.URTeam;

/**
* ゲームの進捗を表示するサイドバーを管理する
*/
public class ProgressionSidebar {

private static final String objectiveName = "progression";
private static Objective objective;

public static void initialize(){
objective = getBoard().getObjective(objectiveName);
if(objective == null)
objective = getBoard().registerNewObjective(objectiveName, "dummy", ChatColor.RED + "ゴールまでの距離");
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
update();
}

public static void update(){
for (int i = 0; i < getData().teams.getTeamsLength(); i++) {
URTeam team = getData().teams.getTeam(i);
if(team.getEndLocation() == null || team.locomotive == null)
continue;

Score score = objective.getScore(team.name);
int distance = (int)team.getEndLocation().distance(team.locomotive.entity.getLocation());
score.setScore(distance);
}
}

public static void destroy(){
if(objective == null)
return;

objective.unregister();
}

private static Scoreboard getBoard() {
return Bukkit.getScoreboardManager().getMainScoreboard();
}

private static URData getData() {
return UnRedstone.getInstance().getData();
}
}
5 changes: 5 additions & 0 deletions src/main/java/quarri6343/unredstone/common/data/URData.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public class URData {
* リスポーン周辺にプラグインによる保護を適用する際の範囲
*/
public static final int respawnProtectionRange = 5;

/**
* サイドバーを更新する周期
*/
public static final int updateSidebarInterval = 20;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.scheduler.BukkitRunnable;
import quarri6343.unredstone.UnRedstone;
import quarri6343.unredstone.common.GlobalTeamHandler;
import quarri6343.unredstone.common.ProgressionSidebar;
import quarri6343.unredstone.common.data.URData;
import quarri6343.unredstone.common.data.URTeam;
import quarri6343.unredstone.utils.UnRedstoneUtils;
Expand Down Expand Up @@ -76,6 +77,10 @@ public void run() {
GlobalTeamHandler.giveEffectToPlayers(PotionEffectType.SPEED, giveBuffInterval * 2, getData().buffStrength.get());
GlobalTeamHandler.giveEffectToPlayers(PotionEffectType.FAST_DIGGING, giveBuffInterval * 2, getData().buffStrength.get());
}

if (count % updateSidebarInterval == 0) {
ProgressionSidebar.update();
}
}

private URData getData() {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/quarri6343/unredstone/common/logic/URLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jetbrains.annotations.Nullable;
import quarri6343.unredstone.UnRedstone;
import quarri6343.unredstone.common.GlobalTeamHandler;
import quarri6343.unredstone.common.ProgressionSidebar;
import quarri6343.unredstone.common.data.Locomotive;
import quarri6343.unredstone.common.data.URData;
import quarri6343.unredstone.common.data.URTeam;
Expand Down Expand Up @@ -82,6 +83,7 @@ private void onGameBegin() {
Bukkit.getOnlinePlayers().forEach(player -> player.showTitle(Title.title(Component.text("ゲームスタート"), Component.empty())));

gameStatus = GameStatus.ACTIVE;
ProgressionSidebar.initialize();
gameRunnable = new GameRunnable(urTeam -> endGame(null, urTeam, URLogic.GameResult.SUCCESS, true)).runTaskTimer(UnRedstone.getInstance(), 0, 1);
}

Expand Down Expand Up @@ -142,6 +144,7 @@ public void endGame(@Nullable Player sender, @Nullable URTeam victoryTeam, GameR
}

gameStatus = GameStatus.ENDING;
ProgressionSidebar.destroy();
if (hasResultScene)
gameEndRunnable = new GameEndRunnable(() -> gameStatus = URLogic.GameStatus.INACTIVE, true).runTaskTimer(UnRedstone.getInstance(), gameResultSceneLength, 1);
else
Expand Down

0 comments on commit 6506371

Please sign in to comment.