简体   繁体   English

您如何使玩家移动到一个点?

[英]How can you make a player move to a point?

I was wondering how you can move a player to a specific point. 我想知道如何将玩家移动到特定地点。 I want the player to follow the mouse when the left button is down. 我希望播放器在鼠标左键按下时跟随鼠标。 Here is my code so far. 到目前为止,这是我的代码。

Entity Class : 实体类别

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();
    }

}

World Class : 世界级

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);
    }

}

Any suggestions on how to fix my setVelocity(float x, float y float vel) method? 关于如何解决我的setVelocity(float x,float y float vel)方法的任何建议吗?

I'm not too familiar with Slick2D, but you can use a 2D vector and rotate to the point, and then just increase x/y. 我对Slick2D不太熟悉,但是您可以使用2D矢量并旋转到该点,然后仅增加x / y。 I looked through the API for half a second and found Vector2f (maybe you could include a vector field in your entity class), not sure if that's what you would usually use in slick. 我看了半秒钟的API,发现Vector2f (也许您可以在实体类中包括一个vector字段),不确定那是否是您通常在平滑处理中使用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM