aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMyles <mylesandmore9@gmail.com>2022-12-02 23:29:31 -0600
committerMyles <mylesandmore9@gmail.com>2022-12-02 23:29:31 -0600
commit3a8fcb3aaec9d1c43f9d7a2f04cdbfb5b43696b3 (patch)
tree50b3496d3b7e52c948b58f4f0986c93ffecd9d1c
parent8273f0654e8dac3ff2ce0da422afce12c54cd0e3 (diff)
downloadTumble-3a8fcb3aaec9d1c43f9d7a2f04cdbfb5b43696b3.tar.gz
Tumble-3a8fcb3aaec9d1c43f9d7a2f04cdbfb5b43696b3.tar.bz2
Tumble-3a8fcb3aaec9d1c43f9d7a2f04cdbfb5b43696b3.zip
new Game management
GameManager.java has been superseded by Game.java this is where the game will be managed from now on; created, scores tracked, ended, etc (this was way harder than it looks to code...) AND IT STILL DOESNT WORK AOIFHJRGRI HRSDO
-rw-r--r--src/main/java/com/MylesAndMore/tumble/Game.java182
-rw-r--r--src/main/java/com/MylesAndMore/tumble/GameManager.java68
2 files changed, 142 insertions, 108 deletions
diff --git a/src/main/java/com/MylesAndMore/tumble/Game.java b/src/main/java/com/MylesAndMore/tumble/Game.java
index 0f54021..aa86e12 100644
--- a/src/main/java/com/MylesAndMore/tumble/Game.java
+++ b/src/main/java/com/MylesAndMore/tumble/Game.java
@@ -1,54 +1,132 @@
package com.MylesAndMore.tumble;
-import org.bukkit.Bukkit;
+import com.MylesAndMore.tumble.api.Generator;
+import org.bukkit.*;
import org.bukkit.entity.Player;
-import org.bukkit.Location;
-import org.bukkit.World;
+import org.bukkit.inventory.ItemStack;
+import org.jetbrains.annotations.NotNull;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
-public final class Game {
+public class Game {
+ // Singleton class logic
// Define the gameInstance
private static Game gameInstance;
// Private Game() constructor for singleton instance
private Game() { }
- // Other private methods for getters and setters go here
-
+ // ONLY Public method to get the game instance
+ public static Game getGame() {
+ if (gameInstance == null) {
+ gameInstance = new Game();
+ }
+ return gameInstance;
+ }
+
+
+
// Define local game vars
// The gameType keeps the current game type (shocker)
- static String gameType = null;
+ private String gameType = null;
+ // The gameState keeps the current state of the game (I'm so creative, I know)
+ private String gameState = null;
+
+ // Initialize a new instance of the Random class for use later
+ private Random Random = new Random();
+ // Define the game world and its spawnpoint as a new Location for use later
+ private World gameWorld = Bukkit.getWorld(TumbleManager.getGameWorld());
+ private Location gameSpawn = gameWorld.getSpawnLocation();
+
+ // Make a list of the game's players for later
+ private List<Player> gamePlayers = null;
// Creates a new Game
// This will return true if the game succeeds creation, and false if not
- private static boolean createGame() {
+ public boolean startGame() {
+ gameState = "starting";
if (Objects.equals(TumbleManager.getGameType(), "shovels")) {
- // shovels logic
+ // Set the correct gameType for the game we're playing, for later
gameType = "shovels";
+ // Generate the correct layers for a Shovels game
+ // The else statement is just in case the generator fails; this command will fail
+ if (generateLayers(gameType)) {
+ // If the layer generation succeeds, give players diamond shovels
+ // Reminder: we need a way for blocks to break instantly and for these to not lose durability!
+ giveItems(new ItemStack(Material.DIAMOND_SHOVEL));
+ // Send players to the game
+ sendPlayers();
+ // Keep in mind that after this runs, this command will complete and return true
+ }
+ else {
+ return false;
+ }
}
else if (Objects.equals(TumbleManager.getGameType(), "snowballs")) {
- // snowballs
gameType = "snowballs";
+ if (generateLayers(gameType)) {
+ // Reminder: we need a way to make this snowball infinite!
+ giveItems(new ItemStack(Material.SNOWBALL));
+ sendPlayers();
+ }
+ else {
+ return false;
+ }
}
else if (Objects.equals(TumbleManager.getGameType(), "mixed")) {
- // mixed (choose random shovels or snowballs)
gameType = "mixed";
+ // Mixed gamemode (choose random shovels or snowballs)
+ if (Random.nextInt(2) == 0) {
+ generateLayers("shovels");
+ giveItems(new ItemStack(Material.DIAMOND_SHOVEL));
+ sendPlayers();
+ }
+ else {
+ generateLayers("snowballs");
+ giveItems(new ItemStack(Material.SNOWBALL));
+ sendPlayers();
+ }
}
else {
// The game type in the config did not match a specified game type; return false to signify that
return false;
}
+ // Update the game's players for later
+ gamePlayers = new ArrayList<>(TumbleManager.getPlayersInGame());
+ gameState = "running";
return true;
}
- private static void sendPlayers() {
- World gameWorld = Bukkit.getWorld(TumbleManager.getGameWorld());
- // Define the game world's spawnpoint as a new Location
- Location gameSpawn = gameWorld.getSpawnLocation();
+ private boolean generateLayers(String gameType) {
+ Location layer = gameSpawn;
+ if (Objects.equals(gameType, "shovels")) {
+ layer.setY(layer.getY() - 1);
+ Generator.generateLayer(layer, 17, 1, Material.SNOW_BLOCK);
+ Generator.generateLayer(layer, 13, 1, Material.AIR);
+ layer.setY(layer.getY() - 1);
+ Generator.generateLayer(layer, 13, 1, Material.GRASS_BLOCK);
+ layer.setY(layer.getY() - 1);
+ Generator.generateLayer(layer, 4, 1, Material.PODZOL);
+ layer.setY(layer.getY() + 1);
+ Generator.generateLayer(layer, 4, 2, Material.GRASS);
+ }
+ else if (Objects.equals(gameType, "snowballs")) {
+ layer.setY(layer.getY() - 1);
+ Generator.generateLayer(layer, 17, 1, Material.COAL_ORE);
+ Generator.generateLayer(layer, 13, 1, Material.AIR);
+ layer.setY(layer.getY() - 1);
+ Generator.generateLayer(layer, 13, 1, Material.GRANITE);
+ Generator.generateLayer(layer, 4, 1, Material.AIR);
+ layer.setY(layer.getY() - 1);
+ Generator.generateLayer(layer, 4, 1, Material.LIME_GLAZED_TERRACOTTA);
+ }
+ else {
+ return false;
+ }
+ return true;
+ }
+
+ private void sendPlayers() {
// Get the X, Y, and Z coords of that location
double x = gameSpawn.getX();
double y = gameSpawn.getY();
@@ -56,42 +134,66 @@ public final class Game {
// Create Locations to scatter players around the first layer
// These are just edited off the original spawn location;
// they assume that the first layer has a radius of 17 blocks (it always will w/ the current generator code)
- List<Location> scatterLocations = Arrays.asList(
- new Location(gameWorld, (x - 16), y, z),
- new Location(gameWorld, x, y, (z - 16)),
- new Location(gameWorld, (x + 16), y, z),
- new Location(gameWorld, x, y, (z + 16)));
+ List<Location> scatterLocations = new ArrayList<>();
+ scatterLocations.addAll(List.of(
+ new Location(gameWorld, (x - 14.5), y, (z + 0.5) , -90, 0),
+ new Location(gameWorld, (x + 0.5), y, (z - 14.5), 0, 0),
+ new Location(gameWorld, (x + 15.5), y, (z + 0.5), 90, 0),
+ new Location(gameWorld, (x + 0.5), y, (z + 15.5), 180, 0 ),
+ new Location(gameWorld, (x - 10.5), y, (z - 10.5), -45, 0),
+ new Location(gameWorld, (x - 10.5), y, (z + 11.5), -135, 0),
+ new Location(gameWorld, (x + 11.5), y, (z - 10.5), 45, 0),
+ new Location(gameWorld, (x + 11.5), y, (z + 11.5), 135, 0))
+ );
// Shuffle the location list so players don't always spawn in the same location (basically, actually scatter the locations)
Collections.shuffle(scatterLocations);
// While there are still players in the lobby, send them to the gameWorld
// This is just a way of sending everybody in the lobby to the game
- for (List<Player> playersInLobby = TumbleManager.getPlayersInLobby(); playersInLobby.size() > 0; playersInLobby = TumbleManager.getPlayersInLobby(), scatterLocations.remove(0)) {
+ for (Player aPlayer : TumbleManager.getPlayersInLobby()) {
+ //for (List<Player> playersInLobby = TumbleManager.getPlayersInLobby(); playersInLobby.size() > 0; playersInLobby = TumbleManager.getPlayersInLobby()) {
// Get a singular player from the player list
- Player aPlayer = playersInLobby.get(0);
+ //Player aPlayer = playersInLobby.get(0);
// Get a singular location from the scatter list
Location aLocation = scatterLocations.get(0);
// Teleport that player to that scatter location
aPlayer.teleport(aLocation);
+ // Remove that location from the list so that it cannot be used again
+ scatterLocations.remove(0);
}
+ }
- // Add a little break because it can take the clients a bit to load into the new world
- // Then, transition to another method because this one is getting really long
- // In that method: set a flag to monitor the playerDeathEvent so we know when all the players have died
- // Also start music
+ public void playerDeath(@NotNull Player player) {
+ player.setGameMode(GameMode.SPECTATOR);
+ // If there are more than 2 players in the game,
+ if (gamePlayers.size() > 2) {
+ // remove that player (who just died) from the gamePlayersArray, effectively eliminating them,
+ gamePlayers.remove(player);
+ }
+ // otherwise, the game must have two people left (and one just died), meaning it is over
+ // This logic is so that it will not remove the last player standing from the list, so we know who the winner is.
+ else {
+ // End the game, passing the winner to the gameEnd method
+ gameEnd(gamePlayers.get(0));
+ }
}
+ private void gameEnd(@NotNull Player winner) {
+ Bukkit.getServer().broadcastMessage(ChatColor.GREEN + winner.getName() + " has won!");
+ }
- // Public method to get the game instance (singleton logic)
- public static Game getGame() {
- if (gameInstance == null) {
- gameInstance = new Game();
+ private void giveItems(ItemStack itemStack) {
+ for (List<Player> playersWithoutItem = TumbleManager.getPlayersInLobby(); playersWithoutItem.size() > 0; playersWithoutItem.remove(0)) {
+ // Get a singular player from the player list
+ Player playerWithoutItem = playersWithoutItem.get(0);
+ // Give that player the specified item
+ playerWithoutItem.getInventory().addItem(itemStack);
}
- return gameInstance;
}
- // Public method to get the game type
- // This is public because you aren't modifying the game, just getting its type, so there shouldn't be any conflicts
- public static String getGameType() {
- return gameType;
- }
+
+ // Methods to get the game type and game state for other classes outside the Game
+ private String getGameType() { return gameType; }
+
+ private String getGameState() { return gameState; }
+
} \ No newline at end of file
diff --git a/src/main/java/com/MylesAndMore/tumble/GameManager.java b/src/main/java/com/MylesAndMore/tumble/GameManager.java
deleted file mode 100644
index a00d764..0000000
--- a/src/main/java/com/MylesAndMore/tumble/GameManager.java
+++ /dev/null
@@ -1,68 +0,0 @@
-package com.MylesAndMore.tumble;
-
-import com.MylesAndMore.tumble.api.Generator;
-import org.bukkit.Bukkit;
-import org.bukkit.Location;
-import org.bukkit.Material;
-import org.bukkit.entity.Player;
-import org.bukkit.inventory.ItemStack;
-
-import java.util.List;
-import java.util.Objects;
-
-public class GameManager {
- public boolean TumbleGame(String gameType) {
- // Run createGame to distribute items
- // Make a method to generate random layers (so we can reuse it later)
- // Run sendWorld (move from startGame)
- // Start blocking new games and start keeping score/rounds
- return true;
- }
-
- public static boolean createGame(String gameType) {
- if (Objects.equals(gameType, "shovels")) {
- // Generate layers
- Location layer = Bukkit.getWorld(TumbleManager.getGameWorld()).getSpawnLocation();
- layer.setY(layer.getY() - 1);
- Generator.generateLayer(layer, 17, 1, Material.SNOW_BLOCK);
- Generator.generateLayer(layer, 13, 1, Material.AIR);
- layer.setY(layer.getY() - 1);
- Generator.generateLayer(layer, 13, 1, Material.GRASS_BLOCK);
- layer.setY(layer.getY() - 1);
- Generator.generateLayer(layer, 4, 1, Material.PODZOL);
- layer.setY(layer.getY() + 1);
- Generator.generateLayer(layer, 4, 2, Material.GRASS);
- // Give players diamond shovels
- giveItems(new ItemStack(Material.DIAMOND_SHOVEL));
- // Pass on the game type
-
- }
- else if (Objects.equals(gameType, "snowballs")) {
- // Generate three layers
- // (Will make this customizable in later versions)
- // Remember, the snowballs don't interact with players!
-
- // Give players infinite snowballs
-
- // Pass on the game type
- }
- else if (Objects.equals(gameType, "mixed")) {
- // Randomly select rounds from above
-
- // Pass on the game type
- }
- else {
- return false;
- }
- return true;
- }
-
- public static void giveItems(ItemStack itemStack) {
- for (List<Player> playersWithoutItem = TumbleManager.getPlayersInLobby(); playersWithoutItem.size() > 0; playersWithoutItem.remove(0)) {
- // Get a singular player from the player list
- Player playerWithoutItem = playersWithoutItem.get(0);
- // Give that player the specified item
- playerWithoutItem.getInventory().addItem(itemStack);
- }
- }
-}