Exemple #1
0
        ////////////////////////////////////////////////////////////////

        /* [USAGE]
         * //
         * // EACH TICK:
         * // ValidateBridgePoint     for the players look at position
         * // CanCreateBridge         if the player has a valid point placed and looks at another valid point
         * ////////////////////////////////////////////////////////////////
         * // ON PLAYER PLACE BRIDGE:
         * // PrepareBridge           when the player wants to place a bridge
         * //////////////////////////////////////////////////////////////*/

        /// <summary>
        /// Checks if a ray cast hit produces a valid bridge point.
        /// </summary>
        /// <param name="hit">In RaycastHit</param>
        /// <param name="outPoint">Optional out in success case</param>
        /// <returns></returns>
        public static BridgePointValidationResult ValidateBridgePoint(RaycastHit hit, out RawBridgePoint outPoint)
        {
            outPoint = RawBridgePoint.INVALID;

            ////////////////////////////////////////////////////////////////
            // Check if hit point is marked as shootable
            ////////////////////////////////////////////////////////////////

            // TODO: Research if Mesh Colliders for box shapes are bad performance wise;
            //Debug.Log("TODO: Find a better way to find out if a point is bridgeable! Resort to tag again? :/ ");
            //Color vertexColorHit    = ((MeshCollider)hit.collider).sharedMesh.colors[hit.triangleIndex];
            //bool isBridgeable       = vertexColorHit.b > 0.5f;
            //if (!isBridgeable)
            //{
            //    return BridgePointValidationResult.NonBridgableWall;
            //}
            //bool isMirror           = vertexColorHit.r > 0.5f;

            bool isMirror = true;

            Vector3 position = hit.point;
            Vector3 normal   = hit.normal;

            outPoint = new RawBridgePoint(position, normal, isMirror);

            return(BridgePointValidationResult.Success);
        }
        BridgeMesh m_BridgeMesh;      // TODO: Move to bridge visualizer

        public BridgeManager()
        {
            m_FirstBridgePoint  = RawBridgePoint.INVALID;
            m_SecondBridgePoint = RawBridgePoint.INVALID;

            m_Bridge     = Bridge.INVALID;
            m_BridgeMesh = BridgeMesh.INVALID;
        }
        ////////////////////////////////////////////////////////////////
        // Private Helpers
        ////////////////////////////////////////////////////////////////

        void ResetBridgeAndBridgePoints()
        {
            Debug.Log("Reset Bridge Points");
            m_FirstBridgePoint  = RawBridgePoint.INVALID;
            m_SecondBridgePoint = RawBridgePoint.INVALID;

            m_Bridge     = Bridge.INVALID;
            m_BridgeMesh = BridgeMesh.INVALID;
        }
 public ValidateBridgeInput(RawBridgePoint startPoint, RawBridgePoint endPoint)
 {
     PointStart = startPoint;
     PointEnd   = endPoint;
 }
        /// <summary>
        /// Places the given bridge point
        /// </summary>
        /// <param name="placePoint"></param>
        /// <returns>True, if the bridge needs to be updated now</returns>
        bool PlaceBridgePoint(RawBridgePoint placePoint, out CreateBridgeParameters outBridgeParamters)
        {
            outBridgeParamters = new CreateBridgeParameters();
            bool firstPointIsValid  = m_FirstBridgePoint.IsValid();
            bool secondPointIsValid = m_SecondBridgePoint.IsValid();


            ////////////////////////////////////////////////////////////////

            if (!firstPointIsValid && !secondPointIsValid)
            {
                // No point set? Set the first point
                m_FirstBridgePoint = placePoint;
            }
            else if (firstPointIsValid && !secondPointIsValid)
            {
                // Does this point work to create a bridge?
                CreateBridgeParameters createBridgeParamters;
                BridgeValidationResult result = BridgePlanner.ValidateBridgePlacement(new ValidateBridgeInput(m_FirstBridgePoint, placePoint), out createBridgeParamters);
                if (result == BridgeValidationResult.Success)
                {
                    // Yes! Lets prepare everything for updating the bridge
                    m_SecondBridgePoint = placePoint;
                    outBridgeParamters  = createBridgeParamters;
                    return(true);
                }
                else
                {
                    // No. Instead, override the first point!
                    m_FirstBridgePoint = placePoint;
                    return(false);
                }
            }
            else if (firstPointIsValid && secondPointIsValid)
            {
                // First try to override second point, if that fails, try to override first point.

                // Try first point.
                CreateBridgeParameters createBridgeParamters;
                BridgeValidationResult result = BridgePlanner.ValidateBridgePlacement(new ValidateBridgeInput(m_FirstBridgePoint, placePoint), out createBridgeParamters);
                if (result == BridgeValidationResult.Success)
                {
                    // Make the new point the second point
                    m_SecondBridgePoint = placePoint;
                    outBridgeParamters  = createBridgeParamters;
                    return(true);
                }
                else
                {
                    result = BridgePlanner.ValidateBridgePlacement(new ValidateBridgeInput(placePoint, m_SecondBridgePoint), out createBridgeParamters);
                    if (result == BridgeValidationResult.Success)
                    {
                        // Make the previous second the first point & the new point the second point
                        m_FirstBridgePoint  = m_SecondBridgePoint;
                        m_SecondBridgePoint = placePoint;
                        outBridgeParamters  = createBridgeParamters;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                Debug.LogError("BridgePlanner: Second point is valid but first is not?!");
            }

            return(false);
        }