Skip to content

Commit

Permalink
Does not allow the snake to rotate 180 degrees (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxEdwards20 authored Apr 12, 2024
1 parent f1c0881 commit 8739247
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Shared/Systems/WormMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,17 @@ private void applyThrust(Entity wormHead, TimeSpan elapsedTime)

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

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

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

// Adjust the head's orientation by the specified radians and that is it not 180
var oppositeDirction = (radians + MathHelper.Pi) % (2 * Math.PI);
var headIsOppositeDirection = Math.Abs(headPosition.orientation - oppositeDirction) < 0.1;
if (headPosition.orientation == radians || headIsOppositeDirection) return;
headPosition.orientation = radians;

// Normalize the orientation to ensure it stays within a valid range (e.g., 0 to 2*PI)
Expand Down

0 comments on commit 8739247

Please sign in to comment.