Refactored package-structure
Extracted Animated-interface
This commit is contained in:
127
src/main/java/se/urmo/game/entities/pacman/PacMan.java
Normal file
127
src/main/java/se/urmo/game/entities/pacman/PacMan.java
Normal file
@ -0,0 +1,127 @@
|
||||
package se.urmo.game.entities.pacman;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import se.urmo.game.collision.CollisionChecker;
|
||||
import se.urmo.game.entities.BaseAnimated;
|
||||
import se.urmo.game.util.Direction;
|
||||
import se.urmo.game.map.GameMap;
|
||||
import se.urmo.game.util.LoadSave;
|
||||
import se.urmo.game.util.MyPoint;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class PacMan extends BaseAnimated {
|
||||
public static final int PACMAN_SIZE = 32;
|
||||
public static final int PACMAN_OFFSET = PACMAN_SIZE / 2;
|
||||
private static final int COLLISION_BOX_SIZE = 16;
|
||||
private static final int COLLISION_BOX_OFFSET = (PACMAN_SIZE - COLLISION_BOX_SIZE) / 2;
|
||||
private final MyPoint startPosition;
|
||||
private static final int ANIMATION_UPDATE_FREQUENCY = 10;
|
||||
private static final double BASE_SPEED = 0.40;
|
||||
private boolean moving = false;
|
||||
private final BufferedImage[][] spriteSheets;
|
||||
private MyPoint position;
|
||||
private final CollisionChecker collisionChecker;
|
||||
@Setter
|
||||
@Getter
|
||||
private Direction direction = Direction.NONE;
|
||||
|
||||
public PacMan(CollisionChecker collisionChecker) {
|
||||
super(ANIMATION_UPDATE_FREQUENCY, 4);
|
||||
this.collisionChecker = collisionChecker;
|
||||
this.position = new MyPoint(
|
||||
26 * GameMap.MAP_TILESIZE + GameMap.OFFSET_X,
|
||||
13 * GameMap.MAP_TILESIZE + GameMap.OFFSET_Y + ((double) GameMap.MAP_TILESIZE / 2));
|
||||
this.startPosition = this.position;
|
||||
this.spriteSheets = loadAnimation();
|
||||
}
|
||||
|
||||
private BufferedImage[][] loadAnimation() {
|
||||
BufferedImage[][] image = new BufferedImage[3][4];
|
||||
BufferedImage[][] spriteMap = new BufferedImage[4][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(PACMAN_SIZE * col, PACMAN_SIZE * row, PACMAN_SIZE, PACMAN_SIZE);
|
||||
}
|
||||
}
|
||||
spriteMap[Direction.RIGHT.ordinal()] = image[0];
|
||||
spriteMap[Direction.LEFT.ordinal()] = Arrays.stream(image[0])
|
||||
.map(i -> LoadSave.rotate(i, 180))
|
||||
.toArray(BufferedImage[]::new);
|
||||
spriteMap[Direction.DOWN.ordinal()] = Arrays.stream(image[0])
|
||||
.map(i -> LoadSave.rotate(i, 90))
|
||||
.toArray(BufferedImage[]::new);
|
||||
spriteMap[Direction.UP.ordinal()] = Arrays.stream(image[0])
|
||||
.map(i -> LoadSave.rotate(i, 270))
|
||||
.toArray(BufferedImage[]::new);
|
||||
return spriteMap;
|
||||
}
|
||||
|
||||
public void draw(Graphics g) {
|
||||
g.drawImage(
|
||||
spriteSheets[direction==Direction.NONE ? 0 : direction.ordinal()][aniIndex],
|
||||
(int) position.x - PACMAN_OFFSET,
|
||||
(int) position.y - PACMAN_OFFSET,
|
||||
PACMAN_SIZE,
|
||||
PACMAN_SIZE, null);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if(moving) {
|
||||
MyPoint mpoint = switch (direction) {
|
||||
case RIGHT -> new MyPoint(position.x + getSpeed(), position.y);
|
||||
case LEFT -> new MyPoint(position.x - getSpeed(), position.y);
|
||||
case UP -> new MyPoint(position.x, position.y - getSpeed());
|
||||
case DOWN -> new MyPoint(position.x, position.y + getSpeed());
|
||||
default -> throw new IllegalStateException("Unexpected value: " + direction);
|
||||
};
|
||||
|
||||
MyPoint destination = collisionChecker.getValidDestination(direction, mpoint, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
|
||||
|
||||
if (destination != null) {
|
||||
position = destination;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private double getSpeed() {
|
||||
return BASE_SPEED * 0.8;
|
||||
}
|
||||
|
||||
public double distanceTo(Point point) {
|
||||
return new Point((int) position.x, (int) position.y).distance(point);
|
||||
}
|
||||
|
||||
public void resetPosition() {
|
||||
position = startPosition;
|
||||
}
|
||||
|
||||
public Image getLifeIcon() {
|
||||
return spriteSheets[0][1];
|
||||
}
|
||||
|
||||
public Rectangle getBounds() {
|
||||
return new Rectangle(
|
||||
(int) (position.x - COLLISION_BOX_OFFSET),
|
||||
(int) (position.y - COLLISION_BOX_OFFSET),
|
||||
COLLISION_BOX_SIZE,
|
||||
COLLISION_BOX_SIZE);
|
||||
}
|
||||
|
||||
public Point getPosition() {
|
||||
return new Point((int) position.x, (int) position.y);
|
||||
}
|
||||
|
||||
public void setMoving(boolean b) {
|
||||
moving = b;
|
||||
paused = !b;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user