Coding: Bring the Noise

%26lt;0011 Em

Color Profiles

leave a comment »

Today’s post isn’t groundbreaking (are they ever) just a wrapper around a nested dictionary.  Turns out this is a really convenient pattern in general – I’m using a more general two-key dictionary in the engine that was built off of the below code.

I got sick of reading lots of this:

Color baseColor = defaultBaseColor;
if(Hostility == Hostility.Friendly)
    baseColor = Color.DarkGreen;
else if (Hostility == Hostility.Enemy)
    baseColor = Color.DarkRed;
BasicShapes.DrawSolidSquare(batch, Position, baseColor, SideLength, Rotation);

Now, the rendering code reads:

public class GameObject
    ColorScheme ColorScheme;
    Hostility Hostility;

    // ...

    protected Color LayerColor(LayerType layer)
        return ColorScheme[Hostility, layer];

    public virtual void Draw(SpriteBatch batch, RenderPass pass)
        // ...
        BasicShapes.DrawSolidSquare(batch, Position,
                LayerColor(LayerType.Base), SideLength, Rotation);

        var InnerPos = Position + new Vector2(SideLength / 2);
        var SmallSide = SideLength / 2;
        BasicShapes.DrawSolidSquare(batch, InnerPos,
                LayerColor(LayerType.Highlight), SmallSide, Rotation);

Source after the jump.

I’ve removed the comments so that the structure is a little clearer.

public class ColorScheme
    Dictionary<Hostility, Dictionary<LayerType, Color>> scheme;

    public ColorScheme()
        scheme = new Dictionary<Hostility, Dictionary<LayerType, Color>>();

    public Color this[Hostility hostility, LayerType layer]
        get { return GetColor(hostility, layer); }
        set { SetColor(hostility, layer, value); }

    public void LoadFromFile(string filename, string profilename)
        throw new NotImplementedException();

    public void SaveToFile(string filename, string profilename)
        throw new NotImplementedException();

    private void SetColor(Hostility hostility, LayerType layer, Color color)
        if (!scheme.ContainsKey(hostility))
                scheme[hostility] = new Dictionary<LayerType, Color>();
        scheme[hostility][layer] = color;

    private Color GetColor(Hostility hostility, LayerType layer)
        if (scheme.ContainsKey(hostility) && scheme[hostility].ContainsKey(layer))
            return scheme[hostility][layer];
            return DefaultScheme[hostility, layer];

    static ColorScheme defaultScheme;
    public static ColorScheme DefaultScheme
            if (defaultScheme == null)
            return defaultScheme;
    static void InitializeDefaultScheme()
        defaultScheme = new ColorScheme();

        defaultScheme[Hostility.Friendly, LayerType.Base] = Color.Green;
        defaultScheme[Hostility.Hostile, LayerType.Base] = Color.DarkSlateGray;
        defaultScheme[Hostility.Neutral, LayerType.Base] = Color.Yellow;
        defaultScheme[Hostility.None, LayerType.Base] = Color.DarkMagenta;
        defaultScheme[Hostility.Player, LayerType.Base] = Color.DarkRed;

        defaultScheme[Hostility.Friendly, LayerType.Highlight] = Color.LawnGreen;
        defaultScheme[Hostility.Hostile, LayerType.Highlight] = Color.LightSlateGray;
        defaultScheme[Hostility.Neutral, LayerType.Highlight] = Color.LightGoldenrodYellow;
        defaultScheme[Hostility.None, LayerType.Highlight] = Color.Magenta;
        defaultScheme[Hostility.Player, LayerType.Highlight] = Color.Red;

Load/save would be nice, and that would remove the need to have an InitializeDefaultScheme function.  Note that a significant optimization can be made if we can constrain the keys such that they can be hashed together, and remove the nesting.  It’s easy to write a general solution that doesn’t require such a restriction though, so I’ll go with that until I find that the double lookup is a bottleneck (unlikely).


Written by delwinna

March 23, 2012 at 4:17 pm

Posted in Projects

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: