In my game when pressing the right mouse button you will place an object on the ground. all objects have the same super class (GameObject). I have a field called selected and will be equal to one certain gameobject at a time. when clicking the right mouse button it checks whats the instance of selected and that how it determines which object to place on the ground. code exapmle: t is the "slot" for which the object will go to.
if (selected instanceof MapleTree) {
t = new MapleTree(game,highLight);
} else if (selected instanceof OakTree) {
t = new OakTree(game,highLight);
}
Now it has to be a "new" instance of the object. Eventually my game will have hundreds of GameObjects and I don't want to have a huge if else statement. How would I make it so it scrolls though the possible kinds of objects and if its the correct type then create a new instance of it...?
When pressing E it will switch the type of selected and is an if else statement as well. How would I do it for this too? here is a code example:
if (selected instanceof MapleTree) {
selected = new OakTree(game);
} else if (selected instanceof OakTree) {
selected = new MapleTree(game);
}