PlayerInput
public enum InputState
{
nothing, pressed, released, held
}
//TODO Change this to match the types of buttons your game can do
public class PlayerInput : IEquatable<PlayerInput>
{
static int currentPlayerInputID = 1;
public static int GetCurrentPlayerInputID()
{
return currentPlayerInputID;
}
public static void IncrementCurrentPlayerInputID()
{
currentPlayerInputID++;
}
//I've included player id just in case you want your game to have local coop but can also play online
public int playerID;
public int playerInputID;
public Vector2 moveAxis = new Vector2();
//TODO add another axis if dual sticking or whatever
//Buttons -add more or rename
public InputState A, B, C, D;
//locally perceived gameTime
public int gameTick;
//...
This class represents a single frame of input/s for a single NetworkEntity/player ingame. Each NetworkEntityManager GameLoop() inputs are collected for the current player(usually the client) and actioned locally, but also queued up to be sent to the Server later so they can be confirmed. Each PlayerInput gets a unique playerInputID and given the playerID they are associated with. The gameTick at that time is also stored at creation so we know when this input occurred.
Note: The rest of the data in the class should be tweaked by you for your game. Currently I send a moveAxis value representing using the directional arrows on the keyboard or left stick on a gamepad, as well as 4 generic buttons A,B,C,D as InputStates so they can represent any keyboard or gamepad button I like and record whether they are pressed, held, released or not. Whatever your inputs are for your game, it will need to be in here.
This class holds data relevant to a users inputs for a frame and is sent to the server often.
Methods
public void WriteInputToPacket(NetDataWriter writer)
Used for writing information from this class to a packet, make sure it mirrors the read. Basically all of the attributes you have in the class should be written.
public void ReadInputFromPacket(NetDataReader reader)
Used to read data from a packet and store it in this classes attributes, make sure it mirrors the write. Basically all of the attributes you have in the class should be read
public static PlayerInput BuildPlayerInputWithNewID(int playerID)
Helper function to make a new PlayerInput object setup with new unique ID and passed the playerID this input is associated with
public bool Equals(PlayerInput other)
Compare to see if this PlayerInput is the same as the other or not
public override int GetHashCode()
Not sure I use this anymore but returns the playerInputID