コード例 #1
0
ファイル: Player1Processor.cs プロジェクト: Chris83/RetroBot
        public Player1Processor(CommandCunk chunk)
        {
            InitController();

            RunChunk(chunk);
        }
コード例 #2
0
ファイル: Player1Processor.cs プロジェクト: Chris83/RetroBot
        private void RunChunk(CommandCunk chunk)
        {
            InputSimulator inputSim = new InputSimulator();

            BringToFront(chunk.Window);

            Command[] commands = chunk.Commands.ToArray();
            int currentAction = 0;
            int nextAction = 1;

            int numActions = commands.Length;

            Stopwatch timer = new Stopwatch();
            int maxTime = commands.Last().End;

            timer.Start();
            while (timer.ElapsedMilliseconds <= maxTime)
            {
                if (currentAction < numActions)
                {
                    if (timer.ElapsedMilliseconds >= commands[currentAction].Start && !commands[currentAction].Pressed)
                    {
                        commands[currentAction].Pressed = true;
                        commands[currentAction].Released = false;
                        // KeyDown -- Console.WriteLine("Pressing key " + actions[currentAction].Key + " for " + (actions[currentAction].End - actions[currentAction].Start).ToString() + "ms");
                        inputSim.Keyboard.KeyDown(controller[(int)commands[currentAction].Key]);
                    }

                    if (timer.ElapsedMilliseconds >= commands[currentAction].End && !commands[currentAction].Released)
                    {
                        commands[currentAction].Released = true;
                        // KeyUp  -- Console.WriteLine("Releasing key " + actions[currentAction].Key);
                        inputSim.Keyboard.KeyUp(controller[(int)commands[currentAction].Key]);
                        currentAction++;
                        nextAction++;
                    }

                    // If we have any combined presses need to check the next one to see if we have to press it early
                    if (nextAction < numActions)
                    {
                        if (timer.ElapsedMilliseconds >= commands[nextAction].Start && !commands[nextAction].Pressed)
                        {
                            commands[nextAction].Pressed = true;
                            commands[nextAction].Released = false;
                            // KeyDown -- Console.WriteLine("Pressing key " + actions[nextAction].Key + " for " + (actions[nextAction].End - actions[nextAction].Start).ToString() + "ms");
                            inputSim.Keyboard.KeyDown(controller[(int)commands[nextAction].Key]);
                        }
                    }
                }
            }
            timer.Stop();

            // This is the old way... The new way is mroe flexible and should take less time to execute,
            // by sending less keys than initially input when able.
            /*bool[] keyPressedUp = new bool[chunk.Commands.Count];
            Key lastKey = new Key();

            for (int i = 0; i < chunk.Commands.Count; i++)
            {
                keyPressedUp[i] = false;

                if (i > 0 && lastKey == chunk.Commands[i])
                {
                    keyPressedUp[i - 1] = true;
                    inputSim.Keyboard.KeyUp(controller[(int)chunk.Commands[i]]);
                    Thread.Sleep(25);
                }

                inputSim.Keyboard.KeyDown(controller[(int)chunk.Commands[i]]);
                Thread.Sleep(25);

                lastKey = chunk.Commands[i];
            }

            for (int i = 0; i < chunk.Commands.Count; i++)
            {
                if (!keyPressedUp[i])
                {
                    inputSim.Keyboard.KeyUp(controller[(int)chunk.Commands[i]]);
                    Thread.Sleep(25);
                }
            }*/
        }