Example #1
0
    public void Load()
    {
        BinaryFormatter bf   = new BinaryFormatter();
        FileStream      file = File.Open(Application.persistentDataPath + "/board.dat", FileMode.Open);
        BoardSave       bs   = (BoardSave)bf.Deserialize(file);

        file.Close();

        for (int i = 0; i < bs.floors.Count; i++)
        {
            if (bs.floors[i] != null)
            {
                FloorSaveData floorData    = bs.floors[i];
                FloorData     databaseData = tileDatabase.GetFloorByID(floorData.Id);
                floorPrefab.GetComponent <SpriteRenderer>().sprite = databaseData.Sprite;
                floorPrefab.GetComponent <Floor>().id = floorData.Id;
                floorPrefab.SetActive(floorData.IsActive);

                GameObject instance =
                    Instantiate(floorPrefab, IndexToPosition(floorData.Position), Quaternion.identity) as GameObject;

                instance.transform.SetParent(boardHolder);

                generatedFloorPositions.Add(floorData.Position, instance);
            }
        }

        for (int i = 0; i < bs.walls.Count; i++)
        {
            if (bs.walls[i] != null)
            {
                WallSaveData wallData     = bs.walls[i];
                Wall         wall         = wallPrefab.GetComponent <Wall>();
                WallData     databaseData = tileDatabase.GetWallByID(wallData.Id);

                wallPrefab.GetComponent <SpriteRenderer>().sprite = databaseData.Sprite;
                wallPrefab.SetActive(wallData.IsActive);

                wall.id      = wallData.Id;
                wall.hp      = wallData.CurrentHealth;
                wall.defence = wallData.Defense;
                wall.maxHp   = databaseData.MaxHealth;

                GameObject instance =
                    Instantiate(wallPrefab, IndexToPosition(wallData.Position), Quaternion.identity) as GameObject;

                instance.transform.SetParent(boardHolder);

                generatedObjectPositions.Add(wallData.Position, instance);

                if (!generatedFloorPositions.ContainsKey(wallData.Position))//Keeps the generator from messing up on biomes that generate only walls
                {
                    generatedFloorPositions.Add(wallData.Position, instance);
                }
            }
        }

        for (int i = 0; i < bs.generatedStructurePositions.Count; i++)
        {
            generatedStructurePositions.Add(bs.generatedStructurePositions[i], true);
        }

        for (int i = 0; i < bs.enemies.Count; i++)
        {
            if (bs.enemies[i] != null)
            {
                EnemySaveData enemyData    = bs.enemies[i];
                Wall          wall         = enemyPrefab.GetComponent <Wall>();
                Enemy         enemy        = enemyPrefab.GetComponent <Enemy>();
                EnemyData     databaseData = tileDatabase.GetEnemyByID(enemyData.Id);

                wall.defence         = enemyData.Defense;
                wall.hp              = enemyData.CurrentHealth;
                wall.maxHp           = databaseData.MaxHealth;
                wall.experienceValue = databaseData.Experience;
                wall.id              = enemyData.Id;
                wall.itemDrop        = databaseData.ItemDrop;
                wall.rarity          = databaseData.DropRarity;

                enemy.enemyDesc    = databaseData.EnemyDesc;
                enemy.enemyLevel   = databaseData.EnemyLevel;
                enemy.enemySpeed   = enemyData.EnemySpeed;
                enemy.playerDamage = enemyData.EnemyAttack;
                enemy.turnCounter  = enemyData.TurnCounter;
                enemy.name         = databaseData.Name;

                enemyPrefab.GetComponent <Animator>().runtimeAnimatorController = databaseData.Animation;

                Vector3 position = IndexToPosition(enemyData.Position);

                AddEnemy(position.x, position.y, enemyPrefab);
            }
        }

        for (int i = 0; i < bs.bags.Count; i++)
        {
            if (bs.bags[i] != null)
            {
                Vector3 position = IndexToPosition(bs.bags[i].Position);
                AddPickup((int)position.x, (int)position.y, bs.bags[i].Items);
            }
        }
    }
Example #2
0
    public void Save()
    {
        BinaryFormatter bf   = new BinaryFormatter();
        FileStream      file = File.Create(Application.persistentDataPath + "/board.dat");
        BoardSave       bs   = new BoardSave();

        foreach (KeyValuePair <int, GameObject> pair in generatedFloorPositions)
        {
            if (pair.Value != null)
            {
                FloorSaveData floorData = new FloorSaveData(pair.Value.GetComponent <Floor>().id, pair.Value.activeSelf, pair.Key);

                bs.floors.Add(floorData);
            }
        }
        foreach (KeyValuePair <int, GameObject> pair in generatedObjectPositions)
        {
            if (pair.Value != null)
            {
                if (pair.Value.tag == "Wall")
                {
                    WallSaveData wallData = new WallSaveData();
                    Wall         wall     = pair.Value.GetComponent <Wall>();
                    wallData.CurrentHealth = wall.hp;
                    wallData.Id            = wall.id;
                    wallData.Defense       = wall.defence;
                    wallData.Position      = pair.Key;

                    wallData.IsActive = pair.Value.activeSelf;

                    bs.walls.Add(wallData);
                }
            }
        }
        foreach (KeyValuePair <int, bool> pair in generatedStructurePositions)
        {
            bs.generatedStructurePositions.Add(pair.Key);
        }

        for (int i = 0; i < manager.enemies.Count; i++)
        {
            if (manager.enemies[i] != null)
            {
                EnemySaveData enemyData = new EnemySaveData();
                Enemy         enemy     = manager.enemies[i];
                Wall          wall      = enemy.GetComponent <Wall>();

                enemyData.CurrentHealth = wall.hp;
                enemyData.Defense       = wall.defence;
                enemyData.Id            = wall.id;
                enemyData.Position      = generateKey((int)enemy.transform.position.x, (int)enemy.transform.position.y);
                enemyData.EnemyAttack   = enemy.playerDamage;
                enemyData.EnemySpeed    = enemy.enemySpeed;
                enemyData.TurnCounter   = enemy.turnCounter;

                bs.enemies.Add(enemyData);
            }
        }

        for (int i = 0; i < lootBags.Count; i++)
        {
            if (lootBags[i] != null)
            {
                Pickup          pickup   = lootBags[i].GetComponent <Pickup>();
                LootbagSaveData saveData = new LootbagSaveData(generateKey((int)lootBags[i].transform.position.x, (int)lootBags[i].transform.position.y), pickup.itemId);
                bs.bags.Add(saveData);
            }
        }

        bf.Serialize(file, bs);
        file.Close();
    }