-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMeleeWeaponLightning.cs
88 lines (69 loc) · 2.99 KB
/
MeleeWeaponLightning.cs
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
using UnityEngine;
using Landfall.TABS;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
namespace HiddenUnits
{
public class MeleeWeaponLightning : CollisionWeaponEffect
{
private void Start()
{
OwnUnit = GetComponent<Weapon>().connectedData.unit;
}
public override void DoEffect(Transform hitTransform, Collision collision)
{
if (!hitTransform.root.GetComponent<Unit>() || hitTransform.root.GetComponent<Unit>().Team == OwnUnit.Team || hitTransform.root.GetComponent<Unit>().data.Dead)
{
return;
}
OldTarget = hitTransform.root.GetComponent<Unit>();
HitList.Add(OldTarget);
OldTarget.data.healthHandler.TakeDamage(damage, Vector3.zero);
SetTarget(transform.position);
StartCoroutine(DoLightning());
}
public IEnumerator DoLightning()
{
for (var i = 0; i < chainCount; i++)
{
for (var j = 0; j < consecutiveChains; j++)
{
if (Target && Target.data && Target.data.healthHandler && lineObject && OldTarget)
{
var line = Instantiate(lineObject, OldTarget.transform, true);
line.transform.FindChildRecursive("T1").position = OldTarget.data.mainRig.position;
line.transform.FindChildRecursive("T2").position = Target.data.mainRig.position;
Target.data.healthHandler.TakeDamage(damage, Vector3.zero);
HitList.Add(Target);
OldTarget = Target;
}
if (OldTarget) SetTarget(OldTarget.data.mainRig.position);
}
yield return new WaitForSeconds(0.1f);
}
HitList.Clear();
}
public void SetTarget(Vector3 source)
{
Target = null;
var hits = Physics.SphereCastAll(transform.position, maxTargetRange, Vector3.up, 0.1f, LayerMask.GetMask(new string[] { "MainRig" }));
var foundUnits = hits
.Select(hit => hit.transform.root.GetComponent<Unit>())
.Where(x => x && !x.data.Dead && x.Team != OwnUnit.Team && !HitList.Contains(x))
.OrderBy(x => (x.data.mainRig.transform.position - source).magnitude)
.Distinct()
.ToArray();
if (foundUnits.Length > 0) Target = foundUnits[0];
}
private Unit Target;
private Unit OldTarget;
private Unit OwnUnit;
private List<Unit> HitList = new List<Unit>();
public float maxTargetRange = 6f;
public int chainCount = 20;
public float damage = 1000f;
public GameObject lineObject;
public int consecutiveChains = 1;
}
}