Skip to content

Commit

Permalink
New movement (#94)
Browse files Browse the repository at this point in the history
* Changes movement system to be focused on the actual values rather than adjustemtns

* Seems better until we do the diagonal moves

* Updates language

* Swithces to using new key pressed

* Updates the keyboard input to limit the spam of turnings

* Adds big fix to the movement. Much smoother now

* Fixes the styling so the menu is readable
  • Loading branch information
MaxEdwards20 authored Apr 16, 2024
1 parent a00b60b commit c6223fe
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 85 deletions.
12 changes: 6 additions & 6 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ private Entity createEntity(Shared.Messages.NewEntity message)
}

// Worm parts

if (message.hasWorm)
{
entity.add(new Worm());
entity.add(new AnchorQueue()); // We implicitly need this because every worm part has it
}

if (message.hasHead)
{
Expand All @@ -177,12 +183,6 @@ private Entity createEntity(Shared.Messages.NewEntity message)
{
entity.add(new ChildId(message.childId));
}

if (message.hasWorm)
{
entity.add(new Worm());
entity.add(new AnchorQueue()); // We implicitly need this because every worm part has it
}

if (message.hasInvincible)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Menu/CreditsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Client.Menu
public class AboutView : GameStateView
{
private SpriteFont m_font;
private const string MESSAGE = "Created by Caden, Max, and Satchel in 2024. Enjoy!";
private const string MESSAGE = "Created by\nCaden, Max, and Satchel.\n2024 - CS 5410\nEnjoy!";
private bool isKeyboardRegistered = false;
private MenuStateEnum newState = MenuStateEnum.Credits;
public override void loadContent(ContentManager contentManager)
Expand Down Expand Up @@ -38,7 +38,7 @@ public override MenuStateEnum processInput(GameTime gameTime)
public override void render(GameTime gameTime)
{
m_spriteBatch.Begin();
Drawing.CustomDrawString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2), Colors.displayColor ,m_spriteBatch);
Drawing.CustomDrawString(m_font, MESSAGE, new Vector2(m_graphics.PreferredBackBufferWidth / 2, m_graphics.PreferredBackBufferHeight / 2), Colors.displayColor ,m_spriteBatch, boxed:true);
m_spriteBatch.End();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/HelpView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Client.Menu
public class HelpView : GameStateView
{
private SpriteFont m_font;
private const string MESSAGE = "Eat food to grow.\nAvoid other sandworms until you are large enough to consume them.\nUse the arrow keys to move and press escape to return to the main menu.\nGood Luck!";
private const string MESSAGE = "Eat food to grow.\nAvoid other sandworms until\nyou are large enough to consume them.\nUse the arrow keys to move\nand press escape to return to the main menu.\nGood Luck!";
private bool isKeyboardRegistered = false;
private MenuStateEnum newState = MenuStateEnum.Help;

Expand Down
1 change: 1 addition & 0 deletions src/Client/Menu/HighScoresView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Linq;
using Client.IO;
using Client.Systems;
using Shared.Components;
using Shared.Entities;
using Shared.Systems;
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/HowToPlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override void render(GameTime gameTime)
// Background Rectangle
var recPosition = new Vector2(m_graphics.PreferredBackBufferWidth / 5 - 20,
m_graphics.PreferredBackBufferHeight / 4 - 50);
Drawing.DrawBlurredRectangle(m_spriteBatch, recPosition, new Vector2(700, 400), 5, 0.6f);
Drawing.DrawBlurredRectangle(m_spriteBatch, recPosition, new Vector2(700, 400), 5, 0.9f);


// Title
Expand Down
1 change: 1 addition & 0 deletions src/Client/Menu/MainMenuView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public override void render(GameTime gameTime)
renderSound = false; // reset the flag
}
// I split the first one's parameters on separate lines to help you see them better
Drawing.DrawBlurredRectangle(m_spriteBatch, new Vector2(m_graphics.PreferredBackBufferWidth / 4, 150), new Vector2(m_graphics.PreferredBackBufferWidth / 2, 500), 5 );
float bottom = drawMenuItem(
m_currentSelection == MenuState.NewGame ? m_fontMenuSelect : m_fontMenu,
"New Game",
Expand Down
4 changes: 2 additions & 2 deletions src/Client/Menu/Styles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void CustomDrawString(SpriteFont font, string message, Vector2 pos
position = new Vector2(position.X - font.MeasureString(message).X / 2, position.Y - font.MeasureString(message).Y / 2);
}
if (boxed) {
DrawBlurredRectangle(spriteBatch, position, font.MeasureString(message), 5, 0.6f);
DrawBlurredRectangle(spriteBatch, position, font.MeasureString(message), 5);
}
if (shaded) {
for (int i = 1; i < 3; i++) {
Expand All @@ -24,7 +24,7 @@ public static void CustomDrawString(SpriteFont font, string message, Vector2 pos
spriteBatch.DrawString(font, message, position, color, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
}

public static void DrawBlurredRectangle(SpriteBatch spriteBatch, Vector2 position, Vector2 size, int blurRadius, float transparency = 0.8f)
public static void DrawBlurredRectangle(SpriteBatch spriteBatch, Vector2 position, Vector2 size, int blurRadius, float transparency = 0.85f)
{
Rectangle blurredRect = new Rectangle((int)(position.X - blurRadius), (int)(position.Y - blurRadius), (int)size.X + blurRadius * 2, (int)size.Y + blurRadius * 2);
Color color = Color.Black * transparency; // Adjust the color and transparency as needed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using Shared.Components;
using Shared.Entities;
using Client.Components;

namespace Shared.Systems;
namespace Client.Systems;

public class GameScoresPersistence
{
Expand Down
53 changes: 37 additions & 16 deletions src/Client/Systems/KeyboardInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class KeyboardInput : Shared.Systems.System
private KeyboardState m_statePrevious = Keyboard.GetState();
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))
public KeyboardInput(List<Tuple<Shared.Components.Input.Type, Keys>> mapping, Controls controls) : base(
typeof(Shared.Components.Worm))
{
m_controls = controls;
}
Expand All @@ -28,77 +29,89 @@ public override void update(TimeSpan elapsedTime)
{
m_keysPressed.Add(key);
}

// We have a dictionary of entities, so we need to iterate through them
foreach (var entity in m_entities)
{
if (!entity.Value.contains<Input>())
{
continue;
}

var inputs = new List<Input.Type>();
var worm = WormMovement.getWormFromHead(entity.Value, m_entities);
// Start with the combinations
if (keyPressed(m_controls.SnakeLeft.key) && keyPressed(m_controls.SnakeUp.key))
if (keysNewlyPressed(m_controls.SnakeUp.key, m_controls.SnakeLeft.key))
{
inputs.Add(Input.Type.PointUpLeft);
inputs.Add(Input.Type.PointUpLeft);
Shared.Systems.WormMovement.upLeft(worm);
} else if (keyPressed(m_controls.SnakeRight.key) && keyPressed(m_controls.SnakeUp.key))
}
else if (keysNewlyPressed( m_controls.SnakeUp.key, m_controls.SnakeRight.key ))
{
inputs.Add(Input.Type.PointUpRight);
Shared.Systems.WormMovement.upRight(worm);
} else if (keyPressed(m_controls.SnakeLeft.key) && keyPressed(m_controls.SnakeDown.key))
}
else if (keysNewlyPressed( m_controls.SnakeDown.key, m_controls.SnakeLeft.key ))
{
inputs.Add(Input.Type.PointDownLeft);
Shared.Systems.WormMovement.downLeft(worm);
} else if (keyPressed(m_controls.SnakeRight.key) && keyPressed(m_controls.SnakeDown.key))
}
else if (keysNewlyPressed( m_controls.SnakeDown.key, m_controls.SnakeRight.key ))
{
inputs.Add(Input.Type.PointDownRight);
Shared.Systems.WormMovement.downRight(worm);
} else if (keyNewlyPressed(m_controls.SnakeLeft.key))
}
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))
}
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))
}
else if (keyNewlyPressed(m_controls.SnakeUp.key))
{
inputs.Add(Input.Type.PointUp);
Shared.Systems.WormMovement.up(worm);
}else if (keyNewlyPressed(m_controls.SnakeDown.key))
}
else if (keyNewlyPressed(m_controls.SnakeDown.key))
{
inputs.Add(Input.Type.PointDown);
Shared.Systems.WormMovement.down(worm);
}


if (inputs.Count > 0)
{
// Assuming you have a messaging system to handle input
MessageQueueClient.instance.sendMessageWithId(new Shared.Messages.Input(entity.Key, inputs, elapsedTime));
MessageQueueClient.instance.sendMessageWithId(new Shared.Messages.Input(entity.Key, inputs,
elapsedTime));
}
}

// Move the current state to the previous state for the next time around
m_statePrevious = keyboardState;
}





public override bool add(Entity entity)
{
if (!base.add(entity))
{
return false;
}

return true;
}

public override void remove(uint id)
{
base.remove(id);
}

/// <summary>
/// Checks to see if a key was newly pressed
/// </summary>
Expand All @@ -111,5 +124,13 @@ private bool keyPressed(Keys key)
{
return m_keysPressed.Contains(key);
}

private bool keysNewlyPressed(Keys key1, Keys key2)
{
return keyPressed(key1) && keyPressed(key2) && (!m_statePrevious.IsKeyDown(key1) ||
!m_statePrevious.IsKeyDown(key2));
}
}
}


3 changes: 1 addition & 2 deletions src/Client/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public void update(TimeSpan elapsedTime, Queue<Message> messages)
}
}
}

// After processing all the messages, perform server reconciliation by
// resimulating the inputs from any sent messages not yet acknowledged by the server.

Expand All @@ -106,7 +105,7 @@ public void update(TimeSpan elapsedTime, Queue<Message> messages)
while (sent.Count > 0)
{
var message = (Shared.Messages.Input)sent.Dequeue();
if (message.type == Shared.Messages.Type.Input)
if (message.type == Shared.Messages.Type.Input && m_entities.ContainsKey(message.entityId))
{
var entity = m_entities[message.entityId];
Debug.Assert(entity.contains<Head>());
Expand Down
1 change: 0 additions & 1 deletion src/Client/Systems/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ private void renderEntity(TimeSpan elapsedTime, SpriteBatch spriteBatch, Entity

if (entity.contains<Invincible>())
{
var invincible = entity.get<Invincible>();
color = Color.Coral;
}

Expand Down
1 change: 1 addition & 0 deletions src/Server/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private void handleInput(Shared.Messages.Input message)
}
if (update)
{
// m_reportThese.Add(message.entityId);
foreach (var e in worm)
{
m_reportThese.Add(e.id);
Expand Down
Loading

0 comments on commit c6223fe

Please sign in to comment.