public void Execute() { if (!PooledObjectManager.Instance.UsePooledObject(_playerConfig.UnitData.UnitPrefabId, out PooledObject pooledObject)) { Debug.LogError($"[{nameof(SpawnPlayerAction)}]: Could not retrieve player prefab with id {_playerConfig.UnitData.UnitPrefabId}!"); OnComplete?.Invoke(); return; } PlayerUnit player = pooledObject as PlayerUnit; if (player == null) { Debug.LogError($"[{nameof(SpawnPlayerAction)}]: Pooled Object retrieved with id {_playerConfig.UnitData.UnitPrefabId} was not of type {nameof(PlayerUnit)}!"); OnComplete?.Invoke(); return; } PlayerInitializationData initData = new PlayerInitializationData() { OverrideUniqueId = _playerConfig.UnitData.UnitPrefabId, UnitData = _playerConfig.UnitData }; player.Initialize(initData); player.Spawn(); OnComplete?.Invoke(); }
/// <summary> /// Goes through the JSON mapFile to find any "3"'s to spawn a sniper unit at that spot in the grid and initialize that unit /// </summary> protected override void SpawnUnits() { int length = GridManagerScript.instance.length; int width = GridManagerScript.instance.width; JsonData data = JSONReadManager.GetItemData("mapFile"); GameObject holder = new GameObject("Player Units"); for (int i = 0; i < length; ++i) { for (int j = 0; j < width; ++j) { int unitID = (int)data["array"][i][j]; GameObject obj = null; int x = j; int z = length - 1 - i; if (unitID == 5) { obj = Instantiate(playerUnitPrefabs[0], holder.transform); } else if (unitID == 6) { obj = Instantiate(playerUnitPrefabs[1], holder.transform); } else if (unitID == 7) { obj = Instantiate(playerUnitPrefabs[2], holder.transform); } if (obj != null) { PlayerUnit unit = obj.GetComponent <PlayerUnit>(); unit.Initialize(this, nextUnitID); ++nextUnitID; TileScript tile = grid[x, z].GetComponent <TileScript>(); unit.Tile = tile; unitList.Add(unit); //Make Obstacles position equal to the grid coordinates given and add half of the size of the mesh renderer.y to not have the obstacle object halfway through the floor obj.transform.position = new Vector3(tile.transform.position.x, 0.0f, tile.transform.position.z); tile.SetOccupy(true); } } } }