private Block CreateBlock() { BlockSaveData saveData = ScriptableObject.CreateInstance <BlockSaveData> (); saveData.id = blockID; saveData.name = "Block_" + blockID; saveData.tileColor = currentColor; saveData.slotPosition = Vector3.zero; saveData.hintPosition = mapTiles[0].transform.localPosition; saveData.tilePositions = new List <Vector3> (); foreach (MapTile mapTile in mapTiles) { saveData.tilePositions.Add(mapTile.transform.localPosition - saveData.hintPosition); } MapBlock block = GameObject.Instantiate <MapBlock> (Map.Instance.mapBlockPrefab); block.Init(saveData); blocks.Add(block.id, block); blockID++; mapTiles = new List <MapTile>(); currentColor = colors[Random.Range(0, colors.Length)]; return(block); }
Dictionary <Vector3Int, BlockSaveData> ConvertBlocksGrid(GameObject[,,] _grid, Vector3Int gridSize) { Dictionary <Vector3Int, BlockSaveData> blockGrid = new Dictionary <Vector3Int, BlockSaveData>(); foreach (GameObject building in _grid) { BlockSaveData blockData = new BlockSaveData(); if (building != null) { Block blockLink = building.GetComponent <Block>(); if (blockLink != null) { Vector3Int coords = blockLink.gridCoordinates; blockData.id = blockLink.scheme.ID; blockData.states = new List <int>(); foreach (KeyValuePair <State, StateBehavior> state in blockLink.states) { blockData.states.Add((int)state.Key); } blockGrid[coords] = blockData; } } } return(blockGrid); }
public BlockSaveData GetSaveData() { BlockSaveData saveData = ScriptableObject.CreateInstance <BlockSaveData> (); saveData.name = name; saveData.id = id; saveData.tileColor = tileColor; saveData.tilePositions = new List <Vector3>(); foreach (BlockTile blockTile in blockTiles) { saveData.tilePositions.Add(blockTile.transform.localPosition); } saveData.slotPosition = slot.transform.localPosition; if (null != hint) { saveData.hintPosition = hint.transform.localPosition; } else { saveData.hintPosition = saveData.slotPosition; } return(saveData); }
public override void Init(BlockSaveData saveData) { base.Init(saveData); transform.SetParent(Map.Instance.hints, false); transform.localPosition = saveData.hintPosition; enableTouchInput = false; name = saveData.name + "_Hint"; Color color = saveData.tileColor; color.a = 0.5f; tileColor = color; sortingOrder = (int)Block.SortingOrder.Hint; gameObject.SetActive(false); }
public virtual void Init(BlockSaveData saveData) { blockTiles = new List <BlockTile>(); foreach (Vector3 tilePosition in saveData.tilePositions) { BlockTile blockTile = GameObject.Instantiate <BlockTile> (blockTilePrefab); blockTile.transform.localPosition = tilePosition; blockTile.Init(this); blockTiles.Add(blockTile); } id = saveData.id; name = saveData.name; tileColor = saveData.tileColor; }
public static Block Load(BlockSaveData data, GameObject container) { if (current_id <= data.Id) { current_id = data.Id + 1; } Block prototype = BlockPrototypes.Instance.Get(data.Internal_Name); Block block = new Block(new Coordinates(data.Coordinates), prototype, container); if (data.Rotation != null && (data.Rotation.X != 0 || data.Rotation.Y != 0 || data.Rotation.Z != 0)) { block.Rotate(data.Rotation.X, data.Rotation.Y, data.Rotation.Z); } block.Base_Pilar_Support = data.Base_Pilar_Support; return(block); }
public override void Init(BlockSaveData saveData) { base.Init(saveData); sortingOrder = (int)Block.SortingOrder.Idle; mapTiles = new List <MapTile>(); transform.SetParent(Map.Instance.blocks, false); transform.localPosition = saveData.tilePositions [0]; slot = GameObject.Instantiate <SlotBlock>(Map.Instance.slotBlockPrefab); slot.Init(saveData); if (saveData.hintPosition != saveData.slotPosition) { hint = GameObject.Instantiate <HintBlock> (Map.Instance.hintBlockPrefab); hint.Init(saveData); } transform.position = slot.transform.position; float minY = float.MaxValue; foreach (BlockTile blockTile in blockTiles) { minY = Mathf.Min(minY, blockTile.transform.position.y); } clickDeltaPosition = transform.position; clickDeltaPosition.y = Mathf.Abs(transform.position.y - minY); if (false == Map.Instance.enabled) { colliderSize = new Vector3(2.0f, 2.0f, 1.0f); } transform.localScale = slot.transform.localScale; initPosition = transform.position; mapTileCount = 0; if (true == Map.Instance.editMode && null != hint) { transform.position = hint.transform.position; transform.localScale = Vector3.one; initPosition = hint.transform.position; } }
public override void Init(BlockSaveData saveData) { base.Init(saveData); sortingOrder = (int)Block.SortingOrder.Slot; tileColor = Color.gray; name = saveData.name + "_Slot"; transform.SetParent(Map.Instance.slots, false); transform.localScale = new Vector3(Map.Instance.blockSlotScale, Map.Instance.blockSlotScale, 1.0f); transform.localPosition = saveData.slotPosition; foreach (BlockTile tile in blockTiles) { tile.transform.localRotation = new Quaternion(0.0f, 0.0f, 180.0f, 0.0f); } if (true == Map.Instance.editMode) { enableTouchInput = true; } else { enableTouchInput = false; } }
/////////////////////////////// /// /// Main function to write savegame /// This is the one you call publicly /// public IEnumerator WriteSaveData(SaveData saveData, Action callback) { double timeStart = Time.time; yield return(null); // Step 0 - Initialize writer Writer writer = new Writer(); if (!writer.OpenSave(Paths.GetSaveFile(GameManager.instance.cityManager.cityName))) { yield break; } Logger.Info("Saving..."); // Step 1 - Miscellaneous data Logger.Debug("Saving misc data..."); writer.WriteUInt8(saveVersion); writer.WriteString(saveData.cityName); writer.WriteString(saveData.playerName); writer.WriteFloat(saveData.timeOfDay); writer.WriteUInt16(saveData.cyclesPassed); writer.WriteBool(saveData.isTutorialRun); // Step 2 - Main grid Logger.Debug("Saving main game grid.."); writer.WriteVector3UInt8(saveData.gridSize); writer.WriteUInt16(saveData.blockGrid.Count); foreach (KeyValuePair <Vector3Int, BlockSaveData> data in saveData.blockGrid) { BlockSaveData blockData = data.Value; writer.WriteVector3UInt8(data.Key); writer.WriteUInt8(blockData.id); writer.WriteUInt8(blockData.states.Count); Logger.Debug("Saved blockData ID#" + blockData.id + " at position " + data.Key.ToString()); foreach (int state in blockData.states) { writer.WriteUInt8(state); } yield return(null); } // Step 3 - Bridges Logger.Debug("Saving " + saveData.bridges.Count + " bridges..."); writer.WriteUInt8(saveData.bridges.Count); foreach (KeyValuePair <Vector3Int, Vector3Int> bridge in saveData.bridges) { writer.WriteVector3UInt8(bridge.Key); writer.WriteVector3UInt8(bridge.Value); yield return(null); } // Step 4 - Populations Logger.Debug("Saving " + saveData.popSaveData.Count + " population informations..."); writer.WriteUInt8(saveData.popSaveData.Count); foreach (PopulationSaveData popSD in saveData.popSaveData) { writer.WriteUInt8(popSD.popId); writer.WriteFloat(popSD.riotRisk); writer.WriteFloat(popSD.averageMood); writer.WriteUInt8(popSD.moodModifiers.Count); foreach (MoodModifier moodMod in popSD.moodModifiers) { writer.WriteFloat(moodMod.amount); writer.WriteUInt8(moodMod.cyclesRemaining); writer.WriteUInt16(moodMod.eventId); } writer.WriteUInt8(popSD.foodModifiers.Count); foreach (FoodModifier foodMod in popSD.foodModifiers) { writer.WriteFloat(foodMod.amount); writer.WriteUInt8(foodMod.cyclesRemaining); } writer.WriteUInt16(popSD.citizens.Count); foreach (CitizenSaveData cit in popSD.citizens) { writer.WriteString(cit.name); yield return(null); } } // Step 5 - Locks Logger.Debug("Saving " + saveData.lockedBuildings.Count + " locks..."); writer.WriteUInt8(saveData.lockedBuildings.Count); foreach (int buildingId in saveData.lockedBuildings) { writer.WriteUInt8(buildingId); yield return(null); } // Step 6 - Population type list Logger.Debug("Saving " + saveData.populationTypeIds.Count + " population types..."); writer.WriteUInt8(saveData.populationTypeIds.Count); foreach (int popId in saveData.populationTypeIds) { writer.WriteUInt8(popId); yield return(null); } // Step 7 - Bulletin pool Logger.Debug("Saving the bulletin pool and current bulletin..."); writer.WriteUInt16(saveData.bulletinList.Count); foreach (BulletinsManager.Bulletin bulletin in saveData.bulletinList) { writer.WriteUInt8(bulletin.eventId); writer.WriteUInt16(bulletin.id); writer.WriteUInt16(bulletin.minCycle); } // Current bulletin writer.WriteUInt8(saveData.currentBulletin.eventId); writer.WriteUInt16(saveData.currentBulletin.id); writer.WriteUInt16(saveData.currentBulletin.minCycle); // Step 8 - Event pool Logger.Debug("Saving the event pool..."); writer.WriteUInt16(saveData.eventsPool.Count); foreach (EventManager.EventMarker marker in saveData.eventsPool) { writer.WriteUInt8(marker.eventId); writer.WriteUInt16(marker.minCycle); writer.WriteFloat(marker.time); } // Last step - Close handler; writer.Close(); Logger.Info("Done in " + (Time.time - timeStart).ToString("n2") + " seconds"); if (callback != null) { callback.Invoke(); } yield return(true); }
/// /// ////////////////////////////// public IEnumerator ReadSaveData(string cityName, Action callback = null) { double timeStart = Time.time; yield return(null); // Step 0 - Initialize reader Reader reader = new Reader(); if (!reader.OpenSave(Paths.GetSaveFile(cityName))) { yield break; } Logger.Info("Reading..."); SaveData diskSaveData = new SaveData(); // Step 1 - Miscellaneous data Logger.Debug("Reading headers..."); int version = reader.ReadUInt8(); if (version != saveVersion) { Logger.Warn("Expected version " + saveVersion.ToString() + ", got version " + version.ToString()); } diskSaveData.cityName = reader.ReadString(); diskSaveData.playerName = reader.ReadString(); diskSaveData.timeOfDay = reader.ReadFloat(); diskSaveData.cyclesPassed = reader.ReadUInt16(); diskSaveData.isTutorialRun = reader.ReadBool(); // Step 2 - Main grid Logger.Debug("Reading grid..."); diskSaveData.gridSize = reader.ReadVector3UInt8(); diskSaveData.blockGrid = new Dictionary <Vector3Int, BlockSaveData>(); int count = reader.ReadUInt16(); for (int i = 0; i < count; i++) { BlockSaveData blockData = new BlockSaveData(); Vector3Int position = reader.ReadVector3UInt8(); blockData.id = reader.ReadUInt8(); int statesCount = reader.ReadUInt8(); blockData.states = new List <int>(); for (int j = 0; j < statesCount; j++) { blockData.states.Add(reader.ReadUInt8()); } Logger.Debug("Reading block id : " + blockData.id + ":" + blockData.states.Count.ToString() + " at position " + position.ToString()); diskSaveData.blockGrid[position] = blockData; yield return(null); } // Step 3 - Bridges int bridgesCount = reader.ReadUInt8(); Logger.Debug("Reading " + bridgesCount + " bridges..."); diskSaveData.bridges = new List <KeyValuePair <Vector3Int, Vector3Int> >(); for (int i = 0; i < bridgesCount; i++) { diskSaveData.bridges.Add(new KeyValuePair <Vector3Int, Vector3Int>(reader.ReadVector3UInt8(), reader.ReadVector3UInt8())); yield return(null); } // Step 4 - Population int popCount = reader.ReadUInt8(); Logger.Debug("Reading " + popCount + " populations..."); diskSaveData.popSaveData = new List <PopulationSaveData> (); for (int i = 0; i < popCount; i++) { int popId = reader.ReadUInt8(); float riotRisk = reader.ReadFloat(); float mood = reader.ReadFloat(); int moodModCount = reader.ReadUInt8(); List <MoodModifier> mods = new List <MoodModifier>(); for (int j = 0; j < moodModCount; j++) { mods.Add(new MoodModifier() { amount = reader.ReadFloat(), cyclesRemaining = reader.ReadUInt8(), eventId = reader.ReadUInt16(), }); } int foodModCount = reader.ReadUInt8(); List <FoodModifier> foods = new List <FoodModifier>(); for (int j = 0; j < foodModCount; j++) { foods.Add(new FoodModifier() { amount = reader.ReadFloat(), cyclesRemaining = reader.ReadUInt8(), }); } int citizenCount = reader.ReadUInt16(); List <CitizenSaveData> cits = new List <CitizenSaveData>(); for (int j = 0; j < citizenCount; j++) { cits.Add(new CitizenSaveData() { name = reader.ReadString() }); yield return(null); } diskSaveData.popSaveData.Add(new PopulationSaveData() { popId = popId, riotRisk = riotRisk, averageMood = mood, moodModifiers = mods, foodModifiers = foods, citizens = cits }); } // Step 5 - Locks int locksCount = reader.ReadUInt8(); Logger.Debug("Reading " + locksCount + " locks..."); diskSaveData.lockedBuildings = new List <int>(); for (int i = 0; i < locksCount; i++) { diskSaveData.lockedBuildings.Add(reader.ReadUInt8()); yield return(null); } // Step 6 - Population type list int typesCount = reader.ReadUInt8(); Logger.Debug("Reading " + typesCount + " population types..."); for (int i = 0; i < typesCount; i++) { diskSaveData.populationTypeIds.Add(reader.ReadUInt8()); } // Step 7 - Bulletin pool Logger.Debug("Reading the bulletin pool and current bulletin..."); int bulletinCount = reader.ReadUInt16(); for (int i = 0; i < bulletinCount; i++) { int eventId = reader.ReadUInt8(); diskSaveData.bulletinList.Add( new BulletinsManager.Bulletin(reader.ReadUInt16(), reader.ReadUInt16()) { eventId = eventId } ); } int currentBulletinEId = reader.ReadUInt8(); diskSaveData.currentBulletin = new BulletinsManager.Bulletin( reader.ReadUInt16(), reader.ReadUInt16() ) { eventId = currentBulletinEId }; // Step 8 - Event pool Logger.Debug("Reading the event pool..."); int eventPoolCount = reader.ReadUInt16(); for (int i = 0; i < eventPoolCount; i++) { diskSaveData.eventsPool.Add(new EventManager.EventMarker() { eventId = reader.ReadUInt8(), minCycle = reader.ReadUInt16(), time = reader.ReadFloat() }); } // Last step - Close handler; Logger.Debug("Closing handler..."); reader.Close(); Logger.Info("Done in " + (Time.time - timeStart).ToString("n2") + " seconds"); loadedData = diskSaveData; if (callback != null) { callback(); } yield return(true); }