#can you share the factory code?

1 messages · Page 1 of 1 (latest)

quasi moon
#

abbreviated

public class CharacterStateFactory {
    Character _character;
    Dictionary<string, CharacterState> _stateDict = new Dictionary<string, CharacterState>();

    public CharacterStateFactory(Character character) {
        _character = character;

        // Active
        _stateDict["Standing"] = new CharacterStateStanding(_character, this);
        _stateDict["Dashing"] = new CharacterStateDashing(_character, this);
        _stateDict["JumpSquatting"] = new CharacterStateJumpSquatting(_character, this);
        _stateDict["Aerial"] = new CharacterStateAerial(_character, this);
        _stateDict["Sliding"] = new CharacterStateSliding(_character, this);
        _stateDict["CommandMovement"] = new CharacterStateCommandMovement(_character, this);
        // ...
    }

    // active
    public CharacterState Standing() => _stateDict["Standing"];
    public CharacterState Dashing() => _stateDict["Dashing"];
    public CharacterState JumpSquatting() => _stateDict["JumpSquatting"];
    public CharacterState Sliding() => _stateDict["Sliding"];
    public CharacterState CommandMovement() => _stateDict["CommandMovement"];
    public CharacterState Aerial() => _stateDict["Aerial"];
    // ...
}
peak wraith
#

You can use reflection to iterate over all types that are derived from another type.

#

you'd then be able to use Activator to construct a new instance of each one

quasi moon
#

okay, cool, I'll see if I can figure that out, thanks

peak wraith
#

The first thing that comes to mind: go through every type in your assembly and test if CharacterState is assignable from it