示例#1
0
    void Update()
    {
        if (isDead)
        {
            return;
        }

        oldPosition = transform.position;

        //Check if the puppy is dying
        if (animator.GetBool("IsDead"))
        {
            StartCoroutine(Die());
            return;
        }

        Steering();

        if (currentSpeed.magnitude > speed)
        {
            currentSpeed = currentSpeed.normalized * speed;
        }

        //Check if the movement direction has changed
        FlipSprite();

        // Calculate new position
        Vector2 newPos = new Vector2(transform.position.x, transform.position.y) + currentSpeed * Time.deltaTime;

        // Update position
        transform.position = new Vector3(newPos.x, newPos.y, 0.0f);

        GridAI.GetInstance().UpdatePosition(this.gameObject, oldPosition, newPos);
    }
示例#2
0
 // public things
 public static GridAI GetInstance()
 {
     if (instance == null)
     {
         instance = new GridAI();
     }
     return(instance);
 }
示例#3
0
 internal void YoDecidoElControlador(GridAI ambienteLocal)
 {
     throw new NotImplementedException();
 }
示例#4
0
 private void Awake()
 {
     // ensrue that the first time the position is written
     GridAI.GetInstance().InitializePosition(this.gameObject, transform.position);
 }
示例#5
0
 private void OnDestroy()
 {
     GridAI.GetInstance().RemoveFromGrid(this.gameObject);
 }
示例#6
0
    void Steering()
    {
        currentPos = transform.position;
        playerPos  = player.transform.position;

        distanceVector = playerPos - currentPos;

        distanceVector.Normalize();

        if (Vector2.Distance(currentPos, playerPos) < detectionArea && aiStats.IsAvoidingPlayer) // Follow/flee player
        {
            desiredSpeed = distanceVector * speed;

            Vector3 aux = Quaternion.Euler(0, 0, 90) * desiredSpeed.normalized;

            desiredSpeed = aux * speed * fleeDir;
        }
        else if (!isWandering)
        {
            StartCoroutine(Wander(wanderTime));
        }

        separationVector = new Vector2(0.0f, 0.0f);

        /*foreach (GameObject p in aiManager.AIList)
         * {
         *  distance = currentPos - new Vector2(p.transform.position.x, p.transform.position.y);
         *  if (distance.magnitude <= area)
         *  {
         *      separationVector += distance;
         *  }
         * }*/

        // Get the closest positions of Grid to separate
        //foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos))
        foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos, area))
        {
            distance          = currentPos - new Vector2(go.transform.position.x, go.transform.position.y);
            separationVector += distance;
        }

        Vector3 auxPerp = Quaternion.Euler(0, 0, 90) * currentSpeed.normalized;

        Vector2 perpendicular = new Vector2(auxPerp.x, auxPerp.y);

        perpendicular.Normalize();


        // Check sides possible collision
        rightSide = currentPos + perpendicular * 0.7f;
        Vector2 leftSide = currentPos - perpendicular * 0.7f;

        Debug.DrawLine(rightSide, rightSide + currentSpeed.normalized * 3.0f, Color.yellow);
        Debug.DrawLine(leftSide, leftSide + currentSpeed.normalized * 3.0f, Color.yellow);

        Vector2      wallAvoidance = new Vector2(0.0f, 0.0f);
        RaycastHit2D rightHit      = Physics2D.Raycast(rightSide, currentSpeed.normalized, wallAvoidDistance);
        RaycastHit2D leftHit       = Physics2D.Raycast(leftSide, currentSpeed.normalized, wallAvoidDistance);

        if (rightHit.collider != null && rightHit.transform.gameObject.tag != "AI" &&
            rightHit.transform.gameObject.tag != "Player")
        {
            wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, rightHit.normal) * rightHit.normal;
        }
        else if (leftHit.collider != null && leftHit.transform.gameObject.tag != "AI" &&
                 leftHit.transform.gameObject.tag != "Player")
        {
            wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, leftHit.normal) * leftHit.normal;
        }

        currentSpeed = currentSpeed + separationVector + wallAvoidance / 2.0f;

        steering      = desiredSpeed - currentSpeed;
        currentSpeed += steering * Time.deltaTime;
    }
    void Steering()
    {
        currentPos = transform.position;
        playerPos  = player.transform.position;

        Color color = Color.red;

        distanceVector = playerPos - currentPos;

        distanceVector.Normalize();

        if (Vector2.Distance(currentPos, playerPos) > detectionArea) // Follow/flee player
        {
            if (!wandering)
            {
                StartCoroutine(Wander(wanderTime));
            }
            else
            {
                color = Color.green;
            }
        }
        else
        {
            desiredSpeed = distanceVector * speed;
            if (player.GetComponent <PlayerStats>().Smell)
            {
                color = Color.yellow;
                Vector3 aux = Quaternion.Euler(0, 0, 90) * desiredSpeed.normalized;

                desiredSpeed = aux * speed * fleeDir;
            }
        }

        separationVector = new Vector2(0.0f, 0.0f);

        // iterate all elements in array

        /*foreach (GameObject p in aiManager.AIList)
         * {
         *  distance = currentPos - new Vector2(p.transform.position.x, p.transform.position.y);
         *  if(distance.magnitude <= area)
         *  {
         *      separationVector += distance;
         *  }
         * }*/

        /*
         * int i = 0;
         * Collider2D[] overlap = Physics2D.OverlapCircleAll(transform.position, area, LayerMask.NameToLayer("AI"));
         * foreach(Collider2D c in overlap)
         * {
         *  i += 1;
         *  distance = currentPos - new Vector2(c.transform.position.x, c.transform.position.y);
         *  separationVector += distance;
         *
         * }
         */

        // Get the closest positions to steer
        //foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos))
        foreach (GameObject go in GridAI.GetInstance().GetClosePositions(currentPos, area))
        {
            distance          = currentPos - new Vector2(go.transform.position.x, go.transform.position.y);
            separationVector += distance;
        }

        Vector3 auxPerp = Quaternion.Euler(0, 0, 90) * currentSpeed.normalized;

        Vector2 perpendicular = new Vector2(auxPerp.x, auxPerp.y);

        perpendicular.Normalize();


        // Check sides possible collision
        rightSide = currentPos + perpendicular * 0.7f;
        leftSide  = currentPos - perpendicular * 0.7f;
        Debug.DrawLine(rightSide, rightSide + currentSpeed.normalized * 6.0f, color);
        Debug.DrawLine(leftSide, leftSide + currentSpeed.normalized * 6.0f, color);

        wallAvoidance = new Vector2(0.0f, 0.0f);
        RaycastHit2D rightHit = Physics2D.Raycast(rightSide, currentSpeed.normalized, wallAvoidDistance);
        RaycastHit2D leftHit  = Physics2D.Raycast(leftSide, currentSpeed.normalized, wallAvoidDistance);

        if (rightHit.collider != null && rightHit.transform.gameObject.tag != "AI" &&
            rightHit.transform.gameObject.tag != "Player")
        {
            wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, rightHit.normal) * rightHit.normal;
            //Debug.DrawLine(rightHit.point, rightHit.point + wallAvoidance.normalized * 1.5f, Color.green);
        }
        else if (leftHit.collider != null && leftHit.transform.gameObject.tag != "AI" &&
                 leftHit.transform.gameObject.tag != "Player")
        {
            wallAvoidance = currentSpeed - 2 * Vector2.Dot(currentSpeed, leftHit.normal) * leftHit.normal;
            //Debug.DrawLine(leftHit.point, leftHit.point + wallAvoidance.normalized * 1.5f, Color.green);
        }

        currentSpeed = currentSpeed + separationVector + wallAvoidance;

        steering      = desiredSpeed - currentSpeed;
        currentSpeed += steering * Time.deltaTime;
    }
示例#8
0
        public void Render()
        {
            GridTraverse(0, 0, Tiles.GetLength(0), Tiles.GetLength(1), (y, x) => {                     //:todo i have no idea why i had to invert the traverse... y and x should be x,y....
                var player   = Omnicatz.Engine.Entities.PlayerInstanceManager.GetPlayer(this.Parrent); // Entities.Agent.Player.GetPlayer(this.Parrent);
                var distance = GridAI <AgentBase> .Distance(Tiles.GetLength(0) / 2, Tiles.GetLength(1) / 2, x, y);
                // if (distance < player.Awareness.Current*2 || showAll) { //InSight


                var visibletile  = Tiles[x, y];
                var visibleAgent = VisibleAgents[x, y];
                //same tile as last render and not an agent
                bool firstPass = lastRender == null;
                bool unchanged;

                if (firstPass)
                {
                    unchanged = false;
                }
                else
                {
                    var lastvisibleAgent = lastRender.VisibleAgents[x, y];
                    unchanged            = lastRender.Tiles[x, y] == visibletile && visibleAgent == null && lastvisibleAgent == null;
                }

                if (unchanged)
                {
                    Console.CursorLeft += 2;     //skip if we have done this before!
                }
                else
                {
                    if (visibleAgent != null)     // change and we have a agent to render which takes priority over tile
                    {
                        // var npc = visibleAgent as NPCBase;
                        //  var hostilityColor = npc.IsHostile ? ConsoleColor.DarkRed : ConsoleColor.Black;


                        //if (npc.AI != null) { <--- Breaking out this to make the game engine more generic
                        //    switch (npc.AI.State) {
                        //        case AIState.Dead:
                        //            Console.Write("DD");
                        //            break;
                        //        case AIState.Sleeping:
                        //            Console.Write("ZZ");
                        //            break;
                        //        default:
                        ConsoleHelper.Write(visibleAgent.Graphics[0], visibleAgent.ColorA);
                        ConsoleHelper.Write(visibleAgent.Graphics[1], visibleAgent.ColorB, visibleAgent.ColorB2);
                        //   break;
                        //  }

                        // } else {
                        //    ConsoleHelper.Write(visibleAgent.Graphics[0], visibleAgent.ColorA);
                        //     ConsoleHelper.Write(visibleAgent.Graphics[1], visibleAgent.ColorB, hostilityColor);
                        //  }
                    }
                    else if (visibletile != null)         // the change was a regular tile that fine render that!
                    {
                        ConsoleHelper.Write(visibletile.Graphics[0], visibletile.ColorA);
                        ConsoleHelper.Write(visibletile.Graphics[1], visibletile.ColorB);
                    }
                    else         // out of bounds and stuff
                    {
                        ConsoleHelper.Write("xx", ConsoleColor.Black, ConsoleColor.Black);
                    }
                }


                //} //InSight
                //else {
                //    Console.CursorLeft += 2;
                //}
            }, () => { Console.WriteLine(); });
            lastRender = this;
        }
示例#9
0
 void Awake()
 {
     grid = GetComponent <GridAI>();
 }
示例#10
0
 void Awake()
 {
     GrillaMundo = GetComponent <GridAI>();
 }