[spoiler]
Hey fellas,
I currently have run into a problem while creating lots of enemies - not that it doesn’t work, I’m just not sure what the best approach would be.
I have about 30 different enemy-types (ants, rats, orcs, dragons, etc.) and they all are described by the same fields. Therefore I have created a superclass called enemy:
public abstract class Enemy
{
/* General Information */
private String type;
private int ac;
/* Health related */
private int maxHP;
private int attackBonus;
private int movement;
/* Attack related */
private String attType;
private int attNo;
private int damage;
private int saveAs;
private int morale;
private int appearingNo; // determines how many monsters of this type appear
/* Drops */
private String treasureType;
private int exp;
public Enemy(String type, int ac, int maxHP, int attackBonus, int movement, String attType, int attNo, int damage, int saveAs, int morale, int appearingNo, String treasureType, int exp) {
this.type = type;
this.ac = ac;
this.maxHP = maxHP;
this.attackBonus = attackBonus;
this.movement = movement;
this.attType = attType;
this.attNo = attNo;
this.damage = damage;
this.saveAs = saveAs;
this.morale = morale;
this.appearingNo = appearingNo;
this.treasureType = treasureType;
this.exp = exp;
}
}
Now when I want to create a giant ant for example I need to call the super.constructor:
public class MobGiantAnt extends Enemy
{
public MobGiantAnt(String type, int ac, int maxHP, int attackBonus, int movement, String attType, int attNo, int damage, int saveAs, int morale, int appearingNo, String treasureType, int exp) {
super(type, ac, maxHP, attackBonus, movement, attType, attNo, damage, saveAs, morale, appearingNo, treasureType, exp);
}
}
Now here is where my question comes to play. All ants share the same stats (same type, same ac, same maxHP, etc.). I obviously don’t want to type in all parameters when I create a new ant but rather have new MobGiantAnt() automatically create an ant with the shared base values. Long story short: Is it better to use static fields (as they are shared by all objects created from that class to my understanding).
public class MobGiantAnt extends Enemy
{
final private static String type = "Giant Ant";
final private static int ac = 17;
final private maxHP = ...
.
.
.
.
public MobGiantAnt() {
super(type, ac, maxHP, attackBonus, movement, attType, attNo, damage, saveAs, morale, appearingNo, treasureType, exp);
}
}
Or to change the fields in the Enemy class to protected and do this:
public class MobGiantAnt extends Enemy
{
public MobGiantAnt() {
this.type = "Giant Ant";
this.ac = 17;
.
.
.
}
}
Excuse me if this is a stupid question, but I often get stuck on stuff like that
To my understanding protected means that only classes in the same package have access to the fields, but generally use of private fields and getters/setters is suggested. Is it good to have different subclasses for each enemy at all?
[/spoiler]