Skip to content

Commit

Permalink
feat: State Condition & Function Mapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
christides11 committed Apr 24, 2022
1 parent 615b961 commit 0619794
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public int MovesetCount
private int nextState = 0;
public FighterManager fighterManager;

[NonSerialized] public StateFunctionMapper functionMapper = new StateFunctionMapper();
[NonSerialized] public StateConditionMapper conditionMapper = new StateConditionMapper();
[NonSerialized] public StateFunctionMapper functionMapperBase = new StateFunctionMapper();
[NonSerialized] public StateConditionMapper conditionMapperBase = new StateConditionMapper();

private void Awake()
{
Expand All @@ -43,6 +43,7 @@ public void Tick()
ChangeState(nextState, 0, true);
}
if (CurrentState == 0) return;
ProcessState();
}

private void ProcessState()
Expand All @@ -61,8 +62,8 @@ private void ProcessState()
}

if (!valid) continue;
if (!conditionMapper.TryCondition(states[CurrentState].data[i].Condition.FunctionMap, fighterManager, states[CurrentState].data[i].Condition)) continue;
functionMapper.functions[states[CurrentState].data[i].FunctionMap](fighterManager, states[CurrentState].data[i]);
if (!conditionMapperBase.TryCondition(states[CurrentState].data[i].Condition.FunctionMap, fighterManager, states[CurrentState].data[i].Condition)) continue;
functionMapperBase.functions[states[CurrentState].data[i].FunctionMap](fighterManager, states[CurrentState].data[i]);
}

if (states[CurrentState].autoIncrement)
Expand All @@ -83,7 +84,7 @@ public void MarkForStateChange(int moveset, int nextState)

public Combat.MovesetDefinition GetMoveset(int index)
{
throw new NotImplementedException();
return fighterManager.definition.movesets[index];
}

public bool ChangeState(int state, int stateFrame = 0, bool callOnInterrupt = true)
Expand All @@ -94,15 +95,15 @@ public bool ChangeState(int state, int stateFrame = 0, bool callOnInterrupt = tr
if(callOnInterrupt && CurrentState != (int)FighterStateEnum.NULL)
{
SetFrame(states[CurrentState].totalFrames+1);
// ProcessState();
ProcessState();
}

CurrentStateFrame = stateFrame;
CurrentState = state;
if(CurrentStateFrame == 0)
{
SetFrame(0);
//ProcessState();
ProcessState();
SetFrame(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static class BaseStateFunctions
public static void ChangeState(IFighterBase fighter, IStateVariables variables)
{
State.ChangeState vars = (State.ChangeState)variables;
fighter.StateManager.MarkForStateChange(vars.stateID);
fighter.StateManager.MarkForStateChange(vars.stateMovesetID, vars.stateID);
}

public static void ApplyGravity(IFighterBase fighter, IStateVariables variables)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,10 @@
using System;
using System.Collections.Generic;
using HnSF.Fighters;

namespace HnSF.Sample.TDAction.State
{
[System.Serializable]
public class StateConditionMapper
public class StateConditionMapper : StateConditionMapperBase
{
public Dictionary<int, Func<IFighterBase, IConditionVariables, bool>> functions =
new Dictionary<int, Func<IFighterBase, IConditionVariables, bool>>();

public StateConditionMapper()
{
functions.Add((int)ConditionFunctionEnum.NONE, BaseConditionFunctions.NoCondition);
}

public virtual bool TryRegisterCondition(int id, Func<IFighterBase, IConditionVariables, bool> condition)
{
if (functions.ContainsKey(id)) return false;
functions.Add(id, condition);
return true;
}

public virtual void RegisterCondition(int id, Func<IFighterBase, IConditionVariables, bool> condition)
{
functions.Add(id, condition);
}

public virtual bool OverrideCondition(int id, Func<IFighterBase, IConditionVariables, bool> condition)
{
functions[id] = condition;
return true;
}

public virtual void RemoveCondition(int id)
{
functions.Remove(id);
}

public virtual bool TryCondition(int id, IFighterBase fighter, IConditionVariables variables)
{
return functions[id](fighter, variables);
}
}
}

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

35 changes: 3 additions & 32 deletions Assets/HnSF/Samples/TDAction/Scripts/State/StateFunctionMapper.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
using System;
using System.Collections.Generic;
using HnSF.Fighters;
using HnSF.Sample.TDAction.State;

namespace HnSF.Sample.TDAction.State
namespace HnSF.Sample.TDAction
{
[System.Serializable]
public class StateFunctionMapper
public class StateFunctionMapper : StateFunctionMapperBase
{
public Dictionary<int, Action<IFighterBase, IStateVariables>> functions =
new Dictionary<int, Action<IFighterBase, IStateVariables>>();

public StateFunctionMapper()
{
functions.Add((int)StateFunctionEnum.CHANGE_STATE, BaseStateFunctions.ChangeState);
}

public virtual bool TryRegisterFunction(int id, Action<IFighterBase, IStateVariables> function)
{
if (functions.ContainsKey(id)) return false;
functions.Add(id, function);
return true;
}

public virtual void RegisterFunction(int id, Action<IFighterBase, IStateVariables> function)
{
functions.Add(id, function);
}

public virtual bool OverrideFunction(int id, Action<IFighterBase, IStateVariables> function)
{
functions[id] = function;
return true;
}

public virtual void RemoveFunction(int id)
{
functions.Remove(id);
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public Vector2[] FrameRanges
[SelectImplementation(typeof(IConditionVariables))] [SerializeField, SerializeReference]
public IConditionVariables condition;

public int stateMovesetID;
public int stateID;
}
}
41 changes: 41 additions & 0 deletions Assets/HnSF/State/StateConditionMapperBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using HnSF.Fighters;

namespace HnSF
{
[System.Serializable]
public class StateConditionMapperBase
{
public Dictionary<int, Func<IFighterBase, IConditionVariables, bool>> functions =
new Dictionary<int, Func<IFighterBase, IConditionVariables, bool>>();

public virtual bool TryRegisterCondition(int id, Func<IFighterBase, IConditionVariables, bool> condition)
{
if (functions.ContainsKey(id)) return false;
functions.Add(id, condition);
return true;
}

public virtual void RegisterCondition(int id, Func<IFighterBase, IConditionVariables, bool> condition)
{
functions.Add(id, condition);
}

public virtual bool OverrideCondition(int id, Func<IFighterBase, IConditionVariables, bool> condition)
{
functions[id] = condition;
return true;
}

public virtual void RemoveCondition(int id)
{
functions.Remove(id);
}

public virtual bool TryCondition(int id, IFighterBase fighter, IConditionVariables variables)
{
return functions[id](fighter, variables);
}
}
}
11 changes: 11 additions & 0 deletions Assets/HnSF/State/StateConditionMapperBase.cs.meta

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

36 changes: 36 additions & 0 deletions Assets/HnSF/State/StateFunctionMapperBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using HnSF.Fighters;

namespace HnSF
{
[System.Serializable]
public class StateFunctionMapperBase
{
public Dictionary<int, Action<IFighterBase, IStateVariables>> functions =
new Dictionary<int, Action<IFighterBase, IStateVariables>>();

public virtual bool TryRegisterFunction(int id, Action<IFighterBase, IStateVariables> function)
{
if (functions.ContainsKey(id)) return false;
functions.Add(id, function);
return true;
}

public virtual void RegisterFunction(int id, Action<IFighterBase, IStateVariables> function)
{
functions.Add(id, function);
}

public virtual bool OverrideFunction(int id, Action<IFighterBase, IStateVariables> function)
{
functions[id] = function;
return true;
}

public virtual void RemoveFunction(int id)
{
functions.Remove(id);
}
}
}
11 changes: 11 additions & 0 deletions Assets/HnSF/State/StateFunctionMapperBase.cs.meta

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

2 changes: 2 additions & 0 deletions HnSF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
<Compile Include="Assets\HnSF\Samples\TDAction\Scripts\State\Variables\ChangeState.cs" />
<Compile Include="Assets\HnSF\Samples\TDAction\Scripts\State\Functions\BaseStateFunctionEnum.cs" />
<Compile Include="Assets\HnSF\Samples\TDAction\Scripts\State\Conditions\BaseConditionFunctionEnum.cs" />
<Compile Include="Assets\HnSF\State\StateConditionMapperBase.cs" />
<Compile Include="Assets\HnSF\State\StateFunctionMapperBase.cs" />
<Compile Include="Assets\HnSF\Samples\TDAction\Scripts\State\StateConditionMapper.cs" />
<Compile Include="Assets\HnSF\Samples\TDAction\Scripts\State\StateFunctionMapper.cs" />
<None Include="Assets\HnSF\HnSF.asmdef" />
Expand Down

0 comments on commit 0619794

Please sign in to comment.