0

我想知道如何将玩家移动到特定点。我希望玩家在左键按下时跟随鼠标。到目前为止,这是我的代码。

实体类

public abstract class Entity {

    private float velX;
    private float velY;
    private Shape s;

    public Entity(Shape s){
        this.s = s;
    }

    public abstract void render(GameContainer gc, Graphics g);
    public abstract void onUpdate(GameContainer gc, StateBasedGame sbg, int d);
    public abstract boolean collidedWithEntity(Entity e);

    public void update(GameContainer gc, StateBasedGame sbg, int d){
        float x = s.getX() + (velX * (float)d);
        float y = s.getY() + (velY * (float)d);
        s.setLocation(x, y);
        onUpdate(gc, sbg, d);
    }

    public float getVelocityX(){
        return velX;
    }

    public float getVelocityY(){
        return velY;
    }

    public float getVelocity(){
        return (float) (Math.sqrt(Math.pow(velX, 2) + Math.pow(velY, 2)));
    }

    public void setVelocityX(float velX){
        this.velX = velX;
    }

    public void setVelocityY(float velY){
        this.velY = velY;
    }

            //This class,
    public void setVelocity(float angel, float vel){
        float velX = (float) (vel * Math.cos(angel));
        float velY = (float) (vel * Math.sin(angel));
        this.velX = velX;
        this.velY = velY;
    }

            //And this one I use to set it
    public void setVelocity(float x, float y, float vel){
        setVelocity((float) Math.atan((y - s.getCenterY())/(x - s.getCenterX())), vel);
    }


    public Shape getShape() {
        return s;
    }

    public float getX(){
        return s.getX();
    }

    public float getY(){
        return s.getY();
    }

}

世界级

public class World {

    private int shiftX = 0;
    private int shiftY = 0;
    private Entity follow;
    private int width;
    private int height;
    private Player p;

    public World(int width, int height){
        this.width = width;
        this.height = height;
        this.p = new Player(this, 400, 400);
        this.follow = p;
    }

    public float getShiftX(){
        return shiftX;
    }

    public float getShiftY(){
        return shiftY;
    }

    public void render(GameContainer gc, Graphics g){
        g.setColor(Color.white);
        g.fillRect(0, 0, gc.getWidth(), gc.getHeight());
        p.render(gc, g);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int d){
        updateView(gc);
        Input in = gc.getInput();

        //Update player velocity and update player.
        if(in.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)){
            p.setVelocity(in.getMouseX(), in.getMouseY(), p.getMaxSpeed());
        }
        else{
            p.setVelocity(0, 0, 0);
        }
        p.update(gc, sbg, d);
    }

    private void updateView(GameContainer gc){
        if(follow != null){
            shiftX = (int) (follow.getShape().getCenterX() + (width / 2));
            shiftY = (int) (follow.getShape().getCenterY() + (height / 2));
        }

        if(shiftX > (width - gc.getWidth())){
            shiftX = (width - gc.getWidth());
        }
        else if(shiftX < 0){
            shiftX = 0;
        }

        if(shiftY > (height - gc.getHeight())){
            shiftY = (height - gc.getHeight());
        }
        else if(shiftY< 0){
            shiftY = 0;
        }
    }

    public void setFollowing(Entity follow){
        this.follow = follow;
    }

    public Entity getFollowing(){
        return follow;
    }

    public int getHeight(){
        return height;
    }

    public int getWidth(){
        return width;
    }

    public Player getPlayer(){
        return p;
    }

    public float getMouseX(int x){
        return (x + shiftX);
    }

    public float getMouseY(int y){
        return (y + shiftY);
    }

}

关于如何修复我的 setVelocity(float x, float y float vel) 方法的任何建议?

4

1 回答 1

0

我对 Slick2D 不太熟悉,但是您可以使用 2D 矢量并旋转到该点,然后增加 x/y。我浏览了 API 半秒钟,发现Vector2f(也许你可以在你的实体类中包含一个向量字段),不确定这是否是你通常在 slick 中使用的。

于 2013-01-25T17:49:25.147 回答