Skip to content

Commit

Permalink
New directions (#53)
Browse files Browse the repository at this point in the history
* Updates movement to support 8 cardinal directions

* Updates the control view

* Updates loading system
  • Loading branch information
MaxEdwards20 authored Apr 9, 2024
1 parent 68dd342 commit 3881ccf
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/Client/Components/Controls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class Controls : Component
public Control SnakeLeft = new Control(Keys.Left);
[DataMember(Name = "SnakeRight")]
public Control SnakeRight = new Control(Keys.Right);
[DataMember(Name = "SnakeUp")]
public Control SnakeUp = new Control(Keys.Up);
[DataMember(Name = "SnakeDown")]
public Control SnakeDown = new Control(Keys.Down);
[DataMember (Name = "UseKeyboard")]
public bool UseKeyboard = true;
}
Expand Down
14 changes: 12 additions & 2 deletions src/Client/Menu/ControlSettingsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public enum ControlStateEnum
{
SnakeLeft,
SnakeRight,
SnakeUp,
SnakeDown,
None
}

Expand Down Expand Up @@ -81,6 +83,12 @@ public override void update(GameTime gameTime)
case ControlStateEnum.SnakeRight:
m_controls.SnakeRight.switchKey(key);
break;
case ControlStateEnum.SnakeUp:
m_controls.SnakeUp.switchKey(key);
break;
case ControlStateEnum.SnakeDown:
m_controls.SnakeDown.switchKey(key);
break;
}
// Now we persist any changes
m_controlsPersistence.SaveControls(m_controls);
Expand Down Expand Up @@ -109,6 +117,8 @@ public override void render(GameTime gameTime)
Drawing.CustomDrawString(m_font, MESSAGE, new Vector2(halfWidth, halfHeight - headerStringSize.Y), Colors.displayColor, m_spriteBatch);
Drawing.CustomDrawString(m_font, "Move Left " + m_controls.SnakeLeft.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 50), getStringColor(ControlStateEnum.SnakeLeft), m_spriteBatch);
Drawing.CustomDrawString(m_font, "Move Right " + m_controls.SnakeRight.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 100), getStringColor(ControlStateEnum.SnakeRight), m_spriteBatch);
Drawing.CustomDrawString(m_font, "Move Up " + m_controls.SnakeUp.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 150), getStringColor(ControlStateEnum.SnakeUp), m_spriteBatch);
Drawing.CustomDrawString(m_font, "Move Down " + m_controls.SnakeDown.key, new Vector2(halfWidth, halfHeight - headerStringSize.Y + 2 + 200), getStringColor(ControlStateEnum.SnakeDown), m_spriteBatch);
m_spriteBatch.End();
}

Expand Down Expand Up @@ -163,7 +173,7 @@ public void MoveUp(GameTime gameTime, float scale)
}
if (controlState == ControlStateEnum.SnakeLeft)
{
controlState = ControlStateEnum.SnakeRight;
controlState = ControlStateEnum.SnakeDown;
}
else
{
Expand All @@ -177,7 +187,7 @@ public void MoveDown(GameTime gameTime, float scale)
{
return;
}
if (controlState == ControlStateEnum.SnakeRight)
if (controlState == ControlStateEnum.SnakeDown)
{
controlState = ControlStateEnum.SnakeLeft;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Client/Systems/ControlsPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void LoadControls(Controls controls) {
// All of them have a default value in case they were not saved
controls.SnakeLeft = m_loadedState.SnakeLeft == null? new Control(Keys.Left) : m_loadedState.SnakeLeft;
controls.SnakeRight = m_loadedState.SnakeRight == null? new Control(Keys.Right) : m_loadedState.SnakeRight;
// controls.UseKeyboard = m_loadedState.UseKeyboard == null ? true: m_loadedState.UseKeyboard; // Real.
controls.SnakeUp = m_loadedState.SnakeUp == null? new Control(Keys.Up) : m_loadedState.SnakeUp;
controls.SnakeDown = m_loadedState.SnakeDown == null? new Control(Keys.Down) : m_loadedState.SnakeDown;
controls.UseKeyboard = true; // FOR TESTING. Allow us to switch keyboard on and off.
}
}
Expand Down
48 changes: 34 additions & 14 deletions src/Client/Systems/KeyboardInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class KeyboardInput : Shared.Systems.System
{
private HashSet<Keys> m_keysPressed = new HashSet<Keys>();
private KeyboardState m_statePrevious = Keyboard.GetState();
private Controls m_controls;
private Controls m_controls = new Controls(); // Default value that is overwritten in the constructor

public KeyboardInput(List<Tuple<Shared.Components.Input.Type, Keys>> mapping, Controls controls) : base(typeof(Shared.Components.Worm))
{
Expand All @@ -22,18 +22,12 @@ public KeyboardInput(List<Tuple<Shared.Components.Input.Type, Keys>> mapping, Co

public override void update(TimeSpan elapsedTime)
{
if (!m_controls.UseKeyboard)
{
return;
}
var keyboardState = Keyboard.GetState();
m_keysPressed.Clear();

foreach (var key in keyboardState.GetPressedKeys())
{
m_keysPressed.Add(key);
}

// We have a dictionary of entities, so we need to iterate through them
foreach (var entity in m_entities)
{
Expand All @@ -43,16 +37,42 @@ public override void update(TimeSpan elapsedTime)
}
var inputs = new List<Input.Type>();
var worm = WormMovement.getWormFromHead(entity.Value, m_entities);
if (keyNewlyPressed(m_controls.SnakeLeft.key))
// Start with the combinations
if (keyPressed(m_controls.SnakeLeft.key) && keyPressed(m_controls.SnakeUp.key))
{
inputs.Add(Input.Type.RotateLeft);
Shared.Systems.WormMovement.ninetyLeft(worm, elapsedTime);
}
if (keyNewlyPressed(m_controls.SnakeRight.key))
inputs.Add(Input.Type.PointUpLeft);
Shared.Systems.WormMovement.upLeft(worm);
} else if (keyPressed(m_controls.SnakeRight.key) && keyPressed(m_controls.SnakeUp.key))
{
inputs.Add(Input.Type.PointUpRight);
Shared.Systems.WormMovement.upRight(worm);
} else if (keyPressed(m_controls.SnakeLeft.key) && keyPressed(m_controls.SnakeDown.key))
{
inputs.Add(Input.Type.PointDownLeft);
Shared.Systems.WormMovement.downLeft(worm);
} else if (keyPressed(m_controls.SnakeRight.key) && keyPressed(m_controls.SnakeDown.key))
{
inputs.Add(Input.Type.PointDownRight);
Shared.Systems.WormMovement.downRight(worm);
} else if (keyNewlyPressed(m_controls.SnakeLeft.key))
{
inputs.Add(Input.Type.PointLeft);
Shared.Systems.WormMovement.left(worm, elapsedTime);
}else if (keyNewlyPressed(m_controls.SnakeRight.key))
{
inputs.Add(Input.Type.PointRight);
Shared.Systems.WormMovement.right(worm, elapsedTime);
}else if (keyNewlyPressed(m_controls.SnakeUp.key))
{
inputs.Add(Input.Type.PointUp);
Shared.Systems.WormMovement.up(worm);
}else if (keyNewlyPressed(m_controls.SnakeDown.key))
{
inputs.Add(Input.Type.RotateRight);
Shared.Systems.WormMovement.ninetyRight(worm, elapsedTime);
inputs.Add(Input.Type.PointDown);
Shared.Systems.WormMovement.down(worm);
}


if (inputs.Count > 0)
{
// Assuming you have a messaging system to handle input
Expand Down
26 changes: 22 additions & 4 deletions src/Client/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,29 @@ public void update(TimeSpan elapsedTime, Queue<Message> messages)
{
switch (input)
{
case Shared.Components.Input.Type.RotateLeft:
Shared.Systems.WormMovement.ninetyLeft(worm, message.elapsedTime);
case Shared.Components.Input.Type.PointLeft:
Shared.Systems.WormMovement.left(worm, message.elapsedTime);
break;
case Shared.Components.Input.Type.RotateRight:
Shared.Systems.WormMovement.ninetyRight(worm, message.elapsedTime);
case Shared.Components.Input.Type.PointRight:
Shared.Systems.WormMovement.right(worm, message.elapsedTime);
break;
case Shared.Components.Input.Type.PointUp:
Shared.Systems.WormMovement.up(worm);
break;
case Shared.Components.Input.Type.PointDown:
Shared.Systems.WormMovement.down(worm);
break;
case Input.Type.PointUpLeft:
Shared.Systems.WormMovement.upLeft(worm);
break;
case Input.Type.PointUpRight:
Shared.Systems.WormMovement.upRight(worm);
break;
case Input.Type.PointDownLeft:
Shared.Systems.WormMovement.downLeft(worm);
break;
case Input.Type.PointDownRight:
Shared.Systems.WormMovement.downRight(worm);
break;
}
}
Expand Down
36 changes: 30 additions & 6 deletions src/Server/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,37 @@ private void handleInput(Shared.Messages.Input message)
{
switch (input)
{
case Shared.Components.Input.Type.RotateLeft:
Shared.Systems.WormMovement.ninetyLeft(worm, message.elapsedTime);
m_reportThese.Add(message.entityId);
case Shared.Components.Input.Type.PointLeft:
Shared.Systems.WormMovement.left(worm, message.elapsedTime);
m_reportThese.Add(entity.id);
break;
case Shared.Components.Input.Type.RotateRight:
Shared.Systems.WormMovement.ninetyRight(worm, message.elapsedTime);
m_reportThese.Add(message.entityId);
case Shared.Components.Input.Type.PointRight:
Shared.Systems.WormMovement.right(worm, message.elapsedTime);
m_reportThese.Add(entity.id);
break;
case Shared.Components.Input.Type.PointUp:
Shared.Systems.WormMovement.up(worm);
m_reportThese.Add(entity.id);
break;
case Shared.Components.Input.Type.PointDown:
Shared.Systems.WormMovement.down(worm);
m_reportThese.Add(entity.id);
break;
case Shared.Components.Input.Type.PointUpLeft:
Shared.Systems.WormMovement.upLeft(worm);
m_reportThese.Add(entity.id);
break;
case Shared.Components.Input.Type.PointUpRight:
Shared.Systems.WormMovement.upRight(worm);
m_reportThese.Add(entity.id);
break;
case Shared.Components.Input.Type.PointDownLeft:
Shared.Systems.WormMovement.downLeft(worm);
m_reportThese.Add(entity.id);
break;
case Shared.Components.Input.Type.PointDownRight:
Shared.Systems.WormMovement.downRight(worm);
m_reportThese.Add(entity.id);
break;
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Shared/Components/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ public class Input : Component
{
public enum Type : UInt16
{
SnakeUp,
RotateLeft,
RotateRight,
PointLeft,
PointRight,
PointUp,
PointDown,
PointUpLeft,
PointUpRight,
PointDownLeft,
PointDownRight,
}

public Input(List<Type> inputs)
Expand Down
7 changes: 4 additions & 3 deletions src/Shared/Entities/WormHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ public static Entity create(Vector2 position, float size, float moveRate, float
entity.add(new Worm());

List<Input.Type> inputs = new List<Input.Type>();
inputs.Add(Input.Type.SnakeUp);
inputs.Add(Input.Type.RotateLeft);
inputs.Add(Input.Type.RotateRight);
foreach (Shared.Components.Input.Type input in Enum.GetValues(typeof(Input.Type)))
{
inputs.Add(input);
}
entity.add(new Input(inputs));

return entity;
Expand Down
58 changes: 46 additions & 12 deletions src/Shared/Systems/WormMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,49 @@ private List<Entity> getHeads()
return heads;
}

public static void ninetyLeft(List<Entity> snake, TimeSpan elapsedTime)
private static float UP_Radians = -MathHelper.PiOver2;
private static float DOWN_Radians = MathHelper.PiOver2;

public static void upLeft(List<Entity> snake)
{
changeDirection(snake, UP_Radians - MathHelper.PiOver4);
}

public static void upRight(List<Entity> snake)
{
changeDirection(snake, UP_Radians + MathHelper.PiOver4);
}

public static void downRight(List<Entity> snake)
{
applyRotation(snake, -MathHelper.PiOver2);
changeDirection(snake, DOWN_Radians - MathHelper.PiOver4);
}

public static void ninetyRight(List<Entity> snake, TimeSpan elapsedTime)
public static void downLeft(List<Entity> snake)
{
changeDirection(snake, DOWN_Radians + MathHelper.PiOver4);
}

public static void left(List<Entity> snake, TimeSpan elapsedTime)
{
applyRotation(snake, MathHelper.PiOver2);
changeDirection(snake, MathHelper.Pi);
}

public static void up(List<Entity> snake)
{
changeDirection(snake, UP_Radians);
}

public static void down(List<Entity> snake)
{
changeDirection(snake, DOWN_Radians);
}

public static void right(List<Entity> snake, TimeSpan elapsedTime)
{
changeDirection(snake, MathHelper.TwoPi);
}

private void applyThrust(Entity wormHead, TimeSpan elapsedTime)
{
// Setup variables
Expand All @@ -56,8 +89,8 @@ private void applyThrust(Entity wormHead, TimeSpan elapsedTime)
var frameTotalMovement = movement.moveRate * (float)elapsedTime.TotalMilliseconds;
var orientation = headPosition.orientation;
float LOCATION_THRESHOLD = 2f;
const float MIN_SEGMENT_SPACING = 40f;
const float IDEAL_SEGMENT_SPACING = 50f;
const float MIN_SEGMENT_SPACING = 20f;
const float IDEAL_SEGMENT_SPACING = 40f;

// Check how close the head is to its child
if (head.contains<ChildId>())
Expand Down Expand Up @@ -115,17 +148,18 @@ private void applyThrust(Entity wormHead, TimeSpan elapsedTime)
}
}

private static void applyRotation(List<Entity> snake, float radians)
private static void changeDirection(List<Entity> worm, float radians)
{
if (snake == null || snake.Count == 0)
if (worm == null || worm.Count == 0)
return;

// Assuming the first entity in the list is the head
var head = snake[0];
var head = worm[0];
var headPosition = head.get<Position>();

// Adjust the head's orientation by the specified radians
headPosition.orientation += radians;
if (headPosition.orientation == radians) return;
headPosition.orientation = radians;

// Normalize the orientation to ensure it stays within a valid range (e.g., 0 to 2*PI)
// This step is important if your system expects orientations within a specific range
Expand All @@ -134,9 +168,9 @@ private static void applyRotation(List<Entity> snake, float radians)

// For each segment, add the current head position to its queue for following
// Skip the head itself, start with the first segment following the head
for (int i = 1; i < snake.Count; i++)
for (int i = 1; i < worm.Count; i++)
{
var segment = snake[i];
var segment = worm[i];
var queueComponent = segment.get<AnchorQueue>();
// Here, we're adding the head's current position as the new target for each segment
// This mimics the behavior where, upon rotation, each segment should aim to move
Expand Down

0 comments on commit 3881ccf

Please sign in to comment.