Skip to content

Commit

Permalink
Worm movement system (#37)
Browse files Browse the repository at this point in the history
* Fixes crash

* Adds worm movement system

* render

* Adds movement as we go

* Snake is moving, but just the head.

* updates comments:

* A

* updates worm to all be mvoing

* updates readme
  • Loading branch information
MaxEdwards20 authored Apr 7, 2024
1 parent 1c88f68 commit cb24bb3
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 151 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ A game of Snake built using C# in the MonoGame framework, themed around everyone
- [ ] Satchel: Pick head, body, and tail texture for the sandworm
- [ ] Satchel: Keyboard vs. Mouse input menu screen - Satchel
- [ ] Caden: Map generation
- [ ] Max: Spice generation when we spawn
- [ ] Max: Snake Movement with the queue system

## Items to Develop

- [ ] Menu Screen to let player name themselves (probably similar to how control selection screen will work) - Satchel
- [ ] 3 different animated sprites for the spice
- [ ] 3 different animated sprites for the spice
- [ ] Mouse input support on Menu Screens
- [ ] Sound effects on death of worm and when food is eaten - Satchel
- [ ] Collision detection. Know whether we hit spice or another sandworm
Expand All @@ -27,6 +27,7 @@ A game of Snake built using C# in the MonoGame framework, themed around everyone
- [ ] Particle system for the death of a sandworm
- [ ] Game continues to show/play even after the player dies
- [ ] The new player should join in a location that has the least density of other snakes; a safe location
- [ ] Spice generation when we spawn

## Done

Expand Down
6 changes: 4 additions & 2 deletions src/Client/ClientMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public ClientMain()
protected override void Initialize()
{
// For Graders: You can change the resolution here
m_graphics.PreferredBackBufferWidth = 1920;
m_graphics.PreferredBackBufferHeight = 1080;
// m_graphics.PreferredBackBufferWidth = 1920;
// m_graphics.PreferredBackBufferHeight = 1080;
m_graphics.PreferredBackBufferWidth = 1000;
m_graphics.PreferredBackBufferHeight = 750;
m_graphics.ApplyChanges();

// Load the controls
Expand Down
21 changes: 11 additions & 10 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Client.Components;
using Shared.Components;
using Shared.Entities;
using Shared.Systems;

namespace Client;

Expand All @@ -19,9 +20,9 @@ public class GameModel
private Systems.Network m_systemNetwork;
private Systems.Camera m_systemCamera;
private Systems.KeyboardInput m_systemKeyboardInput;
private Systems.MouseInput m_systemMouseInput;
private Systems.Interpolation m_systemInterpolation;
private Systems.WormRenderer m_systemWormRenderer;
private Systems.Renderer m_renderer;
private Shared.Systems.WormMovement m_systemWormMovement;
private Controls m_controls;
private GraphicsDeviceManager m_graphics;
private SpriteFont m_font;
Expand All @@ -33,7 +34,7 @@ public void update(TimeSpan elapsedTime)
{
m_systemNetwork.update(elapsedTime, MessageQueueClient.instance.getMessages());
m_systemKeyboardInput.update(elapsedTime);
m_systemMouseInput.update(elapsedTime);
m_systemWormMovement.update(elapsedTime);
m_systemInterpolation.update(elapsedTime);
m_systemCamera.update(elapsedTime);
}
Expand All @@ -44,7 +45,7 @@ public void update(TimeSpan elapsedTime)

public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)
{
m_systemWormRenderer.render(elapsedTime, spriteBatch);
m_renderer.render(elapsedTime, spriteBatch);
}

/// <summary>
Expand All @@ -59,7 +60,8 @@ public bool initialize(ContentManager contentManager, Controls controls, Graphic
m_entities = new Dictionary<uint, Entity>();
m_systemInterpolation = new Systems.Interpolation();
m_systemCamera = new Systems.Camera(new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));
m_systemWormRenderer = new Systems.WormRenderer(m_systemCamera, graphics, m_font);
m_renderer = new Systems.Renderer(m_systemCamera, graphics, m_font);
m_systemWormMovement = new Shared.Systems.WormMovement();
m_systemNetwork = new Systems.Network();

m_systemNetwork.registerNewEntityHandler(handleNewEntity);
Expand All @@ -68,7 +70,6 @@ public bool initialize(ContentManager contentManager, Controls controls, Graphic

m_systemKeyboardInput = new Systems.KeyboardInput(new List<Tuple<Shared.Components.Input.Type, Keys>>
{ }, m_controls);
m_systemMouseInput = new Systems.MouseInput(m_controls);

return true;
}
Expand Down Expand Up @@ -165,8 +166,8 @@ private void addEntity(Entity entity)
// NOTE: Update the systems we use here
m_entities[entity.id] = entity;
m_systemKeyboardInput.add(entity);
m_systemMouseInput.add(entity);
m_systemWormRenderer.add(entity);
m_systemWormMovement.add(entity);
m_renderer.add(entity);
m_systemNetwork.add(entity);
m_systemInterpolation.add(entity);
m_systemCamera.add(entity);
Expand All @@ -181,9 +182,9 @@ private void removeEntity(uint id)
// NOTE: Update the systems we use here
m_entities.Remove(id);
m_systemKeyboardInput.remove(id);
m_systemMouseInput.remove(id);
m_systemWormMovement.remove(id);
m_systemNetwork.remove(id);
m_systemWormRenderer.remove(id);
m_renderer.remove(id);
m_systemInterpolation.remove(id);
m_systemCamera.remove(id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/MenuKeyboardInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public MenuKeyboardInput()



public void registerCommand(Control control, bool keyPressOnly, IInputDevice.CommandDelegate callback)
public void registerCommand(Client.Components.Control control, bool keyPressOnly, IInputDevice.CommandDelegate callback)
{
// If already registered, remove it!
if (m_commandEntries.ContainsKey(control))
Expand Down
15 changes: 5 additions & 10 deletions src/Client/Systems/KeyboardInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class KeyboardInput : Shared.Systems.System
private KeyboardState m_statePrevious = Keyboard.GetState();
private Controls m_controls;

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.Input))
{
m_controls = controls;
}
Expand All @@ -41,22 +41,17 @@ public override void update(TimeSpan elapsedTime)
continue;
}
var inputs = new List<Input.Type>();
if (keyPressed(m_controls.SnakeLeft.key))
if (keyNewlyPressed(m_controls.SnakeLeft.key))
{
inputs.Add(Input.Type.RotateLeft);
Utility.rotateLeft(entity.Value, elapsedTime, m_entities);
Shared.Systems.WormMovement.ninetyLeft(entity.Value, elapsedTime);

}
if (keyPressed(m_controls.SnakeRight.key))
if (keyNewlyPressed(m_controls.SnakeRight.key))
{
inputs.Add(Input.Type.RotateRight);
Utility.rotateRight(entity.Value, elapsedTime, m_entities);

Shared.Systems.WormMovement.ninetyRight(entity.Value, elapsedTime);
}
// Always add thrust
inputs.Add(Input.Type.SnakeUp);
Utility.thrust(entity.Value, elapsedTime, m_entities);

if (inputs.Count > 0)
{
// Assuming you have a messaging system to handle input
Expand Down
87 changes: 0 additions & 87 deletions src/Client/Systems/MouseInput.cs

This file was deleted.

8 changes: 3 additions & 5 deletions src/Client/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Network : Shared.Systems.System
private NewEntityHandler m_newEntityHandler;
private uint m_lastMessageId = 0;
private HashSet<uint> m_updatedEntities = new HashSet<uint>();


/// <summary>
/// Primary activity in the constructor is to setup the command map
Expand Down Expand Up @@ -96,14 +97,11 @@ public void update(TimeSpan elapsedTime, Queue<Message> messages)
{
switch (input)
{
case Shared.Components.Input.Type.SnakeUp:
Shared.Entities.Utility.thrust(entity, message.elapsedTime, m_entities);
break;
case Shared.Components.Input.Type.RotateLeft:
Shared.Entities.Utility.rotateLeft(entity, message.elapsedTime, m_entities);
Shared.Systems.WormMovement.ninetyLeft(entity, message.elapsedTime);
break;
case Shared.Components.Input.Type.RotateRight:
Shared.Entities.Utility.rotateRight(entity, message.elapsedTime, m_entities);
Shared.Systems.WormMovement.ninetyRight(entity, message.elapsedTime);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@

using System;
using System.Collections.Generic;
using Client.Components;
using Client.Menu;

namespace Client.Systems;

public class WormRenderer : Shared.Systems.System
public class Renderer : Shared.Systems.System
{
private Systems.Camera m_camera;
private GraphicsDeviceManager m_graphics;
private SpriteFont m_font;

public WormRenderer(Systems.Camera camera, GraphicsDeviceManager graphics, SpriteFont font) :
public Renderer(Systems.Camera camera, GraphicsDeviceManager graphics, SpriteFont font) :
base(
typeof(Worm)
typeof(Position), typeof(Sprite)
)
{
m_camera = camera;
Expand All @@ -42,8 +43,8 @@ public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)
matrix *= Matrix.CreateTranslation(new Vector3(offset, 0));
matrix *= Matrix.CreateScale(scaleX, scaleY, 1);

spriteBatch.Begin(transformMatrix: matrix);
// TODO: Adjust this to render all of the tails first, then body segments, then heads
// spriteBatch.Begin(transformMatrix: matrix);
spriteBatch.Begin();
var heads = new List<Entity>();
var bodies = new List<Entity>();
var tails = new List<Entity>();
Expand All @@ -55,7 +56,7 @@ public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)
heads.Add(entity);
else if (entity.contains<Tail>())
tails.Add(entity);
else if (entity.contains<ParentId>()) // The body has these
else if (entity.contains<Worm>())
bodies.Add(entity);
else
others.Add(entity);
Expand Down Expand Up @@ -108,7 +109,7 @@ private void renderEntity(TimeSpan elapsedTime, SpriteBatch spriteBatch, Entity

if (entity.contains<Name>())
{
// We want the name position to be above the entity
// We want the name position to be above the head
Vector2 namePosition = new Vector2(position.X - size.X + 10, position.Y - size.Y - 10);
Drawing.DrawPlayerName(m_font, entity.get<Name>().name, namePosition, Color.White, spriteBatch);
}
Expand Down
13 changes: 0 additions & 13 deletions src/Client/Systems/WormMovement.cs

This file was deleted.

6 changes: 5 additions & 1 deletion src/Server/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Shared.Components.Appearance;
using Shared.Entities;
using Shared.Messages;
using Shared.Systems;

namespace Server
{
Expand All @@ -12,7 +13,7 @@ public class GameModel
private HashSet<int> m_clients = new HashSet<int>();
private Dictionary<uint, Entity> m_entities = new Dictionary<uint, Entity>();
private Dictionary<int, uint> m_clientToEntityId = new Dictionary<int, uint>();

private WormMovement m_systemWormMovement = new WormMovement();
Systems.Network m_systemNetwork = new Server.Systems.Network();

/// <summary>
Expand All @@ -23,6 +24,7 @@ public class GameModel
public void update(TimeSpan elapsedTime)
{
m_systemNetwork.update(elapsedTime, MessageQueueServer.instance.getMessages());

Check warning on line 26 in src/Server/GameModel.cs

View workflow job for this annotation

GitHub Actions / buildProject

Possible null reference argument for parameter 'messages' in 'void Network.update(TimeSpan elapsedTime, Queue<Tuple<int, Message>> messages)'.
m_systemWormMovement.update(elapsedTime);
}

/// <summary>
Expand Down Expand Up @@ -89,6 +91,7 @@ private void addEntity(Entity entity)

m_entities[entity.id] = entity;
m_systemNetwork.add(entity);
m_systemWormMovement.add(entity);
}

/// <summary>
Expand All @@ -99,6 +102,7 @@ private void removeEntity(uint id)
{
m_entities.Remove(id);
m_systemNetwork.remove(id);
m_systemWormMovement.remove(id);
}

/// <summary>
Expand Down
Loading

0 comments on commit cb24bb3

Please sign in to comment.