Example #1
0
        public static string SaveGame()
        {
            CameraController cameraController = Camera.main.GetComponent <CameraController>();

            string saveName = DateTime.Now.ToString("MM_dd_yyyy_HH_mm_ss");

            QuickSaveWriter quickSaveWriter = QuickSaveWriter.Create(saveName);

            quickSaveWriter.Write(GameInfo.MAP_SEED, GameInfo.mapSeed);
            quickSaveWriter.Write(GameInfo.MAP_SIZE, GameInfo.mapSize);
            quickSaveWriter.Write("playerTeam", GameInfo.playerTeam);
            quickSaveWriter.Write("cameraPosition", Camera.main.transform.position);
            quickSaveWriter.Write("cameraRotation", Camera.main.transform.rotation);

            quickSaveWriter.Write("gold", cameraController.resources[BaseBehavior.ResourceType.Gold]);
            quickSaveWriter.Write("wood", cameraController.resources[BaseBehavior.ResourceType.Wood]);
            quickSaveWriter.Write("food", cameraController.resources[BaseBehavior.ResourceType.Food]);
            quickSaveWriter.Write("favor", cameraController.resources[BaseBehavior.ResourceType.Favor]);

            int index = 0;

            foreach (GameObject unitObject in GameObject.FindGameObjectsWithTag("Building").Concat(GameObject.FindGameObjectsWithTag("Unit")))
            {
                BaseBehavior unitBaseBehavior = unitObject.GetComponent <BaseBehavior>();
                unitBaseBehavior.Save(ref quickSaveWriter, index);
                index++;
            }
            quickSaveWriter.Write("indexCount", index);

            TerrainGenerator terrainGenerator = Terrain.activeTerrain.GetComponent <TerrainGenerator>();

            // Blind texture
            Color[] colors    = terrainGenerator.blindTexture2D.GetPixels();
            int[]   blindInfo = new int[colors.Length];
            for (int i = 0; i < colors.Length; i++)
            {
                blindInfo[i] = colors[i] == Color.black ? 1 : 0;
            }
            quickSaveWriter.Write("blindTextureData", blindInfo);

            // Binds
            for (int number = 1; number <= 9; number++)
            {
                int[] binds = new int[cameraController.unitsBinds[KeyCode.Alpha0 + number].Count];
                int   i     = 0;
                foreach (GameObject bindObject in cameraController.unitsBinds[KeyCode.Alpha0 + number])
                {
                    BaseBehavior bindObjectBaseBehavior = bindObject.GetComponent <BaseBehavior>();
                    binds[i] = bindObjectBaseBehavior.uniqueId;
                    i++;
                }
                quickSaveWriter.Write(new StringBuilder(15).AppendFormat("{0}_{1}", number, "bind").ToString(), binds);
            }

            quickSaveWriter.Commit();

            return(saveName);
        }