Difference between revisions of "Game Programming Class5"
m (Text replacement - "</csharp>" to "</syntaxhighlight>") |
m (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >") |
||
Line 21: | Line 21: | ||
https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/PacManController.cs | https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/PacManController.cs | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
InputHandler input; //game service to handle input | InputHandler input; //game service to handle input | ||
public Vector2 Direction {get; private set;} | public Vector2 Direction {get; private set;} | ||
Line 53: | Line 53: | ||
https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/MonoGamePacMan.cs | https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/MonoGamePacMan.cs | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
internal PacManController controller { get; private set; } | internal PacManController controller { get; private set; } | ||
... | ... | ||
Line 80: | Line 80: | ||
PlayerController from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/PlayerController.cs | PlayerController from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/PlayerController.cs | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
public Vector2 direction = new Vector2(); | public Vector2 direction = new Vector2(); | ||
private Vector2 keyDirection; | private Vector2 keyDirection; | ||
Line 111: | Line 111: | ||
Player.cs from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/Player.cs | Player.cs from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/Player.cs | ||
− | <csharp> | + | <syntaxhighlight lang="csharp" line="1" > |
private PlayerController controller; | private PlayerController controller; | ||
public Vector2 Direction = new Vector2(1, 0); | public Vector2 Direction = new Vector2(1, 0); |
Revision as of 18:29, 25 January 2016
Contents
Game Services
Review Game Components
IntroGameLibrary
Turn the console class into a service
Each GameService needs to have a unique interface so that there is an entry in the system types table. The interface allows the game to return the correct type.
- GameConsole.cs
All of these files are available in the IntoGameLibrary Project in the classfolders.
Input Controller Mongame
from project IntroPacManMovementController file PacManController.cs
1 InputHandler input; //game service to handle input
2 public Vector2 Direction {get; private set;}
3 ...
4
5 public PacManController(Game game)
6 {
7 //get input handler from game services
8 input = (InputHandler)game.Services.GetService<IInputHandler>();
9 if (input == null)
10 {
11 throw new Exception("PacMan controller depends on InputHandler service please add Input Handler as a service first");
12 }
13 ...
14
15 public void Update() //update accesses InputHandler
16 {
17 //Input for update from analog stick
18 GamePadState gamePad1State = input.GamePads[0]; //HACK hard coded player index
19 #region LeftStick
20 Vector2 pacStickDir = Vector2.Zero;
21 if (gamePad1State.ThumbSticks.Left.Length() > 0.0f)
22 {
23 pacStickDir = gamePad1State.ThumbSticks.Left;
24 pacStickDir.Y *= -1; //Invert Y Axis
25 ...
The MonoGamePacMan game component then uses the controller
1 internal PacManController controller { get; private set; }
2 ...
3 public MonoGamePacMan(Game game)
4 : base(game)
5 {
6 this.controller = new PacManController(game);
7 ...
8 }
9 ...
10 public override void Update(GameTime gameTime)
11 {
12 //Elapsed time since last update
13 float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
14
15 this.controller.Update();
16
17 this.Location += ((this.controller.Direction * (time / 1000)) * Speed); //Simple Move
18 this.Rotate = this.controller.Rotate;
19 ...
Input Controller Unity
PlayerController from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/PlayerController.cs
1 public Vector2 direction = new Vector2();
2 private Vector2 keyDirection;
3 private Vector2 padDirection;
4 ...
5 void Update () {
6
7 HandleKeyboard();
8
9 HandleGamepad();
10 ...
11
12 private void HandleGamepad()
13 {
14 //Gamepad
15 padDirection.x = padDirection.y = 0;
16 //and keyboard unity already sums them together
17 padDirection.x = Input.GetAxis("Horizontal");
18 padDirection.y = Input.GetAxis("Vertical");
19
20 if (padDirection.magnitude > 0)
21 {
22 //Debug.Log(padDirection + " " + padDirection.magnitude + " " + (padDirection.magnitude > 0));
23 direction += padDirection;
24 }
25 }
Player.cs from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/Player.cs
1 private PlayerController controller;
2 public Vector2 Direction = new Vector2(1, 0);
3 ...
4 void Start()
5 {
6 //Get PlayerController from game object
7 controller = GetComponent<PlayerController>();
8 //Log error if controller is null will throw null refernece exception eventually
9 if (controller == null)
10 {
11 Debug.LogWarning("GetComponent of type " + typeof(PlayerController) + " failed on " + this.name, this);
12 }
13 ...
14
15 // Update is called once per frame
16 void Update()
17 {
18 //Check for input from PlayerController
19 if (this.controller.hasInputForMoverment)
20 {
21 this.Direction = this.controller.direction; //Get Direction from PlayerController
Homework
- InputControllerMonogame
Update your movement project in MonoGame to have good separation of concern and use an input class with an input controller.
Use the IntroGameLibrary if you like to quickly make drawable sprites
- InputControllerUnity
Make a similar project to you MonoGame InputController.
Use an Input controller class in Unity to control the sprite. Reuse as much code as possible from the Monogame InputContorller