I have a few constants in a game that I'm doing for hobby. I need to store constants for the total size of the a Physical World and the size of the Screen.
I can position the world's objects to the screen or vice versa.
To position the images on the screen for example I get the position that the body occupies in the physical world and transform to the screen by doing:
(Full Screen Size / Total size of the World) * position of the object in the world.
I came up with an ingenious way to keep these constant while I do conversion operations using the enum below:
import com.badlogic.gdx.math.Vector2;
import static br.com.games.Test.Sizes.*;
public class Test {
public static enum Sizes {
WORLD(new Vector2(25.806f, 15.48f)), SCREEN(new Vector2(800f, 480f)), TRANSFORM(
new Vector2(-1, -1));
private Vector2 value;
private Sizes(Vector2 value) {
this.value = value;
}
private Vector2 inputValue;
private Vector2 sourceValue;
public Sizes value(Vector2 value) {
inputValue = value;
return this;
}
public Sizes from(Sizes size) {
sourceValue = size.value;
return this;
}
public Vector2 to(Sizes size) {
float x = size.value.x / sourceValue.x * inputValue.x;
float y = size.value.y / sourceValue.y * inputValue.y;
return Vector2.Zero.set(x, y);
}
public Vector2 getValue() {
return value;
}
}
}
I can use it this way:
public static void main(String[] args) {
TRANSFORM.value(new Vector2(10, 10)).from(WORLD)
.to(SCREEN);
}
Vector2
class is here.
What do you think of this way of using constants and operations? Do you see any problems? Could you point out some improvement?