2019-08-04 17:35:24 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Saltosion.OneWeapon.AI.Behaviours {
|
|
|
|
|
public class MeleeAttackFollowed : AIBehaviour {
|
|
|
|
|
public Follow Follow;
|
|
|
|
|
[Tooltip("This should be at 0,0,0 locally by default.")]
|
|
|
|
|
public Transform AnimatableTransform;
|
|
|
|
|
public float MeleeRange;
|
|
|
|
|
public float AttackAnimationLength;
|
|
|
|
|
public float ReleaseAnimationLength;
|
|
|
|
|
public float CooldownLength;
|
|
|
|
|
|
|
|
|
|
private bool AttackHit;
|
|
|
|
|
private float AnimationProgress;
|
|
|
|
|
private float CooldownProgress;
|
|
|
|
|
private Vector3 LocalOrigin;
|
|
|
|
|
|
|
|
|
|
private void Start() {
|
|
|
|
|
if (AnimatableTransform.localPosition.magnitude != 0) {
|
|
|
|
|
Debug.LogWarning($"{name} has an animated melee attack, and AnimatableTransform is not at local origin!");
|
|
|
|
|
}
|
|
|
|
|
LocalOrigin = AnimatableTransform.localPosition;
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Reset() {
|
|
|
|
|
AttackHit = false;
|
|
|
|
|
AnimationProgress = 0.0f;
|
|
|
|
|
CooldownProgress = 1.0f;
|
|
|
|
|
AnimatableTransform.localPosition = LocalOrigin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update() {
|
|
|
|
|
if (CooldownProgress > 0) {
|
|
|
|
|
CurrentStatus = "Cooldown";
|
|
|
|
|
CooldownProgress -= Time.deltaTime / CooldownLength;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanBehave(Enemy subject) {
|
2019-08-04 20:09:00 +02:00
|
|
|
|
bool CanBehave = Follow.Target != null && (Follow.Target.position - subject.transform.position).magnitude <= MeleeRange;
|
|
|
|
|
if (!CanBehave) {
|
|
|
|
|
subject.Attacking = false;
|
|
|
|
|
}
|
|
|
|
|
return CanBehave;
|
2019-08-04 17:35:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Execute(Enemy subject) {
|
|
|
|
|
if (CooldownProgress > 0) {
|
2019-08-04 20:09:00 +02:00
|
|
|
|
subject.Attacking = false;
|
2019-08-04 17:35:24 +02:00
|
|
|
|
return false;
|
2019-08-04 20:09:00 +02:00
|
|
|
|
} else {
|
|
|
|
|
subject.Attacking = true;
|
2019-08-04 17:35:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector2 Root = AnimatableTransform.parent.position + LocalOrigin;
|
|
|
|
|
Vector2 Target = Follow.Target.transform.position;
|
|
|
|
|
if (!AttackHit) {
|
|
|
|
|
CurrentStatus = "Hit";
|
|
|
|
|
AnimationProgress += Time.deltaTime / AttackAnimationLength;
|
|
|
|
|
AnimatableTransform.position = Vector2.Lerp(Root, Target, AnimationProgress);
|
|
|
|
|
if (AnimationProgress >= 1.0f) {
|
|
|
|
|
// Attack hit, deal damage and start return animation
|
2019-08-04 20:38:57 +02:00
|
|
|
|
TakeFunDamage();
|
2019-08-04 17:35:24 +02:00
|
|
|
|
AttackHit = true;
|
|
|
|
|
AnimationProgress = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
CurrentStatus = "Retreat";
|
|
|
|
|
AnimationProgress += Time.deltaTime / ReleaseAnimationLength;
|
|
|
|
|
AnimatableTransform.position = Vector2.Lerp(Target, Root, AnimationProgress);
|
|
|
|
|
if (AnimationProgress >= 1.0f) {
|
|
|
|
|
// Attack finished, reset
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 20:38:57 +02:00
|
|
|
|
private void TakeFunDamage() {
|
|
|
|
|
PlayerFun Player = Follow.Target.GetComponent<PlayerFun>();
|
|
|
|
|
if (Player != null) {
|
|
|
|
|
Player.TakeDamage();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 17:35:24 +02:00
|
|
|
|
private void OnDrawGizmosSelected() {
|
|
|
|
|
Gizmos.color = Color.red;
|
|
|
|
|
Gizmos.DrawWireSphere(transform.position, MeleeRange);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|