blob: 71d1aec9cfb63f3588dd11165f1b8c26d4eb7c4a (
plain)
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
|
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class AutoAttack : MonoBehaviour
{
public float cooldownDuration = 1f;
public float cooldownAfterSpawn = -1f;
[Range(0f, 1f)]
public float cooldownRandomization;
[Tooltip("How often this script checks if an attack is possible once the cooldown is down")]
public float recheckTargetInterval = 0.5f;
public List<TargetPriority> targetPriorities = new List<TargetPriority>();
public Weapon weapon;
public float spawnAttackHeight = 0.5f;
private TaggedObject taggedObject;
private float cooldown = -1f;
private bool onCooldown;
[Tooltip("While the getDisabledBy auto attack script is on cooldown, this auto attack script here can't attack.")]
public AutoAttack getsDisaledBy;
private float damageMultiplyer = 1f;
[HideInInspector]
public UnityEvent onAttackTriggered = new UnityEvent();
private Vector3 lastTargetPosition;
public float DamageMultiplyer
{
get
{
return damageMultiplyer;
}
set
{
damageMultiplyer = value;
}
}
public Vector3 LastTargetPosition => lastTargetPosition;
public void ReduceCooldownBy(float _reduceBy)
{
cooldown -= _reduceBy;
}
private void Start()
{
cooldown = cooldownAfterSpawn;
taggedObject = GetComponent<TaggedObject>();
}
private void Update()
{
bool flag = true;
if (getsDisaledBy != null && getsDisaledBy.onCooldown)
{
flag = false;
cooldown += Time.deltaTime;
}
cooldown -= Time.deltaTime;
if (cooldown <= 0f && flag)
{
TaggedObject taggedObject = FindAutoAttackTarget();
if (taggedObject == null)
{
cooldown += recheckTargetInterval;
onCooldown = false;
return;
}
cooldown += cooldownDuration * (1f + (1f - 2f * Random.value) * cooldownRandomization);
weapon.Attack(base.transform.position + spawnAttackHeight * Vector3.up, taggedObject.Hp, Vector3.zero, this.taggedObject, damageMultiplyer);
lastTargetPosition = taggedObject.transform.position;
onAttackTriggered.Invoke();
onCooldown = true;
}
}
public virtual TaggedObject FindAutoAttackTarget()
{
for (int i = 0; i < targetPriorities.Count; i++)
{
TaggedObject taggedObject = targetPriorities[i].FindClosestTaggedObject(base.transform.position);
if (taggedObject != null)
{
return taggedObject;
}
}
return null;
}
}
|