在最后一部分中,您了解了如何创建Sprite,为其设置动画并赋予其Behavior。 但是动画效果不是很好,因为作为Insectoid,您应该始终看起来在飞行中。 记住:安全第一! 我们可以通过创建自定义的TileSetAnimation轻松地做到这一点:
public class RotatingTileSetAnimation extends TileSetAnimation {private double angle = 0;public RotatingTileSetAnimation(TileSet set, int[] indices, float speed) {
super(set, indices, speed);
}public void setAngle(double angle) {
this.angle = angle;
}@Override
public void render(Sprite sprite, GraphicsContext context, float alpha, long delta) {
context.save();
context.translate(sprite.getWidth() / 2, sprite.getHeight() / 2);
context.rotate(angle);
context.translate(-sprite.getWidth() / 2, -sprite.getHeight() / 2);
super.render(sprite, context, alpha, delta); //To change body of generated methods, choose Tools | Templates.
context.restore();
}
}
我们可以根据x和y速度计算旋转角度,并在渲染之前在我们的GraphicsContext中进行设置。 所以这是一个子类:
public class LookAheadTileSetAnimation extends RotatingTileSetAnimation {public LookAheadTileSetAnimation(TileSet set, int[] indices, float speed) {
super(set, indices, speed);
}@Override
public void render(Sprite sprite, GraphicsContext context, float alpha, long delta) {
setAngle(Math.toDegrees(Math.atan2(sprite.getVelocityY(), sprite.getVelocityX())));
super.render(sprite, context, alpha, delta); //To change body of generated methods, choose Tools | Templates.
}
}
结果如下:
很简单,不是吗? 现在,下一步将是添加一些行为到假肢本身。 我希望他们总是检查最接近的敌人并将大炮指向它。 首先,我稍微修改了代码,然后将炮塔再次分为基地和大炮。 因此,当您现在选择一门大炮时,炮塔基座将放置在TileLayer中名为“炮塔基座”的位置。 我只是更改了TurretView类以支持此功能:
class TileSetView extends StackPane {Canvas canvas;
TileSet cannons;
TileSet bases;
int selectedIndex = 0;
Color selected = Color.rgb(100, 100, 255, .2);public TileSetView() {
}public void setTileSet(final TileSet bases, final TileSet cannons) {
this.cannons = cannons;
this.bases = bases;
getChildren().clear();
ImageView turretBases = new ImageView();
turretBases.setImage(bases.getTileImage());ImageView turretCannons = new ImageView();
turretCannons.setImage(cannons.getTileImage());getChildren().addAll(turretBases, turretCannons);canvas = new Canvas(cannons.getTileImage().getWidth(), cannons.getTileImage().getHeight());
getChildren().add(canvas);
canvas.setOnMouseClicked(new EventHandler() {
@Override
public void handle(MouseEvent t) {
double x = t.getX();
double y = t.getY();
selectedIndex = (int) ((int) x / cannons.getTilewidth() + (((int) y / cannons.getTileheight()) * cannons.getNumColumns()));
updateCanvas();
}
});
updateCanvas();
}public int getSelectedGid() {
if (bases == null) {
return -1;
}
return bases.getFirstgid() + selectedIndex;
}public int getSelectedIndex(){
return selectedIndex;
}public void updateCanvas() {
GraphicsContext graphicsContext2D = canvas.getGraphicsContext2D();
graphicsContext2D.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
if (selectedIndex >= 0) {
graphicsContext2D.setFill(selected);
int x = selectedIndex % cannons.getNumColumns();
int y = selectedIndex / cannons.getNumColumns();
graphicsContext2D.fillRect(x * cannons.getTilewidth(), y * cannons.getTileheight(), cannons.getTilewidth(), cannons.getTileheight());
}
}
}
所以这是现在的样子:
接下来,我们添加大炮。 虽然炮塔基地是简单的瓦片,但我们的大炮必须是精灵,因此我们可以向它们添加“行为”:
public class CannonSprite extends Sprite {RotatingTileSetAnimation rotateAnimation;public CannonSprite(GameCanvas parent, RotatingTileSetAnimation animation, String name, double x, double y, int width, int height) {
super(parent, animation, name, x, y, width, height, Lookup.EMPTY);
this.rotateAnimation = animation;
addBehaviour(new SpriteBehavior() {
@Override
public boolean perform(Sprite sprite) {
Sprite closest = null;
double dist = Double.MAX_VALUE;
Collection sprites = sprite.getParent().getSprites();
for (Sprite sprite1 : sprites) {
if (sprite1 instanceof EnemySprite) {
double distance = distance(getX(), getY(), sprite1.getX(), sprite1.getY());
if (distance < dist) {
dist = distance;
closest = sprite1;
}
}
}
if (closest != null) {
rotateAnimation.setAngle(Math.toDegrees(Math.atan2(closest.getY() - sprite.getY(),closest.getX() - sprite.getX())));
}return true;
}
});
}public double distance(double x1, double y1, double x2, double y2) {
return Math.sqrt(
(x1 - x2) * (x1 - x2)
+ (y1 - y2) * (y1 - y2));
}
}
再次,我使用RotatingTileSetAnimation并简单地设置角度,以便大炮指向最近的敌人。 这是我们得到的:
教程的这一部分就是这样。 我们创建了一些自定义动画,以使Insectoids朝正确的方向显示,并使炮塔始终指向关闭目标。 从这些示例中可以看到,游戏引擎试图使向“行为”添加行为非常简单。 在下一部分中,我们将对炮塔进行射击。
翻译自: https://www.javacodegeeks.com/2013/10/tower-defense-in-javafx-3.html