I have walls that I can spawn. They wll move in one of four directions, up, down, right or left. Now I can set, wether the wall is thin, wide, long or short, but the problem is:
Nearly everything I do with the walls depends on wether they move up, down, left or right and because of this I use my direction enum extremely often and have to pass it the current state of the wall everytime.
import java.util.Random;
import com.AndroidTest.game.Constants;
import com.AndroidTest.game.GameWorld.GameWorld;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.PolygonShape;
public class Wall {
public enum Direction {
UP {
@Override
public Vector2 getPosition(Random rand, Size size, Length length) {
return new Vector2(rand.nextFloat()*(Constants.WORLD_WIDTH-size.value)+size.value/2, -length.valueVert);
}
@Override
public float getWidth(Size size, Length length) {
return size.value/2;
}
@Override
public float getHeight(Size size, Length length) {
return length.valueVert;
}
@Override
public Vector2 getLinearVelocity(float speed) {
return new Vector2(0f, speed);
}
@Override
public boolean getDeath(Body body, Size size, Length length) {
return (body.getPosition().y >= Constants.WORLD_HEIGHT+getHeight(size, length)) ? true : false;
}
},
DOWN {
@Override
public Vector2 getPosition(Random rand, Size size, Length length) {
return new Vector2(rand.nextFloat()*(Constants.WORLD_WIDTH-size.value)+size.value/2, Constants.WORLD_HEIGHT+length.valueVert);
}
@Override
public float getWidth(Size size, Length length) {
return size.value/2;
}
@Override
public float getHeight(Size size, Length length) {
return length.valueVert;
}
@Override
public Vector2 getLinearVelocity(float speed) {
return new Vector2(0f, -speed);
}
@Override
public boolean getDeath(Body body, Size size, Length length) {
return (body.getPosition().y <= -getHeight(size, length)) ? true : false;
}
},
RIGHT {
@Override
public Vector2 getPosition(Random rand, Size size, Length length) {
return new Vector2(-length.valueHori, rand.nextFloat()*(Constants.WORLD_HEIGHT-size.value)+size.value/2);
}
@Override
public float getWidth(Size size, Length length) {
return length.valueHori;
}
@Override
public float getHeight(Size size, Length length) {
return size.value/2;
}
@Override
public Vector2 getLinearVelocity(float speed) {
return new Vector2(speed, 0f);
}
@Override
public boolean getDeath(Body body, Size size, Length length) {
return (body.getPosition().x >= Constants.WORLD_WIDTH+getHeight(size, length)) ? true : false;
}
},
LEFT {
@Override
public Vector2 getPosition(Random rand, Size size, Length length) {
return new Vector2(Constants.WORLD_WIDTH+length.valueHori, rand.nextFloat()*(Constants.WORLD_HEIGHT-size.value)+size.value/2);
}
@Override
public float getWidth(Size size, Length length) {
return length.valueHori;
}
@Override
public float getHeight(Size size, Length length) {
return size.value/2;
}
@Override
public Vector2 getLinearVelocity(float speed) {
return new Vector2(-speed, 0f);
}
@Override
public boolean getDeath(Body body, Size size, Length length) {
return (body.getPosition().x <= -getHeight(size, length)) ? true : false;
}
};
public abstract Vector2 getPosition(Random rand, Size size, Length length);
public abstract Vector2 getLinearVelocity(float speed);
public abstract float getWidth(Size size, Length length);
public abstract float getHeight(Size size, Length length);
public abstract boolean getDeath(Body body, Size size, Length length);
public static Direction getRandomDirection() {
Random rand = new Random();
return values()[rand.nextInt(values().length)];
}
}
public enum Length {
THIN(1f, 1f),
SHORT(Constants.WORLD_WIDTH/8, Constants.WORLD_HEIGHT/8),
MEDIUM(Constants.WORLD_WIDTH/4, Constants.WORLD_HEIGHT/4),
LONG(Constants.WORLD_WIDTH/2, Constants.WORLD_HEIGHT/2);
public final float valueHori, valueVert;
private Length(float valueHori, float valueVert) {
this.valueHori = valueHori;
this.valueVert = valueVert;
}
}
public enum Size {
TINY(1f),
SMALL(5f),
MEDIUM(10f),
BIG(15f);
public final float value;
private Size(float value) {
this.value = value;
}
}
public Direction direction;
public Size size;
public Length length;
public float speed;
public Body body;
public Wall(GameWorld gameWorld, Size size, Length length) {
this.direction = Direction.getRandomDirection();
this.size = size;
this.length = length;
this.speed = 20f;
initWall(gameWorld, direction, size, length);
}
public Wall(GameWorld gameWorld, Direction direction, Size size, Length length) {
this.direction = direction;
this.size = size;
this.length = length;
this.speed = 20f;
initWall(gameWorld, direction, size, length);
}
private void initWall(GameWorld gameWorld, Direction direction, Size size, Length length) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.KinematicBody;
bodyDef.position.set(new Vector2(2, 15));
PolygonShape box = new PolygonShape();
Random rand = new Random();
box.setAsBox(direction.getWidth(size, length), direction.getHeight(size, length));
bodyDef.position.set(direction.getPosition(rand, size, length));
body = gameWorld.world.createBody(bodyDef);
body.setLinearVelocity(direction.getLinearVelocity(speed));
body.createFixture(box, 0.0f);
box.dispose();
}
}