Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
leotgo committed Oct 5, 2017
0 parents commit 5a3ab76
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.meta
31 changes: 31 additions & 0 deletions Command/Command.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections;
using UnityEngine;

namespace Patterns.Command
{
public abstract class Command
{

public virtual void Execute(object target, params object[] args)
{
throw new NotImplementedException();
}

public virtual void Undo()
{
throw new NotImplementedException();
}

}

public class NullCommand : Command
{

public override void Execute(object target, params object[] args)
{
Debug.LogError("Tried to execute null command!");
}

}
}
39 changes: 39 additions & 0 deletions Observer/Messages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Linq;
using System.Collections;
using System.Reflection;
using UnityEngine;
using System.Collections.Generic;

namespace Patterns.Observer
{
public enum Message
{
// COMBAT ============================ //

Combat_Index = 1,
Combat_Hit,
Combat_GotHit,
Combat_EnergyChanged,
Combat_EnergyBlockConsumed,
Combat_BodyPartHealthChanged,
Combat_BodyPartDestroyed,
Combat_Punch,
Combat_WeaponShoot,
Combat_EnergyDanger,
Combat_Struck,
Combat_Death,

// MOVEMENT ========================== //

Movement_Index = 300,
Movement_StepGrass,
Movement_StepMetal,

// SYSTEM ============================ //

System_Index = 700,
System_Pause,
System_Unpause
}
}
43 changes: 43 additions & 0 deletions Observer/Observer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using UnityEngine;

namespace Patterns.Observer
{
public interface IObserver {
void OnNotification(object sender, Message subject, params object[] args);
}

public static class MessageSystem
{
private static Dictionary<Message, List<IObserver>> listeners;

public static bool initialized = false;

public static void Init()
{
initialized = true;
listeners = new Dictionary<Message, List<IObserver>>();
}

public static void Notify(this object sender, Message subject, params object[] args)
{
if (sender == null)
Debug.LogError("Sender is null!");

if (listeners.ContainsKey(subject))
foreach (var observer in listeners[subject])
observer.OnNotification(sender, subject, args);
}

public static void Observe(this IObserver observer, Message msgType)
{
if (!listeners.ContainsKey(msgType))
listeners.Add(msgType, new List<IObserver>() { observer });
else
{
if (!listeners[msgType].Contains(observer))
listeners[msgType].Add(observer);
}
}
}
}
21 changes: 21 additions & 0 deletions State/State.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Patterns.StateMachine
{
public class State : MonoBehaviour
{
public Transition[] transitions;

private void Awake()
{
transitions = GetComponents<Transition>();
}

public virtual void OnEnter() { }
public virtual void OnStay() { }
public virtual void OnExit() { }
}
}
36 changes: 36 additions & 0 deletions State/StateMachine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using UnityEngine;
using Patterns.Observer;

namespace Patterns.StateMachine
{
public class StateMachine : MonoBehaviour, IObserver
{
public GameObject owner = null;
public State state = null;

protected virtual void Update()
{
state.OnStay();
}

public virtual void OnNotification(object sender, Message msg, params object[] args)
{
if (owner == null)
return;

if ((GameObject)sender == owner)
{
foreach (var t in state.transitions)
{
if (t.trigger == msg)
{
t.source.OnExit();
state = t.target;
t.target.OnEnter();
}
}
}
}

}
}
18 changes: 18 additions & 0 deletions State/Transition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Patterns.Observer;
using UnityEngine;

namespace Patterns.StateMachine
{
[RequireComponent(typeof(State))]
public class Transition : MonoBehaviour
{
public Message trigger;
[HideInInspector] public State source;
public State target;

void Awake()
{
source = GetComponent<State>();
}
}
}

0 comments on commit 5a3ab76

Please sign in to comment.