Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Allows the game to exit by checking that a specific key or button was pressed
- // on this frame, ks is the currentstate of the keyboard and ps is the current state
- // of a single gamepad. Every frame I save a backup of the current state so that I
- // can compare the current state and the state in the last frame.
- // If I only checked the current state of input devices, this code would execute on
- // every frame the button is down for. Since this code will hide everything and show
- // the menu when the menu is inactive and then exit when the menu is active, a user
- // pushing escape or back from ingame (or a submenu) would see one frame of the main
- // menu and then exit the game unless they pressed escape for only 1/60th of a second.
- // At this point only p1 can control menus because I didn't want my if conditions
- // to get crazy big.
- if (ks.IsKeyDown(Keys.Escape) && _oldKeyboardState.IsKeyUp(Keys.Escape) ||
- ps.IsButtonDown(Buttons.Back) && _oldPadState.IsButtonUp(Buttons.Back))
- {
- if (_menu.Enabled)
- this.Exit();
- else
- {
- HideAllScene();
- SetTitle();
- _menu.Show();
- }
- }
- // As of December 4, a virtual controller object was built to simplify this a bit.
- // Creating virtual controller objects is pretty straight forward, they have defaults
- // for keys and xbox buttons built in, you just need to give the object a refernce to
- // the game object (this) and the controller identifier (PlayerIndex.One/Two)
- PlayerOne = new VirtualController(this, PlayerIndex.One);
- // Since the keyboard controls on player two should be different, we can manually
- // override them. xbox buttons are differentiated by the playerindex so we can leave
- // that alone. Here are all the buttons I currently change for PlayerTwo:
- PlayerTwo = new VirtualController(this, PlayerIndex.Two)
- { // Regardless if it is one button/key or many, it needs to be an array.
- MovementUpKeys = new[] { Keys.Up },
- MovementDownKeys = new[] { Keys.Down },
- MovementLeftKeys = new[] { Keys.Left },
- MovementRightKeys = new[] { Keys.Right },
- AimUpKeys = new[] { Keys.NumPad8 },
- AimDownKeys = new[] { Keys.NumPad2 },
- AimLeftKeys = new[] { Keys.NumPad4 },
- AimRightKeys = new[] { Keys.NumPad6 },
- // I give player two the option of using any of these keys to fire.
- GameFireKeys = new[] { Keys.Add, Keys.Subtract, Keys.Delete, Keys.Insert, }
- };
- // Next is the start of the menu as of December 4th but now I've got virtual
- // controller telling me if each player is pressing a button for going back. The
- // VirtualController object handles old&new states of the keyboard and gamepad. If
- // either player presses back on this frame, ActionBack will return "True" and this
- // code will be executed.
- if (PlayerOne.ActionBack || PlayerTwo.ActionBack)
- {
- if (_menu.Enabled)
- Exit();
- else
- {
- HideAllScene();
- SetTitle();
- _menu.Show();
- GamePad.SetVibration(PlayerOne.PlayerIndex, 0, 0);
- GamePad.SetVibration(PlayerTwo.PlayerIndex, 0, 0);
- }
- }
- // The following is today's code. I've got the two virtual controllers in an
- // array and I can check if any of them has a key or button pressed on this frame
- // by using a lambda expression. "inputs.Any(p => p.ActionBack)"
- var inputs = new[] {PlayerOne, PlayerTwo};
- if (AllowExit && inputs.Any(p => p.ActionBack))
- {
- if (_menu.Enabled)
- {
- MenuEffects[0].Play();
- Exit();
- }
- else
- {
- MenuEffects[0].Play();
- HideAllScene();
- SetTitle();
- _menu.Show();
- inputs.ForEach(p => GamePad.SetVibration(p.PlayerIndex, 0, 0));
- }
- }
- // Using .Any(p => p.ActionBack) is kind of like doing this:
- foreach(VirtualController p in inputs)
- {
- if(p.ActionBack) return true;
- }
- // Also you don't just need to call methods on p, my own lack of an imagination
- // caused me to think lambda expressions were unnecessary when I first experienced
- // them in MVC. Just like in the for each loop you can do anything you want with the
- // parameter. Consider this:
- Components.Where(c => c is Player).ForEach(p =>
- {
- p.Kill();
- Console.WriteLine("Just murdered {0}!", p);
- });
- Components
- .Where(c => Toolbox.IsComponentDead(c))
- .All(component => Components.Remove(component));
- // The first command would find all the components that are of type Player and call their
- // Kill method and write a line to the console. If you want to execute more than one
- // statement together in a linq expression (Just like you would in an if) you surround it
- // with braces {}. The next line shows calling an outside method with the irater as the
- // parameter.
- // Another great thing about linq expressions is that they delay execution until it is
- // required (known as lazy or deferred execution). This can boost performance significantly.
- // See MSDNs description here: http://msdn.microsoft.com/en-ca/library/bb943859.aspx
- // Also check out this example: http://msdn.microsoft.com/en-ca/library/bb669162.aspx
Advertisement
Add Comment
Please, Sign In to add comment