Game Programming Class5

esse quam videri
Revision as of 18:29, 25 January 2016 by Jeff (talk | contribs) (Text replacement - "<csharp>" to "<syntaxhighlight lang="csharp" line="1" >")
Jump to: navigation, search

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

https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/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

https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/MonoGamePacMan.cs

 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