Skip to content

Commit 57d418c

Browse files
committed
Realized Hero State Macine
1 parent 0ee6991 commit 57d418c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+4414
-0
lines changed

HeroStateMachineExample/Assets/Resources.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"androidStore":"GooglePlay"}

HeroStateMachineExample/Assets/Resources/BillingMode.json.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HeroStateMachineExample/Assets/RimuruDev.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HeroStateMachineExample/Assets/RimuruDev/Codebase.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HeroStateMachineExample/Assets/RimuruDev/Codebase/HeroStateMachine.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HeroStateMachineExample/Assets/RimuruDev/Codebase/HeroStateMachine/Base.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// ********************************************************************** //
2+
//
3+
// Copyright (c) 2023 Rimuru Tempest aka RimuruDev. All rights reserved.
4+
// This code is licensed under the MIT license (see LICENSE for details)
5+
// Contact me: [email protected]
6+
//
7+
// ********************************************************************** //
8+
9+
using System;
10+
11+
namespace RimuruDev.Codebase.HeroStateMachine.Base
12+
{
13+
public abstract class State : IDisposable
14+
{
15+
protected readonly StateMachine StateMachine;
16+
17+
protected State(StateMachine stateMachine) =>
18+
StateMachine = stateMachine;
19+
20+
public virtual void Enter()
21+
{
22+
}
23+
24+
public virtual void Exit()
25+
{
26+
}
27+
28+
public virtual void Awake()
29+
{
30+
}
31+
32+
public virtual void Start()
33+
{
34+
}
35+
36+
public virtual void Update()
37+
{
38+
}
39+
40+
public virtual void LateUpdate()
41+
{
42+
}
43+
44+
public virtual void FixedUpdate()
45+
{
46+
}
47+
48+
public virtual void Dispose()
49+
{
50+
}
51+
}
52+
}

HeroStateMachineExample/Assets/RimuruDev/Codebase/HeroStateMachine/Base/State.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// ********************************************************************** //
2+
//
3+
// Copyright (c) 2023 Rimuru Tempest aka RimuruDev. All rights reserved.
4+
// This code is licensed under the MIT license (see LICENSE for details)
5+
// Contact me: [email protected]
6+
//
7+
// ********************************************************************** //
8+
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using UnityEngine;
13+
14+
namespace RimuruDev.Codebase.HeroStateMachine.Base
15+
{
16+
public class StateMachine
17+
{
18+
public event Action<State> OnSwitchState;
19+
20+
private State _currentState;
21+
private readonly Dictionary<Type, State> _states;
22+
23+
public StateMachine() =>
24+
_states = new Dictionary<Type, State>();
25+
26+
public StateMachine(Dictionary<Type, State> states) =>
27+
_states = states ?? throw new ArgumentNullException();
28+
29+
~StateMachine() =>
30+
FullCleanup();
31+
32+
public void AddState(State state)
33+
{
34+
if (state == null)
35+
throw new NullReferenceException();
36+
37+
if (_states.ContainsKey(state.GetType()))
38+
throw new NullReferenceException();
39+
40+
_states.Add(state.GetType(), state);
41+
}
42+
43+
public void AddStates(Dictionary<Type, State> states)
44+
{
45+
if (states == null)
46+
throw new NullReferenceException();
47+
48+
foreach (var state in states.Where(state => state.Key != null || state.Value != null))
49+
_states.Add(state.Key, state.Value);
50+
}
51+
52+
public void SwitchState<TState>() where TState : State
53+
{
54+
var type = typeof(TState);
55+
56+
if (type == null)
57+
throw new NullReferenceException($"{nameof(type)}");
58+
59+
if (_currentState != null && _currentState.GetType() == type)
60+
return;
61+
62+
if (!_states.TryGetValue(type, out var newState))
63+
{
64+
Debug.LogWarning("State not found!");
65+
return;
66+
}
67+
68+
_currentState?.Exit();
69+
70+
_currentState = newState;
71+
72+
_currentState?.Enter();
73+
74+
OnSwitchState?.Invoke(newState);
75+
}
76+
77+
public void Awake() =>
78+
_currentState?.Awake();
79+
80+
public void Start() =>
81+
_currentState?.Start();
82+
83+
public void Update() =>
84+
_currentState?.Update();
85+
86+
public void LateUpdate() =>
87+
_currentState?.LateUpdate();
88+
89+
public void FixedUpdate() =>
90+
_currentState?.FixedUpdate();
91+
92+
public void DisposeState() =>
93+
_currentState?.Dispose();
94+
95+
public void FullCleanup()
96+
{
97+
if (_states == null)
98+
return;
99+
100+
foreach (var state in _states.Where(state => state.Value != null))
101+
state.Value.Dispose();
102+
103+
_states.Clear();
104+
105+
_currentState = null;
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)