-1
我想讓我的炸彈人拿起物品。但這個項目應該只工作5秒。因此,我需要類似布爾collisionWithTiles
方法中的倒計時。物品取貨倒計時?
有什麼建議嗎?
package dev.codenmore.tilegame.entities.creatures;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;
import dev.codenmore.tilegame.Handler;
import dev.codenmore.tilegame.entities.Entity;
import dev.codenmore.tilegame.gfx.Assets;
import dev.codenmore.tilegame.tiles.Star;
import dev.codenmore.tilegame.tiles.Tile;
public abstract class Creature extends Entity
{
public static final int DEFAULT_HEALTH = 10;
public static final float DEFAULT_SPEED = 0.3f;
// Setzt die Größe der Kreaturen in Pixel fest
// Auf diese Variablen wird von dem Konstruktor der Player-Klasse
// zugegriffen
public static final int DEFAULT_CREATURE_WIDTH = 64, DEFAULT_CREATUR_HEIGHT = 64;
protected float speed;
// Setzt das Leben des Spielers auf 10
public Creature(Handler handler, float x, float y, int width, int height)
{
super(handler, x, y, width, height);
health = DEFAULT_HEALTH;
speed = DEFAULT_SPEED;
}
// nimmt die x-Koordinate der Entity-Klasse und addiert
// die protected Variablen x und yMove
public void move(double delta, float moveX, float moveY)
{
if(!checkEntityCollisions())
{
moveX(delta, moveX);
moveY(delta, moveY);
}
}
public void moveX(double delta, float xMove)
{
if (xMove > 0)
{// Bewegung nach rechts
int tx = (int) (x + xMove + bounds.x + bounds.width)/Tile.TILEWIDTH;
if (!collisionWithTile(tx, (int) (y + bounds.y)/Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height)/Tile.TILEHEIGHT))
{
x += xMove;
}
}
else if (xMove < 0)
{// Bewegung nach links
int tx = (int) (x + xMove + bounds.x)/Tile.TILEWIDTH;
if (!collisionWithTile(tx, (int) (y + bounds.y)/Tile.TILEHEIGHT) && !collisionWithTile(tx, (int) (y + bounds.y + bounds.height)/Tile.TILEHEIGHT))
{
x += xMove;
}
}
}
public void moveY(double delta, float yMove)
{
if (yMove < 0)
{// Bewegung hoch
int ty = (int) (y + yMove + bounds.y)/Tile.TILEHEIGHT;
if (!collisionWithTile((int) (x + bounds.x)/Tile.TILEWIDTH, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width)/Tile.TILEWIDTH, ty))
{
y += yMove;
}
}
else if (yMove > 0)
{// Bewegung runter
int ty = (int) (y + yMove + bounds.y + bounds.height)/Tile.TILEHEIGHT;
if (!collisionWithTile((int) (x + bounds.x)/Tile.TILEWIDTH, ty) && !collisionWithTile((int) (x + bounds.x + bounds.width)/Tile.TILEWIDTH, ty))
{
y += yMove;
}
}
}
//TEST
public static boolean test = false;
protected boolean collisionWithTile(int x, int y)
{
//Prüft ob der Stein vorhanden ist und ob eine kollision mit dem Tile Star besteht
if (handler.getWorld().getTile(x, y) == Tile.star && Stone.stone == false){
this.speed = 1;
System.out.println("Tile");
Creature.test = true;
return false;
}
return handler.getWorld().getTile(x, y).isSolid();
}
protected boolean collisionWithEntity(int x, int y)
{
return isSolid();
}
// Getters und Setters für die Protected Variablen
// Speed und Health
public int getHealth()
{
return health;
}
public void setHealth(int health)
{
this.health = health;
}
public float getSpeed()
{
return speed;
}
public void setSpeed(float speed)
{
this.speed = speed;
}
public boolean isSolid()
{
return false;
}
}