Skip to content

Commit

Permalink
feat: Added charge level vars to EntityCombatManager.
Browse files Browse the repository at this point in the history
EntityCombatManager can now handle charge levels.

BREAKING CHANGE: Renamed delegates.
  • Loading branch information
christides11 committed Aug 17, 2020
1 parent 28418b2 commit 066b55c
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 35 deletions.
54 changes: 42 additions & 12 deletions Assets/CAF/Entities/Managers/EntityCombatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,37 @@ namespace CAF.Entities
{
public class EntityCombatManager : MonoBehaviour, IHurtable
{
public delegate void EntityEmptyAction(EntityManager self);
public delegate void EntityHealthChangedAction(EntityManager initializer, EntityManager self, HitInfoBase hitInfo);
public delegate void EntityMovesetChangedAction(EntityManager self, MovesetDefinition lastMoveset);
public event EntityHealthChangedAction OnHit;
public event EntityHealthChangedAction OnHealed;
public event EntityEmptyAction OnEnterHitStop;
public event EntityEmptyAction OnEnterHitStun;
public event EntityEmptyAction OnHitStopAdded;
public event EntityEmptyAction OnHitStunAdded;
public event EntityEmptyAction OnExitHitStop;
public event EntityEmptyAction OnExitHitStun;
public event EntityMovesetChangedAction OnMovesetChanged;
public delegate void EmptyAction(EntityManager self);
public delegate void HealthChangedAction(EntityManager initializer, EntityManager self, HitInfoBase hitInfo);
public delegate void MovesetChangedAction(EntityManager self, MovesetDefinition lastMoveset);
public delegate void ChargeLevelChangedAction(EntityManager self, int lastChargeLevel);
public delegate void ChargeLevelChargeChangedAction(EntityManager self, int lastChargeLevelCharge);
public event HealthChangedAction OnHit;
public event HealthChangedAction OnHealed;
public event EmptyAction OnEnterHitStop;
public event EmptyAction OnEnterHitStun;
public event EmptyAction OnHitStopAdded;
public event EmptyAction OnHitStunAdded;
public event EmptyAction OnExitHitStop;
public event EmptyAction OnExitHitStun;
public event MovesetChangedAction OnMovesetChanged;
public event ChargeLevelChangedAction OnChargeLevelChanged;
public event ChargeLevelChargeChangedAction OnChargeLevelChargeChanged;

public int Team { get; set; } = 0;
public int HitStun { get; protected set; } = 0;
public int HitStop { get; protected set; } = 0;
public int CurrentChargeLevel { get; protected set; } = 0;
public int CurrentChargeLevelCharge { get; protected set; } = 0;
public MovesetAttackNode CurrentAttack { get; protected set; } = null;
public MovesetDefinition CurrentMoveset { get; protected set; } = null;
public HitInfo LastHitBy { get; protected set; }

public EntityManager controller;
public EntityHitboxManager hitboxManager;



protected virtual void Awake()
{
hitboxManager = new EntityHitboxManager(this, controller);
Expand Down Expand Up @@ -63,6 +71,8 @@ public virtual void Cleanup()
{
return;
}
CurrentChargeLevel = 0;
CurrentChargeLevelCharge = 0;
hitboxManager.Reset();
CurrentAttack = null;
}
Expand Down Expand Up @@ -291,6 +301,26 @@ public virtual void SetMoveset(MovesetDefinition moveset)
OnMovesetChanged?.Invoke(controller, oldMoveset);
}

public virtual void SetChargeLevel(int value)
{
int lastChargeLevel = value;
CurrentChargeLevel = value;
OnChargeLevelChanged?.Invoke(controller, lastChargeLevel);
}

public virtual void SetChargeLevelCharge(int value)
{
int oldValue = CurrentChargeLevelCharge;
CurrentChargeLevelCharge = value;
OnChargeLevelChargeChanged?.Invoke(controller, oldValue);
}

public virtual void IncrementChargeLevelCharge()
{
CurrentChargeLevelCharge++;
OnChargeLevelChargeChanged?.Invoke(controller, CurrentChargeLevelCharge-1);
}

public virtual HitReaction Hurt(Vector3 center, Vector3 forward, Vector3 right, HitInfoBase hitInfo)
{
HitReaction hr = new HitReaction();
Expand Down
2 changes: 2 additions & 0 deletions Assets/CAF/Entities/Managers/EntityStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public virtual bool ChangeState(int state, uint stateFrame = 0, bool callOnInter
if (currentStateFrame == 0)
{
currentState.Initialize();
currentStateFrame = 1;
}
currentStateName = currentState.GetName();
OnStatePostChange?.Invoke(controller, oldState, oldStateFrame);
Expand Down Expand Up @@ -96,6 +97,7 @@ public virtual void ChangeState(EntityState state, uint stateFrame = 0, bool cal
if (currentStateFrame == 0)
{
currentState.Initialize();
currentStateFrame = 1;
}
currentStateName = currentState.GetName();
OnStatePostChange?.Invoke(controller, oldState, oldStateFrame);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ MonoBehaviour:
nodes:
- {fileID: 4997239229425092478}
groundAttackCommandNormals: []
groundAttackStartNodes: []
groundAttackStartNodes:
- {fileID: 4997239229425092478}
airAttackCommandNormals: []
airAttackStartNodes: []
--- !u!114 &4997239229425092478
Expand Down Expand Up @@ -59,7 +60,7 @@ MonoBehaviour:
lastNode: {fileID: 0}
executeInputs:
- inputType: 0
buttonID: 0
buttonID: 3
stickDirection: {x: 0, y: 0}
directionDeviation: 0
inputSequence: []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,21 @@ MonoBehaviour:
enemyStepWindows: []
landCancelWindows: []
commandAttackCancelWindows: []
chargeWindows: []
boxGroups:
chargeWindows:
- id: 0
boxGroups:
- id: 1
events: []
references:
version: 1
00000000:
type: {class: ChargeDefinition, ns: CAF.Combat, asm: CAF}
data:
frame: 1
releaseOnCompletion: 1
chargeLevels:
- maxChargeFrames: 30
00000001:
type: {class: BoxGroup, ns: CAF.Combat, asm: CAF}
data:
ID: 0
Expand All @@ -44,8 +52,8 @@ MonoBehaviour:
chargeLevelNeeded: -1
chargeLevelMax: 1
hitboxHitInfo:
id: 1
00000001:
id: 2
00000002:
type: {class: HitInfo, ns: CAF.Combat, asm: CAF}
data:
airOnly: 0
Expand Down
10 changes: 1 addition & 9 deletions Assets/CAF/Samples/TDAction/Characters/Boxer/States/BIdle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ namespace TDAction.Entities.Characters.Boxer
{
public class BIdle : CIdle
{
public override bool CheckInterrupt()
{
EntityManager entityManager = GetEntityController();
if (entityManager.TryAttack())
{
return true;
}
return base.CheckInterrupt();
}

}
}
4 changes: 4 additions & 0 deletions Assets/CAF/Samples/TDAction/Characters/Shared/States/CIdle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public override bool CheckInterrupt()
{
CharacterManager c = GetCharacterController();
CharacterStats stats = (CharacterStats)c.entityDefinition.GetEntityStats();
if (c.TryAttack())
{
return true;
}
if (c.InputManager.GetButton((int)EntityInputs.JUMP).firstPress)
{
c.StateManager.ChangeState((int)CharacterStates.JUMP_SQUAT);
Expand Down
4 changes: 4 additions & 0 deletions Assets/CAF/Samples/TDAction/Characters/Shared/States/CWalk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public override void OnUpdate()
public override bool CheckInterrupt()
{
CharacterManager c = GetCharacterController();
if (c.TryAttack())
{
return true;
}
if (c.InputManager.GetButton((int)EntityInputs.JUMP).firstPress)
{
c.StateManager.ChangeState((int)CharacterStates.JUMP_SQUAT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ protected override void GetInputs()
new InputRecordButton(Input.GetKey(KeyCode.Space)));
recordItem.AddInput((int)EntityInputs.DASH,
new InputRecordButton(Input.GetKey(KeyCode.LeftShift)));
recordItem.AddInput((int)EntityInputs.ATTACK,
new InputRecordButton(Input.GetMouseButton(0)));

InputRecord.Add(recordItem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using TDAction.Combat;
using TDAction.Inputs;
using UnityEngine;

namespace TDAction.Entities.States
Expand All @@ -13,6 +14,8 @@ public override string GetName()
return $"Attack ({GetEntityController().CombatManager.CurrentAttack?.name}).";
}

protected bool charging = true;

public override void Initialize()
{
base.Initialize();
Expand All @@ -23,48 +26,121 @@ public override void Initialize()
GetEntityController().StateManager.ChangeState(currentAttack.stateOverride);
return;
}
charging = true;
}

public override void OnUpdate()
{
EntityManager entityManager = GetEntityController();

AttackDefinition currentAttack =
(TDAction.Combat.AttackDefinition)entityManager.CombatManager.CurrentAttack.attackDefinition;

// Handle lifetime of box groups.
for (int i = 0; i < currentAttack.boxGroups.Count; i++)
{
HandleBoxGroup(i, currentAttack.boxGroups[i]);
}

// Check if we should cancel the attack with something else.
if (CheckCancelWindows(currentAttack))
{
entityManager.CombatManager.Cleanup();
return;
}

CheckInterrupt();
if (CheckInterrupt())
{
return;
}
if (!HandleChargeLevels(entityManager, currentAttack))
{
entityManager.StateManager.IncrementFrame();
}
}

/// <summary>
/// Handles processing the charge levels of the current attack.
/// </summary>
/// <param name="entityManager">The entity itself.</param>
/// <param name="currentAttack">The current attack the entity is doing.</param>
/// <returns>If the frame should be held.</returns>
private bool HandleChargeLevels(EntityManager entityManager, AttackDefinition currentAttack)
{
EntityCombatManager cManager = (EntityCombatManager)entityManager.CombatManager;
if (!charging)
{
return false;
}

if (!entityManager.InputManager.GetButton((int)EntityInputs.ATTACK).isDown)
{
charging = false;
return false;
}

bool result = false;
for(int i = 0; i < currentAttack.chargeWindows.Count; i++)
{
// Not on the correct frame.
if(entityManager.StateManager.CurrentStateFrame != currentAttack.chargeWindows[i].frame)
{
continue;
}

// Still have charge levels to go through.
if(entityManager.CombatManager.CurrentChargeLevel < currentAttack.chargeWindows[i].chargeLevels.Count)
{
cManager.IncrementChargeLevelCharge();
// Charge completed, move on to the next level.
if(cManager.CurrentChargeLevelCharge == currentAttack.chargeWindows[i].chargeLevels[cManager.CurrentChargeLevel].maxChargeFrames)
{
cManager.SetChargeLevel(cManager.CurrentChargeLevel+1);
cManager.SetChargeLevelCharge(0);
}
} else if (currentAttack.chargeWindows[i].releaseOnCompletion)
{
charging = false;
}
result = true;
// Only one charge level can be handled per frame, ignore everything else.
break;
}
return result;
}

/// <summary>
/// Handles the lifetime process of hitboxes and detectboxes.
/// Handles the lifetime process of box groups.
/// </summary>
/// <param name="groupIndex">The group number being processed.</param>
/// <param name="boxGroup">The group being processed.</param>
protected virtual void HandleBoxGroup(int groupIndex, CAF.Combat.BoxGroup boxGroup)
{
EntityManager entityManager = GetEntityController();
// Cleanup the box if it's active frames are over.
if(entityManager.StateManager.CurrentStateFrame == boxGroup.activeFramesEnd + 1)
{
entityManager.CombatManager.hitboxManager.DeactivateHitboxGroup(groupIndex);
}

// Make sure we're in the frame window of the box.
if(entityManager.StateManager.CurrentStateFrame < boxGroup.activeFramesStart
|| entityManager.StateManager.CurrentStateFrame > boxGroup.activeFramesEnd)
{
return;
}

// Check if the charge level requirement was met.
if(boxGroup.chargeLevelNeeded >= 0)
{
int currentChargeLevel = entityManager.CombatManager.CurrentChargeLevel;
if (currentChargeLevel <= boxGroup.chargeLevelNeeded
|| currentChargeLevel > boxGroup.chargeLevelMax)
{
return;
}
}

// Create the box.
switch (boxGroup.hitGroupType)
{
case CAF.Combat.BoxGroupType.HIT:
Expand Down
10 changes: 5 additions & 5 deletions Assets/CAF/Samples/TDAction/Scripts/Inputs/EntityInputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace TDAction.Inputs
{
public enum EntityInputs
{
MOVEMENT,
CAMERA,
JUMP,
ATTACK,
DASH
MOVEMENT = 0,
CAMERA = 1,
JUMP = 2,
ATTACK = 3,
DASH = 4
}
}

0 comments on commit 066b55c

Please sign in to comment.