Skip to content

Commit

Permalink
refactor: Split Hitbox into 2D and 3D.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Hitbox is now separated into Hitbox3D and Hitbox2D, with Hitbox being the base class of the two.
  • Loading branch information
christides11 committed Aug 17, 2020
1 parent a22502b commit 23705e6
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 99 deletions.
91 changes: 4 additions & 87 deletions Assets/CAF/Combat/Boxes/Hitbox.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,27 @@
using CAF.Simulation;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace CAF.Combat
{
public class Hitbox : SimObject
public abstract class Hitbox : SimObject
{
public delegate void HurtAction(GameObject hurtableHit, HitInfoBase hitInfo);
public event HurtAction OnHurt;
public virtual event HurtAction OnHurt;

protected GameObject owner;
protected Transform directionOwner;
protected bool activated;
protected Collider coll;
public HitInfoBase hitInfo;

public List<IHurtable> ignoreList = null;
public List<GameObject> hitHurtables = new List<GameObject>();

public virtual void Initialize(GameObject owner, Transform directionOwner, BoxShapes shape,
HitInfoBase hitInfo, BoxDefinition boxDefinition, List<IHurtable> ignoreList = null)
{
this.owner = owner;
this.directionOwner = directionOwner;
this.ignoreList = ignoreList;
this.hitInfo = hitInfo;

switch (shape)
{
case BoxShapes.Rectangle:
CreateRectangle(boxDefinition.size);
break;
case BoxShapes.Circle:
CreateSphere(boxDefinition.radius);
break;
case BoxShapes.Capsule:
CreateCapsule(boxDefinition.radius, boxDefinition.height);
break;
}
}
public abstract void Initialize(GameObject owner, Transform directionOwner, BoxShapes shape,
HitInfoBase hitInfo, BoxDefinition boxDefinition, List<IHurtable> ignoreList = null);

public virtual void Activate()
{
coll.enabled = true;
activated = true;
}

Expand All @@ -52,7 +30,6 @@ public virtual void Activate()
/// </summary>
public virtual void Deactivate()
{
coll.enabled = false;
activated = false;
}

Expand All @@ -67,41 +44,9 @@ public virtual void ReActivate(List<IHurtable> ignoreList = null)
Activate();
}

/// <summary>
/// Initializes the hitbox as a rectangle type hitbox.
/// </summary>
/// <param name="size">The size of the hitbox.</param>
/// <param name="rotation">The rotation of the hitbox.</param>
protected virtual void CreateRectangle(Vector3 size)
{
BoxCollider bc = gameObject.AddComponent<BoxCollider>();
bc.isTrigger = true;
coll = bc;
bc.size = size;
}

protected virtual void CreateSphere(float radius)
{
SphereCollider sc = gameObject.AddComponent<SphereCollider>();
sc.isTrigger = true;
coll = sc;
sc.radius = radius;
}

protected virtual void CreateCapsule(float radius, float height)
{
CapsuleCollider cc = gameObject.AddComponent<CapsuleCollider>();
cc.isTrigger = true;
coll = cc;
cc.radius = radius;
cc.height = height;
cc.direction = 2;
}

public virtual void CheckHits()
{
CheckHurtables();

hitHurtables.Clear();
}

Expand Down Expand Up @@ -133,33 +78,5 @@ protected virtual void CheckHurtables()
}
}
}

/// <summary>
/// Called every tick for whatever object's are within this hitbox.
/// Gets all the hitboxes and checks if they should be hurt next LateUpdate.
/// </summary>
/// <param name="other">The collider in our hitbox.</param>
protected virtual void OnTriggerStay(Collider other)
{
if (!activated)
{
return;
}

Hurtbox otherHurtbox = null;
if (!other.TryGetComponent<Hurtbox>(out otherHurtbox))
{
return;
}

if (otherHurtbox != null)
{
if (!hitHurtables.Contains(otherHurtbox.Owner)
&& (ignoreList == null || !ignoreList.Contains(otherHurtbox.Hurtable)))
{
hitHurtables.Add(otherHurtbox.Owner);
}
}
}
}
}
108 changes: 108 additions & 0 deletions Assets/CAF/Combat/Boxes/Hitbox2D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using CAF.Simulation;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CAF.Combat
{
public class Hitbox2D : Hitbox
{
public override event Hitbox.HurtAction OnHurt;

protected Collider2D coll;

public override void Initialize(GameObject owner, Transform directionOwner, BoxShapes shape,
HitInfoBase hitInfo, BoxDefinition boxDefinition, List<IHurtable> ignoreList = null)
{
this.owner = owner;
this.directionOwner = directionOwner;
this.ignoreList = ignoreList;
this.hitInfo = hitInfo;

switch (shape)
{
case BoxShapes.Rectangle:
CreateRectangle(boxDefinition.size);
break;
case BoxShapes.Circle:
CreateCircle(boxDefinition.radius);
break;
case BoxShapes.Capsule:
CreateCapsule(boxDefinition.radius, boxDefinition.height);
break;
}
}

protected virtual void CreateRectangle(Vector2 size)
{
BoxCollider2D bc = gameObject.AddComponent<BoxCollider2D>();
bc.isTrigger = true;
coll = bc;
bc.size = size;
}

protected virtual void CreateCircle(float radius)
{
CircleCollider2D cc = gameObject.AddComponent<CircleCollider2D>();
cc.isTrigger = true;
coll = cc;
cc.radius = radius;
}

protected virtual void CreateCapsule(float radius, float height)
{
CapsuleCollider2D cc = gameObject.AddComponent<CapsuleCollider2D>();
cc.isTrigger = true;
coll = cc;
cc.size = new Vector2(radius, height);
}

public override void Activate()
{
base.Activate();
coll.enabled = true;
}

public override void Deactivate()
{
base.Deactivate();
coll.enabled = false;
}

public override void ReActivate(List<IHurtable> ignoreList = null)
{
this.ignoreList = ignoreList;
hitHurtables.Clear();
Activate();
}

protected void OnTriggerStay2D(Collider2D other)
{
CheckForHurtboxes(other);
}

protected virtual void CheckForHurtboxes(Collider2D other)
{
if (!activated)
{
return;
}

Hurtbox otherHurtbox = null;
if (!other.TryGetComponent<Hurtbox>(out otherHurtbox))
{
return;
}

if (otherHurtbox != null)
{
if (!hitHurtables.Contains(otherHurtbox.Owner)
&& (ignoreList == null || !ignoreList.Contains(otherHurtbox.Hurtable)))
{
hitHurtables.Add(otherHurtbox.Owner);
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/CAF/Combat/Boxes/Hitbox2D.cs.meta

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

112 changes: 112 additions & 0 deletions Assets/CAF/Combat/Boxes/Hitbox3D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using CAF.Simulation;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace CAF.Combat
{
public class Hitbox3D : Hitbox
{
public override event Hitbox.HurtAction OnHurt;

protected Collider coll;

public override void Initialize(GameObject owner, Transform directionOwner, BoxShapes shape,
HitInfoBase hitInfo, BoxDefinition boxDefinition, List<IHurtable> ignoreList = null)
{
this.owner = owner;
this.directionOwner = directionOwner;
this.ignoreList = ignoreList;
this.hitInfo = hitInfo;

switch (shape)
{
case BoxShapes.Rectangle:
CreateRectangle(boxDefinition.size);
break;
case BoxShapes.Circle:
CreateSphere(boxDefinition.radius);
break;
case BoxShapes.Capsule:
CreateCapsule(boxDefinition.radius, boxDefinition.height);
break;
}
}

/// <summary>
/// Initializes the hitbox as a rectangle type hitbox.
/// </summary>
/// <param name="size">The size of the hitbox.</param>
/// <param name="rotation">The rotation of the hitbox.</param>
protected virtual void CreateRectangle(Vector3 size)
{
BoxCollider bc = gameObject.AddComponent<BoxCollider>();
bc.isTrigger = true;
coll = bc;
bc.size = size;
}

protected virtual void CreateSphere(float radius)
{
SphereCollider sc = gameObject.AddComponent<SphereCollider>();
sc.isTrigger = true;
coll = sc;
sc.radius = radius;
}

protected virtual void CreateCapsule(float radius, float height)
{
CapsuleCollider cc = gameObject.AddComponent<CapsuleCollider>();
cc.isTrigger = true;
coll = cc;
cc.radius = radius;
cc.height = height;
cc.direction = 2;
}

public override void Activate()
{
base.Activate();
coll.enabled = true;
}

public override void Deactivate()
{
base.Deactivate();
coll.enabled = false;
}

/// <summary>
/// Called every tick for whatever object's are within this hitbox.
/// Gets all the hitboxes and checks if they should be hurt next LateUpdate.
/// </summary>
/// <param name="other">The collider in our hitbox.</param>
protected virtual void OnTriggerStay(Collider other)
{
CheckForHurtboxes(other);
}

protected virtual void CheckForHurtboxes(Collider other)
{
if (!activated)
{
return;
}

Hurtbox otherHurtbox = null;
if (!other.TryGetComponent<Hurtbox>(out otherHurtbox))
{
return;
}

if (otherHurtbox != null)
{
if (!hitHurtables.Contains(otherHurtbox.Owner)
&& (ignoreList == null || !ignoreList.Contains(otherHurtbox.Hurtable)))
{
hitHurtables.Add(otherHurtbox.Owner);
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/CAF/Combat/Boxes/Hitbox3D.cs.meta

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

1 change: 0 additions & 1 deletion Assets/CAF/Entities/Managers/EntityCombatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ public virtual void IncrementChargeLevelCharge()

public virtual HitReaction Hurt(Vector3 center, Vector3 forward, Vector3 right, HitInfoBase hitInfo)
{
Debug.Log("Hit.");
HitReaction hr = new HitReaction();
hr.reactionType = HitReactionType.Hit;
OnHit?.Invoke(null, controller, hitInfo);
Expand Down
Loading

0 comments on commit 23705e6

Please sign in to comment.