Adding fruits

This commit is contained in:
Urban Modig
2025-08-30 08:45:43 +02:00
parent 23fce8dded
commit 63b2b7ae68
8 changed files with 150 additions and 9 deletions

View File

@ -0,0 +1,45 @@
package se.urmo.game.entities;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
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) {
//return pacman.distanceTo(position) < GameMap.MAP_TILESIZE / 2.0;
Rectangle pacmanBounds = pacman.getBounds();
Rectangle fruitBounds = new Rectangle(position.x, position.y, sprite.getWidth(), sprite.getHeight());
return pacmanBounds.intersects(fruitBounds);
}
}

View File

@ -76,7 +76,7 @@ public class PacMan {
position.y - PACMAN_OFFSET,
PACMAN_SIZE,
PACMAN_SIZE, null);
g.drawImage(COLLISION_BOX, position.x - COLLISION_BOX_OFFSET, position.y - COLLISION_BOX_OFFSET, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null);
//g.drawImage(COLLISION_BOX, position.x - COLLISION_BOX_OFFSET, position.y - COLLISION_BOX_OFFSET, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE, null);
//g.setColor(Color.BLUE);
//g.fillRect(position.x-1, position.y-1, 3, 3);
}
@ -129,4 +129,8 @@ public class PacMan {
public Image getLifeIcon() {
return movmentImages[0][1];
}
public Rectangle getBounds() {
return new Rectangle(position.x - COLLISION_BOX_OFFSET, position.y - COLLISION_BOX_OFFSET, COLLISION_BOX_SIZE, COLLISION_BOX_SIZE);
}
}