r/Zenject•
daleth90

[Question]Simple FTG with MVC Pattern

Hello, I'm new to DI Container, so there's still something I couldn't understand.

Here's a little fighting game.

public class Game : ITickable, IInitializable
{
    public Game( [InjectOptional( "Player_1P" )]Character player_1P, 
                    [InjectOptional( "Player_2P" )]Character player_2P)
    { }
}

======================================== public class Character { public Character( CharacterView view ) { } }

public class CharacterView : Monobehaviear
{

}

======================================== public class PlayerController : ITickable { public PlayerController( Character character ) { } }

What I think is MVC Pattern. Here's the installer.

public class GameInstaller : MonoInstaller
{
    [SerializeField]
    private GameObject characterPrefab = null;

    [SerializeField]
    private GameObject redCharacterPrefab = null;

    [SerializeField]
    private GameObject greenCharacterPrefab = null;

    [InjectOptional( "Player_1P" )]
    private System.Type character_1 = typeof( Character );

    [InjectOptional( "Player_2P" )]
    private System.Type character_2 = typeof( Character );

    public override void InstallBindings()
    {
        Container.Bind<Game>().ToSingle();
        Container.Bind<IInitializable>().ToSingle<Game>();
        
        // I guess I can use ZenUtil.LoadScene to deal with character selection
        Container.Bind<Character>().ToTransient<Character>();
        Container.Bind<Character>( "Player_1P" ).ToTransient( character_1 );
        Container.Bind<Character>( "Player_2P" ).ToTransient( character_2 );

        Container.Bind<CharacterView>().ToTransientPrefab<CharacteView>( baseCharacterPrefab );
        Container.Bind<CharacterView>().ToTransientPrefab<CharacteView>( redCharacterPrefab ).WhenInjectedInto<Red>();
        Container.Bind<CharacterView>().ToTransientPrefab<CharacteView>( greenCharacterPrefab ).WhenInjectedInto<Green>();
        // Red & Green, which inherited from Character

        //How to bind my controller
    }
}

========================================= The binding between Character and CharacterView seems OK.

My question is:

  1. I wonder the character injection by [InjectionOptional] is used correct. It seems complex, or just because I'm still not used to it. I want to know if there's better solution.

  2. I don't know how to bind my controller since I need to have a controller in each of two character.

Thanks~

Posted Jul 1, 2015, 4:56 PM UTC · edited

22
Loading comments…