Reputation: 315
I am trying to write some combat methods but I am having some difficulty.
Code:
int attackPower;
int defencePower;
int healthPoints;
int startingHealthPoints;
int remainingHealthPoints;
int damageDone = 0;
//Constructor
public Combat(int initialAttackPower, int initialDefencePower, int initialHealthPoints) {
attackPower = initialAttackPower;
defencePower = initialDefencePower;
healthPoints = initialHealthPoints;
}
//Methods
public void setAttackPower(int newAttackPower) {
attackPower = newAttackPower;
}
public void setDefencePower(int newDefencePower) {
defencePower = newDefencePower;
}
public void setHealthPoints(int newHealthPoints) {
healthPoints = newHealthPoints;
}
int getAttackPower() {
return attackPower;
}
int getDefencePower() {
return defencePower;
}
int getHealthPoints() {
return healthPoints;
}
int getDamageDone() {
damageDone = 5;
return damageDone;
}
int getStartingHealthPoints(int currentHP) {
if (damageDone == 0) {
return startingHealthPoints = healthPoints;
}
else {
return startingHealthPoints = currentHP + damageDone;
}
}
int getRemainingHealthPoints() {
return remainingHealthPoints = startingHealthPoints - damageDone;
}
MAIN:
Combat combat1 = new Combat(15,20,100);
System.out.println("Total HP: " + combat1.getHealthPoints());
System.out.println("Starting HP: " + combat1.getStartingHealthPoints(combat1.getRemainingHealthPoints()));
System.out.println("Attack 1: " + combat1.getDamageDone());
System.out.println("After Attack 1 HP: " + combat1.getRemainingHealthPoints());
System.out.println("Before Attack 1 HP: " + combat1.getStartingHealthPoints(combat1.getRemainingHealthPoints()));
System.out.println("Attack 2: " + combat1.getDamageDone());
System.out.println("After Attack 2 HP: " + combat1.getRemainingHealthPoints());
System.out.println("Before Attack 2 HP: " + combat1.getStartingHealthPoints(combat1.getRemainingHealthPoints()));
System.out.println("Total HP: " + combat1.getHealthPoints());
Main Ouput:
Total HP: 100
Starting HP: 100
Attack 1: 5
After Attack 1 HP: 95
Before Attack 1 HP: 100
Attack 2: 5
After Attack 2 HP: 95
Before Attack 2 HP: 100
Total HP: 100
Ideally after each attack the startingHealth and remainingHealth should change accordingly. So for example, after attack 2 HP should be 90 and before attack HP should be 95. Then for after attack 3 HP should be 85 and before HP should be 90. So on and so forth. I'm probably making a simple mistake, but I could really use some help.
Upvotes: 1
Views: 131
Reputation: 396
Your "before" would be: getHealthPoints()
followed by some sort of receiveAttack() method where you pass in the health points representing the strength of the attack.
public void receiveAttack(int damage) {
healthPoints -= damage;
}
Now, call getHealthPoints() again and you will have your total.
If you need to store "most recent damage", sure, add another field for this and store it when the object receives an attack.
Upvotes: 2