Making StarterGame 2 player
by Ty Newton · 01/21/2007 (1:20 am) · 1 comments
Now I've had an interesting time modifying the StarterGame example so it supports 2 players on the keyboard. Here's the short story:
I'll apologize in advance for using programmer speak in this. sorry, but I can't help it.
Open VC#E and create a new project from the StarterGame example. You'll notice the MovementComponent.cs file in the project. Open it and you'll see this is where the keyboard events are bound (_SetupInputMap() method). _SetupInputMap() binds the owner of MovementComponent to the arrow and WASD keys. The owner of MovementComponent is the object that you attached it to in TGBX.
It's important to understand that a MovementComponent object is instantiated for each object that you attach it to in TGBX. ie. if you create 2 sprites (1 for each player) and then attach a MovementComponent to both of them; then 2 MovementComponent objects will be instantiated (1 for each sprite) when you run your game.
How do you know which, of the many, MovementComponents you are when in your code?
The PlayerNumber property can be used to differentiate between the different instantiations of MovementComponent. e.g. if (PlayerNumber == 0) then ... You set the player number in TGBX when you attach MovementComponent to your object.
You can also use the _owner object that is stored by the _InitComponent() method. _owner is a T2DSceneObject and it has a Name property. If you set the Name property in TGBX (script section when in edit mode) you can do a comparison in your code e.g. if (_owner.Name.equals("player1")) then ...
Having many MovementComponents instantiated makes it pretty easy to add 2 player support to StarterGame. Simply modify _SetupInputMap() so the arrow keys are bound to player 0 and WASD are bound to player 1. e.g. starting at line 79.
[code]
// keyboard controls
int keyboardId = InputManager.Instance.FindDevice(keyboard);
if (keyboardId >= 0)
{
switch (playerIndex)
{
case 0:
// arrow keys
inputMap.BindMove(keyboardId, (int)Keys.Right, MoveMapTypes.StickDigitalRight, 0);
inputMap.BindMove(keyboardId, (int)Keys.Left, MoveMapTypes.StickDigitalLeft, 0);
inputMap.BindMove(keyboardId, (int)Keys.Up, MoveMapTypes.StickDigitalUp, 0);
inputMap.BindMove(keyboardId, (int)Keys.Down, MoveMapTypes.StickDigitalDown, 0);
break;
case 1:
// WASD
inputMap.BindMove(keyboardId, (int)Keys.D, MoveMapTypes.StickDigitalRight, 0);
inputMap.BindMove(keyboardId, (int)Keys.A, MoveMapTypes.StickDigitalLeft, 0);
inputMap.BindMove(keyboardId, (int)Keys.W, MoveMapTypes.StickDigitalUp, 0);
inputMap.BindMove(keyboardId, (int)Keys.S, MoveMapTypes.StickDigitalDown, 0);
break;
default:
break;
}
}
[\code]
This uses playerIndex to differentiate between the different instantiations of MovementComponent. If you look at the _InitComponent() method you'll see that playerIndex is actually PlayerNumber.
Now you have a MovementComponent class that can properly handle 2 players. All you have to do is create 2 objects in TGBX, attach MovementComponent to both of them and set the PlayerNumber (in TGBX) to 0 and 1 respectively.
Thanks to Thomas Buscaglia for setting me straight on this stuff (I went around in circles for a little while) and providing the code above.
I'll apologize in advance for using programmer speak in this. sorry, but I can't help it.
Open VC#E and create a new project from the StarterGame example. You'll notice the MovementComponent.cs file in the project. Open it and you'll see this is where the keyboard events are bound (_SetupInputMap() method). _SetupInputMap() binds the owner of MovementComponent to the arrow and WASD keys. The owner of MovementComponent is the object that you attached it to in TGBX.
It's important to understand that a MovementComponent object is instantiated for each object that you attach it to in TGBX. ie. if you create 2 sprites (1 for each player) and then attach a MovementComponent to both of them; then 2 MovementComponent objects will be instantiated (1 for each sprite) when you run your game.
How do you know which, of the many, MovementComponents you are when in your code?
The PlayerNumber property can be used to differentiate between the different instantiations of MovementComponent. e.g. if (PlayerNumber == 0) then ... You set the player number in TGBX when you attach MovementComponent to your object.
You can also use the _owner object that is stored by the _InitComponent() method. _owner is a T2DSceneObject and it has a Name property. If you set the Name property in TGBX (script section when in edit mode) you can do a comparison in your code e.g. if (_owner.Name.equals("player1")) then ...
Having many MovementComponents instantiated makes it pretty easy to add 2 player support to StarterGame. Simply modify _SetupInputMap() so the arrow keys are bound to player 0 and WASD are bound to player 1. e.g. starting at line 79.
[code]
// keyboard controls
int keyboardId = InputManager.Instance.FindDevice(keyboard);
if (keyboardId >= 0)
{
switch (playerIndex)
{
case 0:
// arrow keys
inputMap.BindMove(keyboardId, (int)Keys.Right, MoveMapTypes.StickDigitalRight, 0);
inputMap.BindMove(keyboardId, (int)Keys.Left, MoveMapTypes.StickDigitalLeft, 0);
inputMap.BindMove(keyboardId, (int)Keys.Up, MoveMapTypes.StickDigitalUp, 0);
inputMap.BindMove(keyboardId, (int)Keys.Down, MoveMapTypes.StickDigitalDown, 0);
break;
case 1:
// WASD
inputMap.BindMove(keyboardId, (int)Keys.D, MoveMapTypes.StickDigitalRight, 0);
inputMap.BindMove(keyboardId, (int)Keys.A, MoveMapTypes.StickDigitalLeft, 0);
inputMap.BindMove(keyboardId, (int)Keys.W, MoveMapTypes.StickDigitalUp, 0);
inputMap.BindMove(keyboardId, (int)Keys.S, MoveMapTypes.StickDigitalDown, 0);
break;
default:
break;
}
}
[\code]
This uses playerIndex to differentiate between the different instantiations of MovementComponent. If you look at the _InitComponent() method you'll see that playerIndex is actually PlayerNumber.
Now you have a MovementComponent class that can properly handle 2 players. All you have to do is create 2 objects in TGBX, attach MovementComponent to both of them and set the PlayerNumber (in TGBX) to 0 and 1 respectively.
Thanks to Thomas Buscaglia for setting me straight on this stuff (I went around in circles for a little while) and providing the code above.
Torque Owner Thomas Buscaglia