I’m working in javascript and attempting to make a very simple battle system. The issue is, I have to declare the players’ and monsters’ health in an if statement (because there are different player classes and monsters), but whenever I do this once the calculations in the if statement are done (monsterHealth -= playerAttack) the monster health doesn’t update because the condition of the if statement (the current monster’s original stats) is still active. Does anyone know how I might fix this? (the problem happens both with the playerHealth and the monsterHealth).
This is the class for the monster stats:
#pragma strict
public class MonsterData extends MonoBehaviour
{
static public var monsterHealth : int;
static public var monsterArmor: int;
static public var monsterAttack : int;
static public var curMonster : String;
static public var totDamage1 : int = PlayerStats.strength + PlayerStats.weaponDamage1 - monsterArmor;
static public var totDamage2 : int = PlayerStats.strength + PlayerStats.weaponDamage2 - monsterArmor;
static public var monNum : Monster = new Monster(0);
public class Monster
{
public function Monster(t : int)
{
totDamage1 = PlayerStats.strength + PlayerStats.weaponDamage1 - monsterArmor;
totDamage2 = PlayerStats.strength + PlayerStats.weaponDamage2 - monsterArmor;
if(t == 1)
{
curMonster = "Mutated Wolf";
monsterHealth = 16;
monsterArmor = 2;
monsterAttack = (Random.Range(2, 4));
}
}
}
}
And this is the GUI that runs the battle:
if(GameGUI.gamestage == -201)
{
if(GameGUI.backNum == 4)
{
GUI.skin = nightText;
}
else
{
GUI.skin = text;
}
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 - 300, 500, 500), Mon.curMonster);
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 - 250, 500, 500), "" + Mon.monsterHealth);
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 + 200, 500, 500), "Me");
GUI.Box(Rect(Screen.width/2 - 250, Screen.height/2 + 250, 500, 500), "" + PlayerStats.playerHealth);
if(GUI.Button(Rect(Screen.width/2 - 250, Screen.height/2 - 100, 500, 35), PlayerStats.attack1))
{
Mon.monsterHealth -= Mon.totDamage1;
PlayerStats.playerHealth = PlayerStats.playerHealth - Mon.monsterAttack;
Mon.monsterAttack = (Random.Range(2, 4));
}
if(GUI.Button(Rect(Screen.width/2 - 250, Screen.height/2 - 50, 500, 35), PlayerStats.attack2))
{
Mon.monsterHealth -= Mon.totDamage2;
PlayerStats.playerHealth = PlayerStats.playerHealth - Mon.monsterAttack;
Mon.monsterAttack = (Random.Range(2, 4));
}
if(Mon.monsterHealth <= 0)
{
Mon.monsterHealth = 0;
Debug.Log("0");
}
if(PlayerStats.playerHealth == 0)
{
GameGUI.gamestage = -3;
}
}