[英]How can you make a player move to a point?
我想知道如何將玩家移動到特定地點。 我希望播放器在鼠標左鍵按下時跟隨鼠標。 到目前為止,這是我的代碼。
實體類別 :
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)方法的任何建議嗎?
我對Slick2D不太熟悉,但是您可以使用2D矢量並旋轉到該點,然后僅增加x / y。 我看了半秒鍾的API,發現Vector2f
(也許您可以在實體類中包括一個vector字段),不確定那是否是您通常在平滑處理中使用的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.