Skip to content

Commit

Permalink
feat: Editor for StateHurtboxDefinition.
Browse files Browse the repository at this point in the history
StateHurtboxDefinition can now properly add custom hurtboxes and box definitions in the inspector.
  • Loading branch information
christides11 committed Oct 31, 2020
1 parent 4479d7e commit 9d26e39
Show file tree
Hide file tree
Showing 15 changed files with 4,787 additions and 2,438 deletions.
67 changes: 66 additions & 1 deletion Assets/CAF/Combat/HurtboxGroup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CAF.Combat
Expand All @@ -22,5 +24,68 @@ public HurtboxGroup(HurtboxGroup other)
{

}

[NonSerialized] protected Dictionary<string, Type> boxDefinitionTypes = new Dictionary<string, Type>();
[NonSerialized] protected bool boxesDropdown = false;
public virtual void DrawInspector(float indentLevel)
{
#if UNITY_EDITOR
ID = EditorGUILayout.IntField("ID", ID);
activeFramesStart = EditorGUILayout.IntField("Active Frames (Start)", activeFramesStart);
activeFramesEnd = EditorGUILayout.IntField("Active Frames (End)", activeFramesEnd);
attachToEntity = EditorGUILayout.Toggle("Attached to Entity", attachToEntity);
attachTo = EditorGUILayout.TextField("Attached To", attachTo);
GUILayout.BeginHorizontal();
GUILayout.Space(EditorGUI.indentLevel * indentLevel);
if (GUILayout.Button("Add Box"))
{
GenericMenu menu = new GenericMenu();

boxDefinitionTypes = new Dictionary<string, Type>();
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var givenType in a.GetTypes())
{
if (givenType.IsSubclassOf(typeof(BoxDefinitionBase)))
{
boxDefinitionTypes.Add(givenType.FullName, givenType);
}
}
}

foreach (string hType in boxDefinitionTypes.Keys)
{
string destination = hType.Replace('.', '/');
menu.AddItem(new GUIContent(destination), true, OnBoxDefinitionTypeSelected, hType);
}
menu.ShowAsContext();
}
GUILayout.EndHorizontal();

boxesDropdown = EditorGUILayout.Foldout(boxesDropdown, $"Boxes ({boxes.Count})");
if (boxesDropdown)
{
for (int i = 0; i < boxes.Count; i++)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField($"Box {i}", GUILayout.MaxWidth(100));
if (GUILayout.Button("X", GUILayout.Width(40)))
{
boxes.RemoveAt(i);
return;
}
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel++;
boxes[i].DrawInspector();
EditorGUI.indentLevel--;
}
}
#endif
}

protected virtual void OnBoxDefinitionTypeSelected(object t)
{
boxes.Add((BoxDefinitionBase)Activator.CreateInstance(boxDefinitionTypes[(string)t]));
}
}
}
2 changes: 1 addition & 1 deletion Assets/CAF/Combat/StateHurtboxDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace CAF.Combat
[CreateAssetMenu(fileName = "StateHurtbox", menuName = "CAF/StateHurtboxDefinition")]
public class StateHurtboxDefinition : ScriptableObject
{
public List<HurtboxGroup> hurtboxGroups = new List<HurtboxGroup>();
[SerializeReference] public List<HurtboxGroup> hurtboxGroups = new List<HurtboxGroup>();
}
}
60 changes: 60 additions & 0 deletions Assets/CAF/Editor/Combat/StateHurtboxDefinitionEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CAF.Combat
{
[CustomEditor(typeof(StateHurtboxDefinition), true)]
public class StateHurtboxDefinitionEditor : Editor
{
StateHurtboxDefinition t;

protected Dictionary<string, Type> hurtboxGroupTypes = new Dictionary<string, Type>();

private void OnEnable()
{
hurtboxGroupTypes.Clear();
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var givenType in a.GetTypes())
{
if (givenType.IsSubclassOf(typeof(HurtboxGroup)) || givenType == typeof(HurtboxGroup))
{
hurtboxGroupTypes.Add(givenType.FullName, givenType);
}
}
}
}

public override void OnInspectorGUI()
{
t = target as StateHurtboxDefinition;

if (GUILayout.Button("Add Hurtbox Group"))
{
GenericMenu menu = new GenericMenu();

foreach (string hType in hurtboxGroupTypes.Keys)
{
string destination = hType.Replace('.', '/');
menu.AddItem(new GUIContent(destination), true, OnHurtboxGroupTypeSelected, hType);
}
menu.ShowAsContext();
}

for(int i = 0; i < t.hurtboxGroups.Count; i++)
{
EditorGUILayout.LabelField($"Hurtbox Group {i}");
EditorGUI.indentLevel++;
t.hurtboxGroups[i].DrawInspector(15);
EditorGUI.indentLevel--;
}
}

private void OnHurtboxGroupTypeSelected(object type)
{
t.hurtboxGroups.Add((HurtboxGroup)Activator.CreateInstance(hurtboxGroupTypes[(string)type]));
}
}
}
11 changes: 11 additions & 0 deletions Assets/CAF/Editor/Combat/StateHurtboxDefinitionEditor.cs.meta

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

33 changes: 24 additions & 9 deletions Assets/CAF/Entities/Managers/EntityHurtboxManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,29 @@ public virtual void CreateHurtboxes(uint frame)
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.

// STRAY HURTBOX CLEANUP //
for (int s = currentHurtboxDefinition.hurtboxGroups[i].boxes.Count; s < hurtboxGroups[i].Count; s++)
{
DestroyHurtbox(hurtboxGroups[i][s]);
hurtboxGroups[i].RemoveAt(s);
}

// CHECK CLEANUP WINDOW //
if (manager.StateManager.CurrentStateFrame > currentHurtboxDefinition.hurtboxGroups[i].activeFramesEnd
|| manager.StateManager.CurrentStateFrame < currentHurtboxDefinition.hurtboxGroups[i].activeFramesStart)
{
CleanupHurtboxGroup(i);
continue;
}

// CREATE HURTBOX GROUPS //
for(int w = 0; w < currentHurtboxDefinition.hurtboxGroups[i].boxes.Count; w++)
{
Hurtbox hurtbox;
Expand All @@ -56,15 +71,9 @@ public virtual void CreateHurtboxes(uint frame)
// 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.
// CLEANUP EXTRA HURTBOXES //
foreach(int k in hurtboxGroups.Keys)
{
if(k >= currentHurtboxDefinition.hurtboxGroups.Count)
Expand All @@ -73,6 +82,7 @@ public virtual void CreateHurtboxes(uint frame)
CleanupHurtboxGroup(k);
}
}

for(int h = 0; h < hurtboxGroupsToDelete.Count; h++)
{
hurtboxGroups.Remove(h);
Expand All @@ -86,6 +96,7 @@ private void CleanupHurtboxGroup(int groupID)
{
DestroyHurtbox(hurtboxGroups[groupID][i]);
}
hurtboxGroups[groupID].Clear();
}

protected virtual void SetHurtboxInfo(int groupID, int hurtboxIndex)
Expand All @@ -98,6 +109,10 @@ protected virtual Hurtbox CreateHurtbox()
throw new NotImplementedException();
}

/// <summary>
/// Deactivates a hurtbox and returns it to the pool.
/// </summary>
/// <param name="hurtbox">The hurtbox to deactivate.</param>
protected virtual void DestroyHurtbox(Hurtbox hurtbox)
{
hurtboxPool.Add(hurtbox);
Expand Down
72 changes: 0 additions & 72 deletions Assets/CAF/Samples/TDAction/Characters/Boxer/Boxer.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -81,77 +81,6 @@ SpriteRenderer:
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!1 &4181345096658579073
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 5849832185125281421}
- component: {fileID: 5972442731912996721}
- component: {fileID: 4156745514149257954}
m_Layer: 11
m_Name: Hurtbox
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &5849832185125281421
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4181345096658579073}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 6667267786158460213}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!61 &5972442731912996721
BoxCollider2D:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4181345096658579073}
m_Enabled: 1
m_Density: 1
m_Material: {fileID: 0}
m_IsTrigger: 1
m_UsedByEffector: 0
m_UsedByComposite: 0
m_Offset: {x: 0, y: 0}
m_SpriteTilingProperty:
border: {x: 0, y: 0, z: 0, w: 0}
pivot: {x: 0, y: 0}
oldSize: {x: 0, y: 0}
newSize: {x: 0, y: 0}
adaptiveTilingThreshold: 0
drawMode: 0
adaptiveTiling: 0
m_AutoTiling: 0
serializedVersion: 2
m_Size: {x: 1, y: 2}
m_EdgeRadius: 0
--- !u!114 &4156745514149257954
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4181345096658579073}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1eb180182734aa940bb02293681b97db, type: 3}
m_Name:
m_EditorClassIdentifier:
owner: {fileID: 6667267786158460212}
--- !u!1 &6667267786158460212
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -190,7 +119,6 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 3889041244988254788}
- {fileID: 5849832185125281421}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@ MonoBehaviour:
m_Name: Hurtboxes_Idle
m_EditorClassIdentifier:
hurtboxGroups:
- ID: 0
activeFramesStart: 1
activeFramesEnd: 60
boxes:
- id: 0
- id: 0
- id: 0
- id: 0
attachToEntity: 1
attachTo:
- id: 0
- id: 1
references:
version: 1
00000000:
type: {class: , ns: , asm: }
type: {class: HurtboxGroup, ns: CAF.Combat, asm: CAF}
data:
ID: 0
activeFramesStart: 1
activeFramesEnd: 1
boxes: []
attachToEntity: 1
attachTo:
00000001:
type: {class: HurtboxGroup, ns: CAF.Combat, asm: CAF}
data:
ID: 0
activeFramesStart: 1
activeFramesEnd: 1
boxes: []
attachToEntity: 1
attachTo:
Loading

0 comments on commit 9d26e39

Please sign in to comment.