HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Enums for the four directions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thefourforenumsdirections

Problem

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

Solution


  1. Missing model



You are missing a model or if it is there it is mixed together with the UI. For example: You have an Enum "Size" that internally holds UI information (float) and externally promotes a model fragment (TINY, SMALL, ...).

I would force myself to write the "game" without any UI first so I have the essence decoupled.

So the first step for me would be to separate those fragments.

  1. Idea to distribute responsibilities of Direction



Your Direction Enum owns responsibilities that it shouldn't. My suggestion is to represent the directions for 2D navigation as tuples of the factors -1, 0 and 1.

public enum Direction {

    UP(0, -1), DOWN(0, 1), LEFT(-1, 0), RIGHT(1, 0);

    private Direction(float x, float y) {
        this.x = x;
        this.y = y;
    }

    private float x;
    private float y;

    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }

}


For example to calculate the linear velocity now only ONE method is necessary:

public Vector2 getLinearVelocity(float speed, Direction direction) {
    return new Vector2(speed * direction.getX(), speed * direction.getY());
}


The trick is to use the factor 0 to wipe out the summands not needed and to keep the important summands with -1 or 1. For the other methods I think it will be much more effort. But it's worth a try.

Code Snippets

public enum Direction {

    UP(0, -1), DOWN(0, 1), LEFT(-1, 0), RIGHT(1, 0);

    private Direction(float x, float y) {
        this.x = x;
        this.y = y;
    }

    private float x;
    private float y;

    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }

}
public Vector2 getLinearVelocity(float speed, Direction direction) {
    return new Vector2(speed * direction.getX(), speed * direction.getY());
}

Context

StackExchange Code Review Q#115278, answer score: 4

Revisions (0)

No revisions yet.