物品提货倒计时?

时间:2016-11-30 13:47:16

标签: java collision-detection

我希望我的炸弹人拿起物品。但是这个项目应该只工作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;
    }
}

1 个答案:

答案 0 :(得分:0)

我们让它发挥作用

### first create some sample data that looks like final_data$DateCancelled

sample_dates<-append(seq(as.Date('2016-01-01'),as.Date('2016-01-10'),by = 1), 
                 seq(as.Date('2016-01-01'),as.Date('2016-01-03'),by = 1)
                 )

### find the unique values that exist and put them in a list

unique_dates<-list(dates=unique(sample_dates))

###loop through each value of date within the original data set which in this case is "sample_dates" and output counts to "unique_dates" list

for(i in 1:length(unique_dates$dates)){

unique_dates$counts[[i]]<-length(subset(sample_dates,
                                        sample_dates==unique_dates$dates[[i]]))

    }

### format output as data frame
final_output<-data.frame(unique_dates)

> final_output
        dates counts
1  2016-01-01      2
2  2016-01-02      2
3  2016-01-03      2
4  2016-01-04      1
5  2016-01-05      1
6  2016-01-06      1
7  2016-01-07      1
8  2016-01-08      1
9  2016-01-09      1
10 2016-01-10      1