Я делаю игру Tower Defense на Java с использованием библиотеки Slick2D.
Проблема с моей математикой. Прямо сейчас пуля будет стрелять по текущим координатам X, Y врагов - но поскольку противник переместился, когда пуля достигнет X, Y будет отставать. Любая идея, как это исправить, кроме ускорения пули? Пуля математика находится в нижней части класса пули.
public class Bullets implements GameObject {
private ArrayList<Bullet> bulletList = new ArrayList<Bullet>();
Enemy enemy = new Enemy();
BasicTower basicTower = new BasicTower();
public Shape bulletCircle = null;
public boolean collides = false;
public int bulletCount;
public Bullets() throws SlickException {
}
@Override
public Vector2f getPosition() {
return null;
}
@Override
public void init(GameContainer gc, StateBasedGame stateBasedGame) throws SlickException {
}
@Override
public void render(GameContainer gc, StateBasedGame stateBasedGame, Graphics g) throws SlickException {
for (int i = 0; i < bulletList.size(); i++) {
Bullet bullet = bulletList.get(i);
bulletCircle = new Circle(bullet.location.getX(),bullet.location.getY(),10);
if (bulletCircle.intersects( enemy.playerRectangle )){
bulletCount++;
bulletList.remove( i );
collides = true;
}else{
collides = false;
}
g.setColor( red );
g.fill(bulletCircle);
}
}
@Override
public void update(GameContainer gc, StateBasedGame stateBasedGame, int delta) throws SlickException {
//Update the bullet's position.
Input input = gc.getInput();
//enemy.update(gc, stateBasedGame, delta);
for (int i = 0; i < bulletList.size(); i++) {
Bullet bullet = bulletList.get(i);
bullet.move();
}
}
public void addNewBullet2(float x1, float y1, float x2, float y2) {
bulletList.add(new Bullet(x1*64+48,y1*64+48, x2, y2));
}
class Bullet {
float startX = 0;
float startY = 0;
float destX = 0;
float destY = 0;
Point location = new Point(0, 0);
float speed; //how fast this moves.
float dx;
float dy;
public Bullet(float startX, float startY, float destX, float destY) {
this.startX = startX;
this.startY = startY;
location.setLocation(startX, startY);
this.destX = destX;
this.destY = destY;
recalculateVector(destX, destY);
}
public void recalculateVector(float destX, float destY) {
float rad = (float) (Math.atan2(destX - startX, startY - destY));
//Can set different speeds here, if you wanted.
speed = 5;
this.dx = (float) Math.sin(rad) * speed;
this.dy = -(float) Math.cos(rad) * speed;
}
public void move() {
float x = location.getX();
float y = location.getY();
x += dx;
y += dy;
location.setLocation(x, y);
}
}