I don’t know, if you reuse your Enemy instance on purpose, but I think you have problems to grasp the concepts of classes and instances. Take a look at http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html.
I think you want something like this:
public class Enemy
{
private boolean survive = true;
private String name;
private int health;
/**
* This constructs an Enemy with a name and loads the initial data from it's info file
*/
public Enemy(String name)
{
this.name = name;
loadEnemyInfo(name);
}
/**
* Gets the name of this Enemy instance
*/
public int getName()
{
return name;
}
/**
* Gets the current health of this Enemy instance
*/
public int getHealth()
{
return health;
}
/**
* Hits the Enemy instance and does a certain damage
*/
public void hit(int damage)
{
health = health - damage;
}
/**
* Returns, if this Enemy instance is still alive
*/
public boolean isAlive()
{
if(health<1){survive = false;}
return survive;
}
/**
* Loads this Enemy instances inital values from an info file
* This is protected to be callable from this or derived classes, but not from the outside.
*/
protected void loadEnemyInfo()
{
int[] enemyInfo = new int[5];
String line;
BufferedReader reader = null;
try
{
File inFile = new File("resources/enemies/"+enemy+".txt");
reader = new BufferedReader(new FileReader("resources/enemies/"+enemy+".txt"));
line = null;
int x = 0;
while ((line=reader.readLine()) != null)
{
enemyInfo[x] = Integer.parseInt(line);
x++;
}
}
catch(FileNotFoundException e)
{
System.err.println("Library not complete");
}
finally // always close streams in a finally block!!!
{
if(reader!=null)
{
try { reader.close() }
catch(IOException ignore){}
}
}
catch(IOException e){}
catch(NumberFormatException e){}
health = enemyInfo[0];//over here it is ten
}
}
So you can use it like
public class Battle extends JPanel implements ActionListener {
// (...)
private Enemy[] enemies = null;
// (...)
public Battle() {
// (...)
enemies = new Enemy[]{
new Enemy("Ogre"),
new Enemy("Ogre"),
new Enemy("Ogre"),
new Enemy("Ogre"),
new Enemy("Gimp"),
new Enemy("Gimp"),
new Enemy("Rat"),
new Enemy("Rat"),
new Enemy("Rat"),
new Enemy("Rat"),
new Enemy("Rat"),
new Enemy("Overlord")
};
// (...)
}
// (...)
}
This way you create an array of enemies that consists of 4 instance with initial values loaded from the “Orge.txt” file, 2 instances from the “Gimp.txt” file, 5 instance loaded from the “Rat.txt” file and one from the “Overlord.txt”.
Some additional notes on common java code style:
- Use uppercase classes (Battle instead of battle) - lowercase names are usually used as variable names
- Use loadEnemyInfo is better than getEnemyInfo, because getXXX usually describes the accessor of a “Property”
- Make every member variable of a class private and provide get/is/set Methods to expose them to the outside only if desired
- I changes checkAlive to isAlive, since the status to be alive is a “Property” of the Enemy. (isXXX describes the accessor of a boolean “Property”
Edit: missed the “String” type for the “name” variable and the “enemy[]” declaration was not correct.