/// <summary> /// Actually set the resources from this list to the input part; if the current part resources match this list exactly they will be updated in-place, /// else all resources from the part will be cleared and the new list of resources added. /// </summary> /// <param name="part"></param> /// <param name="fill"></param> public void setResourcesToPart(Part part) { int len = part.Resources.Count; if (len == resourceList.Count)//potentially the same resources exist as we are trying to setup { bool foundAll = true; foreach (String name in resourceList.Keys) { ResourceListEntry entry = resourceList[name]; if (part.Resources.Contains(name))//go ahead and set them as found; if not all are found we'll delete them anyway... { PartResource pr = part.Resources[name]; pr.maxAmount = entry.max; pr.amount = entry.fill; } else { foundAll = false; break; } } if (foundAll) { SSTUModInterop.updatePartResourceDisplay(part); return; } } part.Resources.list.Clear(); PartResource[] resources = part.GetComponents <PartResource>(); len = resources.Length; for (int i = 0; i < len; i++) { GameObject.Destroy(resources[i]); } ConfigNode resourceNode; foreach (String name in resourceList.Keys) { ResourceListEntry entry = resourceList[name]; resourceNode = new ConfigNode("RESOURCE"); resourceNode.AddValue("name", name); resourceNode.AddValue("maxAmount", entry.max); resourceNode.AddValue("amount", entry.fill); part.AddResource(resourceNode); } SSTUModInterop.updatePartResourceDisplay(part); }
/// <summary> /// Remove a specified number of units from the resource fill and maximum values; if fill>max after this, fill will be set to max. If max==0, resource will be removed from list entirely /// </summary> /// <param name="name"></param> /// <param name="removeFromMax"></param> public void removeResource(String name, float removeFromFill, float removeFromMax) { if (resourceList.ContainsKey(name)) { ResourceListEntry entry = resourceList[name]; entry.fill -= removeFromFill; entry.max -= removeFromMax; if (entry.fill > entry.max) { entry.fill = entry.max; } if (entry.fill < 0) { entry.fill = 0; } if (entry.max <= 0) { removeResource(name); } } }
/// <summary> /// Blindly set the quanity of a resource. Adds new resource if not present, removes resource if present and max==0. /// </summary> /// <param name="name"></param> /// <param name="fill"></param> /// <param name="max"></param> public void setResource(string name, float fill, float max) { if (max == 0) { removeResource(name); return; } if (fill > max) { fill = max; } if (resourceList.ContainsKey(name)) { ResourceListEntry entry = resourceList[name]; entry.fill = fill; entry.max = max; } else { ResourceListEntry entry = new ResourceListEntry(name, fill, max); resourceList.Add(entry.name, entry); } }
/// <summary> /// Add a resource, by units, setting/adjusting fill and max units by the units specified /// </summary> /// <param name="name"></param> /// <param name="fillMaxUnits"></param> public void addResource(string name, float fill, float max) { if (resourceList.ContainsKey(name)) { ResourceListEntry entry = resourceList[name]; entry.max += max; entry.fill += fill; if (entry.fill > entry.max) { entry.fill = entry.max; } } else { if (fill > max) { fill = max; } ResourceListEntry entry = new ResourceListEntry(name, fill, max); resourceList.Add(entry.name, entry); } }