// Save the game and world data public void Save(ref Game.SaveDataPacket saveData) { Vector2Int mapSize = mMapGenerationPayload.size; int mapTileCount = mapSize.x * mapSize.y; // World dimensions saveData.WorldWidth = mapSize.x; saveData.WorldHeight = mapSize.y; saveData.WaterMap = new bool[mapTileCount]; // Loop through and save the water map for (int x = 0; x < mapSize.x; x++) { for (int y = 0; y < mapSize.y; y++) { saveData.WaterMap[x + (y * mapSize.x)] = mWaterMap[x, y]; } } saveData.TileData = new Game.TileSaveData[mapTileCount]; // Loop and save the tile data for (int x = 0; x < mapSize.x; x++) { for (int y = 0; y < mapSize.y; y++) { if (!mWaterMap[x, y] && mMap[x][y] != null) { saveData.TileData[x + (y * mapSize.x)] = new Game.TileSaveData(mMap[x][y].TileName, mMap[x][y].Rotation); } else { saveData.TileData[x + (y * mapSize.x)] = new Game.TileSaveData("", 0); } } } }
// Generate the world using the save data private void Generate(Game.SaveDataPacket saveData) { Vector2Int Size = mMapGenerationPayload.size; mMap = new EnvironmentTile[Size.x][]; // Loop through for each tile coordinate for (int x = 0; x < Size.x; ++x) { mMap[x] = new EnvironmentTile[Size.y]; for (int y = 0; y < Size.y; ++y) { bool isWater = mWaterMap[x, y]; EnvironmentTile tile = null; Vector3 position = GetRawPosition(x, y); // Check to see if the tile should be a water tile or not if (isWater) { AddWaterTile(position, ref tile, mMapGenerationPayload.size, x, y); } else { Game.TileSaveData saveInstance = saveData.TileData[x + (y * Size.x)]; AddLandTile(position, ref tile, mMapGenerationPayload.size, x, y, saveInstance.N, saveInstance.R); } } } }
private void GenerateWaterMap(Game.SaveDataPacket saveData) { // Generate a water map that represents what tiles of the map should be water and what ones wont be mWaterMap = new bool[saveData.WorldWidth, saveData.WorldHeight]; for (int x = 0; x < saveData.WorldWidth; x++) { for (int y = 0; y < saveData.WorldHeight; y++) { mWaterMap[x, y] = saveData.WaterMap[x + (y * saveData.WorldWidth)]; } } }
// Generate a new world using the save data public void GenerateWorld(Character Character, GenerationPayload generationPayload, Game.SaveDataPacket saveData) { mMapGenerationPayload = generationPayload; // Load the world save data GenerateWaterMap(saveData); Generate(saveData); SetupConnections(); // Move the player to there last position MovePlayerToStart(Character, mMap[saveData.PlayerX][saveData.PlayerY]); mCharacter = Character; // Load all entities for (int i = 0; i < saveData.Entities.Length; i++) { foreach (Entity e in EntityInstances) { if (saveData.Entities[i].N == e.entityName) { EnvironmentTile tile = mMap[saveData.Entities[i].X][saveData.Entities[i].Y]; Entity ent = GameObject.Instantiate(e, Environment.instance.transform); RegisterEntity(ent); ent.transform.position = tile.Position; ent.transform.rotation = Quaternion.identity; ent.CurrentPosition = tile; break; } } } }