-
Dynamic Subclasses?
I'm working on a game that uses tiles. It's setup so players can make their own maps with the default tiles given. I want to make it so users can can create their own tiles with their own properties in a text file and be loaded to the game with all of its properties as a tile subclass. Is it possible to create subclasses like this?
-
More than it could be an object isn't it? What you really mean by dynamic classes?
-
Ok, here is an example of some tiles programmed normally-
Code:
public class Tile
{
Color c;
Image texture;
int alpha;
int bounce;
boolean solid;
boolean buildable;
boolean smoothDraw;
etc...
public Tile()
{
c = new Color(Color.BLACK);
texture = null
bounce = 0;
etc...
}
}
public class Grass extends Tile
{
public Grass()
{
c = new Color(Color.GREEN);
etc...
}
}
public class Dirt extends Tile
{
etc...
But what is a user wants to make a tile called "Trampoline" for his/her map that has a bounce property of 20. So that when a user comes down on it they will bounce. Can I dynamically generate this subclass based on that property?
I know it's possible to make a Tile object and just set all these values one by one in a loop. But a map may have tens of thousands of tiles. Is that the only way it can be done or can I dynamically make the "Trampoline" subclass someway and add it to the Tile grid?
-
One solution is to rather than making a bunch of classes, make one Tile class that is flexible enough to change properties. And why not change the values in a loop? Even if you have many tiles this can work.
-
That's what I originally thought but I also thought that was a little primitive. So I guess I will go with that.
-
Yes, in a one class make possible to change required parameters according to your design. Then you may need to deals with additional values in a situation, if any. Otherwise only thing you've to do is assign appropriate values.