Skip to content

Commit

Permalink
feat: Functionally of EntityHurtboxManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
christides11 committed Oct 22, 2020
1 parent 36e3dff commit 8d7e273
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 6 deletions.
96 changes: 95 additions & 1 deletion Assets/CAF/Entities/Managers/EntityHurtboxManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CAF.Input;
using CAF.Combat;
using CAF.Input;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -10,9 +11,102 @@ public class EntityHurtboxManager : MonoBehaviour
{
[SerializeField] protected EntityManager manager;

protected StateHurtboxDefinition currentHurtboxDefinition;

protected List<Hurtbox> hurtboxPool = new List<Hurtbox>();
protected Dictionary<int, List<Hurtbox>> hurtboxGroups = new Dictionary<int, List<Hurtbox>>();

public virtual void Tick()
{
CreateHurtboxes(manager.StateManager.CurrentStateFrame);
}

List<int> hurtboxGroupsToDelete = new List<int>();
public virtual void CreateHurtboxes(uint frame)
{
if(currentHurtboxDefinition == null)
{
return;
}

// Create hurtbox groups.
for(int i = 0; i < currentHurtboxDefinition.hurtboxGroups.Count; i++)
{
if (!hurtboxGroups.ContainsKey(i))
{
hurtboxGroups.Add(i, new List<Hurtbox>());
}
// Create Hurtboxes.
for(int w = 0; w < currentHurtboxDefinition.hurtboxGroups[i].boxes.Count; w++)
{
Hurtbox hurtbox;
// Group doesn't already have a hurtbox, create one.
if(hurtboxGroups[i].Count < w)
{
hurtbox = CreateHurtbox();
hurtboxGroups[i].Add(hurtbox);
}
else
{
// Group has a hurtbox here already.
hurtbox = hurtboxGroups[i][w];
}
hurtbox.gameObject.SetActive(true);

// Set the hurtbox's position/rotation/etc.
SetHurtboxInfo(i, w);
}
// Cleanup stray hurtboxes.
for(int s = currentHurtboxDefinition.hurtboxGroups[i].boxes.Count; s < hurtboxGroups[i].Count; s++)
{
DestroyHurtbox(hurtboxGroups[i][s]);
hurtboxGroups[i].RemoveAt(s);
}
}

// Cleanup stray hurtbox groups.
foreach(int k in hurtboxGroups.Keys)
{
if(k >= currentHurtboxDefinition.hurtboxGroups.Count)
{
hurtboxGroupsToDelete.Add(k);
CleanupHurtboxGroup(k);
}
}
for(int h = 0; h < hurtboxGroupsToDelete.Count; h++)
{
hurtboxGroups.Remove(h);
}
hurtboxGroupsToDelete.Clear();
}

private void CleanupHurtboxGroup(int groupID)
{
for(int i = 0; i < hurtboxGroups[groupID].Count; i++)
{
DestroyHurtbox(hurtboxGroups[groupID][i]);
}
}

protected virtual void SetHurtboxInfo(int groupID, int hurtboxIndex)
{
throw new NotImplementedException();
}

protected virtual Hurtbox CreateHurtbox()
{
throw new NotImplementedException();
}

protected virtual void DestroyHurtbox(Hurtbox hurtbox)
{
hurtboxPool.Add(hurtbox);
hurtbox.gameObject.SetActive(false);
}

public virtual void SetHurtboxDefinition(StateHurtboxDefinition stateHurtboxDefinition)
{
currentHurtboxDefinition = stateHurtboxDefinition;
}
}
}
22 changes: 18 additions & 4 deletions Assets/CAF/Samples/TDAction/Characters/Boxer/Boxer.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ GameObject:
- component: {fileID: 1031207210466028850}
- component: {fileID: 6300165297845272074}
- component: {fileID: 1715174515004957371}
- component: {fileID: -2960863564831657620}
m_Layer: 8
m_Name: Boxer
m_TagString: Untagged
Expand Down Expand Up @@ -353,10 +354,10 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
manager: {fileID: 8147816337253296054}
forceMovement: {x: 0, y: 0, z: 0}
forceGravity: {x: 0, y: 0, z: 0}
forceDamage: {x: 0, y: 0, z: 0}
forcePushbox: {x: 0, y: 0, z: 0}
forceMovement: {x: 0, y: 0}
forceGravity: {x: 0, y: 0}
forceDamage: {x: 0, y: 0}
forcePushbox: {x: 0, y: 0}
--- !u!114 &1031207210466028850
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -399,6 +400,19 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
health: 0
--- !u!114 &-2960863564831657620
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6667267786158460212}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 204e422f772d87d4f8b23557a37b17b0, type: 3}
m_Name:
m_EditorClassIdentifier:
manager: {fileID: 8147816337253296054}
--- !u!1 &8500522958957405919
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using CAF.Combat;
using System.Collections;
using System.Collections.Generic;
using TDAction.Managers;
using UnityEngine;

namespace TDAction.Entities
{
public class EntityHurtboxManager : CAF.Entities.EntityHurtboxManager
{
protected override void SetHurtboxInfo(int groupID, int hurtboxIndex)
{

}

protected override Hurtbox CreateHurtbox()
{
Hurtbox hurtbox;
// Hurtbox in the pool.
if(hurtboxPool.Count > 0)
{
hurtbox = hurtboxPool[0];
hurtboxPool.RemoveAt(0);
}
else
{
hurtbox = GameObject.Instantiate(GameManager.instance.hurtboxPrefab, gameObject.transform, false);
}
hurtbox.gameObject.SetActive(false);
return hurtbox;
}
}
}

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

4 changes: 3 additions & 1 deletion Assets/CAF/Samples/TDAction/Scripts/Managers/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using CAF.Combat;
using UnityEngine;

namespace TDAction.Managers
{
Expand All @@ -16,6 +17,7 @@ public class GameManager : MonoBehaviour
[SerializeField] private GameHandler gameHandler = null;
[SerializeField] private TDAction.Entities.EntityManager playerEntity;
[SerializeField] private Vector3 playerSpawnPosition;
public Hurtbox hurtboxPrefab;

private void Awake()
{
Expand Down
1 change: 1 addition & 0 deletions TDAction_CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Compile Include="Assets\CAF\Samples\TDAction\Scripts\Entities\HealthManager.cs" />
<Compile Include="Assets\CAF\Samples\TDAction\Scripts\Entities\Managers\EntityCombatManager.cs" />
<Compile Include="Assets\CAF\Samples\TDAction\Scripts\Entities\Managers\EntityHitboxManager.cs" />
<Compile Include="Assets\CAF\Samples\TDAction\Scripts\Entities\Managers\EntityHurtboxManager.cs" />
<Compile Include="Assets\CAF\Samples\TDAction\Scripts\Entities\Managers\EntityInputManager.cs" />
<Compile Include="Assets\CAF\Samples\TDAction\Scripts\Entities\Managers\EntityManager.cs" />
<Compile Include="Assets\CAF\Samples\TDAction\Scripts\Entities\Managers\EntityPhysicsManager.cs" />
Expand Down

0 comments on commit 8d7e273

Please sign in to comment.