-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeaponWielder.cs
More file actions
111 lines (89 loc) · 3.52 KB
/
Copy pathWeaponWielder.cs
File metadata and controls
111 lines (89 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using FK.Common;
using FK.Common.Extensions;
using FK.Tulip.Combat.Data;
using FK.Tulip.Data.Items;
using FK.Tulip.Gameplay;
using UnityEngine;
using Vertx.Attributes;
namespace FK.Tulip.Combat
{
public sealed class WeaponWielder : MonoBehaviour, IWeapon
{
[SerializeField, Required] private WeaponWielderConfigAsset config;
[Header("References")]
[SerializeField, Required] private Health health;
[SerializeField, Required] private ItemWielder itemWielder;
#if UNITY_EDITOR
[Header("Debug")]
[SerializeField, ReadOnlyField] private Collider2D[] hitColliders;
#endif
public WeaponAsset Asset => weaponAsset;
public Health Owner => health;
private ProjectileManager projectileManager;
private WeaponAsset weaponAsset;
private RaycastHit2D[] hitResults;
private readonly List<Health> damagedTargets = new();
private void Awake()
{
hitResults = new RaycastHit2D[config.MaxHitsPerRaycast];
projectileManager = FindAnyObjectByType<ProjectileManager>();
}
private void OnEnable()
{
itemWielder.OnSwingPerform += Attack;
itemWielder.OnThrowPerform += Throw;
}
private void OnDisable()
{
itemWielder.OnSwingPerform -= Attack;
itemWielder.OnThrowPerform -= Throw;
}
private void Throw(ItemAsset item, Vector3 aimPoint)
{
if (item.Is(out weaponAsset) && weaponAsset.IsThrowable)
projectileManager.Fire(weaponAsset, health, aimPoint);
}
private void Attack(ItemAsset item, Vector3 targetPoint)
{
if (item.IsNot(out weaponAsset))
return;
foreach (Hurtbox target in GetTargets(transform.position, targetPoint))
{
if (target.GetHit(this))
damagedTargets.Add(target.Owner);
}
}
private IEnumerable<Hurtbox> GetTargets(Vector2 origin, Vector2 aimPoint)
{
Vector2 direction = (aimPoint - origin).normalized;
int maxHits = weaponAsset.IsMultiTarget ? config.MaxHitsPerRaycast : 1;
int piercedCount = 0;
int hitCount = Physics2D.Raycast(origin, direction, config.HitContactFilter, hitResults, weaponAsset.Range);
#if UNITY_EDITOR
hitColliders = hitResults.Select(hit => hit.collider).ToArray();
Array.Resize(ref hitColliders, hitCount);
#endif
Debug.DrawRay(origin, direction * weaponAsset.Range, Color.green, 1f);
damagedTargets.Clear();
for (int i = 0; i < hitCount; i++)
{
if (piercedCount >= maxHits)
break; // reached max piercing
RaycastHit2D hit = hitResults[i];
if (!hit) continue;
if (config.IsObstacle(hit.transform))
break; // we hit an obstacle (hit results are pre-sorted by distance)
if (!hit.collider.TryGetComponent(out Hurtbox hurtbox) || !hurtbox.enabled)
continue; // skip colliders with no Hurtbox
if (damagedTargets.Contains(hurtbox.Owner))
continue; // we've hit this target already
piercedCount++;
yield return hurtbox;
}
// Log.Info($"{Owner.Entity.name} detected {piercedCount} target HurtBoxes.");
}
}
}