void UpdateResourceStorage() { PlayerResourceStorageMetaData data = PlayerResourceStorageMetaData.Load(); ResourceStorageMetaData rsData = data.GetResourceMetaData(resourceId); maxResource = rsData.maxResource; currentResource = rsData.currentResource; EventManager.GetInstance().ExecuteEvent <EventResourceStorageUpdate> (new EventResourceStorageUpdate(resourceId, currentResource)); }
/* * public bool DecreaseCombatUnit(int decreaseAmount) * { * if(numberOfCombatUnit.Count <= 0) * { * return false; * } * * if((CurrentCombatUnit-decreaseAmount) < 0) * { * return false; * } * * numberOfCombatUnit [0] -= decreaseAmount; * * Save (); * * return true; * } */ /* * public void AddCombatUnit(int amount) * { * if(numberOfCombatUnit.Count <= 0) * { * return; * } * * int addAmount = amount; * * if((CurrentCombatUnit+amount) > maxCombatUnit) * { * addAmount = maxCombatUnit - CurrentCombatUnit; * } * * numberOfCombatUnit [0] += addAmount; * * Save (); * } */ /// <summary> /// Produces combat unit. /// </summary> /// <returns><c>true</c>, if combat unit was produced, <c>false</c> otherwise.</returns> /// <param name="unitType">Unit type.</param> /// <param name="produceAmount">Produce amount.</param> /// <param name="duration">Duration.</param> /// <param name="resourceCost">Resource cost.</param> public bool ProduceCombatUnit(CombatUnitType unitType, int produceAmount, int duration, Dictionary <ResourceType, float> resourceCost) { if (producingUnit) { return(false); } PlayerResourceStorageMetaData data = PlayerResourceStorageMetaData.Load(); if (duration <= 0) { AddCombatUnit(unitType, produceAmount); foreach (ResourceType type in resourceCost.Keys) { data.CostResource(type, produceAmount * resourceCost[type]); } Save(); return(true); } producingUnit = true; producingUnitType = unitType; producingAmount = produceAmount; producingDuration = duration; producingStartTime = DateTime.Now; producingEndTime = DateTime.Now.AddSeconds(producingDuration); //todo: cost resource foreach (ResourceType type in resourceCost.Keys) { data.CostResource(type, produceAmount * resourceCost[type]); } Save(); return(true); }
/// <summary> /// Transfers the resource to player. /// </summary> /// <returns><c>true</c>, if resource to player was transfered, <c>false</c> otherwise.</returns> public bool TransferResourceToPlayer() { PlayerResourceStorageMetaData data = PlayerResourceStorageMetaData.Load(); float transferedResource = data.TransferResourceToPlayer(resourceId, currentResourceStore); currentResourceStore -= transferedResource; this.Save(); if (transferedResource > 0) { return(true); } else { return(false); } }
/// <summary> /// Calculates the battle result. /// </summary> public void CalculateResult() { BattleReportMetaData reportData = BattleReportMetaData.Load(); int fAttackerSum = GetAttackerSum(); int fDefenderSum = GetDefenderSum(); if ((fAttackerSum - fDefenderSum) > 0) //attacker win, defender defeat { Debug.Log("Attacker " + GetAttackerSum() + " vs " + targetName + " " + GetDefenderSum() + " win"); float ratio = (float)fDefenderSum * 100f / (float)fAttackerSum / 100f; CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load(); //calculate unit lost for attacker List <UnitLostInfo> attackerLostInfo = new List <UnitLostInfo>(); foreach (CombatUnit unit in attackerUnit.Keys) { int lostUnit = Mathf.Clamp((int)(ratio * attackerUnit[unit]), 1, attackerUnit[unit]); UnitLostInfo info = new UnitLostInfo(unit.unitType, attackerUnit[unit], lostUnit); //return unit data.AddCombatUnit(unit.unitType, info.to); attackerLostInfo.Add(info); } //calculate unit lost for defender List <UnitLostInfo> targetLostInfo = new List <UnitLostInfo>(); foreach (CombatUnit unit in targetUnit.Keys) { float penalty = Mathf.Clamp((1 - ratio) * 10f, 1f, 10f); int lostUnit = Mathf.Clamp((int)(ratio * penalty * targetUnit[unit]), 1, targetUnit[unit]); //TODO:Decrease ai player unit targetLostInfo.Add(new UnitLostInfo(unit.unitType, targetUnit[unit], lostUnit)); } result = AttackResult.Win; reportData.AddBattleReport(result, targetName, "Player", attackerLostInfo, targetLostInfo, resourceAward); //add resource PlayerResourceStorageMetaData pData = PlayerResourceStorageMetaData.Load(); foreach (ResourceType type in resourceAward.Keys) { pData.AddResourceForType(type, resourceAward[type]); } } else if ((fAttackerSum - fDefenderSum) < 0) //attacker defeat, defender win { Debug.Log("Attacker " + GetAttackerSum() + " vs " + targetName + " " + GetDefenderSum() + " fail"); float ratio = (float)fAttackerSum * 100f / (float)fDefenderSum / 100f; CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load(); //calculate unit lost for attacker List <UnitLostInfo> attackerLostInfo = new List <UnitLostInfo>(); foreach (CombatUnit unit in attackerUnit.Keys) { float penalty = Mathf.Clamp((1 - ratio) * 10f, 1f, 10f); int lostUnit = Mathf.Clamp((int)(ratio * penalty * attackerUnit[unit]), 1, attackerUnit[unit]); UnitLostInfo info = new UnitLostInfo(unit.unitType, attackerUnit[unit], lostUnit); //return unit data.AddCombatUnit(unit.unitType, info.to); attackerLostInfo.Add(info); } //calculate unit lost for defender List <UnitLostInfo> targetLostInfo = new List <UnitLostInfo>(); foreach (CombatUnit unit in targetUnit.Keys) { int lostUnit = Mathf.Clamp((int)(ratio * targetUnit[unit]), 1, targetUnit[unit]); //TODO:Decrease ai player unit targetLostInfo.Add(new UnitLostInfo(unit.unitType, targetUnit[unit], lostUnit)); } result = AttackResult.Lost; reportData.AddBattleReport(result, targetName, "Player", attackerLostInfo, targetLostInfo); } else //draw { Debug.Log("Attacker " + GetAttackerSum() + " vs " + targetName + " " + GetDefenderSum() + " draw"); float ratio = (float)fAttackerSum * 100f / (float)fDefenderSum / 100f * 0.5f; CombatUnitManagerMetaData data = CombatUnitManagerMetaData.Load(); //calculate unit lost for attacker List <UnitLostInfo> attackerLostInfo = new List <UnitLostInfo>(); foreach (CombatUnit unit in attackerUnit.Keys) { int lostUnit = Mathf.Clamp((int)(ratio * 0.3f * attackerUnit[unit]), 1, attackerUnit[unit]); UnitLostInfo info = new UnitLostInfo(unit.unitType, attackerUnit[unit], lostUnit); //return unit data.AddCombatUnit(unit.unitType, info.to); attackerLostInfo.Add(info); } //calculate unit lost for defender List <UnitLostInfo> targetLostInfo = new List <UnitLostInfo>(); foreach (CombatUnit unit in targetUnit.Keys) { int lostUnit = Mathf.Clamp((int)(ratio * 0.3f * targetUnit[unit]), 1, targetUnit[unit]); //TODO:Decrease ai player unit targetLostInfo.Add(new UnitLostInfo(unit.unitType, targetUnit[unit], lostUnit)); } result = AttackResult.Draw; reportData.AddBattleReport(result, targetName, "Player", attackerLostInfo, targetLostInfo); } }