Reputation:
I'm making a mobile game where the player is given four (non repeating) colors that they can swipe together and create a new color. The game will display 1-4 boxes on the screen and the player must swipe the colors together and guess the correct combination in order to move on the next round. I've already calculated all possible combinations the player can swipe and stored the color for that combination in a script called "ColorDictionary.cs". This is a static script that contains 100+ Color32 variables that my other scripts can go and grab depending on what the player swipes. Here are a couple screen shots for a better idea of whats going on.
This one is easy, the color to get is light red which requires the player to swipe the Red and White circles together
Now lets say the player isn't very smart and swipes Red with Blue, the picture below demonstrates how the UI changed colors accordingly to display the output of the color combinations the player puts in.
This is done by checking what colors the player submitted then going and getting the right combination from the ColorDictionary script. Here is one very small snippet of the ColorManager doing a check to calculate and grab the correct color from the dictionary:
// YELLOW - RED
if (redActive) {
if (magentaActive) {
if (blackActive) { currentColor = ColorDictionary.tallPoppy; }
else if (whiteActive) { currentColor = ColorDictionary.salmon; }
else { currentColor = ColorDictionary.sunsetOrange; }
} else if (blueActive) {
if (blackActive) { currentColor = ColorDictionary.nutmeg; }
else if (whiteActive) { currentColor = ColorDictionary.muddyWaters; }
else { currentColor = ColorDictionary.brownRust; }
} else if (cyanActive) {
if (blackActive) { currentColor = ColorDictionary.yellowMetal; }
else if (whiteActive) { currentColor = ColorDictionary.indianKhaki; }
else { currentColor = ColorDictionary.teak; }
} else if (greenActive) {
if (blackActive) { currentColor = ColorDictionary.verdunGreen; }
else if (whiteActive) { currentColor = ColorDictionary.roti; }
else { currentColor = ColorDictionary.pirateGold; }
} else {
if (blackActive) { currentColor = ColorDictionary.chelseaGem; }
else if (whiteActive) { currentColor = ColorDictionary.texasRose; }
else { currentColor = ColorDictionary.flushOrange; }
}
}
This if statement ladder goes on for awhile (only checking the combinations for active colors though) and a color only becomes active when the player clicks it.
Upvotes: 1
Views: 101
Reputation: 32898
The way you're using a bunch of if-statements is quite messy. Instead of doing that, store the boxes as an enum
:
[Flags]
public enum ColorBox
{
Red = 1, Blue = 2, White = 4, Yellow = 8
}
And create a lookup dictionary of color combinations as follows:
public static Dictionary<ColorBox, ColorDictionary> CreateComboTable()
{
var table = new Dictionary<ColorBox, ColorDictionary>();
table[ColorBox.Red | ColorBox.Blue | ColorBox.White] =
ColorDictionary.muddyWater;
table[ColorBox.Red | ColorBox.Blue | ColorBox.Yellow] =
ColorDictionary.brownRust;
// And so on...
return table;
}
Even better is to use JSON or the like to store the color combinations, but this is just a start.
Then, once the player chooses two colors, look up the appropriate combination in the dictionary (using ContainsKey
or the []
syntax as appropriate).
Upvotes: 1