Good morning!
I did the whole thing before Same offered the solution to the challenge and since nothing is breaking (yet), I wanted you learned opinion about my version:
- I decided to reward Inside the Die() method
public void TakeDamage(GameObject instigator,float damage)
{
currentHealth= Mathf.Max(currentHealth-damage, 0);
print(currentHealth);
if(currentHealth<= 0)
{
if(instigator!=null)
{
TriggerDeath(instigator);
}
else
{
TriggerDeath(null);
}
}
}
private void TriggerDeath(GameObject theOnceWhoDidTheKilling)
{
if(isDead) return;
GetComponent<Animator>().SetTrigger("dying");
GetComponent<CapsuleCollider>().enabled= false;
GetComponent<ActionScheduler>().CancelCurrentAction();
RewardExperience(theOnceWhoDidTheKilling);
isDead = true;
}
- If it was the enemy killing us or something else killing the enemy
void RewardExperience(GameObject killer)
{
float experienceToReward = baseStats.GetExperienceFromBaseStats();
if(killer== null) return;
PlayerExperience playerExperience= killer.GetComponent<PlayerExperience>();
if(playerExperience == null) return;
playerExperience.GainExperience(experienceToReward);
}
- To prevent death on load from awarding undeserved XP
public void RestoreState(object state)
{
currentHealth = (float) state;
if (currentHealth <= 0)
{
TriggerDeath(null);
}
}
So I am not too sure how It would go if for example I want to gain XP if something else kills the enemy after I just damage it. Will the code eventually break something?