PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/Samples/Blocks/Game.cs

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