BloodAndGore/Assets/Scripts/AI/Behaviours/RangedAttackFollowed.cs

54 lines
1.6 KiB
C#
Raw Normal View History

2019-08-18 20:32:07 +02:00
using UnityEngine;
using Saltosion.OneWeapon.Guns;
namespace Saltosion.OneWeapon.AI.Behaviours {
[RequireComponent(typeof(Follow))]
public class RangedAttackFollowed : AIBehaviour {
public GameObject BulletPrefab;
public float Range;
public float Cooldown;
public bool Attacking { get { return CanBehave(); } }
private Follow Follow;
private float CurrentCooldown;
private void Start() {
Follow = GetComponent<Follow>();
}
private void Update() {
if (CurrentCooldown > 0) {
CurrentCooldown -= Time.deltaTime;
}
}
public override bool CanBehave() {
return Follow.Target != null && Follow.GetDistance() <= Range;
}
public override void Execute() {
CurrentStatus = "PewPew";
if (CurrentCooldown <= 0) {
Shoot();
CurrentCooldown = Cooldown;
CurrentStatus = "PEW"; // A joke that lasts for a frame
}
}
private void Shoot() {
2019-08-18 23:26:42 +02:00
Vector2 Direction = Follow.TargetDirection.normalized;
2019-08-18 20:32:07 +02:00
GameObject ShotBulletObj = Instantiate(BulletPrefab, transform.position + (Vector3)Direction, new Quaternion());
Bullet ShotBullet = ShotBulletObj.GetComponent<Bullet>();
2019-08-18 23:26:42 +02:00
ShotBullet.Direction = Direction;
2019-08-18 20:32:07 +02:00
ShotBullet.InitialRotation = Mathf.Atan2(Direction.y, Direction.x);
}
private void OnDrawGizmosSelected() {
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, Range);
}
}
}