Files
PacMan/src/main/java/se/urmo/game/state/PlayingState.java
2025-08-17 10:53:59 +02:00

66 lines
1.7 KiB
Java

package se.urmo.game.state;
import se.urmo.game.collision.CollisionChecker;
import se.urmo.game.collision.GhostCollisionChecker;
import se.urmo.game.entities.Ghost;
import se.urmo.game.entities.PacMan;
import se.urmo.game.main.Game;
import se.urmo.game.map.GameMap;
import se.urmo.game.util.Direction;
import java.awt.*;
import java.awt.event.KeyEvent;
public class PlayingState implements GameState {
private final Game game;
private final GameStateManager gameStateManager;
private final Ghost ghost;
private PacMan pacman;
private GameMap map;
public PlayingState(Game game, GameStateManager gameStateManager) {
this.game = game;
this.gameStateManager = gameStateManager;
this.map = new GameMap();
this.pacman = new PacMan(game, new CollisionChecker(map));
this.ghost = new Ghost(game, new GhostCollisionChecker(map), null);
}
@Override
public void update() {
pacman.update();
ghost.update();
}
@Override
public void render(Graphics2D g) {
map.draw(g);
pacman.draw(g);
ghost.draw(g);
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W -> move(Direction.UP);
case KeyEvent.VK_S -> move(Direction.DOWN);
case KeyEvent.VK_A -> move(Direction.LEFT);
case KeyEvent.VK_D -> move(Direction.RIGHT);
}
}
private void move(Direction direction) {
pacman.setMoving(true);
pacman.setDirection(direction);
}
@Override
public void keyReleased(KeyEvent e) {
pacman.setMoving(false);
pacman.setDirection(Direction.NONE);
}
public GameMap getMap() {
return map;
}
}