-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5a3ab76
Showing
7 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.meta |
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
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!"); | ||
} | ||
|
||
} | ||
} |
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
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 | ||
} | ||
} |
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
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); | ||
} | ||
} | ||
} | ||
} |
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
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() { } | ||
} | ||
} |
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
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(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
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
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>(); | ||
} | ||
} | ||
} |