Created
February 12, 2016 02:44
-
-
Save Blecki/e54bda589941d2d50f18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
public class FishStatsTemplate : MonoBehaviour | |
{ | |
public GameObject Parent; // Set this to the fish's parent prefab. EG, fast dolphin has a reference to dolphin. | |
public GameObject Zone; // Set by the spawner script to the zone that spawned it. | |
// Feel free to add new stats or change them or whatever. | |
public float Speed = 1.0f; | |
public float Attack = 1.0f; | |
public float GetModifiedSpeed() { return GetStat("Speed"); } | |
public float GetModifiedAttack() { return GetStat("Attack"); } | |
private float GetStat(string BackingFieldName) | |
{ | |
// Can cache this statically if it's a problem. | |
var backingField = GetType().GetField(BackingFieldName); | |
return CalcStat(backingField); | |
} | |
private float CalcStat(System.Reflection.FieldInfo Field) | |
{ | |
// Since this is never called except through an accessor method, no reason to check if the field | |
// actually exists. We know it must. | |
var value = 0.0f; | |
if (Parent != null) | |
{ | |
var parentStats = Parent.GetComponent<FishStatsTemplate>(); | |
if (parentStats != null) | |
value = parentStats.CalcStat(Field); | |
} | |
value *= (Field.GetValue(this) as float?).Value; | |
if (Zone != null) | |
{ | |
var zoneStats = Zone.GetComponent<FishStatsTemplate>(); | |
if (zoneStats != null) | |
value *= zoneStats.CalcStat(Field); | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment