Reputation: 73
Ok so I've been reading up on good practices for using constants and enums but I still am confused as to what should I do.
Basically I need to store strings with paths to assets like
"/assets/sprites/ball.png"
and then i need simple name strings like "ball" and I want them to be related to each other. Those strings will be used very often and in different files/packages all across my game's code.
I have a TextureAtlas class that given the name ("ball") already provides me with the actual resource (graphics ready to draw etc). But when I need to load the resource i need to give actual path, not just name. I want to have 1 place where I will have all the constants declared and it would be nice if both the "name" constant and "path" constant were related somehow. So that I don't have to put actual path to the file whenever I want something to use that graphic, instead I want to use constant with the name String.
I hope you can understand what I mean.
Well, I think I could use a map, where key would be the name ("ball") and value would be path ("/assets/ball.png") but I still want to be able to just lets say, write something like this
Assets.GetSprite(Assets.BALL);
and get an actual resource, or even better
Assets.GetSprite(BALL);
So there is this problem of constants, that I need to define name and corresponding path for my graphics in 1 place and be able to use it from anywhere in the program if I need it. It all needs to be in one place so that whenever I add new resource, like new sound or new sprite to the game I won't have to look all over my program to find the place where it should be manually typed, I want to have that 1 file that I will go over and just add new constant or something like that for that 1 file.
Cheers, Pablo.
Upvotes: 1
Views: 1884
Reputation: 7057
It might be a good idea to use properties file.
config.properties
ball = /assets/sprites/ball.png
user = /assets/sprites/user.png
To read these values, at the game startup, do following
FileHandle propertiesFileHandle = Gdx.files
.internal(PROPERTIES_FILE_PATH);
PROPERTIES = new Properties();
try {
PROPERTIES.load(new BufferedInputStream(propertiesFileHandle.read()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Now you can share this PROPERTIES
object wherever you need.
String ballPath = PROPERTIES.getProperty("ball");
Hope this helps.
Upvotes: 2
Reputation: 1062
You can create more complicated enums with parameters. Take a look at the Planet enum in this example http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html or here is an example specific to your project. Remember that are required to create the constructor and the constructor must be private.
public class Asset {
public enum AssetType {
BALL("/assets/sprites/ball.png"),
USER("/assets/sprites/user.png");
private final String path;
AssetsType(String path) {
this.path = path;
}
public String getPath() {
return this.path;
}
}
private AssetType assetType;
public Asset(AssetType assetType) {
this.assetType = assetType;
}
// ...
}
Upvotes: 1
Reputation: 5676
I would go for something like this:
public enum Assets {
BALL, PLAYER // and so on
}
public class Configuration {
Map<Assets, String> values= new HashMap<Assets, String>();
public String getPathFor(Assets asset){
if(!values.containsKey(asset)) throw new IllegalArgumentException();
return values.get(asset);
}
/* Contains further logic to persist configuration */
}
So you have well defined Assets
and a Configuration
object, where you store further Information.
Upvotes: 1
Reputation: 68985
What is the problem with the Map
? Create a static HashMap
and store you mapping there. You can directly get your values from there.
OR
if you simply need mappings where you key and values are constants then you can put them in an interface
(Variables are by default public
static
and final
).
Upvotes: 1