简体   繁体   中英

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?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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