//Constructor sets the game for a singleplayer game //SingletonPattern private Control() { escape = ConsoleKey.Escape.GetHashCode(); controlInterfaces = new ControlInterface[1]; threads = new Thread[1]; controlInterfaces[0] = new ControlInterface("player1", new ControlKeySetting()); threads[0] = new Thread(new ParameterizedThreadStart(transferKeyLoop)); }
//This is the only option to change the gam multiplayer. Not importent yet. public void setPlayerCount(byte controlInterfaceCount) { lock (semaforSetSyncObject) { semafor = controlInterfaceCount; if (controlInterfaceCount == controlInterfaces.Length) { return; } controlInterfaces = new ControlInterface[controlInterfaceCount]; threads = new Thread[controlInterfaceCount]; for (int i = 0; i < controlInterfaceCount; i++) { controlInterfaces[i] = new ControlInterface("player" + (i + 1).ToString(), new ControlKeySetting()); threads[i] = new Thread(new ParameterizedThreadStart(transferKeyLoop)); } } }
//Transports key code to one of the ControlInterfaces. private void transferKeyLoop(object param) { ControlInterface target = controlInterfaces[(int)param]; while (true) { are.WaitOne(); int keyCode = pressedKeyCode; if (pressedKeyCode == escape) { break; } bool found = target.keyHandler(this, pressedKeyCode); lock (semaforSetSyncObject) { semafor++; if (semafor == controlInterfaces.Length && found == false) { Console.WriteLine("Invalidate transferKeyLoop"); Graphic.Invalidate(); } } } }