Yet another enum alternative

Hi there, great series so far, really enjoying it!

I am using a different way of drawing the grid objects (the Shapes asset tool) and so the color is in my case specified directly in the code. I did not want to litter the GridSystemVisual class with all sorts of enums, colors, serializefields etc. so I decided to choose a different approach.

I made a really simple static class called GridColorProvider

public static class GridColorProvider
{
    private static readonly Color MOVE_COLOR = Color.white;
    private static readonly Color SHOOT_COLOR = Color.red;
    private static readonly Color SPIN_COLOR = Color.blue;
    private static readonly Color DEFAULT_COLOR = Color.gray;
    
    public static Color GetGridCellColor(BaseAction selectedAction)
    {
        return selectedAction switch
        {
            MoveAction moveAction => MOVE_COLOR,
            ShootAction shootAction => SHOOT_COLOR,
            SpinAction spinAction => SPIN_COLOR,
            _ => DEFAULT_COLOR
        };
    }
}

Then I can simply call

_color = GridColorProvider.GetGridCellColor((sender as UnitActionSystem).SelectedAction);

before updating the grid visuals on selected action changed.

This static GridColorProvider could be easily changed to ScriptableObject too.

2 Likes

That’s an interesting approach, good job!

I like the more compact switch statement of it.
However, just using the pre-defined base colours seems somewhat limiting.

While one surely can use any random RGB value as a colour, what if one wanted to do more sophisticated visuals? So I think one should at least have a selection of materials…

You can actually define any color you want using the Color() constructor…

private static readonly Color SHOOT_COLOR = new Color(1f, .25f, 0f);
1 Like

Privacy & Terms