using System; using System.Collections.Generic; namespace DesertPaintLab { public class PaintRecipe { public struct RGB { public int r; public int g; public int b; }; private List reagents = new List(); public PaintRecipe() { } public void AddReagent(String reagentName) { reagents.Add(reagentName); } public void Clear() { reagents.Clear(); } byte CalculateColor(int baseSum, int pigmentCount, int reactSum) { return (byte)Math.Max(Math.Min(Math.Round((((float)baseSum / (float)pigmentCount) + (float)reactSum)), 255), 0); } // Compute the color including reactions based on the player's profile public void ComputeReactedColor(PlayerProfile profile, ref PaintColor paintColor) { RGB baseColor; baseColor.r = 0; baseColor.g = 0; baseColor.b = 0; RGB reactionColor; reactionColor.r = 0; reactionColor.g = 0; reactionColor.b = 0; int pigmentCount = 0; string prevReagent = null; // track visited reagents so the reaction is only applied once SortedDictionary reagentSet = new SortedDictionary(); List prevReagents = new List(); foreach (string reagentName in reagents) { if (reagentName == null) { continue; } Reagent reagent = ReagentManager.GetReagent(reagentName); if (!reagent.IsCatalyst) { baseColor.r += reagent.Color.Red; baseColor.g += reagent.Color.Green; baseColor.b += reagent.Color.Blue; pigmentCount += 1; } if (prevReagent == null || !prevReagent.Equals(reagentName)) { if (!reagentSet.ContainsKey(reagentName) && reagentSet.Count <= 4) { reagentSet[reagentName] = true; // Run reactions. foreach (Reagent otherReagent in prevReagents) { Reaction reaction = profile.FindReaction(otherReagent, reagent); if (reaction != null) { reactionColor.r += reaction.Red; reactionColor.g += reaction.Green; reactionColor.b += reaction.Blue; } } prevReagents.Add(reagent); } } prevReagent = reagentName; } paintColor.Red = CalculateColor(baseColor.r, pigmentCount, reactionColor.r); paintColor.Green = CalculateColor(baseColor.g, pigmentCount, reactionColor.g); paintColor.Blue = CalculateColor(baseColor.b, pigmentCount, reactionColor.b); } // Compute the base color without any reactions public void ComputeBaseColor(ref PaintColor color) { RGB baseColor; baseColor.r = 0; baseColor.g = 0; baseColor.b = 0; int pigmentCount = 0; foreach (string reagentName in reagents) { if (reagentName == null) { continue; } Reagent reagent = ReagentManager.GetReagent(reagentName); if (!reagent.IsCatalyst) { baseColor.r += reagent.Color.Red; baseColor.g += reagent.Color.Green; baseColor.b += reagent.Color.Blue; pigmentCount += 1; } } color.Red = CalculateColor(baseColor.r, pigmentCount, 0); color.Green = CalculateColor(baseColor.g, pigmentCount, 0); color.Blue = CalculateColor(baseColor.b, pigmentCount, 0); } } }