プレイヤーを特定のポイントに移動する方法を知りたいと思っていました。左ボタンが押されたときにプレイヤーがマウスを追従するようにします。これまでの私のコードは次のとおりです。
エンティティ クラス:
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) メソッドを修正する方法に関する提案はありますか?