Game Development Reference
In-Depth Information
Since these are the only times when the properties change (the events), these are the
only points where their values need to be validated. See the following code sample
4-2 for a refactored enemy, which includes C# properties and a much reduced
Update function:
using UnityEngine;
using System.Collections;
public class EnemyObject : MonoBehaviour
{
//-------------------------------------------------------
//C# accessors for private variables
public int Health
{
get{return _health;}
set
{
//Clamp health between 0-100
_health = Mathf.Clamp(value, 0, 100);
//Check if dead
if(_health <= 0)
{
OnDead();
return;
}
//Check health and raise event if required
if(_health <= 20)
{
OnHealthLow();
return;
}
}
}
//-------------------------------------------------------
public int Ammo
{
get{return _ammo;}
set
{
//Clamp ammo between 0-50
_ammo = Mathf.Clamp(value,0,50);
 
Search WWH ::




Custom Search