private void Cleanup()
 {
     if (movementState == MovementState.Stopped)
     {
         gridColliderUnderSlidable = null;
         slidable = null;
     }
 }
Ejemplo n.º 2
0
 // Use this for initialization
 void Awake()
 {
     //Check if there is already an instance of SoundManager
     if (instance == null)
     {
         //if not, set it to this.
         instance = this;
     }
     //If instance already exists:
     else if (instance != this)
     {
         //Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
         Destroy(gameObject);
     }
 }
    private void UpdateMoveState()
    {
        //We check if we have more than one touch happening.
        //We also check if the first touches phase is Ended (that the finger was lifted)
        if (inputHandler.IsInputDown())
        {
            //We transform the touch position into word space from screen space and store it.
            Vector3 touchPosWorld = Camera.main.ScreenToWorldPoint(inputHandler.GetInputScreenPosition());

            RaycastHit slidableHit;
            // Does the ray intersect with any grid colliders?
            // Bit shift the index of the layer to get a bit mask
            int slidableLayerMask = 1 << Constants.SLIDABLE_LAYER;
            Physics.Raycast(touchPosWorld, raycastDirection, out slidableHit, float.MaxValue, slidableLayerMask);

            if (slidableHit.collider != null)
            {
                Slidable touchedSlidable = slidableHit.collider.GetComponent <Slidable>();

                // Early exit if THIS grid movement controller is not relevant.
                if (!touchedSlidable.BelongsTo(grid))
                {
                    return;
                }
                slidable      = touchedSlidable;
                movementState = MovementState.Moving;
            }

            if (slidable != null)
            {
                // check if we should update our tracked grid collider
                int        gridColliderLayerMask = 1 << Constants.GRID_COLLIDER_LAYER;
                RaycastHit gridColliderHit;
                Physics.Raycast(slidable.transform.position, raycastDirection, out gridColliderHit, float.MaxValue, gridColliderLayerMask);

                if (gridColliderHit.collider != null)
                {
                    gridColliderUnderSlidable = gridColliderHit.collider.GetComponent <GridCollider>();
                }
            }
        }
        if (inputHandler.HasInputEnded())
        {
            movementState = MovementState.Stopped;
            Debug.Log("input ended");
        }
    }