-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Split Hitbox into 2D and 3D.
BREAKING CHANGE: Hitbox is now separated into Hitbox3D and Hitbox2D, with Hitbox being the base class of the two.
- Loading branch information
1 parent
a22502b
commit 23705e6
Showing
11 changed files
with
277 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.