Files
@ f28757bb21cb
Branch filter:
Location: ATITD-Tools/Desert-Paint-Lab/PaintRecipe.cs
f28757bb21cb
4.3 KiB
text/x-csharp
Refactor recipe / reaction computation into a common class. Add some file utilities for supporting Mac bundles. Add some scripts for building Mac app bundles. Add a help window that shows the missing reactions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 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<string> reagents = new List<string>();
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<string,bool> reagentSet = new SortedDictionary<string,bool>();
List<Reagent> prevReagents = new List<Reagent>();
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);
}
}
}
|