I'm toying with the idea of a character based game. The characters, robots, monsters, heroes (and weapons I suppose) will all have various attributes as per a D&D game for example.
They player should be able to configure those attributes to personalize their character, so maybe they'll be given 100 points at the start to allocate how they see fit to things like strength, speed, etc...
Also, I'd like the player to be able to add additional options, like maybe shields or individual spells, things like that.
Is there a standard design pattern for things like that?
I'm hacking my way through a prototype right now, and I've been considering an Options
object that accepts an Option
in the constructor, with methods addOption()
and getOption
.
For my implementation of Option
I did the following:
class Option{
final String optionKey;
String optionString;
int optionInt;
long optionLong;
float optionFloat;
char optionType;
public Option(String optionKey, Object option){
this.optionKey = optionKey;
if(option instanceOf String){
optionString = (String) option;
optionType = 's';
}else if(option instanceOf int){
optionInt = (int) option;
optionType = 'i';
}else if(option instanceOf long){
optionLong = (long) option;
optionType = 'l';
}else if(option instanceOf float){
optionFloat = (float) option;
optionType = 'f';
}
}
public String getOptionKey(){
return optionKey;
}
public String getOptionValue(){
return optionValue;
}
public void setOptionValue(String optionValue){
this.optionValue = optionValue;
}
public void getOptionType(){
return optionType;
}
}
But now I'm thinking this is a bit much, and perhaps I should be using an options class that encapsulates a Hashmap instead. So maybe something like this:
class Options{
HashMap<String, Object> options;
public Options(String key, Object value){
options = new HashMap<String, Object>();
options.put(key, value);
}
public void putOption(String key, Object value){
options.put(key, value);
}
public void getStringOption(String key){
return (String) options.get(key);
}
public void getFloatOption(String key){
return (float) options.get(key);
}
//....... and so on...
}
I'm thinking the version directly above is better, is there a standard for this type of thing?
Component
which many game engine do. check out this link. \$\endgroup\$ – Emadpres Dec 21 '14 at 13:31