Adding fruits
This commit is contained in:
45
src/main/java/se/urmo/game/entities/Fruit.java
Normal file
45
src/main/java/se/urmo/game/entities/Fruit.java
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user