Files
PacMan/src/main/java/se/urmo/game/entities/collectibles/Fruit.java
Urban Modig d4b980f522 Moved around
2025-09-03 00:42:48 +02:00

45 lines
1.3 KiB
Java

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.util.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);
}
}