Restructuring
This commit is contained in:
110
src/main/java/se/urmo/game/entities/PacMan.java
Normal file
110
src/main/java/se/urmo/game/entities/PacMan.java
Normal file
@ -0,0 +1,110 @@
|
||||
package se.urmo.game.entities;
|
||||
|
||||
import se.urmo.game.collision.CollisionChecker;
|
||||
import se.urmo.game.util.Direction;
|
||||
import se.urmo.game.main.Game;
|
||||
import se.urmo.game.main.GamePanel;
|
||||
import se.urmo.game.map.GameMap;
|
||||
import se.urmo.game.util.LoadSave;
|
||||
import se.urmo.game.util.MiscUtil;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public class PacMan {
|
||||
public static final int PACMAN_SIZE = GamePanel.TILE_SIZE;
|
||||
private final Game game;
|
||||
private int aniTick = 0;
|
||||
private int aniIndex = 0;
|
||||
private static final int ANIMATION_UPDATE_FREQUENCY = 25;
|
||||
private int speed = 1;
|
||||
private boolean moving;
|
||||
private final BufferedImage[][] movmentImages = new BufferedImage[4][4];
|
||||
//private int xPos = 14 * 16 + 8, yPos = 29 * 16; // top left of object
|
||||
private Point position;
|
||||
private static final BufferedImage innerBox = MiscUtil.createOutlinedBox(8, 8, Color.yellow, 2);
|
||||
private CollisionChecker collisionChecker;
|
||||
private Direction direction = Direction.NONE;
|
||||
|
||||
public PacMan(Game game, CollisionChecker collisionChecker) {
|
||||
this.game = game;
|
||||
this.collisionChecker = collisionChecker;
|
||||
position = new Point(26 * 16 + 8 + GameMap.OFFSET_X, 13 * 16 + GameMap.OFFSET_Y);
|
||||
loadAnimation();
|
||||
}
|
||||
|
||||
private void loadAnimation() {
|
||||
BufferedImage[][] image = new BufferedImage[3][4];
|
||||
|
||||
BufferedImage img = LoadSave.GetSpriteAtlas("sprites/PacManAssets-PacMan.png");
|
||||
for (int row = 0; row < 3; row++) {
|
||||
for (int col = 0; col < 4; col++) {
|
||||
image[row][col] = img.getSubimage(32 * col, 32 * row, PACMAN_SIZE, PACMAN_SIZE);
|
||||
}
|
||||
}
|
||||
movmentImages[Direction.RIGHT.ordinal()] = image[0];
|
||||
movmentImages[Direction.LEFT.ordinal()] = Arrays.stream(image[0])
|
||||
.map(i -> LoadSave.rotate(i, 180))
|
||||
.toArray(BufferedImage[]::new);
|
||||
movmentImages[Direction.DOWN.ordinal()] = Arrays.stream(image[0])
|
||||
.map(i -> LoadSave.rotate(i, 90))
|
||||
.toArray(BufferedImage[]::new);
|
||||
movmentImages[Direction.UP.ordinal()] = Arrays.stream(image[0])
|
||||
.map(i -> LoadSave.rotate(i, 270))
|
||||
.toArray(BufferedImage[]::new);
|
||||
}
|
||||
|
||||
|
||||
public void draw(Graphics g) {
|
||||
g.drawImage(innerBox, position.x, position.y, PACMAN_SIZE, PACMAN_SIZE, null);
|
||||
//g.setColor(Color.RED);
|
||||
//g.drawLine(xPos, yPos, xPos, yPos);
|
||||
//g.drawImage(movmentImages[direction][aniIndex], xPos, yPos, PACMAN_SIZE, PACMAN_SIZE, null);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
updateAnimationTick();
|
||||
if(direction == Direction.NONE) return;
|
||||
|
||||
Point newPosition = switch (direction){
|
||||
case RIGHT -> new Point(position.x + speed, position.y);
|
||||
case LEFT -> new Point(position.x - speed, position.y);
|
||||
case UP -> new Point(position.x , position.y - speed);
|
||||
case DOWN -> new Point(position.x, position.y + speed);
|
||||
default -> throw new IllegalStateException("Unexpected value: " + direction);
|
||||
};
|
||||
|
||||
Point destination = collisionChecker.getValidDestination(direction, newPosition, PACMAN_SIZE, PACMAN_SIZE);
|
||||
|
||||
if(destination != null) {
|
||||
position = destination;
|
||||
System.out.println("Position: + " + position);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void updateAnimationTick() {
|
||||
if (moving) {
|
||||
aniTick++;
|
||||
if (aniTick >= ANIMATION_UPDATE_FREQUENCY) {
|
||||
aniTick = 0;
|
||||
aniIndex++;
|
||||
if (aniIndex >= 3) {
|
||||
aniIndex = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setMoving(boolean moving) {
|
||||
this.moving = moving;
|
||||
}
|
||||
|
||||
public void setDirection(Direction direction) {
|
||||
this.moving = true;
|
||||
this.direction = direction;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user