Exemplo n.º 1
0
    /// <summary>
    /// Unity Event: Checks for and manages joining of pieces
    /// </summary>
    /// <param name="ColObj">Colliding piece</param>
    void OnTriggerEnter(Collider ColObj)
    {
        if (ColObj.name.Contains("Piece"))
        {
            if (JpPuzzleControllerInstance.IsHoldingPiece)
            {
                // Is it a side collision  or top or bottom collision using collision range

                Vector3 CollisionVector = transform.position - ColObj.transform.position;
                CollisionVector.Normalize();

                float CollisionThreshold          = JpPuzzleControllerInstance.PieceJoinSensitivity;
                float DirectionCollisionThreshold = 0.3f;

                GameObject        ColGameObject    = ColObj.gameObject;
                JPPieceController ColObjController = ColGameObject.GetComponent <JPPieceController>();

                //Check for correct or wrong piece collision for display of wrong piece animation
                if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID())
                {
                    JpPuzzleControllerInstance.WrongPieceCollision(ColGameObject);
                }

                if (CollisionVector.y < CollisionThreshold && CollisionVector.y > -CollisionThreshold)
                {
                    if (CollisionVector.x < DirectionCollisionThreshold) //Right Collision
                    {
                        //Debug.Log("Right Collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized);

                        if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Left))
                        {
                            OnRightCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController);
                        }
                    }
                    else if (CollisionVector.x > -DirectionCollisionThreshold) //Left Collision
                    {
                        //Debug.Log("Left collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized);

                        if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Right))
                        {
                            OnLeftCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController);
                        }
                    }
                }

                else if (CollisionVector.x < CollisionThreshold && CollisionVector.x > -CollisionThreshold)
                {
                    if (CollisionVector.y < DirectionCollisionThreshold) //Top Collision
                    {
                        //Debug.Log("Top Collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized);

                        if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Bottom))
                        {
                            OnTopCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController);
                        }
                    }
                    else if (CollisionVector.y > DirectionCollisionThreshold) //Bottom collision
                    {
                        //Debug.Log("Bottom collision Piece " + ThisPieceData.ID + " " + CollisionVector.normalized);

                        if (ColObjController.ThisPieceData.HaveJoint(EJointPosition.Top))
                        {
                            OnBottomCollision(ColObjController.ThisPieceData.ID, ColGameObject, ColObjController.ThisPieceData, ColObjController);
                        }
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
    void OnRightCollision(int CollidingPieceId, GameObject ColP, SPieceInfo ColPPieceData, JPPieceController ColObjController)
    {
        //Check if this is child of current holding piece then apply collision logic
        bool IsChildOfHoldingPiece = false;

        JPPieceController[] ChildrenControllers = transform.GetComponentsInChildren <JPPieceController>();
        foreach (JPPieceController item in ChildrenControllers)
        {
            if (ThisPieceData.ID == item.ThisPieceData.ID)
            {
                IsChildOfHoldingPiece = true;
                break;
            }
        }


        if (ThisPieceData.ID == JpPuzzleControllerInstance.HoldingPieceID() || IsChildOfHoldingPiece)
        {
            if (ThisPieceData.ID == CollidingPieceId - 1)
            {
                //If is child of holding piece make it parent
                //Make this piece parent of all
                Transform Temp = transform.root;
                transform.parent = null;
                Temp.parent      = transform;



                JpPuzzleControllerInstance.UnholdPiece();

                Vector3 CalculatedPos = new Vector3();


                float LeftJointWorldScale  = 0;
                float RightJointWorldScale = 0;
                float TopJointWorldScale   = 0;
                float BotJointWorldScale   = 0;

                float ColPLeftJointWorldScale  = 0;
                float ColPRightJointWorldScale = 0;
                float ColPTopJointWorldScale   = 0;
                float ColPBotJointWorldScale   = 0;


                //Calculate required data
                CalculateDataForCollisionPlacement(ColPPieceData, out ColPLeftJointWorldScale, out ColPRightJointWorldScale,
                                                   out ColPTopJointWorldScale, out ColPBotJointWorldScale);
                CalculateDataForCollisionPlacement(ThisPieceData, out LeftJointWorldScale, out RightJointWorldScale,
                                                   out TopJointWorldScale, out BotJointWorldScale);


                //Calculate X
                //Calculated X without joints
                float CalcXWJ = ColP.transform.position.x - JpPuzzleControllerInstance.PieceWidthInWorld;


                CalculatedPos.x = CalcXWJ - (ColPRightJointWorldScale / 2) + (ColPLeftJointWorldScale / 2) +
                                  (RightJointWorldScale / 2) - (LeftJointWorldScale / 2);



                //Calculate Y
                CalculatedPos.y = ColP.transform.position.y + (TopJointWorldScale / 2) - (BotJointWorldScale / 2)
                                  - (ColPTopJointWorldScale / 2) + (ColPBotJointWorldScale / 2);



                CalculatedPos.z = transform.position.z;

                transform.position = CalculatedPos;

                ColP.transform.root.parent = transform;

                CheckForPuzzleComplete(transform);
            }
        }
    }