Hi
Following on from a different thread about AI I decided to have a little think about creating something that would be easy to implement and easy to manage regarding game characters.
I came up with this, I would very much appreciate comments/tips on this (apologies - this is fairly lengthy):
using UnityEngine;
///<summary>
/// Enum of the possible creature actions.
///</summary>
public enum CreatureAction { DoNothing, Sleeping, Eating, Sitting, Standing }
///<sumamry>
/// Interface which all creature behaviour scripts must implement.
///</summary>
public interface ICreatureBehaviour {
CreatureAction ActionThisScriptImplements { get; }
}
///<summary>
/// Sealed class to manage a creatures actions.
///</summary>
public sealed class Creature : MonoBehaviour {
///<summary>
/// Unity exposed creature behaviours - upto 10
///</summary>
public ICreatureBehaviour[] BehaviourScripts = new ICreatureBehaviour[10];
private CreatureAction _currentAction = CreatureAction.DoNothing;
///<summary>
/// Gets or Sets the current creature behaviour.
///</summary>
public CreatureAction CurrentAction {
set {
_currentAction = value;
ActivateScripts();
}
get {
return _currentAction;
}
}
// activate/deactivate a script based upon its creature action.
private void ActivateScripts(){
for(int i = 0; i < BehaviourScripts.Length; i++)
{
if(BehaviourScripts[i] != null)
{
if(BehaviourScripts[i].ActionThisScriptImplements == _currentAction)
((GameObject)BehaviourScripts[i]).active = true;
else
((GameObject)BehaviourScripts[i]).active = false;
}
}
}
// activate scripts on awake
private void Awake() {
ActivateScripts();
}
}
The idea is that the above script would be attached to the game object (the creature/character), and a number of behaviour scripts would also be added to the game object - but would also be added in the editor to the ICreatureBehaviour array. When you wanted to change the current ‘behaviour’ of the creature you would simple change the value in the public property CurrentAction.
Here is the Behaviour scripts:
using UnityEngine;
///<summary>
/// Sealed class to implement an example sleeping behaviour of a creature.
///</summary>
public sealed class SleepingBehaviour : MonoBehaviour, ICreatureBehaviour {
///<summary>
/// Gets the Action that this script implements.
/// </sleeping>
public CreatureAction ActionThisScriptImplements
{
get {
return CreatureAction.Sleeping;
}
}
// TODO: Implement the behaviours in here...
}
///<summary>
/// Sealed class to implement an example eating behaviour of a creature.
///</summary>
public sealed class EatingBehaviour : MonoBehaviour, ICreatureBehaviour {
///<summary>
/// Gets the Action that this script implements.
/// </sleeping>
public CreatureAction ActionThisScriptImplements
{
get {
return CreatureAction.Eating;
}
}
// TODO: Implement the behaviours in here...
}
// you get the idea...
I know there are many ways to implement this sort of stuff. Does anybody see any pitfalls/problems with this?
I haven’t actually tested this yet, it’s just a thought!
Ta
JT