/// <summary> /// Gets the available unit to produce for type. /// How many unit can produce for type. /// This is base on how much resource and available unit space /// </summary> public int GetAvaliableUnitToProduceForType(CombatUnitType type) { CombatUnit unit = GetCombatUnitInfoByType(type); Dictionary <ResourceType, float> costResource = unit.GetResourceCost; if (costResource != null) { int unitMinProduction = availableUnitToProduce; foreach (ResourceType rType in costResource.Keys) { ResourceStorage rs = Player.Instance.GetResourceStorage(rType); unitMinProduction = Mathf.Min(Mathf.FloorToInt(rs.currentResource / costResource[rType]), unitMinProduction); } return(unitMinProduction); } else { Debug.LogError("Combat unit type " + type.ToString() + " has no resource cost setup"); return(0); } }
/// <summary> /// Decreases the combat unit. /// </summary> /// <returns><c>true</c>, if combat unit was decreased, <c>false</c> otherwise.</returns> /// <param name="type">Type.</param> /// <param name="decreaseAmount">Decrease amount.</param> public bool DecreaseCombatUnit(CombatUnitType type, int decreaseAmount) { if (numberOfCombatUnit.Count <= 0) { return(false); } for (int i = 0; i < availableCombatUnit.Count; i++) { if (type == availableCombatUnit[i]) { if ((numberOfCombatUnit[i] - decreaseAmount) >= 0) { numberOfCombatUnit[i] -= decreaseAmount; } else { numberOfCombatUnit[i] = 0; } Save(); return(true); } } Debug.LogError("No unit type found " + type.ToString()); return(false); }
/* * void ChangeProduceCount(int val) * { * CombatUnitManager mgr = ((UITrainingNavController)_navigationController).combatUnitManager; * * produceCount = val; * * unitLabel.text = val.ToString (); * * //change button disable/enable * if((produceCount > 0) && (produceCount < mgr.GetAvaliableUnitToProduceForType(unitType))) * { * increaseBtn.isEnabled = true; * decreaseBtn.isEnabled = true; * } * else if(produceCount <= 0) * { * increaseBtn.isEnabled = true; * decreaseBtn.isEnabled = false; * } * else * { * increaseBtn.isEnabled = false; * decreaseBtn.isEnabled = true; * } * * * //if was not slider alter the value...then change slider value * if(UISlider.current == null) * { * //we don't want to receive event * EventDelegate.Remove (unitSlider.onChange, OnUnitSliderChange); * * unitSlider.value = (float)val / (float)mgr.GetAvaliableUnitToProduceForType(unitType); * * EventDelegate.Set (unitSlider.onChange, OnUnitSliderChange); * } * } * * * public void OnIncreaseUnitCount() * { * CombatUnitManager mgr = ((UITrainingNavController)_navigationController).combatUnitManager; * * int incVal = unitAddSubVal; * * int val = Mathf.Clamp (produceCount + incVal, 0, mgr.GetAvaliableUnitToProduceForType(unitType)); * * ChangeProduceCount (val); * } * * public void OnDecreaseUnitCount() * { * CombatUnitManager mgr = ((UITrainingNavController)_navigationController).combatUnitManager; * * int decVal = -1 * unitAddSubVal; * * int val = Mathf.Clamp (produceCount + decVal, 0, mgr.GetAvaliableUnitToProduceForType(unitType)); * * ChangeProduceCount (val); * } * * void OnUnitSliderChange() * { * CombatUnitManager mgr = ((UITrainingNavController)_navigationController).combatUnitManager; * * if(float.IsNaN(UISlider.current.value)) * { * return; * } * * float sliderVal = UISlider.current.value; * * int val = (int)((float)mgr.GetAvaliableUnitToProduceForType(unitType) * sliderVal); * * * ChangeProduceCount (val); * * } */ /* * void OnInputFieldChange() * { * if(string.IsNullOrEmpty(UIInput.current.value)) * { * return; * } * * CombatUnitManager mgr = ((UITrainingNavController)_navigationController).combatUnitManager; * * produceCount = Mathf.Clamp(int.Parse (UIInput.current.value), 0, mgr.availableUnitToProduce); * * UIInput.current.value = produceCount.ToString (); * * if((produceCount > 0) && (produceCount < mgr.availableUnitToProduce)) * { * increaseBtn.isEnabled = true; * decreaseBtn.isEnabled = true; * } * else if(produceCount <= 0) * { * increaseBtn.isEnabled = true; * decreaseBtn.isEnabled = false; * } * else * { * increaseBtn.isEnabled = false; * decreaseBtn.isEnabled = true; * } * * //if was not slider alter the value...then change slider value * if(UISlider.current == null) * { * EventDelegate.Remove (unitSlider.onChange, OnUnitSliderChange); * * unitSlider.value = (float)produceCount / (float)mgr.availableUnitToProduce; * * EventDelegate.Set (unitSlider.onChange, OnUnitSliderChange); * } * } */ public void OnProduceUnit() { if (produceCount <= 0) { return; } CombatUnitManager mgr = ((UITrainingNavController)_navigationController).combatUnitManager; if (mgr.ProduceCombatUnit(unitType, produceCount)) { Debug.Log("Produce unit " + unitType.ToString() + " with " + produceCount); _navigationController.CloseNavigationController(); } }