void Start()
    {
        cam          = UnityEngine.Camera.main;
        tileWid      = 0.5f;
        m_halfWidth  = 0.5f;
        m_halfHeight = 0.5f;
        unWalkable   = new List <Vector2>();

        unWalkable = TileCheck.unWalkable;
        m_start.setValues(transform.position.x, transform.position.y);

        m_end.setValues(target.transform.position.x, target.transform.position.y);

        m_old.setValues(-100f, -100f);
        m_new = m_end;
        m_current.setValues(this.transform.position.x, this.transform.position.y);
        disableCheck = false;
        activatePath = false;
        //m_halfWidth = current.bounds.size.x / 2;
        //m_halfHeight = current.bounds.size.y / 2;
    }
    void FixedUpdate()
    {
        if (disableCheck == false)
        {
            checkIfInVision();
        }



        if (activatePath == true)
        {
            if (finalPath == null)
            {
                m_new.setValues(target.transform.position.x, target.transform.position.y);

                m_current.setValues(this.transform.position.x, this.transform.position.y);


                m_end.setValues(target.transform.position.x, target.transform.position.y);

                doPathFinding();
                m_old = m_new;
                if (finalPath == null)
                {
                    return;
                } //if its still null after checking for path ideally youd like to stop it from calculating always if its null,
                  //however this should never even occur, there should always be a path, if there isnt its bad level design OR our character is somewhere where it shouldnt get

                //
            }
            else
            {
                if (finalPath.returnCount() > 0)
                {
                    movementTarget = new Vector3(finalPath.get0().getPosition().x, finalPath.get0().getPosition().y, 0);
                }
            }

            if (transform.position == movementTarget)
            {
                //WE ARE CLOSE ENOUGH FOR THE CONSOLE TO THINK WE EQUAL, SO MAKE SURE WE DO EQUAL!!
                transform.position = movementTarget;
                m_new.setValues(target.transform.position.x, target.transform.position.y);
                m_current.setValues(this.transform.position.x, this.transform.position.y);


                m_end.setValues(target.transform.position.x, target.transform.position.y);



                //Only do this everytime the player has moved once, dont keep checking!

                if (m_old.x != m_new.x || m_old.y != m_new.y) //check if the values have changed since last update also do an extra check if there is a path(results) BUT for some reason our final path is null!! THIS SHOULD NEVER OCCUR!!! But if it were to i want to make a go around
                {
                    if (enemyMove.disableMovement == false)
                    {
                        doPathFinding();
                        m_old = m_new;
                    }
                }
            }
        }
    }
Example #3
0
/*
 *  public bool isInList(Vector2 checking)
 *  {
 *
 *      for(int i =0; i == m_unWalkables.Count; i++)
 *      {
 *          if(m_unWalkables[i].x == checking.x && m_unWalkables[i].y == checking.y)
 *          {
 *              return true;
 *          }
 *      }
 *      return false;
 *  }
 */

    public List <Node.Position> getAdjacentNodes(float posX, float posY, float colWidth)
    {
        //ADD A STEP INSTEAD OF 1 THAT IS THE SIZE OF THE GRID CELL
        List <Node.Position> m_results = new List <Node.Position>();

        Node.Position current = new Node.Position();
        current.setValues(posX, posY);

        if (isWalkable(current.x + (colWidth), current.y) == true)
        {
            Node.Position plusOneX = new Node.Position();
            plusOneX.setValues(posX + (colWidth), posY);
            m_results.Add(plusOneX);
        }

        if (isWalkable(current.x - (colWidth), current.y) == true)
        {
            Node.Position minusOneX = new Node.Position();
            minusOneX.setValues(posX - (colWidth), posY);
            m_results.Add(minusOneX);
        }

        if (isWalkable(current.x, current.y + (colWidth)) == true)
        {
            Node.Position plusOneY = new Node.Position();
            plusOneY.setValues(posX, posY + colWidth);
            m_results.Add(plusOneY);
        }

        if (isWalkable(current.x, current.y - (colWidth)) == true)
        {
            Node.Position minusOneY = new Node.Position();
            minusOneY.setValues(posX, posY - colWidth);
            m_results.Add(minusOneY);
        }

        if (isWalkable(current.x + colWidth, current.y + (colWidth)) == true)
        {
            Node.Position plusOneXplusY = new Node.Position();
            plusOneXplusY.setValues(posX + colWidth, posY + colWidth);
            m_results.Add(plusOneXplusY);
        }
        if (isWalkable(current.x + colWidth, current.y - colWidth) == true)
        {
            Node.Position plusOneXminusY = new Node.Position();
            plusOneXminusY.setValues(posX + colWidth, posY - colWidth);
            m_results.Add(plusOneXminusY);
        }
        if (isWalkable(current.x - colWidth, current.y - colWidth) == true)
        {
            Node.Position minusOneXminusY = new Node.Position();
            minusOneXminusY.setValues(posX - colWidth, posY - colWidth);
            m_results.Add(minusOneXminusY);
        }
        if (isWalkable(current.x - colWidth, current.y + colWidth) == true)
        {
            Node.Position minusOneXplusY = new Node.Position();
            minusOneXplusY.setValues(posX - colWidth, posY + colWidth);
            m_results.Add(minusOneXplusY);
        }

        //checks all the neighbors and adds them to the results list if they are walkable
        return(m_results);
    }