I am currently creating a small game using C#, and I am having some trouble working out how to handle user input. I can move my model around the map normally, however I am looking to implement a system where the user can input a number of different commands, and then when they hit enter, the string of commands will be executed. I have tried to implement a queue system to take the user input, and pass them to the model, but I haven't been able to get it to work. My input handler class:
class InputHandler
{
Queue<int> userCommands;
/*
* FORWARD = 1
* REVERSE = 2
* RIGHT TURN = 3
* LEFT TURN = 4
*/
public InputHandler()
{
//userCommands = new Queue<Keys>();
userCommands = new Queue<int>();
}
public void EmptyQueue()
{
userCommands.Clear();
}
public Queue<int> GetUserCommands()
{
KeyboardState ks = Keyboard.GetState();
int move;
while(ks.IsKeyUp(Keys.Enter))
{
if(ks.IsKeyDown(Keys.W))
{
move = 1;
userCommands.Enqueue(move);
}
if(ks.IsKeyDown(Keys.S))
{
move = 2;
userCommands.Enqueue(move);
}
if(ks.IsKeyDown(Keys.D))
{
move = 3;
userCommands.Enqueue(move);
}
if(ks.IsKeyDown(Keys.A))
{
move = 4;
userCommands.Enqueue(move);
}
}
return userCommands;
}
}
The Update method for the model:
public void Update(GameTime gametime, BoundingSphere bounding, int move)
{
KeyboardState ks = Keyboard.GetState();
oldModelYaw = modelYaw;
oldModelPosition = modelPosition;
if (!isMoving)
{
if (move == 1)
{
isMoving = true;
moveType = FORWARD;
}
if (move == 2)
{
isMoving = true;
moveType = REVERSE;
}
if (move == 3)
{
isMoving = true;
moveType = RIGHT_TURN;
}
if (move == 4)
{
isMoving = true;
moveType = LEFT_TURN;
}
}
if (isMoving)
{
switch (moveType)
{
case FORWARD:
if (count <= 100)
{
modelPosition += modelRotation * velocity;
count++;
}
else
{
count = 0;
isMoving = false;
}
break;
case REVERSE:
if (count <= 100)
{
modelPosition -= modelRotation * velocity;
count++;
}
else
{
count = 0;
isMoving = false;
}
break;
case RIGHT_TURN:
if (count <= 180)
{
modelYaw -= MathHelper.ToRadians(0.5f);
modelRotation = Vector3.Transform(modelDirection, Matrix.CreateFromYawPitchRoll(modelYaw, 0.0f, 0.0f));
count++;
}
else
{
count = 0;
isMoving = false;
}
break;
case LEFT_TURN:
if (count <= 180)
{
modelYaw += MathHelper.ToRadians(0.5f);
modelRotation = Vector3.Transform(modelDirection, Matrix.CreateFromYawPitchRoll(modelYaw, 0.0f, 0.0f));
count++;
}
else
{
count = 0;
isMoving = false;
}
break;
}
modelPosition.Y = GetHeight();
translation = Matrix.CreateTranslation(modelPosition);
rotation = Matrix.CreateRotationY(modelYaw) * GetModelNormal();
bounding.Center = modelPosition;
if (scenery.TestCollision(bounding))
{
modelPosition = oldModelPosition;
modelYaw = oldModelYaw;
modelPosition.Y = GetHeight();
translation = Matrix.CreateTranslation(modelPosition);
rotation = Matrix.CreateRotationY(modelYaw) * GetModelNormal();
bounding.Center = modelPosition;
}
}
}
The Update method in the game class
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
if(commandQueue.Count <= 0)
{
commandQueue = input.GetUserCommands();
}
else
{
model.Update(gameTime, rock.GetBoundingSphere, userCommands.Dequeue());
camera.Update(gameTime, m_model.GetPosition, m_model.GetDirection);
base.Update(gameTime);
}
}
If anyone could give me some help with this, or point me in the right direction, I would greatly appreciate it.