Refactored package-structure

Extracted Animated-interface
This commit is contained in:
Urban Modig
2025-08-31 17:54:53 +02:00
parent 61d9df04f9
commit 2e9e7cc45e
24 changed files with 223 additions and 142 deletions

View File

@ -0,0 +1,44 @@
package se.urmo.game.entities.collectibles;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import se.urmo.game.entities.pacman.PacMan;
import se.urmo.game.map.GameMap;
import se.urmo.game.state.FruitType;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
@Slf4j
public class Fruit {
private final Point position;
private final BufferedImage sprite;
@Getter
private final int score;
private final long spawnTime;
private final long lifetimeMs = 9000; // ~9 seconds
public Fruit(FruitType type) {
this.position = new Point(GameMap.colToScreen(13), GameMap.rowToScreen(16)); ;
this.sprite = type.getSprite();
this.score = type.getScore();
this.spawnTime = System.currentTimeMillis();
}
public void draw(Graphics g) {
g.drawImage(sprite, position.x + GameMap.MAP_TILESIZE / 2, position.y, null);
}
public boolean isExpired() {
return System.currentTimeMillis() - spawnTime > lifetimeMs;
}
public boolean collidesWith(PacMan pacman) {
Rectangle pacmanBounds = pacman.getBounds();
Rectangle fruitBounds = new Rectangle(position.x, position.y, sprite.getWidth(), sprite.getHeight());
return pacmanBounds.intersects(fruitBounds);
}
}