Difference between revisions of "Game Programming Class5"

esse quam videri
Jump to: navigation, search
(Advanced Sprites/Collision)
m (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"")
 
(14 intermediate revisions by the same user not shown)
Line 15: Line 15:
 
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.
  
==Advanced Sprites/Collision==
+
==Input Controller Mongame==
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
+
from project IntroPacManMovementController file PacManController.cs
  
3 tutorial on App Hub
+
https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/PacManController.cs
  
*http://xbox.create.msdn.com/en-us/education/catalog/tutorial/collision_2d_rectangle
+
<syntaxhighlight lang="csharp">
*http://xbox.create.msdn.com/en-US/education/catalog/tutorial/collision_2d_perpixel
+
InputHandler input; //game service to handle input
*http://xbox.create.msdn.com/en-US/education/catalog/tutorial/collision_2d_perpixel_transformed
+
public Vector2 Direction {get; private set;}
 +
...
  
==PerPixelCollision==
+
public PacManController(Game game)
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.
+
        {
 +
            //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");
 +
            }
 +
...
  
The definition for both methods is in the Sprite class (Sprite.cs)
+
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
 +
...
 +
</syntaxhighlight>
 +
 +
The MonoGamePacMan game component then uses the controller
  
==Chase and Evade==
+
https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeff/IntroPacManMovementController/MonoGamePacMan.cs
  
Example of simple states and simple vector geometry.
+
<syntaxhighlight lang="csharp">
 +
internal PacManController controller { get; private set; }
 +
...
 +
public MonoGamePacMan(Game game)
 +
            : base(game)
 +
        {
 +
            this.controller = new PacManController(game);
 +
            ...
 +
        }
 +
...
 +
public override void Update(GameTime gameTime)
 +
        {
 +
            //Elapsed time since last update
 +
            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
 +
           
 +
            this.controller.Update();
  
[http://creators.xna.com/en-US/sample/chaseevade Better example not yet implemented]
+
            this.Location += ((this.controller.Direction * (time / 1000)) * Speed);      //Simple Move
 +
            this.Rotate = this.controller.Rotate;
 +
...
  
==Physics==
+
</syntaxhighlight>
  
 +
==Input Controller Unity==
  
http://farseerphysics.codeplex.com/
+
PlayerController from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/PlayerController.cs
  
==Version Control==
+
<syntaxhighlight lang="csharp">
Subversion (SVN)
+
public Vector2 direction = new Vector2();
 +
    private Vector2 keyDirection;
 +
    private Vector2 padDirection;
 +
...
 +
void Update () {
 +
       
 +
        HandleKeyboard();
 +
 
 +
        HandleGamepad();
 +
...
 +
 
 +
private void HandleGamepad()
 +
    {
 +
        //Gamepad
 +
        padDirection.x = padDirection.y = 0;
 +
        //and keyboard unity already sums them together
 +
        padDirection.x = Input.GetAxis("Horizontal");
 +
        padDirection.y = Input.GetAxis("Vertical");
 +
 
 +
        if (padDirection.magnitude > 0)
 +
        {
 +
            //Debug.Log(padDirection + " " + padDirection.magnitude + " " + (padDirection.magnitude > 0));
 +
            direction += padDirection;
 +
        }
 +
    }
 +
 
 +
</syntaxhighlight>
 +
 
 +
Player.cs from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/Player.cs
 +
 
 +
<syntaxhighlight lang="csharp">
 +
private PlayerController controller;
 +
    public Vector2 Direction = new Vector2(1, 0);
 +
...
 +
void Start()
 +
    {
 +
        //Get PlayerController from game object
 +
        controller = GetComponent<PlayerController>();
 +
        //Log error if controller is null will throw null refernece exception eventually
 +
        if (controller == null)
 +
        {
 +
            Debug.LogWarning("GetComponent of type " + typeof(PlayerController) + " failed on " + this.name, this);
 +
        }
 +
...
 +
 
 +
// Update is called once per frame
 +
    void Update()
 +
    {
 +
        //Check for input from PlayerController
 +
        if (this.controller.hasInputForMoverment)
 +
        {
 +
            this.Direction = this.controller.direction; //Get Direction from PlayerController
 +
</syntaxhighlight>
  
[http://svnbook.red-bean.com/ Free SVN Book]
 
  
===Windows Client and tools===
 
* [http://tortoisesvn.tigris.org/ TortoiseSVN] (Windows)
 
* [http://ankhsvn.open.collab.net/ AnkhSVN]
 
* [http://www.visualsvn.com/visualsvn/ VisualSVN]
 
* [http://www.winmerge.org/ WinMerge]
 
  
 
==Homework==
 
==Homework==
  
'''Collision test game.'''
+
*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
  
Create a game that collides two Sprites. you can use either simple rectangle collision or per pixel as shown in class
+
*InputControllerUnity
  
===Proposal for midterm game.===
+
Make a similar project to you MonoGame InputController.
  
Prepare for a short (5-10) minute pitch of your midterm game. The proposals will happen next week in class. The proposal should include.
+
Use an Input controller class in Unity to control the sprite. Reuse as much code as possible from the Monogame InputContorller
# '''Game Idea''': the basic idea
 
# '''Game Description''': longer description
 
# '''Game Goal''': how do you win or what are you trying to accomplish
 
# '''Audience''': who will play
 
# '''User Control''': how does the player control the game
 

Latest revision as of 03:21, 9 February 2016

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

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
...

The MonoGamePacMan game component then uses the controller

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

internal PacManController controller { get; private set; }
...
public MonoGamePacMan(Game game)
            : base(game)
        {
            this.controller = new PacManController(game);
            ...
         }
...
public override void Update(GameTime gameTime)
        {
            //Elapsed time since last update
            float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            
            this.controller.Update();

            this.Location += ((this.controller.Direction * (time / 1000)) * Speed);      //Simple Move 
            this.Rotate = this.controller.Rotate;
...

Input Controller Unity

PlayerController from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/PlayerController.cs

public Vector2 direction = new Vector2();
    private Vector2 keyDirection;
    private Vector2 padDirection;
...
void Update () {
		        
        HandleKeyboard();

        HandleGamepad();
...

private void HandleGamepad()
    {
        //Gamepad
        padDirection.x = padDirection.y = 0;
        //and keyboard unity already sums them together
        padDirection.x = Input.GetAxis("Horizontal");
        padDirection.y = Input.GetAxis("Vertical");

        if (padDirection.magnitude > 0)
        {
            //Debug.Log(padDirection + " " + padDirection.magnitude + " " + (padDirection.magnitude > 0));
            direction += padDirection;
        }
    }

Player.cs from https://iam.colum.edu:8443/!/#GPMonogame3/view/head/trunk/jeffUnity/UnityGameDemos/Assets/Scripts/PacMan/Player.cs

private PlayerController controller;
    public Vector2 Direction = new Vector2(1, 0);
...
void Start()
    {
        //Get PlayerController from game object
        controller = GetComponent<PlayerController>();
        //Log error if controller is null will throw null refernece exception eventually
        if (controller == null)
        {
            Debug.LogWarning("GetComponent of type " + typeof(PlayerController) + " failed on " + this.name, this);
        }
...

// Update is called once per frame
    void Update()
    {
        //Check for input from PlayerController
        if (this.controller.hasInputForMoverment)
        {
            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