111 lines
3.3 KiB
C#
111 lines
3.3 KiB
C#
using STRIPS;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AI;
|
|
|
|
namespace Obscurum.ModularAI
|
|
{
|
|
public class MeleeResolver : AStripsActionResolver
|
|
{
|
|
[SerializeField]
|
|
private Animator animator;
|
|
[SerializeField]
|
|
private NavMeshAgent navAgent;
|
|
|
|
private PlayerComponent player;
|
|
|
|
private bool inAnimation = false;
|
|
[SerializeField]
|
|
private float attackAnimationDuration = 1.5f;
|
|
[SerializeField]
|
|
private float screamAnimationDuration = 1.5f;
|
|
[SerializeField]
|
|
private float health = 10f;
|
|
|
|
public override void Attack()
|
|
{
|
|
if (!inAnimation)
|
|
{
|
|
animator.SetTrigger("Attack");
|
|
StartCoroutine(InAnimationHolder(attackAnimationDuration));
|
|
}
|
|
}
|
|
|
|
public override void Hide()
|
|
{
|
|
|
|
}
|
|
|
|
public override void LookAt(Vector3 position)
|
|
{
|
|
// Create a direction vector that ignores the y-axis difference by setting the y component of both positions to be the same.
|
|
Vector3 direction = new Vector3(position.x, this.transform.position.y, position.z) - this.transform.position;
|
|
|
|
// Create a rotation that looks in the 'direction' but only rotates around the y-axis.
|
|
Quaternion toRotation = Quaternion.LookRotation(direction);
|
|
|
|
// Apply the rotation over time to smooth the transition.
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, Time.deltaTime * 150f);
|
|
}
|
|
|
|
public override void LookAt()
|
|
{
|
|
LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
|
|
}
|
|
|
|
public override void MoveTo(Vector3 position)
|
|
{
|
|
navAgent.SetDestination(position);
|
|
}
|
|
public override void MoveTo()
|
|
{
|
|
MoveTo(player.transform.position);
|
|
}
|
|
|
|
public override void Roar()
|
|
{
|
|
if(!inAnimation)
|
|
{
|
|
animator.SetTrigger("AttackScream");
|
|
StartCoroutine(InAnimationHolder(screamAnimationDuration));
|
|
}
|
|
}
|
|
public override void Idle()
|
|
{
|
|
this.navAgent.isStopped = true;
|
|
this.navAgent.speed = 0;
|
|
}
|
|
|
|
private IEnumerator InAnimationHolder(float duration)
|
|
{
|
|
inAnimation = true;
|
|
yield return new WaitForSeconds(duration);
|
|
inAnimation = false;
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
player = PlayerComponent.Instance;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
this.animator.SetFloat("Speed", navAgent.velocity.magnitude / 4);
|
|
}
|
|
|
|
public override bool Collect(ref StripsVariableMapping.PackageMapping package)
|
|
{
|
|
package.SetMapping("Distance2Player", Vector3.Distance(player.transform.position, transform.position).ToString());
|
|
package.SetMapping("IsPlayerVisible", true.ToString());
|
|
package.SetMapping("AIHealth", health.ToString());
|
|
return true;
|
|
}
|
|
}
|
|
}
|