Advertisement
dawciobiel

GameState

Jun 23rd, 2025
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | Gaming | 0 0
  1. package com.dawciobiel.pacman.game;
  2.  
  3. import com.dawciobiel.pacman.entities.Field;
  4. import com.dawciobiel.pacman.entities.Food;
  5. import com.dawciobiel.pacman.entities.Ghost;
  6. import com.dawciobiel.pacman.entities.Player;
  7. import com.dawciobiel.pacman.entities.PowerPellet;
  8. import com.dawciobiel.pacman.utils.GameConstants;
  9.  
  10. import java.util.List;
  11.  
  12. /**
  13.  * Klasa zarządzająca stanem gry Pacman.
  14.  * Zawiera wszystkie zmienne stanu gry i metody do ich zarządzania.
  15.  */
  16. public class GameState {
  17.  
  18.     // === GAME STATE ===
  19.     public static int score;
  20.     public static int lives;
  21.     public static boolean gameOver;
  22.     public static boolean gameWon = false;
  23.  
  24.     public static Field[][] fields;
  25.  
  26.     public static Player pacman;
  27.     public static List<Ghost> ghosts;
  28.     public static List<Food> foods;
  29.     public static List<PowerPellet> powerPellets;
  30.  
  31.     // === POWER PELLET ===
  32.     private boolean isPowerPelletActive;
  33.     private long powerPelletStartTime;
  34.     public static int ghostMultiplier = 1; // Mnożnik punktów za duchy. Zmienia po aktywacji power-up-ów
  35.  
  36.     /**
  37.      * Konstruktor inicjalizujący stan gry z domyślnymi wartościami.
  38.      */
  39.     public GameState() {
  40.         reset();
  41.     }
  42.  
  43.     /**
  44.      * Dodaje punkty do wyniku.
  45.      *
  46.      * @param points liczba punktów do dodania
  47.      */
  48.     public static void addScore(int points) {
  49.         score += points;
  50.     }
  51.  
  52.     // === BUSINESS METHODS ===
  53.     public static void loseLife() {
  54.         lives--;
  55.     }
  56.  
  57.     public static int getLives() {
  58.         return lives;
  59.     }
  60.  
  61.     public void setLives(int lives) {
  62.         GameState.lives = lives;
  63.     }
  64.  
  65.     /**
  66.      * Resetuje stan gry do wartości początkowych.
  67.      */
  68.     public void reset() {
  69.         score = GameConstants.INITIAL_SCORE;
  70.         lives = GameConstants.INITIAL_LIVES;
  71.         gameOver = false;
  72.         gameWon = false;
  73.         this.isPowerPelletActive = false;
  74.         this.powerPelletStartTime = 0;
  75.         this.ghostMultiplier = GameConstants.INITIAL_GHOST_MULTIPLIER;
  76.     }
  77.  
  78.     /**
  79.      * Dodaje punkty za zjedzenie ducha z aktualnym mnożnikiem.
  80.      *
  81.      * @return liczba punktów które zostały dodane
  82.      */
  83.     public int addGhostScore() {
  84.         int points = GameConstants.GHOST_POINTS * ghostMultiplier;
  85.         addScore(points);
  86.         ghostMultiplier *= GameConstants.GHOST_MULTIPLIER_INCREMENT;
  87.  
  88.         return points;
  89.     }
  90.  
  91.     /**
  92.      * Aktywuje power pellet.
  93.      *
  94.      * @param currentTime aktualny czas systemowy w nanosekundach
  95.      */
  96.     public void activatePowerPellet(long currentTime) {
  97.         this.isPowerPelletActive = true;
  98.         this.powerPelletStartTime = currentTime;
  99.         this.ghostMultiplier = GameConstants.INITIAL_GHOST_MULTIPLIER;
  100.     }
  101.  
  102.     /**
  103.      * Sprawdza, czy power pellet wygasł i go dezaktywuje, jeśli tak.
  104.      *
  105.      * @param currentTime aktualny czas systemowy w nanosekundach
  106.      * @return true jeśli power pellet właśnie wygasł, false w przeciwnym razie
  107.      */
  108.     public boolean checkAndDeactivatePowerPellet(long currentTime) {
  109.         if (isPowerPelletActive && currentTime - powerPelletStartTime > GameConstants.POWER_PELLET_DURATION_NANOS) {
  110.  
  111.             isPowerPelletActive = false;
  112.             ghostMultiplier = GameConstants.INITIAL_GHOST_MULTIPLIER;
  113.             return true; // power pellet właśnie wygasł
  114.         }
  115.         return false;
  116.     }
  117.  
  118.     /**
  119.      * Resetuje stan po śmierci gracza (ale nie resetuje całej gry).
  120.      */
  121.     public void resetAfterDeath() {
  122.         this.isPowerPelletActive = false;
  123.         this.ghostMultiplier = GameConstants.INITIAL_GHOST_MULTIPLIER;
  124.     }
  125.  
  126.     // === GETTERY ===
  127.  
  128.     /**
  129.      * Kończy grę jako wygraną.
  130.      */
  131.     public void winGame() {
  132.         gameWon = true;
  133.     }
  134.  
  135.     /**
  136.      * Sprawdza czy gra powinna się skończyć (wygrana lub przegrana).
  137.      *
  138.      * @return true jeśli gra się skończyła
  139.      */
  140.     public boolean isGameFinished() {
  141.         return gameOver || gameWon;
  142.     }
  143.  
  144.     public int getScore() {
  145.         return score;
  146.     }
  147.  
  148.     public void setScore(int score) {
  149.         GameState.score = score;
  150.     }
  151.  
  152.     public boolean isGameOver() {
  153.         return gameOver;
  154.     }
  155.  
  156.     public void setGameOver(boolean gameOver) {
  157.         GameState.gameOver = gameOver;
  158.     }
  159.  
  160.     public boolean isGameWon() {
  161.         return gameWon;
  162.     }
  163.  
  164.     public void setGameWon(boolean gameWon) {
  165.         GameState.gameWon = gameWon;
  166.     }
  167.  
  168.     // === SETTER ===
  169.  
  170.     public boolean isPowerPelletActive() {
  171.         return isPowerPelletActive;
  172.     }
  173.  
  174.     public long getPowerPelletStartTime() {
  175.         return powerPelletStartTime;
  176.     }
  177.  
  178.     public int getGhostMultiplier() {
  179.         return ghostMultiplier;
  180.     }
  181.  
  182.     /**
  183.      * Zwraca czas pozostały do końca działania power pelleta w milisekundach.
  184.      *
  185.      * @param currentTime aktualny czas systemowy w nanosekundach
  186.      * @return czas pozostały w milisekundach, 0 jeśli power pellet nie jest aktywny
  187.      */
  188.     public long getPowerPelletTimeRemaining(long currentTime) {
  189.         if (!isPowerPelletActive) {
  190.             return 0;
  191.         }
  192.  
  193.         long elapsedNanos = currentTime - powerPelletStartTime;
  194.         long remainingNanos = GameConstants.POWER_PELLET_DURATION_NANOS - elapsedNanos;
  195.  
  196.         return Math.max(0, remainingNanos / 1_000_000L); // konwersja na milisekundy
  197.     }
  198.  
  199.     @Override
  200.     public String toString() {
  201.         return String.format("GameState{score=%d, lives=%d, gameOver=%s, gameWon=%s, powerPelletActive=%s}", score, lives, gameOver, gameWon, isPowerPelletActive);
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement