/// <summary> /// Callback triggered when the server has acknowledge the battle results and returned /// a battle summary. /// </summary> /// <param name="data">Summary data</param> private void OnBattleSummarySuccess(BattleSummaryData data) { IsLoading = false; UIManager.OnShowLoadingView(false); // Start respawning WorldService.GetInstance().StartRespawn(LocationId); if (data.winner) { if (data.wonTheGame) { UIManager.OnShowGameVictoryView(); } else { UIManager.OnShowLootResultsDialog("Victory!", data.rewards.items); } } else { UIManager.OnShowLootResultsDialog("Defeat!", data.rewards.items); } // Hide this location until next respawn if (Model != null) { Model.SetActive(false); } }
/// <summary> /// Actual implementation at Update that can be derived and overriden. /// </summary> protected virtual void UpdateImpl() { if (LocationId != null && WorldService.GetInstance().IsRespawning(LocationId)) { RespawingState(); } }
/// <summary> /// Triggered when the server call to get the chest details returns successfully. /// This function sets the chest in respawn mode. /// </summary> /// <param name="data">Rewards data provided by the server.</param> private void OnSuccess(RewardsData data) { IsLoading = false; if (data == null) { // The chest is respawning return; } if (Animator != null) { // Open the chest for now Animator.SetBool("open", true); Animator.SetBool("reset", false); } StartCoroutine(CloseChest()); // Preemptively update the player's data PlayerService.GetInstance().AddToInventory(data.items); // Preemptively start respawning WorldService.GetInstance().StartRespawn(LocationId); UIManager.OnShowLoadingView(false); if (UIManager != null) { UIManager.OnShowLootResultsDialog("Loot!", data.items); } }
/// <summary> /// Triggered by the UI when a new game needs to be created. /// This event listener resets player and world data. /// </summary> public void OnNewGame() { _spawnedGameObjects.Clear(); // Clear the world ClearContainer(PlayableLocationsContainer); _gameStarted = false; PlayerService.GetInstance().Init(new PlayerData()); WorldService.GetInstance().Init(new WorldData()); // Maps data stay the same (we are at the same location)PLAYER_DATA_INITIALIZED, _startupCheckList = new List <string> { PLAYABLE_LOCATIONS_INITIALIZED }; // Reset Player data StartCoroutine(ServerManager.PostPlayerData(null, data => { PlayerService.GetInstance().Init(data); _startupCheckList.Remove(PLAYER_DATA_INITIALIZED); CheckStartConditions(); }, s => { Debug.LogError(s); })); StartCoroutine(ServerManager.DeleteWorldData(data => { // Generate new world UpdateWorldData(LatLng, MaxDistance); }, s => { Debug.LogError(s); })); }
/// <summary> /// Initializes this location with the information associated to the provided location id /// </summary> /// <param name="locationId"></param> public void Init(string locationId) { LocationId = locationId; location = WorldService.GetInstance().GetSpawnLocation(locationId); UIManager = GameObject.FindGameObjectWithTag("UIManager").GetComponent <UIManager>(); ServerManager = GameObject.FindGameObjectWithTag("ServerManager") .GetComponent <ServerManager>(); }
/// <summary> /// Implementation of the action state. /// The code checks the prerequisites for the chest location, and triggers a request /// to the server to get the content of the chest if conditions are met. /// </summary> protected override void ActionState() { if (IsLoading) { return; } base.ActionState(); if (string.IsNullOrEmpty(LocationId)) { Debug.LogError("Incorrect Location Id!"); return; } // Check if this chest is active? // If not show a floating popup with timeout information location = WorldService.GetInstance().GetSpawnLocation(LocationId); if (WorldService.GetInstance().IsRespawning(LocationId)) { UIManager.OnShowLoadingView(false); DateTime t = DateTime.Parse(location.respawnTime); TimeSpan timeLeft = t.Subtract(DateTime.Now); UIManager.OnShowMessageDialog("Chest locked. Time left: ", timeLeft); return; } // Check pre-requisites if (location.keyTypeId == GameConstants.GOLD_KEY && PlayerService.GetInstance().GetNumberOfGoldKeys() >= location.numberOfKeysToActivate) { try { IsLoading = true; StartCoroutine(ServerManager.PostChest(LocationId, OnSuccess, OnError)); } catch (System.Exception e) { IsLoading = false; UIManager.OnShowLoadingView(false); Debug.LogError(e.ToString()); } } else { UIManager.OnShowLoadingView(false); UIManager.OnShowMessageDialog("You need " + location.numberOfKeysToActivate + " Gold Keys. \n You have " + PlayerService.GetInstance().GetNumberOfGoldKeys() + " of them!"); } }
public static WorldService GetInstance() { if (_instance == null) { _instance = new WorldService(); } return(_instance); }
/// <summary> /// Implementation of the action state. /// This function checks if all pre-requisites are met to interact with the tower. /// If all conditions are met, it triggers a call to the server to get battle details. /// </summary> protected override void ActionState() { if (IsLoading) { return; } base.ActionState(); if (string.IsNullOrEmpty(LocationId)) { Debug.LogError("Incorrect PlaceId!"); return; } // Check if this station is active? // If not show a floating popup with timeout information location = WorldService.GetInstance().GetSpawnLocation(LocationId); // Check pre-requisites if (location.keyTypeId == GameConstants.DIAMOND_KEY && PlayerService.GetInstance().GetNumberOfDiamondKeys() >= location.numberOfKeysToActivate) { // All good. Let's request our rewards try { IsLoading = true; StartCoroutine( ServerManager.PostBattleData(LocationId, OnBattleSuccess, OnError)); } catch (System.Exception e) { Debug.LogError("Failed to free the leader in this tower! " + e); IsLoading = false; UIManager.OnShowLoadingView(false); } } else { UIManager.OnShowMessageDialog("You need " + location.numberOfKeysToActivate + " Diamond Keys. \n You have " + PlayerService.GetInstance().GetNumberOfDiamondKeys() + " of them!"); UIManager.OnShowLoadingView(false); } }
/// <summary> /// Creates all new spawn locations for the game from the World Data provided /// by the server. /// </summary> /// <param name="worldData">World Data</param> private void CreateNewLocations(WorldData worldData) { // Render the new data on the map: // Only render the playable locations that are within range of the loaded map. // The API returns all locations within the overlapping cells - which may be way larger // than our map. CreateAssets(WorldService.GetInstance().GetMinions(), MinionPrefab, PlayableLocationsContainer); CreateAssets(WorldService.GetInstance().GetTowers(), TowerPrefab, PlayableLocationsContainer); CreateAssets(WorldService.GetInstance().GetChests(), ChestPrefab, PlayableLocationsContainer); CreateAssets(WorldService.GetInstance().GetEnergyStations(), EnergyStationPrefab, PlayableLocationsContainer); }
/// <summary> /// When the minion is clicked on, we start a new battle if the conditions are met. /// </summary> protected override void ActionState() { if (IsLoading) { return; } base.ActionState(); if (string.IsNullOrEmpty(LocationId)) { Debug.LogError("Incorrect PlaceId!"); UIManager.OnShowLoadingView(false); return; } // Check if this station is active? // If not show a floating popup with timeout information location = WorldService.GetInstance().GetSpawnLocation(LocationId); if (WorldService.GetInstance().IsRespawning(LocationId)) { DateTime t = DateTime.Parse(location.respawnTime); TimeSpan timeLeft = t.Subtract(DateTime.Now); // Hide Minion... until it is respawned UIManager.OnShowLoadingView(false); UIManager.OnShowMessageDialog("Minion in transit", timeLeft); return; } try { IsLoading = true; StartCoroutine(ServerManager.PostBattleData(LocationId, OnBattleSuccess, OnError)); } catch (System.Exception e) { Debug.LogError("Failed to get battle data! " + e); IsLoading = false; UIManager.OnShowLoadingView(false); } }
/// <summary> /// Triggered when the server request to refill the player's energy returns successfully. /// This code starts the respawn on the energy station. /// </summary> /// <param name="data"></param> private void OnSuccess(EnergyData data) { IsLoading = false; if (data == null) { // The station is recharging return; } // Preemptively start respawning WorldService.GetInstance().StartRespawn(LocationId); // Init the player's data PlayerService.GetInstance().IncreaseEnergyLevel(data.amountRestored); UIManager.OnShowLoadingView(false); UIManager.OnShowMessageDialog("Energy recharged to 100%!"); }
/// <summary> /// Implementation of the action state. /// If the location is not respawning, the function makes a server call to retrieve /// the amount of energy restored for the avatar. /// </summary> protected override void ActionState() { if (IsLoading) { return; } base.ActionState(); location = WorldService.GetInstance().GetSpawnLocation(LocationId); if (string.IsNullOrEmpty(LocationId)) { Debug.LogError("Incorrect Location Id!"); UIManager.OnShowLoadingView(false); return; } // Check if this station is active? // If not show a floating popup with timeout information if (WorldService.GetInstance().IsRespawning(LocationId)) { DateTime t = DateTime.Parse(location.respawnTime); TimeSpan timeLeft = t.Subtract(DateTime.Now); UIManager.OnShowMessageDialog("Station recharging. Time left: ", timeLeft); UIManager.OnShowLoadingView(false); return; } try { IsLoading = true; StartCoroutine(ServerManager.PostRechargingStation(LocationId, OnSuccess, OnError)); } catch (System.Exception e) { Debug.LogError("Failed to restore energy! " + e); IsLoading = false; UIManager.OnShowLoadingView(false); } }
/// <summary> /// Triggered when world data is loaded successfully. /// Note that by world data we imply all game specific locations as opposed to maps data /// loaded through the Maps Unity SDK. /// </summary> /// <param name="worldData">World Data</param> private void OnWorldDataLoaded(WorldData worldData) { // Init the playerservice with the new batch of data WorldService.GetInstance().Init(worldData); // Init gameobjects on the map IEnumerable <string> keysFromWorldData = WorldService.GetInstance().GetSpawnLocationsIds(); // Delete all gameobjects not on the new list int deletedEntries = 0; List <string> missingKeysFromLocalCache = new List <string>(_spawnedGameObjects.Keys.Except(keysFromWorldData)); foreach (string k in missingKeysFromLocalCache) { Destroy(_spawnedGameObjects[k].gameObject); _spawnedGameObjects.Remove(k); deletedEntries++; } // Add all new locations CreateNewLocations(worldData); // Show/hide objects based on distance from avatar foreach (string k in _spawnedGameObjects.Keys) { SpawnLocation sl = WorldService.GetInstance().GetSpawnLocation(k); Vector3 pos = MapsService.Coords.FromLatLngToVector3( new LatLng(sl.snappedPoint.latitude, sl.snappedPoint.longitude)); _spawnedGameObjects[k].SetActive(Vector3.Distance(Avatar.transform.position, pos) <= MaxDistance); } _startupCheckList.Remove(PLAYABLE_LOCATIONS_INITIALIZED); CheckStartConditions(); _worldDataIsLoading = false; }