Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
8.1 kB
1
Indexable
Never
using UnityEngine;
using System.Collections;
using Pathfinding;
using System.IO;
using System.Linq;

namespace Pathfinding {
	/// <summary>
	/// Sets the destination of an AI to the position of a specified object.
	/// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
	/// This component will then make the AI move towards the <see cref="target"/> set on this component.
	///
	/// See: <see cref="Pathfinding.IAstarAI.destination"/>
	///
	/// [Open online documentation to see images]
	/// </summary>
	[UniqueComponent(tag = "ai.destination")]
	[HelpURL("http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php")]
	public class AIDestinationSetter : VersionedMonoBehaviour {
        /// <summary>The object that the AI should move to</summary>
        public CharacterController controller;

        public Transform target;
		private GameObject player;
		IAstarAI ai;
		private Vector3 LastPosition;
        private Vector3 LastPosition2;
        public float timeRemaining = 0.3f;
        public bool timerIsRunning = false;
        public float timeRemaining2 = 3f;
        public bool timerIsRunning2 = false;
        public bool timerGonienieGracza = false;
        public float timeRemaining3 = 1.5f; 
        private bool first = false;
        private bool punkt_zdobyty=false;
        private bool zadanie = false;
        bool firstFirst = false;
        RaycastHit2D hit;
        GameObject gracz;

        public bool WidziGracza;

        public LayerMask entity;

        bool sleep = false;
        private void Start()
        {
            controller = GetComponent<CharacterController>();
            player = GameObject.FindGameObjectWithTag("Player");
            LastPosition = transform.position;
            timerIsRunning = true;

        }
        void OnEnable () {

			ai = GetComponent<IAstarAI>();
			// Update the destination right before searching for a path as well.
			// This is enough in theory, but this script will also update the destination every
			// frame as the destination is used for debugging and may be used for other things by other
			// scripts as well. So it makes sense that it is up to date every frame.
			if (ai != null) ai.onSearchPath += Update;

		}

		void OnDisable () {
			if (ai != null) ai.onSearchPath -= Update;
		}



		/// <summary>Updates the AI's destination every frame</summary>
		void Update () {

            AIPath scriptAI = gameObject.GetComponent<AIPath>();
            //znalezieniegracza
            gracz = GameObject.FindGameObjectWithTag("Player");

            //sprawdzenie czy jest w zasiegu wzroku
            var rayDirection = gracz.transform.position - transform.position;
            hit = Physics2D.Raycast(transform.position, rayDirection, 20f, ~entity);
            if (hit.collider.gameObject == gracz)
            {
                WidziGracza = true;
                firstFirst = true;
                timeRemaining3 = 2f;
    }
            else
            {
                WidziGracza = false;
            }

            Vector3 playerPos = GameObject.FindGameObjectWithTag("Player").transform.position;
            Rigidbody2D rb = GetComponent<Rigidbody2D>();

            //jeśli gracz jest w obrębie 20 kratek i go widzi
            if (player.transform != null && ai != null && (playerPos - transform.position).magnitude < 20 && !sleep && WidziGracza == true)
            {
                scriptAI.endReachedDistance = 5;
                LastPosition = player.transform.position;
                ai.destination = LastPosition;
                first = true;
            }

            //uspiony przez atak
            if (sleep)
            {
                ai.destination = ai.position;
            }

            //gdy gracza nie ma w obrębie 20 kratek lub go nie widzi
            if (((playerPos - transform.position).magnitude > 20 && !sleep) | WidziGracza == false )
			{   //sprawdzanie czy jesteśw pobliżu ostatniej pozycji
                scriptAI.endReachedDistance = 0;

                if ((transform.position.x > LastPosition.x - 1 && transform.position.x < LastPosition.x + 1 &&
                    transform.position.y > LastPosition.y - 1 && transform.position.y < LastPosition.y + 1) | zadanie==true)  
                {

                    if (first) //uruchamianie w momencie gdy tryb czuwania zostaje uruchomiony po zgubieniu gracza
                    {
                        if ((playerPos - transform.position).magnitude < 40) timerGonienieGracza = true;
                        else first = false;
                    }
                    if (!first)
                    {
                        zadanie = false;
                        //wyzwolenie timera generującego losowe miejsce w okręgu ostatniej pozycji
                        timerIsRunning = true;
                    }
                }
            }
        }

        private void FixedUpdate()
        {
            if (timerGonienieGracza)
            {
                if (timeRemaining3 > 0)
                {
                    timeRemaining3 -= Time.deltaTime;
                }
                else
                {
                    if (firstFirst)
                    {
                        firstFirst = false; 
                        LastPosition = gracz.transform.position;
                        LastPosition2 = gracz.transform.position;
                        ai.destination = gracz.transform.position;
                    }
                    if (transform.position.x > LastPosition.x - 1 && transform.position.x < LastPosition.x + 1 &&
                        transform.position.y > LastPosition.y - 1 && transform.position.y < LastPosition.y + 1)
                    {
                        first = false;
                        timerGonienieGracza = false;
                        timeRemaining3 = 1.5f;
                    }
                }
            }

            //timer gdy nie może dojsc do punktu przez 3 sekundy
            if (timerIsRunning2)
            {
                if (timeRemaining2 > 0)
                {
                    timeRemaining2 -= Time.deltaTime;
                }
                else
                {
                    timerIsRunning2 = false;
                    timeRemaining2 = 3f;
                    if (!punkt_zdobyty) zadanie = true;

                    else punkt_zdobyty = true;
                }
            }
            //timer generowanie losowego punktu co 2 sekundy
            if (timerIsRunning)
            {
                if (timeRemaining > 0)
                {
                    timeRemaining -= Time.deltaTime;
                }
                else
                {
                    LastPosition = PickRandomPoint(10, 4, false);
                    timerIsRunning = false;
                    timeRemaining = 2f;
                    ai.destination = LastPosition;
                    punkt_zdobyty = false; 
                    timerIsRunning2 = true;
                }
            }
        }
        public void Sleeping(bool spi) //Usypianie
		{
			sleep = spi;
		}
        Vector3 PickRandomPoint(float radius, float min,bool nowy) //wyznacza losowe miejsce na okręgu
        {
            if (radius == min) return new Vector3(0,0);

            if (nowy) LastPosition2 = gracz.transform.position;

            var point = Random.insideUnitSphere * radius;
            float x = point.x + LastPosition2.x;
            float y = point.y + LastPosition2.y;
                if ((x < transform.position.x + min && x > transform.position.x - min)
                  | (y < transform.position.y + min && y > transform.position.y - min))
                { point = PickRandomPoint(radius, min, nowy); }
                else point += LastPosition2;
            return point;
        }


    }
}