// Load stage private void Load(StageData stageData) { // Unload Unload(); // Set instance instance = this; // Get stage data data = stageData; // Set size width = tileSize * data.columns; height = tileSize * data.rows; Debug.Log("GRID MANAGER - LOAD STAGE: " + data.stageID); // Generate tiles tiles = new Dictionary <int, GridTile>(); foreach (TileData tileDatum in data.tiles) { // Ensure unique index if (tiles.ContainsKey(tileDatum.tileIndex)) { Debug.LogError("GRID MANAGER - CANNOT HAVE MULTIPLE TILES FOR INDEX: " + tileDatum.tileIndex); continue; } // Get tile prefab GridTile prefab = tileDatum.tilePrefab >= 0 && tileDatum.tilePrefab < tilePrefabs.Length ? tilePrefabs[tileDatum.tilePrefab] : null; if (prefab == null) { Debug.LogError("GRID MANAGER - NO TILE FOR PREFAB INDEX: " + tileDatum.tilePrefab); continue; } // Load tile instance GridTile inst = Pool.instance.Load(prefab.gameObject).GetComponent <GridTile>(); inst.movable = null; if (inst == null) { Debug.LogError("GRID MANAGER - CANNOT INSTANTIATE TILE PREFAB: " + prefab.gameObject.name); continue; } // Set position Transform tileTransform = inst.transform; tileTransform.SetParent(transform); tileTransform.localPosition = GetTilePosition(tileDatum.tileIndex); // Load data inst.LoadData(tileDatum); tiles[tileDatum.tileIndex] = inst; } // Generate props List <GameObject> winList = new List <GameObject>(); props = new GridProp[data.props.Length]; for (int p = 0; p < data.props.Length; p++) { // Prop datum PropData propDatum = data.props[p]; // Ensure tile exists if (!tiles.ContainsKey(propDatum.tileIndex)) { Debug.LogError("GRID MANAGER - NO TILE FOUND FOR INDEX: " + propDatum.tileIndex); continue; } // Get tile GridTile tile = tiles[propDatum.tileIndex]; if (tile.movable != null) { Debug.LogError("GRID MANAGER - CANNOT HAVE MULTIPLE PROPS ON THE SAME TILE FOR INDEX: " + propDatum.tileIndex); continue; } // Get prop prefab GridProp prefab = propDatum.propPrefab >= 0 && propDatum.propPrefab < propPrefabs.Length ? propPrefabs[propDatum.propPrefab] : null; if (prefab == null) { Debug.LogWarning("GRID MANAGER - NO PROP FOR PREFAB INDEX: " + propDatum.propPrefab); prefab = propPrefabs[0]; } // Load prop instance GridProp inst = Pool.instance.Load(prefab.gameObject).GetComponent <GridProp>(); if (inst == null) { Debug.LogError("GRID MANAGER - CANNOT INSTANTIATE PROP PREFAB: " + prefab.gameObject.name); continue; } // Set position Transform trans = inst.transform; trans.SetParent(transform); trans.localPosition = GetTilePosition(propDatum.tileIndex); Direction direction = propDatum.direction; if (direction == Direction.None) { int random = UnityEngine.Random.Range(0, 5) + 1; direction = (Direction)random; } trans.localRotation = GetRotation(direction); // Load data inst.LoadData(propDatum); props[p] = inst; // Add win effect if (propDatum.isWinProp) { GameObject winEffect = Pool.instance.Load(winPropEffect); winEffect.transform.SetParent(inst.transform); winEffect.transform.localPosition = new Vector3(0f, 1f, 0f); winEffect.transform.localRotation = Quaternion.identity; winEffect.transform.localScale = Vector3.one; winList.Add(winEffect); } } winEffects = winList.ToArray(); // Generate characters if (GameManager.instance.players != null && GameManager.instance.players.Count > 0) { if (GameManager.instance.players.Count > data.players.Length) { Debug.Log("STAGE: " + data.stageID + "\nPLAYERS: " + data.players.Length); } characters = new GridCharacter[GameManager.instance.players.Count]; for (int p = 0; p < GameManager.instance.players.Count; p++) { // Player GamePlayer player = GameManager.instance.players[p]; PlayerData playerData = data.players[p]; // Get character data int index = player.characterIndex; if (index < 0 || index >= GameManager.instance.gameData.characters.Length) { index = UnityEngine.Random.Range(0, GameManager.instance.gameData.characters.Length); } GameCharacter character = GameManager.instance.gameData.characters[index]; // Get prefab GridCharacter prefab = GetCharacterPrefab(character.characterID); // Get instance GridCharacter mover = Pool.instance.Load(prefab.gameObject).GetComponent <GridCharacter>(); mover.playerIndex = p; mover.Place(character.characterVariant, playerData.tileIndex, playerData.direction); // Set character characters[p] = mover; } } // Delegate if (onGridLoaded != null) { onGridLoaded(this); } // Set game intro if (GameManager.instance != null) { GameManager.instance.SetState(GameState.GameIntro); } }