Untitled

 avatar
unknown
plain_text
2 years ago
3.0 kB
6
Indexable
using UnityEngine;
using System.Collections;

public class CapsuleCatch : MonoBehaviour
{
    private bool isCaught = false;
    private GameObject capturedMonsterPrefab;
    private MonsterStats capturedMonsterStats;

    public Animator captureAnimator;
    public float destroyDelay = 8f;
    private Rigidbody capsuleRigidbody;

    private void Start()
    {
        capsuleRigidbody = GetComponent<Rigidbody>();
    }

    private void OnTriggerEnter(Collider other)
    {
        if (!isCaught && other.CompareTag("EnemyMonster"))
        {
            CaptureMonster(other.gameObject);
        }
    }

    private void CaptureMonster(GameObject monster)
    {
        MonsterStats monsterStats = monster.GetComponent<Monster>().stats;

        if (monsterStats != null)
        {
            monster.tag = "PlayerMonster";
            capturedMonsterPrefab = monsterStats.monsterPrefab;
            capturedMonsterStats = monsterStats;

            if (captureAnimator != null)
            {
                captureAnimator.SetTrigger("IsCapture");

                // Freeze only the position of the capsule while the capture animation is playing
                if (capsuleRigidbody != null)
                {
                    capsuleRigidbody.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
                }

                // Set the rotation along the X-axis to 0
                transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
            }

            StartCoroutine(DestroyAfterDelay());
            isCaught = true;

            Object.Destroy(monster);
        }
        else
        {
            Debug.LogError("MonsterStats component not found on the captured monster.");
        }
    }

    // Method called when the capture animation is complete
    public void OnCaptureAnimationComplete(MonsterStats stats)
    {
        // Continue with any logic after the animation is complete
        NotifyPlayer();
    }

    private void NotifyPlayer()
    {
        if (capturedMonsterPrefab != null && capturedMonsterStats != null)
        {
            PlayerScript player = FindFirstObjectByType<PlayerScript>();
            if (player != null)
            {
                player.monsterUIManager.UpdateMonsterInfoText(capturedMonsterPrefab, capturedMonsterStats);
                player.AddToCapturedMonsters(capturedMonsterPrefab, capturedMonsterStats);
            }
        }
    }

    private IEnumerator DestroyAfterDelay()
    {
        yield return new WaitForSeconds(destroyDelay);

        // Destroy the capsule GameObject after the specified delay
        Destroy(gameObject);
    }

    private new static T FindFirstObjectByType<T>() where T : MonoBehaviour
    {
        T[] objects = Object.FindObjectsOfType<T>(true);
        return objects.Length > 0 ? objects[0] : null;
    }
}
Editor is loading...
Leave a Comment