Difference between revisions of "Game Programming Class5"

esse quam videri
Jump to: navigation, search
(Homework)
(Advanced Sprites/Collision)
Line 14: Line 14:
  
 
All of these files are available in the IntoGameLibrary Project in the classfolders.
 
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
 +
 +
<csharp>
 +
InputHandler input; //game service to handle input
 +
public Vector2 Direction {get; private set;}
 +
...
 +
 +
public PacManController(Game game)
 +
        {
 +
            //get input handler from game services
 +
            input = (InputHandler)game.Services.GetService<IInputHandler>();
 +
            if (input == null)
 +
            {
 +
                throw new Exception("PacMan controller depends on InputHandler service please add Input Handler as a service first");
 +
            }
 +
...
 +
 +
public void Update() //update accesses InputHandler
 +
        {
 +
            //Input for update from analog stick
 +
            GamePadState gamePad1State = input.GamePads[0]; //HACK hard coded player index
 +
            #region LeftStick
 +
            Vector2 pacStickDir = Vector2.Zero;
 +
            if (gamePad1State.ThumbSticks.Left.Length() > 0.0f)
 +
            {
 +
                pacStickDir = gamePad1State.ThumbSticks.Left;
 +
                pacStickDir.Y *= -1;      //Invert Y Axis
 +
...
 +
</csharp>
 +
 +
The MonoGamePacMan game component then uses the controller
 +
 +
https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/MonoGamePacMan.cs
 +
 +
<csharp>
 +
internal PacManController controller { get; private set; }
 +
...
 +
public MonoGamePacMan(Game game)
 +
            : base(game)
 +
        {
 +
            this.controller = new PacManController(game);
 +
 +
</csharp>
 +
 +
==Input Controller Unity==
 +
 +
  
 
==Advanced Sprites/Collision==
 
==Advanced Sprites/Collision==

Revision as of 02:59, 6 October 2015

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

<csharp> InputHandler input; //game service to handle input public Vector2 Direction {get; private set;} ...

public PacManController(Game game)

       {
           //get input handler from game services
           input = (InputHandler)game.Services.GetService<IInputHandler>();
           if (input == null)
           {
               throw new Exception("PacMan controller depends on InputHandler service please add Input Handler as a service first");
           }

...

public void Update() //update accesses InputHandler

       {
           //Input for update from analog stick
           GamePadState gamePad1State = input.GamePads[0]; //HACK hard coded player index
           #region LeftStick
           Vector2 pacStickDir = Vector2.Zero;
           if (gamePad1State.ThumbSticks.Left.Length() > 0.0f)
           {
               pacStickDir = gamePad1State.ThumbSticks.Left;
               pacStickDir.Y *= -1;      //Invert Y Axis

... </csharp>

The MonoGamePacMan game component then uses the controller

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

<csharp> internal PacManController controller { get; private set; } ... public MonoGamePacMan(Game game)

           : base(game)
       {
           this.controller = new PacManController(game);

</csharp>

Input Controller Unity

Advanced Sprites/Collision

Simple Collision

Check if two rectangles intersect. By adding a rectangle that represents the area of the sprite it's easy to ask XNA if two rectangles intersect

3 tutorial on App Hub

PerPixelCollision

Load both textures into a color array. Make a rectangle of the intersection of the two textures. Check all the pixels in intersection of the color arrays for intersecting pixels.

The definition for both methods is in the Sprite class (Sprite.cs)

Chase and Evade

Example of simple states and simple vector geometry.

Better example not yet implemented

PacMan Chase and Evade example in repo IntroChaseEvade demostratea

  • Collision
  • State for feeble AI
  • Sprite Markers

Homework

InputControllerMonogame

InputControllerUnity