// Update is called once per frame
    void Update()
    {
        if (!playerRef)
        {
            playerRef = GameObject.FindGameObjectWithTag("Player").GetComponent<TWCharacterController>();
        }

        if (gameStateManagerRef.inGame && playerRef)
        {
                if(Input.GetMouseButtonDown(0) &&
                //used to be pointinsideobject, not pointinsideobjectbutton - doug
                // if the finger is within the bounds of this object
                (worldCollisionRef.PointInsideObjectButton(gameObject,
                gameStateManagerRef.GetTouchController().GetLastFingerPosition()) ||
                worldCollisionRef.PointInsideObjectButton(gameObject, Input.mousePosition)) &&

                !gameStateManagerRef.GetInputManager().tearManagerRef.PlayerCurrentlyTearing &&
                !gameStateManagerRef.GetInputManager().foldRef.currentlyFolding &&

                // not performing a tear or a fold
                !gameStateManagerRef.GetInputManager().GetFingerGesture().Equals(InputManager.FingerGesture.TEAR) &&
                !gameStateManagerRef.GetInputManager().GetFingerGesture().Equals(InputManager.FingerGesture.FOLD))
            {
               // gameStateManagerRef.GetScreenManager().DisplayScreen(ScreenAreas.RestartLevel);
                playerRef.playerIsDead = true;

            }
        }
    }
    public void Start()
    {
        //Grab mainObject prefab to access managers effectively
        mainObject = GameObject.FindGameObjectsWithTag("MainObject")[0];
        if (GameObject.FindGameObjectsWithTag("MainObject").Length > 1)
        {
            GameObject[] mainObjectList = GameObject.FindGameObjectsWithTag("MainObject");
            for (int i = 0; i < mainObjectList.Length; ++i)
            {
                if (mainObjectList[i].GetComponent<GameStateManager>().objectSaved)
                    mainObject = mainObjectList[i];
            }
        }

        // Notice, these are attached to the MainObject
        gameStateManagerRef = mainObject.GetComponent<GameStateManager>();
        animationManagerRef = gameStateManagerRef.GetAnimationManager();
        inputManagerRef = gameStateManagerRef.GetInputManager();
        worldCollisionRef = gameStateManagerRef.GetWorldCollision();

        // This script is attached to the player, so we use 'gameObject' here
        controllerRef = gameObject.GetComponent<TWCharacterController>();

        paperObject = GameObject.FindGameObjectWithTag("background");
    }
    // Use this for initialization
    void Start()
    {
        GameObject mainObject = GameObject.FindGameObjectsWithTag("MainObject")[0];

        if (GameObject.FindGameObjectsWithTag("MainObject").Length > 1)
        {
            GameObject[] mainObjectList = GameObject.FindGameObjectsWithTag("MainObject");
            for (int i = 0; i < mainObjectList.Length; ++i)
            {
                if (mainObjectList[i].GetComponent<GameStateManager>().objectSaved)
                    mainObject = mainObjectList[i];
            }
        }

        // Ensures all necessary scripts are added for the MainObject
        gameStateManagerRef = mainObject.GetComponent<GameStateManager>();
        gameStateManagerRef.EnsureGameScriptsAdded();
        worldCollisionRef = gameStateManagerRef.GetWorldCollision();
        playerRef = GameObject.FindGameObjectWithTag("Player").GetComponent<TWCharacterController>();

        //Init local reference - J.C.
        TearManagerLocalRef = GameObject.FindGameObjectWithTag("TearManager").GetComponent<TearManager>();
    }
    // Update is called once per frame
    void Update()
    {
        if (gameStateManagerRef.inGame)
        {
            if (charController == null)
            {
                if (GameObject.FindGameObjectWithTag("Player"))
                    charController = GameObject.FindGameObjectWithTag("Player").GetComponent<TWCharacterController>();
            }
        }

        //added this because inputmanger wasn't working for me, just comment all of this out if you know how to fix it
        //douglas
        //if(Input.touchCount == 1)
        //{
        //    if (ReturnTouchType() == TouchType.SWIPE)
        //    {
        //        if (charController != null)
        //            charController.Jump();
        //    }
        //    else if (Input.deviceOrientation == DeviceOrientation.LandscapeLeft)
        //    {
        //        if (Input.touches[0].position.x > 800.0f)
        //            charController.horizontalMove(5);
        //        else if (Input.touches[0].position.x < 200.0f)
        //            charController.horizontalMove(-5);
        //    }
        //    else if (Input.deviceOrientation == DeviceOrientation.LandscapeRight)
        //    {
        //        if (Input.touches[0].position.x > 800.0f)
        //            charController.horizontalMove(-5);
        //        else if (Input.touches[0].position.x < 200.0f)
        //            charController.horizontalMove(5);
        //    }
        //    else if (Input.deviceOrientation == DeviceOrientation.Portrait)
        //    {
        //        if (Input.touches[0].position.y > 400.0f)
        //            charController.horizontalMove(5);
        //        else if (Input.touches[0].position.y < 100.0f)
        //            charController.horizontalMove(-5);
        //    }

        //    else if (Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown)
        //    {
        //        if (Input.touches[0].position.y > 400.0f)
        //            charController.horizontalMove(-5);
        //        else if (Input.touches[0].position.y < 100.0f)
        //            charController.horizontalMove(5);
        //    }
        //}
        //both lines here are placeholders for testing
        //		TouchType blah = ReturnTouchType();
        //		if(blah != TouchType.NONE){
        //			Debug.Log(blah.ToString());
        //			List<Vector2> lol = GetFingerPositions();
        //			for(int i = 0; i < lol.Count; i++){
        //				Debug.Log ("Finger " + i + " position: " + lol[i].x + " , " + lol[i].y);
        //			}
        //		}
    }
    /// <summary>
    /// Start this instance.
    /// </summary>
    private void Start()
    {
        // Init manger refs
        touchController = GameObject.FindGameObjectWithTag("MainObject").GetComponent<TouchController>();
        gameStateManagerRef = GameObject.FindGameObjectWithTag("MainObject").GetComponent<GameStateManager>();
        gameStateManagerRef = GameObject.FindGameObjectWithTag("MainObject").GetComponent<GameStateManager>();
        inputManagerRef = GameObject.FindGameObjectWithTag("MainObject").GetComponent<InputManager>();
        soundManagerRef = GameObject.FindGameObjectWithTag("MainObject").GetComponent<SoundManager>();

        PlayerObjectRef = GameObject.FindGameObjectWithTag("Player");
        twCharController = PlayerObjectRef.GetComponent<TWCharacterController>();
        unfoldBorderObjectRef = GameObject.FindGameObjectWithTag("unfoldborder");

        //Initialize the tornPlatform list which will be modified
        //during runtime when player triggeres tearing mechanic
        TornPlatforms = new List<GameObject>();

        //Initialize the centerPosition dictionary
        centerPositions = new Dictionary<GameObject, Vector3>();

        //Initialize the list storing the tearable objects which need
        //to be parented to the world cut piece
        objectsBelongingToCutPiece = new List<GameObject>();

        //Initialize decalObject dictionary storeing every decal object for proper updating and cleanup
        decalObjects = new Dictionary<GameObject, int>();

        //Create new dictionary for platforms original positions
        originalPlatformPositions = new Dictionary<GameObject, Vector3>();

        //Init
        haveCheckedDecalCollision = new Dictionary<GameObject, bool>();

        GameObject[] platforms = GameObject.FindGameObjectsWithTag("Platform");
        Platforms = new List<GameObject>();
        foreach(GameObject go in platforms)
        {
            Platforms.Add(go);
        }
        platforms = null;

        //Store original positions of paltforms
        foreach(GameObject go in Platforms)
        {
            originalPlatformPositions.Add(go, go.transform.position);
        }

        OriginalPlatformTopology = new Dictionary <GameObject, int[]>();
        foreach(GameObject go in Platforms)
        {
            OriginalPlatformTopology.Add(go, go.GetComponent<MeshFilter>().mesh.triangles);
        }
        OriginalPlatformTopology.Add(MainStartingWorldPaper, MainStartingWorldPaper.GetComponent<MeshFilter>().mesh.triangles);

        //init local ref
        TearLimitBounds = GameObject.FindGameObjectWithTag("TearLimit");

        CreateDecalPool();
    }
    void Start()
    {
        // the following is now needed due to the prefab of 'MainObject'
        GameObject mainObject = GameObject.FindGameObjectsWithTag("MainObject")[0];
        gameStateManagerRef = mainObject.GetComponent<GameStateManager>();
        gameStateManagerRef.EnsureCoreScriptsAdded();
        screenManagerRef = mainObject.GetComponent<ScreenManager>();
        soundManagerRef = mainObject.GetComponent<SoundManager>();
        characterControllerRef = GameObject.FindGameObjectWithTag("Player").GetComponent<TWCharacterController>();
        mainCamera = GameObject.FindGameObjectWithTag("MainCamera");

        // get a reference to the tear manager
        tearManager = GameObject.FindGameObjectWithTag("TearManager").GetComponent<TearManager>();

        // get a reference to fold
        fold = GameObject.FindGameObjectWithTag("FoldObject").GetComponent<Fold>();

        LvlGoalCoreRef = GameObject.FindGameObjectWithTag("GoalCore").GetComponent<LvlGoalCore>();

        // Get the global variables reference.
        GameObject gVar = GameObject.FindGameObjectsWithTag("globalVariables")[0];
        globalVariables = gVar.GetComponent<GVariables>();

        // if the goal is on the backside move necessary components by offset to be at correct location.
        if(goalOnBackSide){
            // change the box collider on the goal core
            Vector3 GoalCollider = this.gameObject.GetComponent<BoxCollider>().center;
            GoalCollider = new Vector3(GoalCollider.x, GoalCollider.y, GoalCollider.z + (offset*2f));
            this.gameObject.GetComponent<BoxCollider>().center = GoalCollider;

            // change the graphics child object position
            Vector3 GraphicTrans = transform.FindChild("Graphics").transform.position;
            GraphicTrans = new Vector3(GraphicTrans.x, GraphicTrans.y, GraphicTrans.z + offset);
            transform.FindChild("Graphics").transform.position = GraphicTrans;

            // change the goal core child object.
            Vector3 GoalCoreTrans = transform.FindChild("GoalCore").transform.position;
            GoalCoreTrans = new Vector3(GoalCoreTrans.x, GoalCoreTrans.y, GoalCoreTrans.z + offset);
            transform.FindChild("GoalCore").transform.position = GoalCoreTrans;
        }
        buttonCamera = GameObject.FindGameObjectWithTag("button");
    }
    public void Update()
    {
        if(gameStateManagerRef.inGame)
        {
            if(GameObject.FindGameObjectWithTag("Player"))
                if(characterController == null)
                    characterController = GameObject.FindGameObjectWithTag("Player").GetComponent<TWCharacterController>();

            if(ifInGame)
            {
                ifInGame = false;
                chooseAnimation(currentAnim);
            }
        }
    }
    // Initializes any references that were not set in Start().
    private void InitRemainingRefs()
    {
        //if(!allLoaded){
            if(!paperObject){
                paperObject = GameObject.FindGameObjectWithTag("background");
            }

            if(!tearBorder){
                tearBorder = GameObject.FindGameObjectWithTag("DeadSpace");
                foldBorder = GameObject.FindGameObjectWithTag("foldborder");
                unfoldBorder = GameObject.FindGameObjectWithTag("unfoldborder");
                unfoldBlocker = GameObject.FindGameObjectWithTag("RayTraceBlocker");
                moveBorder = GameObject.FindGameObjectWithTag("insideBorder");

                menuButton = GameObject.Find("MenuButton_Prefab");
                restartButton = GameObject.Find("RestartButton_Prefab");
            }

            if(GameObject.FindGameObjectWithTag("TearManager")){
                tearManagerRef = GameObject.FindGameObjectWithTag("TearManager").GetComponent<TearManager>();
            }

            if(GameObject.FindGameObjectWithTag("Player")){
                playerObject = GameObject.FindGameObjectWithTag("Player");
                unfoldCollisionRef = playerObject.GetComponent<UnfoldCollision>();
                // JUSTIN, CHAR CONTR IS ATTACHED TO THE PLAYER, NOT MAIN OBJECT - D.A.
                controllerRef = GameObject.FindGameObjectWithTag("Player").GetComponent<TWCharacterController>();
            }

            if(GameObject.FindGameObjectWithTag("FoldObject")){
                foldRef = GameObject.FindGameObjectWithTag("FoldObject").GetComponent<Fold>();
            }
            if(paperObject && tearManagerRef && playerObject && foldRef){
                allLoaded = true;
            }
        //}
    }
    private void InitGameRefs()
    {
        if (playerObject)
        {
            if (characterController == null)
                characterController = playerObject.GetComponent<TWCharacterController>();
        }

        else
        {
            playerObject = GameObject.FindGameObjectWithTag("Player");
        }

        if (characterController)
            if (!characterController.playerIsDead && deathAnimComplete)
                deathAnimComplete = false;
    }