Hi everyone,
I’m currently working on a project in which I have different types of “bullets”. I have a superclass called Projectile to handle all the needed functions that are common to all types of bullets. In fact, the only thing that can change from one bullet type to another are some variables : damage and pierce.
Because I will spawn a lot of each bullet type, I created one subclass for each bullet type, in where I had in mind to define damage and pierce as final static variables so that they would only be stored once in memory for each bullet type (I think this is how it works with static var).
So this is what I got for now :
class Projectile{
int damage, pierce; //if I don't define them here, I can't access them with superclass functions
Projectile(Tower fired_from_tower, float x_dep, float y_dep, float direction){
//some stuff
}
//some functions
}
class Dart extends Projectile{ //this is the entire subclass and other bullet types are equivalent
static final int damage = 1, pierce = 1;
Dart(Tower fired_from_tower, float x_dep, float y_dep, float direction){
super(fired_from_tower, x_dep, y_dep, direction);
}
}
The problem is, if I remove int damage, pierce; in the superclass, the superclass functions can’t recognize them, but if I do, and try to access these variables in the superclass, they’re value are still 0 (the value only changed in the subclasses).
My question is: is there a way to get the subclasses values from the superclass? Also, I just have this subclasses struc to save a bit of memory but since my whole project isn’t very optimised, if that wouldn’t save much (I would at most spawn 500 of each bullet type), I could just stay with the superclass Projectile and a set_damage_and_pierce() function (but I would loose the static final adventage)
Thanks!