Skip to content

Commit

Permalink
#9 Fix hit event issue (are called multiple times)
Browse files Browse the repository at this point in the history
TODO: Need optimization
  • Loading branch information
IDstorage committed Sep 2, 2022
1 parent d513184 commit 58f159a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
35 changes: 24 additions & 11 deletions project-kata-unity/Assets/Scripts/Behaviours/CombatEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async void DoLineAttack(AnimationEvent param)

var track = trailData.tracks[param.intParameter];

HashSet<CustomBehaviour> hitList = new HashSet<CustomBehaviour>();
Dictionary<CustomBehaviour, HashSet<Collider>> hitList = new Dictionary<CustomBehaviour, HashSet<Collider>>();

boxCastQueue.Clear();

Expand All @@ -92,26 +92,39 @@ public async void DoLineAttack(AnimationEvent param)

for (int j = 0; j < hits.Length; ++j)
{
if (ReferenceEquals(hits[j], weaponCollider)) continue;
var hit = hits[j];
if (ReferenceEquals(hit, weaponCollider)) continue;

var root = hits[j].GetComponent<RootSelector>().Root;
if (root == null)
var selector = hit.GetComponent<RootSelector>();
if (selector == null) continue;
if (selector.Root == null)
{
Debug.Log("RootSelector: Root is Null");
continue;
}

if (hitList.Contains(root)) continue;
hitList.Contains(root);
if (ReferenceEquals(selector.Root, rootBehaviour)) continue;

EventDispatcher.Instance.Send(new HitEvent()
if (!hitList.ContainsKey(selector.Root))
{
sender = rootBehaviour,
receiver = root,
hitPart = hits[j]
});
hitList.Add(selector.Root, new HashSet<Collider>());
}

if (hitList[selector.Root].Contains(hit)) continue;

hitList[selector.Root].Add(hit);
}
}

foreach (var hits in hitList)
{
EventDispatcher.Instance.Send(new HitEvent()
{
sender = rootBehaviour,
receiver = hits.Key,
hitParts = hits.Value
});
}
}

public void CollectInputEvent(AnimationEvent param)
Expand Down
11 changes: 7 additions & 4 deletions project-kata-unity/Assets/Scripts/Behaviours/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ public void Attack()

public void Hit(HitEvent e)
{
if (StateMachine.CurrentState.ID == StateID.PlayerDefense
&& e.hitPart.CompareTag("Weapon"))
if (StateMachine.CurrentState.ID == StateID.PlayerDefense)
{
Block();
return;
foreach (var part in e.hitParts)
{
if (!part.CompareTag("Weapon")) continue;
Block();
return;
}
}

Debug.Log($"Hit! {e.sender.name} -> {e.receiver.name}");
Expand Down
2 changes: 1 addition & 1 deletion project-kata-unity/Assets/Scripts/Events/HitEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class HitEvent : Anomaly.BaseEvent
{
public Collider hitPart;
public HashSet<Collider> hitParts;
}

public class HitEventStream : Anomaly.EventStream<HitEvent>
Expand Down

0 comments on commit 58f159a

Please sign in to comment.