C# - Mapping input objects to effect objects in a typesafe way

Viewed 165

The problem I'm trying to solve is conceptually very simple. I'll explain it in terms of a game, but I believe the concept would apply to any objects with an input, effect relationship:

An ability consists of a list of input objects and a list of effect objects. The input objects define types of user interactions (like selecting a ground tile or selecting a unit), and each input object will get a certain type of target from the user (e.g. tile or unit). The effect objects define effects on a specific type of target (for example moving to a ground tile or damaging a unit).

Using an ability in code would consist of:

  • Prompting the user input objects sequentially (waiting for the user to finish the input for each one before moving on to the next)
  • Executing the effects sequentially (each effect will take get its data from the input object it is mapped to)

So an example of a defining an ability could be:

Ability: {
   Inputs: {
       Select Tile,
       Select Unit
   }

   Effects: {
       Move to (input 1),
       Deal 10 damage (input 2)
   }
}

Ideally I want to make the mapping from input to effect typesafe, since each type of effect has a type of target data that it's expecting (e.g. tile or unit). Here's an example of code I wrote to represent the action, input, and effect objects in code:

    public class AbilityData
    {
        List<InputData<Target>> inputs;

        List<AbilityEffect> effects;

        public void exampleCreate()
        {
            BaseInputData<Tile> input = new TileSelectData();
            inputs.Add(input);

            effect = new List<AbilityEffect>();
            BaseAbilityEffect<Tile> effect = new BaseAbilityEffect<Tile>(new TileEffect(), input);
            effects.Add(effect);
        }
        public void exampleRun()
        {
            foreach (InputData<AbilityInput> input in inputs)
            {
                input.promptInput();
            }

            foreach (AbilityEffect effect in effects)
            {
                effect.execute();
            }
        }
    }
   public interface AbilityEffect
    {
        void execute();
    }

    public class BaseAbilityEffect<T> : AbilityEffect where T : Target
    {
        InputData<T> input;

        TargetEffect<T> effect;

        public BaseAbilityEffect(TargetEffect<T> tEffect, InputData<T> input) {
            this.input = input;
            this.effect = tEffect;
        }

        public void execute()
        {
            effect.execute(input.getInput());
        }
    }

    public class TargetEffect<T> where T : Target
    {
        public virtual void execute(T input) { }
    }

    public class UnitEffect : TargetEffect<Unit>
    {
        public override void execute(Unit unit)
        {
            // Do something on the unit
        }
    }

    public class TileEffect : TargetEffect<Tile>
    {
        public override void execute(Tile tile)
        {
            // Do something on the tile
        }
    }

    public interface InputData<out T>
    {
        void promptInput();

        T getInput();
    }

    public abstract class BaseInputData<T> : InputData<T> where T : Target
    {
        public abstract T getInput();

        public abstract void promptInput();
    }

    public class TileSelectData : BaseInputData<Tile>
    {
        public override Tile getInput()
        {
            // Return the stored input
        }

        public override void promptInput()
        {
            // prompt for the input and store it.
        }
    }

    public class UnitSelectData : BaseInputData<Unit>
    {
        public override Unit getInput()
        {
            // Return the stored input
        }

        public override void promptInput()
        {
            // prompt for the input and store it.
        }
    }

This seems like it will work fine in the normal local case, but the problem arises when you need to provide an override for the input. For example, in a networked game, the clients will trigger the input and then send the targets to the master server. The master server would then need to override the input objects to have the targets it received, and then call the effects.

So I want to add something like

void overrideInput(T);

to the InputData interface, but because it uses covariance, I can't use the generic type parameter as a parameter to any of the interface function.

Is there a way around this limitation? The concept seems very simple: ensure that the effect objects are only matched with input objects of the same target type. Of course I could accomplish this in brute force with a bunch of unsafe casts, but it seems like there should be a better way.

1 Answers

I made your code compile by adding another type Tinput. I don't know if that's what you wanted:

    public class AbilityData
    {
        List<InputData<Target,Target>> inputs;

        List<AbilityEffect> effects;

        public void exampleCreate()
        {
            BaseInputData<Tile,Target> input = new TileSelectData();
            inputs.Add(input);

            var effects = new List<AbilityEffect>();
            var effect = new BaseAbilityEffect<Tile,Target>(new TileEffect(), input);
            effects.Add(effect);
        }
        public void exampleRun()
        {
            foreach (InputData<AbilityInput, AbilityInput> input in inputs)
            {
                input.promptInput();
            }

            foreach (AbilityEffect effect in effects)
            {
                effect.execute();
            }
        }
    }
    public interface AbilityEffect
    {
        void execute();
    }

    public class BaseAbilityEffect<T,Tinput> : AbilityEffect 
        where T : Target,Tinput
    {
        InputData<T,Tinput> input;

        TargetEffect<T> effect;

        public BaseAbilityEffect(TargetEffect<T> tEffect, InputData<T,Tinput> input)
        {
            this.input = input;
            this.effect = tEffect;
        }

        public void execute()
        {
            effect.execute(input.getInput());
        }
    }

    public class TargetEffect<T> where T : Target
    {
        public virtual void execute(T input) { }
    }

    public class UnitEffect : TargetEffect<Unit>
    {
        public override void execute(Unit unit)
        {
            // Do something on the unit
        }
    }

    public class TileEffect : TargetEffect<Tile>
    {
        public override void execute(Tile tile)
        {
            // Do something on the tile
        }
    }

    public interface InputData<out T,Tinput>
        where T: Tinput
    {
        void promptInput();

        T getInput();

        void overrideInput(Tinput input);
    }

    public class Target
    {
    }

    public class Tile : Target
    {
    }

    public class TileSelectData : BaseInputData<Tile,Target>
    {
        public TileSelectData()
        {
        }
    }

    public class Unit : Target
    {
    }

    public class AbilityInput
    {
    }

    public class BaseInputData<T,Tinput> : InputData<T,Tinput>
        where T:Tinput
    {
        public T getInput()
        {
            throw new NotImplementedException();
        }

        public void overrideInput(Tinput input)
        {
            throw new NotImplementedException();
        }

        public void promptInput()
        {
            throw new NotImplementedException();
        }
    }
Related