Created
August 9, 2018 04:18
-
-
Save phoenleo/46de250d9c9eb2cbb1c0fc20c816ab20 to your computer and use it in GitHub Desktop.
Example Covariance And Contravariance in C Sharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using UnityEngine; | |
| using Random = System.Random; | |
| public class EntityReducer | |
| { | |
| private Contexts _contexts; | |
| public EntityReducer (Contexts contexts) { | |
| _contexts = contexts; | |
| } | |
| public void Dispatch (IGameAction action) { | |
| if (TryToInvokeCallback<InitGameAction>(action, InitGame) || | |
| TryToInvokeCallback<LoadMatchThreeBoardAction>(action, LoadMatchThreeBoard)) | |
| { | |
| return; | |
| } | |
| } | |
| void InitGame (Contexts contexts, InitGameAction action) { | |
| Dispatch(new LoadMatchThreeBoardAction { | |
| row = 3, | |
| column = 3 | |
| }); | |
| } | |
| void LoadMatchThreeBoard (Contexts contexts, LoadMatchThreeBoardAction action) { | |
| for (int i = 0; i < action.row; i++) { | |
| for (int j = 0; j < action.column; j++) { | |
| _contexts.matchThree.CreateMatchThreeEntity( | |
| new IntVector2(i, j), | |
| i == 0 ? MatchType.A : (j % 2 == 0 ? MatchType.B : MatchType.C) | |
| ); | |
| } | |
| } | |
| } | |
| /* | |
| |------------------- | |
| | Utilities | |
| |------------------- | |
| */ | |
| bool TryToInvokeCallback <T>(IGameAction action, Action<Contexts, T> callback) where T : IGameAction { | |
| if (action.GetType() == typeof(T)) { | |
| callback(_contexts, (T)action); | |
| return true; | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment