示例#1
0
    /// <summary>
    /// Determines whether the entety can continue moving.
    /// </summary>
    /// <param name="direction"></param>
    /// <param name="ignoreIgnores">Whether or not nodes with the flag 'ignore' should be ignored, ignoreIgnores = true > no such nodes will be used.</param>
    /// <returns>Whether it can move</returns>
    public bool PlaceFree(Directions direction, bool ignoreIgnores)
    {
        bool placeFree = false;

        //Looks whether the node it last passed is connected to a node in the direction pacman is moving at. This will be always true on a path, but once it reaches a new node, it might no longer be true, and pacman would stop there.
        for (int i = 0; i < lastNode.neighbourDirections.Length; i++)
        {
            if (lastNode.neighbourDirections[i] == direction)
            {
                if (!ignoreIgnores)
                {
                    placeFree          = true;
                    transform.rotation = Quaternion.identity;
                    this.direction     = (lastNode.trueNeighbourDirections[i] - 90) / -180 * Mathf.PI;
                    transform.Rotate(new Vector3(0, 0, -(lastNode.trueNeighbourDirections[i] - 90)));
                    break;
                }
                if (ignoreIgnores)
                {
                    if (!lastNode.neighbours[i].GetComponent <NodeScript>().ignore)
                    {
                        placeFree          = true;
                        transform.rotation = Quaternion.identity;
                        this.direction     = (lastNode.trueNeighbourDirections[i] - 90) / -180 * Mathf.PI;
                        transform.Rotate(new Vector3(0, 0, -(lastNode.trueNeighbourDirections[i] - 90)));
                        break;
                    }
                }
            }
        }

        if (nextNode != null && !placeFree) //nextnode is null if the player is not moving.
        {
            //to turn directions midway the line, look at the neighbour in the direction of current movement attached to the lastnode.
            if (nextNode.GetComponent <NodeScript>().GetReturnPathDirection(lastNode) != Directions.None)
            {
                if (nextNode.GetComponent <NodeScript>().GetReturnPathDirection(lastNode) == direction)
                {
                    for (int i = 0; i < nextNode.neighbourDirections.Length; i++)
                    {
                        if (nextNode.neighbourDirections[i] == direction)//if multiple paths have the same diretion, too bad
                        {
                            if (ignoreIgnores)
                            {
                                if (!nextNode.neighbours[i].GetComponent <NodeScript>().ignore)
                                {
                                    placeFree          = true;
                                    transform.rotation = Quaternion.identity;
                                    this.direction     = (nextNode.trueNeighbourDirections[i] - 90) / 180 * Mathf.PI;
                                    transform.Rotate(new Vector3(0, 0, -(nextNode.trueNeighbourDirections[i] - 90)));
                                    break;
                                }
                            }
                            if (!ignoreIgnores)
                            {
                                placeFree          = true;
                                transform.rotation = Quaternion.identity;
                                this.direction     = (nextNode.trueNeighbourDirections[i] - 90) / 180 * Mathf.PI;
                                transform.Rotate(new Vector3(0, 0, -(nextNode.trueNeighbourDirections[i] - 90)));
                                break;
                            }
                        }
                    }
                }
            }
        }
        if (!placeFree)//the player cannot move, and thus won't, hence no next node the player is heading to.
        {
            nextNode = null;
        }
        return(placeFree);
    }