One last animated obstacle : Revolving Obstacle !
OK, here is probably the last Obstacle I’ll create. After that, I think I have enough to design cool levels. Revolving Obstacles... revolves... yeah I know, kinda obvious.
So here is the (short and easy) code for this ObstacleRevolving.java :
public class ObstacleRevolving extends Obstacle{ private float speed = 90; public ObstacleRevolving(World world, OrthographicCamera camera, MapObject rectangleObject) { super(world, camera, rectangleObject); //Rotation speed if(rectangleObject.getProperties().get("Speed") != null) speed = Float.parseFloat((String) rectangleObject.getProperties().get("Speed")); body.setFixedRotation(false); body.setAngularVelocity(speed*MathUtils.degreesToRadians); } @Override public BodyType getBodyType(){ return BodyType.KinematicBody; } @Override public void activate(){ active = !active; if(active) body.setAngularVelocity(speed*MathUtils.degreesToRadians); else body.setAngularVelocity(0); } }
About this code :
- ObstacleRevolving extends Obstacle
- ObstacleRevolving is formed by a KinematicBody
- I read the properties of the TiledMapObject to determine the rotation speed...
- ... and I apply the angular velocity
- Finally, the activate() function called when we use an ItemSwitch to control the ObstacleRevolving sets the angular velocity to 0 or to the speed value, depending on if we turn the ObstacleRevolving on or off.
And guess what code we put in the main for loop of the TiledMapReader.java ? Yeah, you’re right :
for (RectangleMapObject rectangleObject : objects.getByType(RectangleMapObject.class)) { if(rectangleObject.getProperties().get("Type") != null){ ... //Revolving obstacles else if(rectangleObject.getProperties().get("Type").equals("Revolving")){ ObstacleRevolving obstacle = new ObstacleRevolving(world, camera, rectangleObject); obstacles.add(obstacle); } ... }And here is the result !