Beispiel #1
0
    //load game
    public void Load()
    {
        if (File.Exists(savePath))
        {
            //I/O
            FileStream file = File.Open(savePath, FileMode.Open);

            //load part
            PlayerData playerData = (PlayerData)bf.Deserialize(file);
            file.Close();

            score = playerData.score;
            PlayerPrefs.SetInt("CurrentScore", score);

            float[] c = playerData.coords;
            coords = new Vector3(c[0], c[1], c[2]);
            player.transform.position = coords;

            /*
             * Sample Hierarchy of GameObject Tile
             * to change the checkpoint image
             *  Tile
             *      -> Obstacles
             *      -> Checkpoint1
             *          -> CheckpointScript
             *
             * Process:
             *  - Find the child gameobject using the name
             *  - Call the ChangeState() of the child script
             */

            //name of the loaded checkpoint
            checkpointTag = playerData.checkpointTag;

            //gets the list its children
            Transform[] childrenObj = tileObject.GetComponentsInChildren <Transform>();

            foreach (Transform obj in childrenObj)
            {
                //if name matches with checkpointTag, change the state
                if (obj.name == checkpointTag)
                {
                    CheckpointScript objScript = obj.GetComponent <CheckpointScript>();
                    objScript.ChangeState();
                    break;
                }
            }
        }
    }
    // triggers when player touches the checkpoint
    private void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.layer == 10)
        {
            //gets SpriteRenderer and changes the image
            checkScript = col.GetComponent <CheckpointScript>();

            //activate these scripts if the checkpoint was not saved yet
            if (!checkScript.isTouched)
            {
                checkScript.ChangeState();
                GameplayScript.current.AddScore(scores["checkpoint"]);
                GameplayScript.current.SetCheckpoint(col.transform.position, col.name);
                StartCoroutine(checkScript.Notify());
                Debug.Log("Checkpoint Saved!");
            }
        }
    }
    //load game
    public void Load()
    {
        if (File.Exists(saveLoad.savePath))
        {
            //I/O
            FileStream file = File.Open(saveLoad.savePath, FileMode.Open);

            //load part
            PlayerData playerData = (PlayerData)saveLoad.bf.Deserialize(file);
            file.Close();
            MapName savedMap = (MapName)playerData.map;

            //don't load if the data is for a different map
            if (savedMap == 0 || savedMap.Equals(mapName))
            {
                score = playerData.score;
                PlayerPrefs.SetInt("CurrentScore", score);

                float[] c = playerData.coords;
                coords = new Vector3(c[0], c[1], c[2]);
                player.transform.position = coords;

                var playerMove = player.GetComponent <PlayerMovement>();
                AbilityProcessor.Fetch((PlayerAbilities)playerData.abilityType, playerMove);

                ProjectileProcessor.SetProjectileName((PlayerProjectiles)playerData.projectileType);

                /*
                 * Sample Hierarchy of GameObject Tile
                 * to change the checkpoint image
                 *  Tile
                 *      -> Obstacles
                 *      -> Checkpoint1
                 *          -> CheckpointScript
                 *
                 * Process:
                 *  - Find the child gameobject using the name
                 *  - Call the ChangeState() of the child script
                 */

                //name of the loaded checkpoint
                checkpointTag = playerData.checkpointTag;

                //gets the list its children
                Transform[] childrenObj = tileObject.GetComponentsInChildren <Transform>();

                foreach (Transform obj in childrenObj)
                {
                    //if name matches with checkpointTag, change the state
                    if (obj.name == checkpointTag)
                    {
                        CheckpointScript objScript = obj.GetComponent <CheckpointScript>();
                        objScript.ChangeState();
                        break;
                    }
                }
            }
            else
            {
                Debug.Log("Data is for the different map. Data is not loaded");
            }
        }
    }