Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 5 additions & 27 deletions Assets/Scripts/Enemy/OnePunchMan/OnePunchManStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@

namespace StatePattern.Enemy
{
public class OnePunchManStateMachine : IStateMachine
public class OnePunchManStateMachine : GenericStateMachine<OnePunchManController>
{
private OnePunchManController Owner;
private IState currentState;
protected Dictionary<States, IState> States = new Dictionary<States, IState>();

public OnePunchManStateMachine(OnePunchManController Owner)
public OnePunchManStateMachine(OnePunchManController Owner) : base(Owner)
{
this.Owner = Owner;
CreateStates();
Expand All @@ -18,28 +15,9 @@ public OnePunchManStateMachine(OnePunchManController Owner)

private void CreateStates()
{
States.Add(StateMachine.States.IDLE, new IdleState(this));
States.Add(StateMachine.States.ROTATING, new RotatingState(this));
States.Add(StateMachine.States.SHOOTING, new ShootingState(this));
States.Add(StateMachine.States.IDLE, new IdleState<OnePunchManController>(this));
States.Add(StateMachine.States.ROTATING, new RotatingState<OnePunchManController>(this));
States.Add(StateMachine.States.SHOOTING, new ShootingState<OnePunchManController>(this));
}

private void SetOwner()
{
foreach(IState state in States.Values)
{
state.Owner = Owner;
}
}

public void Update() => currentState?.Update();

protected void ChangeState(IState newState)
{
currentState?.OnStateExit();
currentState = newState;
currentState?.OnStateEnter();
}

public void ChangeState(States newState) => ChangeState(States[newState]);
}
}
35 changes: 6 additions & 29 deletions Assets/Scripts/Enemy/PatrolMan/PatrolManStateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@

namespace StatePattern.Enemy
{
public class PatrolManStateMachine : IStateMachine
public class PatrolManStateMachine : GenericStateMachine<PatrolManController>
{
private PatrolManController Owner;
private IState currentState;
protected Dictionary<States, IState> States = new Dictionary<States, IState>();

public PatrolManStateMachine(PatrolManController Owner)
public PatrolManStateMachine(PatrolManController Owner) : base(Owner)
{
this.Owner = Owner;
CreateStates();
Expand All @@ -19,29 +15,10 @@ public PatrolManStateMachine(PatrolManController Owner)

private void CreateStates()
{
States.Add(StateMachine.States.IDLE, new IdleState(this));
States.Add(StateMachine.States.PATROLLING, new PatrollingState(this));
States.Add(StateMachine.States.CHASING, new ChasingState(this));
States.Add(StateMachine.States.SHOOTING, new ShootingState(this));
States.Add(StateMachine.States.IDLE, new IdleState<PatrolManController>(this));
States.Add(StateMachine.States.PATROLLING, new PatrollingState<PatrolManController>(this));
States.Add(StateMachine.States.CHASING, new ChasingState<PatrolManController>(this));
States.Add(StateMachine.States.SHOOTING, new ShootingState<PatrolManController>(this));
}

private void SetOwner()
{
foreach (IState state in States.Values)
{
state.Owner = Owner;
}
}

public void Update() => currentState?.Update();

protected void ChangeState(IState newState)
{
currentState?.OnStateExit();
currentState = newState;
currentState?.OnStateEnter();
}

public void ChangeState(States newState) => ChangeState(States[newState]);
}
}
6 changes: 3 additions & 3 deletions Assets/Scripts/Enemy/States/ChasingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

namespace StatePattern.Enemy
{
public class ChasingState : IState
public class ChasingState<T> : IState where T : EnemyController
{
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private GenericStateMachine<T> stateMachine;
private PlayerController target;

public ChasingState(IStateMachine stateMachine) => this.stateMachine = stateMachine;
public ChasingState(GenericStateMachine<T> stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter()
{
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scripts/Enemy/States/IdleState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace StatePattern.Enemy
{
public class IdleState : IState
public class IdleState<T> : IState where T : EnemyController
{
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private GenericStateMachine<T> stateMachine;
private float timer;

public IdleState(IStateMachine stateMachine) => this.stateMachine = stateMachine;
public IdleState(GenericStateMachine<T> stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter() => ResetTimer();

Expand All @@ -18,7 +18,7 @@ public void Update()
timer -= Time.deltaTime;
if (timer <= 0)
{
if (Owner.GetType() == typeof(OnePunchManController))
if (typeof(T) == typeof(OnePunchManController))
stateMachine.ChangeState(States.ROTATING);
else
stateMachine.ChangeState(States.PATROLLING);
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Enemy/States/PatrollingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace StatePattern.Enemy
{
public class PatrollingState : IState
public class PatrollingState <T>: IState where T : EnemyController
{
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private GenericStateMachine <T> stateMachine;
private int currentPatrollingIndex = -1;
private Vector3 destination;

public PatrollingState(IStateMachine stateMachine) => this.stateMachine = stateMachine;
public PatrollingState(GenericStateMachine <T> stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter()
{
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Enemy/States/RotatingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace StatePattern.Enemy
{
public class RotatingState : IState
public class RotatingState<T> : IState where T : EnemyController
{
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private GenericStateMachine<T> stateMachine;
private float targetRotation;

public RotatingState(IStateMachine stateMachine) => this.stateMachine = stateMachine;
public RotatingState(GenericStateMachine<T> stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter() => targetRotation = (Owner.Rotation.eulerAngles.y + 180) % 360;

Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/Enemy/States/ShootingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace StatePattern.Enemy
{
public class ShootingState : IState
public class ShootingState <T> : IState where T :EnemyController
{
public EnemyController Owner { get; set; }
private IStateMachine stateMachine;
private GenericStateMachine<T> stateMachine;
private PlayerController target;
private float shootTimer;

public ShootingState(IStateMachine stateMachine) => this.stateMachine = stateMachine;
public ShootingState(GenericStateMachine<T> stateMachine) => this.stateMachine = stateMachine;

public void OnStateEnter()
{
Expand Down
33 changes: 33 additions & 0 deletions Assets/Scripts/StateMachine/GenericStateMachine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using StatePattern.Enemy;
using System.Collections.Generic;

namespace StatePattern.StateMachine
{
public class GenericStateMachine<T> where T : EnemyController
{
protected T Owner;
protected IState currentState;
protected Dictionary<States, IState> States = new Dictionary<States, IState>();

public GenericStateMachine(T Owner) => this.Owner = Owner;

public void Update() => currentState?.Update();

protected void ChangeState(IState newState)
{
currentState?.OnStateExit();
currentState = newState;
currentState?.OnStateEnter();
}

public void ChangeState(States newState) => ChangeState(States[newState]);

protected void SetOwner()
{
foreach (IState state in States.Values)
{
state.Owner = Owner;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions Assets/Scripts/StateMachine/IStateMachine.cs

This file was deleted.

12 changes: 12 additions & 0 deletions Assets/Scripts/StateMachine/States.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace StatePattern.StateMachine
{
public enum States
{
IDLE,
ROTATING,
SHOOTING,
PATROLLING,
CHASING
}

}
11 changes: 11 additions & 0 deletions Assets/Scripts/StateMachine/States.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading