diff options
Diffstat (limited to 'Thronefall_v1.0/Decompile/AutoAttack.cs')
-rw-r--r-- | Thronefall_v1.0/Decompile/AutoAttack.cs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/Thronefall_v1.0/Decompile/AutoAttack.cs b/Thronefall_v1.0/Decompile/AutoAttack.cs new file mode 100644 index 0000000..71d1aec --- /dev/null +++ b/Thronefall_v1.0/Decompile/AutoAttack.cs @@ -0,0 +1,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; + } +} |