public static ActivePet CreateFromSnapshot(PetSnapshot snapshot) { ActivePet pet = new ActivePet(); pet.SetSnapshot(snapshot); return(pet); }
public static ActivePet CreatePet(Species species, int stage, string treeName) { if (species == null) { return(null); } double timestamp = Timestamp.GetTimeStamp(); PetSnapshot petSnapshot = new PetSnapshot(); petSnapshot.nickname = ""; petSnapshot.species = species.speciesName; petSnapshot.birth = timestamp; petSnapshot.longetivity = Random.Range((int)species.longetivityMin, (int)species.longetivityMax + 1); petSnapshot.treeName = treeName; petSnapshot.stage = stage; petSnapshot.stageStamp = timestamp; petSnapshot.minWeight = species.minWeight; petSnapshot.maxWeight = species.maxWeight; petSnapshot.Weight = species.baseWeight; petSnapshot.careMistakeCost = species.careMistakeCost; petSnapshot.hungerRate = Random.Range(species.hungerRateMin, species.hungerRateMax + 1); petSnapshot.strengthRate = Random.Range(species.strengthRateMin, species.strengthRateMax + 1); petSnapshot.attentionRate = Random.Range(species.attentionRateMin, species.attentionRateMax + 1); petSnapshot.hungerStamp = timestamp; petSnapshot.strengthStamp = timestamp; petSnapshot.attentionStamp = timestamp; petSnapshot.happiness = 50; petSnapshot.happinessRate = Random.Range(species.happinessRateMin, species.happinessRateMax + 1); petSnapshot.happinessStamp = timestamp; petSnapshot.discipline = 50; petSnapshot.disciplineRate = Random.Range(species.disciplineRateMin, species.disciplineRateMax + 1); petSnapshot.disciplineStamp = timestamp; petSnapshot.energy = 20; petSnapshot.energyRecoveryRate = species.energyRecoveryRate; petSnapshot.energyStamp = timestamp; petSnapshot.s_atk = species.atk; petSnapshot.s_spd = species.spd; petSnapshot.s_def = species.def; petSnapshot.g_atk = Random.Range(0, 17); petSnapshot.g_spd = Random.Range(0, 17); petSnapshot.g_def = Random.Range(0, 17); ActivePet pet = new ActivePet(); pet.SetSnapshot(petSnapshot); return(pet); }
public static IEnumerator CreatePet(ActivePet newPet, string username) { string json = JsonUtility.ToJson(newPet.GetSnapshotCopy()); byte[] data = System.Text.Encoding.Default.GetBytes(json); // Create a PUT request because Unity apperantly cannot UnityWebRequest request = new UnityWebRequest(HOST + username + "/pet", "PUT"); request.uploadHandler = (UploadHandler) new UploadHandlerRaw(data); request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); if (request.isNetworkError) { if (offline_mode) { yield return("0000-0000-0000"); } else { Debug.Log("Error sending data to server"); } } else { byte[] result = request.downloadHandler.data; string resJson = System.Text.Encoding.Default.GetString(result); PetSnapshot newSnapshot = JsonUtility.FromJson <PetSnapshot>(resJson); yield return(newSnapshot._id); } }
public static IEnumerator UpdatePet(PetSnapshot snapshot) { string json = JsonUtility.ToJson(snapshot); //Debug.Log(json); byte[] data = System.Text.Encoding.Default.GetBytes(json); // Create a POST request because Unity apperantly cannot UnityWebRequest request = new UnityWebRequest(HOST + "pet", "POST"); request.uploadHandler = (UploadHandler) new UploadHandlerRaw(data); request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); if (!request.isNetworkError) { Debug.Log("Successfully Updated pet"); yield return(true); } else { if (offline_mode) { yield return(true); } else { Debug.Log("Error updating pet"); yield return(false); } } }
float CalculateNextAttack(PetSnapshot agent) { float modifiedSpeed = agent.spd * (0.5f + 0.5f * (float)agent.Strength / 5); float nextAttack = 1 / (modifiedSpeed / 50); nextAttack += fightTimer; return(nextAttack); }
public void EvolveTo(PetSnapshot newSnapshot) { // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); SetSnapshot(newSnapshot); // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void Misbehave() { // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.misbehaveStamp = Timestamp.GetTimeStamp(); // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void UpdateHapiness(int amount) { // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.happiness += amount; snapshot.happinessStamp = Timestamp.GetTimeStamp(); // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void ReduceEnergy(int amount) { // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.energy = energy - amount; snapshot.energyStamp = Timestamp.GetTimeStamp(); Debug.Log(snapshot.energy); // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void AddCareMistake() { // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.careMistakes++; snapshot.longetivity -= careMistakeCost; // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void Sleep(int hours) { if (IsSleeping()) { return; } // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.sleepHours = hours; snapshot.sleepStamp = Timestamp.GetTimeStamp(); // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void Injure(int amount) { if (IsInjured()) { return; } // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.injury = amount; snapshot.injuryStamp = Timestamp.GetTimeStamp(); // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void Wake() { if (IsSleeping() == false) { return; } // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.wakeStamp = Timestamp.GetTimeStamp(); snapshot.energy = CalculateEnergy(); // We "save" the energy replenished until this point // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public IEnumerator SaveSnapshot(ActivePet pet, PetSnapshot snapshot, PetSnapshot backup) { UIManager.instance.SetLoading(true, "Syncing", true); Coroutine <bool> routine = this.StartCoroutine <bool>(DataService.UpdatePet(snapshot)); yield return(routine.coroutine); UIManager.instance.SetLoading(false, "", true); if (routine.returnVal == false) { Debug.Log("No response from server: Aborting save and restoring backup."); pet.SetSnapshot(backup); } }
public void Scold() { // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); if (CanDiscipline()) { snapshot.discipline = discipline + 10; snapshot.disciplineStamp = Timestamp.GetTimeStamp(); // Remove misbehave timestamp snapshot.misbehaveStamp = 0; } snapshot.happiness -= 10; snapshot.happinessStamp = Timestamp.GetTimeStamp(); // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
public void Feed(int hungerChange, int weightChange, int happinessChange, int disciplineChange, int energyChange) { double stamp = Timestamp.GetTimeStamp(); // Create backup in case server doesn't respond PetSnapshot backup = GetSnapshotCopy(); snapshot.Hunger = hunger + hungerChange; snapshot.hungerStamp = stamp; snapshot.Weight += weightChange; snapshot.happiness += happinessChange; snapshot.discipline += disciplineChange; snapshot.disciplineStamp = stamp; snapshot.energy += energyChange; snapshot.isStarving = false; // Send snapshot to server GameManager.instance.StartCoroutine(GameManager.instance.SaveSnapshot(this, snapshot, backup)); }
void StartBattle(PetSnapshot player, PetSnapshot opponent) { this.player = player; this.enemy = opponent; orderTime = fightPhaseDuration / orderSize; turn = 0; InitializeVisualPets(); // Initialize UI uiManager.UpdatePlayerStrength(player.Strength); uiManager.UpdateEnemyStrength(enemy.Strength); uiManager.SetOrders(orderSize); StartOrderPhase(); }
void StartBattle(PetSnapshot player, PetSnapshot opponent) { this.player = player; this.enemy = opponent; playerStartHealth = playerHealth; enemyStartHealth = enemyHealth; InitializeVisualPets(); // Initialize UI uiManager.UpdatePlayerStrength(player.Strength); uiManager.UpdateEnemyStrength(enemy.Strength); fightTimer = 0f; fightStarted = true; CalculateNextAttack(player); CalculateNextAttack(enemy); }
public static PetSnapshot Evolve(ActivePet from, Species to) { double timestamp = Timestamp.GetTimeStamp(); PetSnapshot snapshot = from.GetSnapshotCopy(); snapshot.species = to.speciesName; snapshot.longetivity = Random.Range((int)to.longetivityMin, (int)to.longetivityMax + 1); snapshot.stage = from.stage + 1; snapshot.stageStamp = timestamp; snapshot.minWeight = to.minWeight; snapshot.maxWeight = to.maxWeight; if (from.maxWeight - from.minWeight == 0) { snapshot.Weight = to.baseWeight; } else { snapshot.Weight = Mathf.FloorToInt(((from.weight - from.minWeight) / (from.maxWeight - from.minWeight)) * (to.maxWeight - to.minWeight) + to.minWeight); } snapshot.careMistakeCost = to.careMistakeCost; snapshot.hungerRate = Random.Range(to.hungerRateMin, to.hungerRateMax + 1); snapshot.strengthRate = Random.Range(to.strengthRateMin, to.strengthRateMax + 1); snapshot.attentionRate = Random.Range(to.attentionRateMin, to.attentionRateMax + 1); snapshot.happinessRate = Random.Range(to.happinessRateMin, to.happinessRateMax + 1); snapshot.disciplineRate = Random.Range(to.disciplineRateMin, to.disciplineRateMax + 1); snapshot.energy = 20; snapshot.energyRecoveryRate = to.energyRecoveryRate; snapshot.energyStamp = timestamp; snapshot.s_atk = to.atk; snapshot.s_spd = to.spd; snapshot.s_def = to.def; return(snapshot); }
void Start() { PetSnapshot player = new PetSnapshot(); player.species = "Chamiri"; player.Strength = 5; player.g_atk = 255; player.g_def = 255; player.g_spd = 255; PetSnapshot enemy = new PetSnapshot(); enemy.species = "Nanasu"; enemy.Strength = 5; enemy.g_atk = 255; enemy.g_def = 255; enemy.g_spd = 255; playerHealth = enemyHealth = 100; StartBattle(player, enemy); }
public PetSnapshot GetSnapshotCopy() { PetSnapshot copy = new PetSnapshot(); copy._id = snapshot._id; copy.species = snapshot.species; copy.treeName = snapshot.treeName; copy.stage = snapshot.stage; copy.stageStamp = snapshot.stageStamp; copy.birth = snapshot.birth; copy.nickname = snapshot.nickname; copy.careMistakes = snapshot.careMistakes; copy.careMistakeCost = snapshot.careMistakeCost; copy.Weight = snapshot.Weight; copy.minWeight = snapshot.minWeight; copy.maxWeight = snapshot.maxWeight; copy.starveAt = snapshot.starveAt; copy.Hunger = snapshot.Hunger; copy.hungerRate = snapshot.hungerRate; copy.hungerStamp = snapshot.hungerStamp; copy.Strength = snapshot.Strength; copy.strengthRate = snapshot.strengthRate; copy.strengthStamp = snapshot.strengthStamp; copy.Attention = snapshot.Attention; copy.attentionRate = snapshot.attentionRate; copy.attentionStamp = snapshot.attentionStamp; copy.happiness = snapshot.happiness; copy.happinessRate = snapshot.happinessRate; copy.happinessStamp = snapshot.happinessStamp; copy.discipline = snapshot.discipline; copy.disciplineRate = snapshot.disciplineRate; copy.disciplineStamp = snapshot.disciplineStamp; copy.energy = snapshot.energy; copy.energyStamp = snapshot.energyStamp; copy.sleepStamp = snapshot.sleepStamp; copy.sleepHours = snapshot.sleepHours; copy.s_atk = snapshot.s_atk; copy.s_spd = snapshot.s_spd; copy.s_def = snapshot.s_def; copy.g_atk = snapshot.g_atk; copy.g_spd = snapshot.g_spd; copy.g_def = snapshot.g_def; copy.t_atk = snapshot.t_atk; copy.t_spd = snapshot.t_spd; copy.t_def = snapshot.t_def; return(copy); }
public void SetSnapshot(PetSnapshot snapshot) { this.snapshot = snapshot; }