PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/Samples/Blocks/Game.cs

#
C# | 133 lines | 113 code | 16 blank | 4 comment | 5 complexity | 5db2f857fdaaea550dd0003ce7a5d1a2 MD5 | raw file
Possible License(s): Apache-2.0
  1. using DeltaEngine.Commands;
  2. using DeltaEngine.Core;
  3. using DeltaEngine.Datatypes;
  4. using DeltaEngine.Input;
  5. using DeltaEngine.ScreenSpaces;
  6. namespace Blocks
  7. {
  8. /// <summary>
  9. /// Knits the main control classes together by feeding events raised by one to another
  10. /// </summary>
  11. public class Game
  12. {
  13. public Game(Window window)
  14. {
  15. menu = new MainMenu();
  16. //ncrunch: no coverage start
  17. menu.InitGame += () =>
  18. {
  19. menu.Hide();
  20. StartGame();
  21. }; //ncrunch: no coverage end
  22. menu.QuitGame += window.CloseAfterFrame;
  23. window.Title = "Blocks";
  24. }
  25. private MainMenu menu;
  26. public UserInterface UserInterface { get; private set; }
  27. public Controller Controller { get; private set; }
  28. public bool IsInGame { get; set; }
  29. public void StartGame()
  30. {
  31. UserInterface = new UserInterface(menu.BlocksContent);
  32. Controller = new Controller(DisplayMode, menu.BlocksContent);
  33. IsInGame = true;
  34. Initialize();
  35. }
  36. private void Initialize()
  37. {
  38. SetDisplayMode();
  39. SetControllerEvents();
  40. SetInputEvents();
  41. }
  42. private void SetDisplayMode()
  43. {
  44. var aspectRatio = ScreenSpace.Current.Viewport.Aspect;
  45. DisplayMode = aspectRatio >= 1.0f ? Orientation.Landscape : Orientation.Portrait;
  46. }
  47. protected Orientation DisplayMode { get; set; }
  48. private void SetControllerEvents()
  49. {
  50. Controller.AddToScore += UserInterface.AddToScore;
  51. Controller.Lose += UserInterface.Lose;
  52. }
  53. private void SetInputEvents()
  54. {
  55. CreateCommands();
  56. SetKeyboardEvents();
  57. SetMouseEvents();
  58. SetTouchEvents();
  59. }
  60. private void CreateCommands()
  61. {
  62. commands = new Command[9];
  63. commands[0] = new Command(() => StartMovingBlockLeft());
  64. commands[1] = new Command(() => { Controller.isBlockMovingLeft = false; });
  65. commands[2] = new Command(() => StartMovingBlockRight());
  66. commands[3] = new Command(() => { Controller.isBlockMovingRight = false; });
  67. commands[4] = new Command(() => Controller.RotateBlockAntiClockwiseIfPossible());
  68. commands[5] = new Command(() => { Controller.IsFallingFast = true; });
  69. commands[6] = new Command(() => { Controller.IsFallingFast = false; });
  70. commands[7] = new Command(position => { Pressing(position); });
  71. commands[8] = new Command(() => { Controller.IsFallingFast = false; });
  72. }
  73. private Command[] commands;
  74. private void SetKeyboardEvents()
  75. {
  76. commands[0].Add(new KeyTrigger(Key.CursorLeft));
  77. commands[1].Add(new KeyTrigger(Key.CursorLeft, State.Releasing));
  78. commands[2].Add(new KeyTrigger(Key.CursorRight));
  79. commands[3].Add(new KeyTrigger(Key.CursorRight, State.Releasing));
  80. commands[4].Add(new KeyTrigger(Key.CursorUp));
  81. commands[4].Add(new KeyTrigger(Key.Space));
  82. commands[5].Add(new KeyTrigger(Key.CursorDown));
  83. commands[6].Add(new KeyTrigger(Key.CursorDown, State.Releasing));
  84. }
  85. private void StartMovingBlockLeft()
  86. {
  87. Controller.MoveBlockLeftIfPossible();
  88. Controller.isBlockMovingLeft = true;
  89. }
  90. private void StartMovingBlockRight()
  91. {
  92. Controller.MoveBlockRightIfPossible();
  93. Controller.isBlockMovingRight = true;
  94. }
  95. private void SetMouseEvents()
  96. {
  97. commands[7].Add(new MouseButtonTrigger());
  98. commands[8].Add(new MouseButtonTrigger(MouseButton.Left, State.Releasing));
  99. }
  100. private void Pressing(Vector2D position)
  101. {
  102. if (position.X < 0.4f)
  103. Controller.MoveBlockLeftIfPossible(); //ncrunch: no coverage
  104. else if (position.X > 0.6f)
  105. Controller.MoveBlockRightIfPossible(); //ncrunch: no coverage
  106. else if (position.Y < 0.5f)
  107. Controller.RotateBlockAntiClockwiseIfPossible(); //ncrunch: no coverage
  108. else
  109. Controller.IsFallingFast = true;
  110. }
  111. private void SetTouchEvents()
  112. {
  113. commands[7].Add(new TouchPositionTrigger());
  114. commands[8].Add(new TouchPositionTrigger(State.Releasing));
  115. }
  116. }
  117. }