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; private bool lookAt = false; 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)); lookAt = true; } public override void MoveTo(Vector3 position) { navAgent.SetDestination(position); } public override void MoveTo() { MoveTo(player.transform.position); } public override void Dodge() { Vector3 difVector= transform.position-player.transform.position; //Vector3 dirVector = new Vector3(difVector.z,difVector.y,difVector.x); Vector3 dirVector = transform.right; navAgent.SetDestination(navAgent.transform.position - (dirVector.normalized * 10f)); navAgent.speed = 4f; } public override void Roar() { if(!inAnimation) { animator.SetTrigger("AttackScream"); StartCoroutine(InAnimationHolder(screamAnimationDuration)); } } public override void Idle() { //this.navAgent.isStopped = true; //this.navAgent.speed = 0; lookAt = false; } 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); if (lookAt) { LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z)); } } public override bool Collect(ref StripsVariableMapping.PackageMapping package) { package.SetMapping("Distance2Player", Vector3.Distance(player.transform.position, transform.position).ToString()); package.SetMapping("AIHealth", health.ToString()); Ray ray = new Ray(); ray.origin = transform.position; ray.direction = player.transform.position- transform.position; RaycastHit visibleHit; if(Physics.Raycast(ray, out visibleHit)) { //print("Detection:" + visibleHit.transform.name); package.SetMapping("IsPlayerVisible", visibleHit.transform.parent.CompareTag("Player").ToString()); } Vector3 pistolForward = PlayerInteractionHandler.instance.Pistol.transform.up; Vector3 pistolPosition = PlayerInteractionHandler.instance.Pistol.transform.position; Ray r = new Ray(); r.origin = pistolPosition; r.direction = pistolForward; RaycastHit hit; if (Physics.Raycast(r, out hit, 50)) { package.SetMapping("AimedAt", (hit.transform == this.gameObject.transform).ToString()); } return true; } } }