My friend and I are working on a game. There is an abstract item class and some specific items:
abstract class Item{
PVector Start;
PVector End;
float Progress;
boolean delete;
boolean flammable;
int BurnNumbers;
Item(float Speed, PVector Pos) {
Start = Pos;
End = Pos;
Progress = 0f;
delete = false;
flammable = false;
BurnNumbers = 0;
}
}
class Iron extends Item{
Iron(PVector Pos) {
super(1f, Pos);
}
Iron() {super(1f, new PVector());}
}
class Gold extends Item{
Gold(PVector Pos) {
super(1f, Pos);
}
Gold() {super(1f, new PVector());}
}
class Copper extends Item{
Copper(PVector Pos) {
super(1f, Pos);
}
Copper() {super(1f, new PVector());}
}
class Coal extends Item{
Coal(PVector Pos) {
super(1f, Pos);
flammable = true;
BurnNumbers = 5;
}
Coal() {super(1f, new PVector());}
}
We save the items in JSONObjects as a string. To reload a game, we have to make new item objects out of the strings.
I know you can do something like this with Class.forname(String), but I haven’t been able to use it. That’s why we’ve solved it this way up until now:
Item getItem(String Name) {
switch(Name) {
case "Iron": return new Iron();
case "Gold": return new Gold();
case "Copper": return new Copper();
case "Coal": return new Coal();
default: println("Item \""+Name+"\" does not exist or it has to be updated in getItem()."); return null;
}
}
Anyone know how to write the getItem() function without a switch?
However I’m not sure if I recommend to do so Your switch statement looks a bit wordy or repetitive, but it’s a good way to keep track of what is going on.