/// <summary> /// Gets the modifier from the dictionary /// The resource should be one of the public constants if not returns null /// </summary> /// <param name="resourceName">The resource to get</param> /// <returns>The modifier requested</returns> private float GetModifier(PrimaryResource resource) { float modifier; modifierMap.TryGetValue(resource, out modifier); return(modifier); }
// <summary> /// Constructor for a active modifier which takes a string /// </summary> /// <param name="toParse">The string to parse to a activeeffect</param> public ActiveEffect(String toParse) { String[] effectElements = toParse.Split(':'); if (int.TryParse(effectElements[2], out value)) { resource = new PrimaryResource(value, effectElements[1]); } }
/// <summary> /// Gets the requested resource /// The resource should be one of the public constants /// </summary> /// <param name="resourceName">Name of the resource to get</param> /// <returns></returns> public int GetResource(String resourceName) { PrimaryResource resource = GetPrimaryResource(resourceName); if (resource != null) { return(resource.GetAmount()); } return(-1); }
/// <summary> /// Applies the modifier to the adjustment to a minimum of 1 /// </summary> /// <param name="res">The resource to get the modifer for</param> /// <param name="adjustment">The adjustment to make</param> /// <returns></returns> private int ApplyModifier(PrimaryResource res, int adjustment) { float mod = GetModifier(res); adjustment = Convert.ToInt32(adjustment * mod); if (Math.Abs(adjustment) < 1) { return(-1); } return(adjustment); }
/// <summary> /// Updates the modifiers with new modifiers /// </summary> /// <param name="modifiers">List of modifiers to be updated</param> public void UpdateModifers(List <PassiveEffect> modifiers) { foreach (PassiveEffect mod in modifiers) { PrimaryResource res = GetPrimaryResource(mod.GetResourceName()); if (res != null) { float modifier; if (modifierMap.TryGetValue(res, out modifier)) { modifierMap[res] = mod.GetModifier(); } } } }
/// <summary> /// Adjusts a resouce based on the resouce name passed and the adjustment value /// The resource should be one of the public constants /// </summary> /// <param name="resourceName">Name of the resource to modify</param> /// <param name="adjustment">Adjustment to make</param> public void AdjustResource(String resourceName, int adjustment) { PrimaryResource resource = GetPrimaryResource(resourceName); if (resource != null) { int currentResounce = resource.GetAmount(); if (adjustment < 0) { adjustment = ApplyModifier(resource, adjustment); } int result = currentResounce + adjustment; resource.SetAmount(result); primaryResources[resourceName] = resource; } }
/// <summary> /// Generates a PC based on the string input /// Check the string is valid before calling this function /// </summary> /// <param name="toParse">String to be converted into a PC</param> public PlayerCharacter(String toParse) { this.primaryResources = new Dictionary <string, PrimaryResource>(); this.modifierMap = new Dictionary <PrimaryResource, float>(); String[] resources = toParse.Split(','); foreach (String curr in resources) { String[] resource = curr.Split(':'); int amount; float mod; if (int.TryParse(resource[1], out amount) && float.TryParse(resource[2], out mod)) { PrimaryResource temp = new PrimaryResource(amount, resource[0]); primaryResources.Add(resource[0], temp); modifierMap.Add(temp, mod); } } }
/// <summary> /// Generates a standard PC /// </summary> public PlayerCharacter() { PrimaryResource health = new PrimaryResource(100, HEALTH); PrimaryResource hunger = new PrimaryResource(100, HUNGER); PrimaryResource thirst = new PrimaryResource(100, THIRST); PrimaryResource sanity = new PrimaryResource(100, SANITY); this.primaryResources = new Dictionary <string, PrimaryResource>(); primaryResources.Add(HEALTH, health); primaryResources.Add(HUNGER, hunger); primaryResources.Add(THIRST, thirst); primaryResources.Add(SANITY, sanity); this.modifierMap = new Dictionary <PrimaryResource, float>(); modifierMap.Add(health, 1.0f); modifierMap.Add(hunger, 1.0f); modifierMap.Add(thirst, 1.0f); modifierMap.Add(sanity, 1.0f); }
/// <summary> /// Parses this PC to a string format suitable for saving /// </summary> /// <returns>The parsed PC</returns> public String ParseToString() { String parsed = ""; PrimaryResource health = GetPrimaryResource(HEALTH); PrimaryResource hunger = GetPrimaryResource(HUNGER); PrimaryResource thirst = GetPrimaryResource(THIRST); PrimaryResource sanity = GetPrimaryResource(SANITY); if (health == null || hunger == null || thirst == null || sanity == null) { parsed = "ERROR"; } else { parsed = health.ParseToString() + ":" + GetModifier(health) + "," + hunger.ParseToString() + ":" + GetModifier(hunger) + "," + thirst.ParseToString() + ":" + GetModifier(thirst) + "," + sanity.ParseToString() + ":" + GetModifier(sanity); } return(parsed); }
public ActiveEffect() { value = 0; resource = new PrimaryResource(value, PlayerCharacter.HEALTH); }
/// <summary> /// Modifies a primary resource of the current PC /// </summary> /// <param name="resource">Primary resource to modify</param> /// <param name="amount">Amount to modify the resource by</param> public void ModifyPrimaryResource(PrimaryResource resource, int amount) { currentPC.AdjustResource(resource.GetName(), amount); }