Noobie here... I've started learning monogame development and currently working on a simple snake game. I'm having trouble with the snake's movement, I figured I could make it tile-based movement but eventually decided to create turning points everytime the head changes direction, insert those into a list and remove a point when the last body unit have passed through it.
The method that gets keyboard input and creates the turning points:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
player.update();
if (Keyboard.GetState().IsKeyDown(Keys.Up) || Keyboard.GetState().IsKeyDown(Keys.Down))
{
if (Keyboard.GetState().IsKeyDown(Keys.Up))
player.direction.Y = -3f;
else
player.direction.Y = 3f;
player.direction.X = 0;
/**
* Creating a turnPoint that will save the change in movement
* and where it occured.
* **/
player.turnPoint.Add(new Player.turningPoint(player.position, player.direction));
return;
}
if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.Right))
{
if (Keyboard.GetState().IsKeyDown(Keys.Left))
player.direction.X = -3f;
else
player.direction.X = 3f;
player.direction.Y = 0;
player.turnPoint.Add(new Player.turningPoint(player.position, player.direction));
}
base.Update(gameTime);
}
GameObject which is used as the snake's tail basically consists of 2 Vectors, one for position, the other for movement direction.
public struct turningPoint // Used to save the point and specific change in movement
{
public Vector2 location { get; }
public Vector2 velocityChange { get; }
public turningPoint(Vector2 location, Vector2 velocityChange)
{
this.location = location;
this.velocityChange = velocityChange;
}
}
public List<turningPoint> turnPoint = new List<turningPoint>();
private List<GameObject> bodyUnits = new List<GameObject>();
And finally, the method in whic I update the body units:
private void updateBodyUnits()
{
for(int i = 0; i < bodyUnits.Count; i++)
{
//Update the unit's position using it's current movement direction
bodyUnits[i].position.X += bodyUnits[i].direction.X;
bodyUnits[i].position.Y += bodyUnits[i].direction.Y;
foreach(turningPoint point in turnPoint)
{
if(bodyUnits[i].position.X == point.location.X && bodyUnits[i].position.Y == point.location.Y)
{ //If the unit is currently on a specific turning point, change it's direction
bodyUnits[i].direction.X = point.velocityChange.X;
bodyUnits[i].direction.Y = point.velocityChange.Y;
if (i == bodyUnits.Count - 1) //Remove turn point if the last
//unit passed through
turnPoint.Remove(point);
break;
}
}
}
}
Basically it kinda works, if the movement the snake is low. If I set direction. X\Y to 1, or 2 even it kinda works, but above that it seems as if the units just speed through the turning point, ignoring it (and currently there just the head with 3 units of tail). So, is my code ridiculously inefficient? Or the method I am using is wrong altogether?