Skip to content

Commit 5a3ab76

Browse files
committed
First commit
0 parents  commit 5a3ab76

File tree

7 files changed

+189
-0
lines changed

7 files changed

+189
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.meta

Command/Command.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections;
3+
using UnityEngine;
4+
5+
namespace Patterns.Command
6+
{
7+
public abstract class Command
8+
{
9+
10+
public virtual void Execute(object target, params object[] args)
11+
{
12+
throw new NotImplementedException();
13+
}
14+
15+
public virtual void Undo()
16+
{
17+
throw new NotImplementedException();
18+
}
19+
20+
}
21+
22+
public class NullCommand : Command
23+
{
24+
25+
public override void Execute(object target, params object[] args)
26+
{
27+
Debug.LogError("Tried to execute null command!");
28+
}
29+
30+
}
31+
}

Observer/Messages.cs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Linq;
3+
using System.Collections;
4+
using System.Reflection;
5+
using UnityEngine;
6+
using System.Collections.Generic;
7+
8+
namespace Patterns.Observer
9+
{
10+
public enum Message
11+
{
12+
// COMBAT ============================ //
13+
14+
Combat_Index = 1,
15+
Combat_Hit,
16+
Combat_GotHit,
17+
Combat_EnergyChanged,
18+
Combat_EnergyBlockConsumed,
19+
Combat_BodyPartHealthChanged,
20+
Combat_BodyPartDestroyed,
21+
Combat_Punch,
22+
Combat_WeaponShoot,
23+
Combat_EnergyDanger,
24+
Combat_Struck,
25+
Combat_Death,
26+
27+
// MOVEMENT ========================== //
28+
29+
Movement_Index = 300,
30+
Movement_StepGrass,
31+
Movement_StepMetal,
32+
33+
// SYSTEM ============================ //
34+
35+
System_Index = 700,
36+
System_Pause,
37+
System_Unpause
38+
}
39+
}

Observer/Observer.cs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using UnityEngine;
3+
4+
namespace Patterns.Observer
5+
{
6+
public interface IObserver {
7+
void OnNotification(object sender, Message subject, params object[] args);
8+
}
9+
10+
public static class MessageSystem
11+
{
12+
private static Dictionary<Message, List<IObserver>> listeners;
13+
14+
public static bool initialized = false;
15+
16+
public static void Init()
17+
{
18+
initialized = true;
19+
listeners = new Dictionary<Message, List<IObserver>>();
20+
}
21+
22+
public static void Notify(this object sender, Message subject, params object[] args)
23+
{
24+
if (sender == null)
25+
Debug.LogError("Sender is null!");
26+
27+
if (listeners.ContainsKey(subject))
28+
foreach (var observer in listeners[subject])
29+
observer.OnNotification(sender, subject, args);
30+
}
31+
32+
public static void Observe(this IObserver observer, Message msgType)
33+
{
34+
if (!listeners.ContainsKey(msgType))
35+
listeners.Add(msgType, new List<IObserver>() { observer });
36+
else
37+
{
38+
if (!listeners[msgType].Contains(observer))
39+
listeners[msgType].Add(observer);
40+
}
41+
}
42+
}
43+
}

State/State.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.Events;
5+
6+
namespace Patterns.StateMachine
7+
{
8+
public class State : MonoBehaviour
9+
{
10+
public Transition[] transitions;
11+
12+
private void Awake()
13+
{
14+
transitions = GetComponents<Transition>();
15+
}
16+
17+
public virtual void OnEnter() { }
18+
public virtual void OnStay() { }
19+
public virtual void OnExit() { }
20+
}
21+
}

State/StateMachine.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using UnityEngine;
2+
using Patterns.Observer;
3+
4+
namespace Patterns.StateMachine
5+
{
6+
public class StateMachine : MonoBehaviour, IObserver
7+
{
8+
public GameObject owner = null;
9+
public State state = null;
10+
11+
protected virtual void Update()
12+
{
13+
state.OnStay();
14+
}
15+
16+
public virtual void OnNotification(object sender, Message msg, params object[] args)
17+
{
18+
if (owner == null)
19+
return;
20+
21+
if ((GameObject)sender == owner)
22+
{
23+
foreach (var t in state.transitions)
24+
{
25+
if (t.trigger == msg)
26+
{
27+
t.source.OnExit();
28+
state = t.target;
29+
t.target.OnEnter();
30+
}
31+
}
32+
}
33+
}
34+
35+
}
36+
}

State/Transition.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Patterns.Observer;
2+
using UnityEngine;
3+
4+
namespace Patterns.StateMachine
5+
{
6+
[RequireComponent(typeof(State))]
7+
public class Transition : MonoBehaviour
8+
{
9+
public Message trigger;
10+
[HideInInspector] public State source;
11+
public State target;
12+
13+
void Awake()
14+
{
15+
source = GetComponent<State>();
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)