public NodeWalkable GetNextWalkableNodeInDirection(NodeWalkable currentNode, Vector3Int direction, Vector3Int currentUpVector)
        {
            Debug.AssertFormat(direction.magnitude == 1, this, "Direction is invalid: {0}", direction);

            NodeWalkable nextWalkableNode = default;

            if (m_GridModel != null && currentNode != null)
            {
                nextWalkableNode = m_GridModel.TryGetWalkableNode(currentNode.Coordinates + direction);

                if (nextWalkableNode == null && currentNode.UpVectors.Length > 1)
                {
                    for (int i = 0; i < currentNode.UpVectors.Length; ++i)
                    {
                        Vector3Int targetUpVector = currentNode.UpVectors[i];
                        if (currentUpVector != targetUpVector)
                        {
                            Quaternion rotation         = Quaternion.FromToRotation(currentUpVector, targetUpVector);
                            Vector3Int rotatedDirection = Vector3IntUtil.Rotate(direction, rotation);

                            nextWalkableNode = m_GridModel.TryGetWalkableNode(currentNode.Coordinates + rotatedDirection);

                            if (nextWalkableNode != null)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            Debug.Assert(nextWalkableNode != null, "Failed to get next walkable node.", this);

            return(nextWalkableNode);
        }
Esempio n. 2
0
        private void UpdatePosition(WorldGridController worldGridController)
        {
            NodeWalkable nextNode = worldGridController.GetNextWalkableNodeInDirection(ActiveNode, m_MovementDirection, m_UpVector);

            if (nextNode != null && nextNode != ActiveNode)
            {
                NodeWalkable previousNode = ActiveNode;
                ActiveNode = nextNode;

                Vector3Int movementDirection = ActiveNode.Coordinates - previousNode.Coordinates;
                if (movementDirection != m_MovementDirection)
                {
                    Quaternion rotation = Quaternion.FromToRotation(m_MovementDirection, movementDirection);
                    m_UpVector = Vector3IntUtil.Rotate(m_UpVector, rotation);

                    m_MovementDirection = movementDirection;
                }

                // Check before adding active node to path!
                bool collidedWithSelf     = m_PathNodes.Contains(ActiveNode) == true;
                bool collidedWithObstacle = ActiveNode.IsObstacle == true;

                m_PathNodes.Enqueue(ActiveNode);

                if (collidedWithSelf == true || collidedWithObstacle == true)
                {
                    Die();
                }
            }
        }
Esempio n. 3
0
 private void UpdateDirection(float angleInDegrees)
 {
     if (angleInDegrees != 0)
     {
         Quaternion rotation = Quaternion.AngleAxis(angleInDegrees, m_UpVector);
         m_MovementDirection = Vector3IntUtil.Rotate(m_MovementDirection, rotation);
     }
 }
Esempio n. 4
0
        public void SetBlockAt(RaycastHit hit, BlockDefinition block, bool adjacent = false)
        {
            int multiplier = adjacent ? 1 : -1;

            Vector3Int position = Vector3IntUtil.FloorFromVector3(hit.point + hit.normal * (multiplier * 0.5f));

            this.lastHitStart = transform.position;
            this.lastHitEnd   = position;

            //Debug.Log($"{hit.point} {position} {hit.normal}");

            WorldManager.Active.SetBlock(position, block);
        }