Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/main/java/edu/sdccd/cisc191/game/GalacticShip.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
* Each ship has a name, health, attack power, and combat abilities, and can engage in combat.
*/
public class GalacticShip {
// TODO: make field final, since they are never changed after initialization
private String name;
private int health;
private int attackPower;
// TODO: make field final, since they are never changed after initialization
private List<CombatAbility> combatAbilities;

/**
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/edu/sdccd/cisc191/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.sdccd.cisc191.subsystems.CombatSystem;
import edu.sdccd.cisc191.subsystems.ExplorationSystem;
import edu.sdccd.cisc191.subsystems.ResourceManagement;
// TODO: remove unused import
import edu.sdccd.cisc191.game.PlayerInventory;
import edu.sdccd.cisc191.utilities.GameUI;

Expand All @@ -27,6 +28,8 @@

// Main Game Class (Integrates JavaFX, Shipyard System, and Exploration System)
public class Game extends Application {
// TODO: break Game into different classes for UI, game logic, and subsystems
// TODO: create separate packages for game resources, like Planet, Player, PlayerInventory, etc.
private Shipyard shipyard;
private ExplorationSystem explorationSystem;
private ResourceManagement resourceManagement;
Expand Down Expand Up @@ -56,7 +59,8 @@ public static void main(String[] args) { // Launch the JavaFX application
}

@Override
public void start (Stage primaryStage) { // Main window
public void start (Stage primaryStage) {// Main window
// TODO: split this portion to load game into a separate method
// Initialize game components
shipyard = new Shipyard();
explorationSystem = new ExplorationSystem();
Expand All @@ -72,17 +76,21 @@ public void start (Stage primaryStage) { // Main window
gameState = GameState.MENU;

// Create UI elements
// TODO: separate these methods to load specific javafx components into separate components
statusLabel = new Label("Welcome to Galactic Strategy! (Press ENTER to start)");
fleetListView = new ListView<>();
updateFleetDisplay(); // Load fleet data

gameLog = new TextArea();
gameLog.setEditable(false);
// TODO: make this view larger in order to display more player information
gameLog.setPrefHeight(150);

resourceLabel = new Label("Resources:\n" + inventory.displayResources());

// Shipyard UI Buttons
// TODO: utilize JavaFX CSS styling to add more detail and differentiation between these buttons
// TODO: display all buttons if the GameState is menu, paused, or gameover
Button buildFighterBtn = new Button("Build Fighter (10 Minerals, 5 Energy)");
Button buildCruiserBtn = new Button("Build Cruiser (15 Minerals, 7 Energy)");
Button buildBattleshipBtn = new Button("Build Battleship (20 Minerals, 10 Energy)");
Expand Down Expand Up @@ -114,20 +122,22 @@ public void start (Stage primaryStage) { // Main window
HBox moveRow2 = new HBox(10, leftBtn, downBtn, rightBtn);
VBox movementControls = new VBox(5, moveRow1, moveRow2);

// TODO: add UI component to display amount of minerals, energy of user within the javafx UI

// Layout for the UI
VBox layout = new VBox(10);
layout.getChildren().addAll(
statusLabel, locationLabel, fleetListView, buildFighterBtn, buildCruiserBtn,
buildBattleshipBtn, upgradeShipBtn, gatherDilithiumBtn,
planetSelector, exploreBtn, movementControls, gameLog, resourceLabel
);;
);

Scene scene = new Scene(layout, 500, 600);

// Keyboard controls for game state changes
scene.setOnKeyPressed(event -> handleKeyPress(event.getCode()));


primaryStage.setTitle("Galactic Strategy"); // Set Title window (Primary stage)
primaryStage.setScene(scene); // Attach the scene to the primary stage
primaryStage.show(); // Displays primary stage (Opens window)
Expand All @@ -148,16 +158,17 @@ public void handle(long now) { // 'now' is the current timestamp in nanoseconds
}

// Handles keyboard input for game state management
// TODO: remap buttons, ENTER and ESCAPE for start and ending respectively don't appear to work, at least on Mac
private void handleKeyPress(KeyCode key) {
switch (key) {
case ENTER:
case A:
if (gameState == GameState.MENU) gameState = GameState.PLAYING;
break;
case P:
if (gameState == GameState.PLAYING) gameState = GameState.PAUSED;
else if (gameState == GameState.PAUSED) gameState = GameState.PLAYING;
break;
case ESCAPE:
case E:
gameState = GameState.GAME_OVER;
break;
}
Expand Down Expand Up @@ -195,12 +206,13 @@ private void handleMove(String direction) {
locationLabel.setText("Location: (" + r + "," + c + ")");
statusLabel.setText(planet.equals("Unknown") ? "Empty space" : "Arrived at " + planet);
resourceLabel.setText("Resource:\n" + inventory.displayResources());
gameLog.appendText("Moved " + direction + " to (" + r + "," + c + ")\n");
gameUI.appendMessage("Moved " + direction + " to (" + r + "," + c + ")\n");
} else {
statusLabel.setText("Move failed (no fuel or out of bounds)");
}
}

// TODO: delete this method to test combat that is never used
private void runCombatExample() {
GalacticShip playerShip = new GalacticShip("Enterprise", 100, 20);
GalacticShip enemyShip = new GalacticShip("Klingon Raider", 80, 18);
Expand All @@ -212,14 +224,14 @@ private void runCombatExample() {
// Optionally, display result in the UI if gameUI is available
if (gameUI != null) {
if (playerShip.isDestroyed()) {

gameUI.appendMessage(playerShip.getName() + "was destroyed! GAME OVER!");
} else if (enemyShip.isDestroyed()) {
gameUI.appendMessage(playerShip.getName() + "was destroyed! Victory!");
}
}
}

// TODO: delete this method to load UI that was never used
private void setupUI(Stage stage) {
gameUI = new GameUI();
StackPane root = new StackPane();
Expand All @@ -238,6 +250,7 @@ private void setupUI(Stage stage) {
* @param shipType The Type of ship to build
*/
private void buildShip(String shipType, int mineralsCost, int energyCost) {
System.out.println(mineralsCost + " " + energyCost + " " + shipType);
if (inventory == null || !inventory.useResource("Minerals", mineralsCost) || !inventory.useResource("Energy", energyCost)) {
statusLabel.setText("Not enough resources to build " + shipType);
return;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/edu/sdccd/cisc191/game/GameBoard.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.sdccd.cisc191.game;

public class GameBoard {
// TODO: planets and resourceCosts could be final, since they are never changed after initialization
private int[][] planets; // Represents planets in the galaxy (0 = empty, other values = planet IDs)
private int[][] resourceCosts; // Represents resource cost to traverse each cell
private final int rows = 5;
Expand Down Expand Up @@ -63,6 +64,7 @@ public int getPlanetId(int row, int col) {
return planets[row][col];
}

// TODO: write javadocs for the remaining methods within the class
public int getResourceCost(int row, int col) {
return resourceCosts[row][col];
}
Expand All @@ -71,6 +73,7 @@ public boolean inBounds(int row, int col) {
return row >= 0 && row < rows && col >= 0 && col < cols;
}

// TODO: delete or implement unused displayBoard method
public void displayBoard() {
System.out.println("Planets:");
for (int i = 0; i < rows; i++) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/edu/sdccd/cisc191/game/Planet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Planet {
public final String neptune;

// Existing 5-argument constructor
// TODO: remove extra unused constructor
public Planet(String mars, String jupiter, String earth, String andromeda, String neptune) {
this.mars = mars;
this.earth = earth;
Expand All @@ -26,6 +27,8 @@ public Planet(String planetName) {
this.neptune = "Neptune".equalsIgnoreCase(planetName) ? planetName : null;
}

// TODO: implement or delete unused methods
// TODO: for these methods, if the planet is not found, return a default value of null.
public String getEarth() {
return earth != null ? earth : "Earth";
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/edu/sdccd/cisc191/game/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* Each player has a name and a fleet of GalacticShips.
*/
public class Player {
// TODO: set the name and fleet fields to final, since they are never changed after initialization
private String name;
// TODO: make fleet a LinkedList (video explanation says that this should be a LinkedList but is type List)
private List<GalacticShip> fleet;

/**
Expand All @@ -22,6 +24,7 @@ public Player(String name) {
this.fleet = new ArrayList<>();
}

// TODO: use a library like lombok to auto generate getters and setters
public String getName() {
return name;
}
Expand All @@ -35,6 +38,7 @@ public List<GalacticShip> getFleet() {
*
* @param ship The GalacticShip to add.
*/
// TODO: remove the duplicate ship addition to the ArrayList
public void addShip(GalacticShip ship) {
fleet.add(ship);
fleet.add(ship);
Expand All @@ -52,4 +56,6 @@ public int getTotalFleetHealth() {
}
return totalHealth;
}

// TODO: implement a method to use a Linear Search to find ships within the fleet List
}
2 changes: 2 additions & 0 deletions src/main/java/edu/sdccd/cisc191/game/PlayerInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import java.util.Map;

// Manages player's inventory of resources
// TODO: implement PlayerInventory to a database
public class PlayerInventory implements Serializable {
private Map<String, Resource> resources;

public PlayerInventory() {
resources = new HashMap<>();
resources.put("Fuel", new Resource("Dilithium"));
// TODO: remove duplicate "Minerals" entry, or rename the first minerals to be Fuel
resources.put("Minerals", new Resource("Fuel"));
resources.put("Minerals", new Resource("Minerals"));
resources.put("Energy", new Resource("Energy"));
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/sdccd/cisc191/game/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Resource implements Serializable {

public Resource(String name) {
this.name = name;
// TODO: no parameter in the constructor for amount, either remove it from the constructor or set it to 0 by default
this.amount = amount;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/edu/sdccd/cisc191/game/Shipyard.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Shipyard {
// Constructs a Shipyard with predefined ship options
public Shipyard() {
this.availableShips = new HashMap<>();
this.playerFleet = new ArrayList<>();
this.playerFleet = new ArrayList<>(); // TODO: integrate with the Player by allowing a Shipyard to take in a Player object, and initializing playerFleet to the the player's fleet object
this.shipBuilderPool = Executors.newFixedThreadPool(2); // Allows 2 ships to be built at a time

initializeShipyard();
Expand Down Expand Up @@ -110,6 +110,7 @@ public void displayPlayerFleet() {
}
}

// TODO: add a method to be used in saveShipyardState() and loadShipyardState() to check first if the file exists, and if it doesn't to create a file ships.json in the resources folder
/*
* Retrieves the player's fleet
* @return List of GalacticShips in the player's fleet
Expand All @@ -124,6 +125,7 @@ private void saveShipyardState() {
out.writeObject(playerFleet);
System.out.println("Shipyard state saved.");
} catch (IOException e) {
// TODO: if file doesn't exist, create it.
System.err.println("Error saving shipyard state: " + e.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* - Provides utility methods for managing game state
*/

// TODO: delete or implement unused Server code
public class MultiplayerHandler {

public final Map<String, PlayerData> players; // Map to store player data by unique ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

public class MultiplayerServer {
private static final int PORT = 5000; // Port for communication
// TODO: clientWriters never changed, so it could be final
private static Set<PrintWriter> clientWriters = new HashSet<>();

// TODO: replace System.out.println calls with loggers
public static void main(String[] args) {
System.out.println("Multiplayer Server Started...");
try (ServerSocket serverSocket = new ServerSocket (PORT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
public class ResourceManagement {
private final Lock lock = new ReentrantLock();

// TODO: clean up multiple different gatherResources methods
public void gatherResources(Player player, String resourceName, PlayerInventory inventory) {
lock.lock();
try {
int collectedAmount = (int) (Math.random() * 10 + 5); // Random between 5-15
inventory.addResource(resourceName, collectedAmount);
System.out.println(player.getName() + " collcted " + collectedAmount + " " + resourceName + "!");
// TODO: replace System.out.println with logging this information to the in-game UI log
System.out.println(player.getName() + " collected " + collectedAmount + " " + resourceName + "!");
} finally {
lock.unlock();
}
Expand All @@ -25,7 +27,8 @@ public void gatherResources(Player player2, Resource dilithium) {
lock.lock();
try {
int collectedAmount = (int) (Math.random() * 10 + 5);
System.out.println(player2.getName() + " collcted " + collectedAmount + " " + player2.getName() + "!");
// TODO: replace System.out.println with logging this information to the in-game UI log
System.out.println(player2.getName() + " collected " + collectedAmount + " " + player2.getName() + "!");
} finally {
lock.unlock();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/edu/sdccd/cisc191/utilities/Leaderboard.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package edu.sdccd.cisc191.utilities;

// TODO: delete or implement unused Leaderboard class
public class Leaderboard {
}