Skip to content

Commit

Permalink
Upgraded worm movement (#32)
Browse files Browse the repository at this point in the history
* Fixes crash

* Adds keyboard input types

* Adds the entities that we need

* Adds the player and segments to the initial setup

* Adds update for the entities

* Adds readme
  • Loading branch information
MaxEdwards20 authored Apr 4, 2024
1 parent 247f123 commit 03f9261
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ A game of Snake built using C# in the MonoGame framework, themed around everyone
- [ ] Record players score, kills and highest position. Probably can be added to the `GameScores` object.
- [ ] Game over screen with score, kills, and highest position achieved
- [ ] Particle system for the death of a sandworm
- [ ] Upgrade our movement system to reduce the lag in rotation

## Done

- [x] Max: Upgrade our movement system to reduce the lag in rotation
- [x] Max: Add name support for the player
- [x] Max: Setup Snake Movement in the screen
- [x] Max: Decide how to build the snake. Tons of entities? Or one entity with a list of positions?
Expand Down
15 changes: 10 additions & 5 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class GameModel
private Systems.KeyboardInput m_systemKeyboardInput;
private Systems.MouseInput m_systemMouseInput;
private Systems.Interpolation m_systemInterpolation;
private Systems.Renderer m_systemRenderer;
private Systems.WormRenderer _mSystemWormRenderer;
private Controls m_controls;
private GraphicsDeviceManager m_graphics;
private SpriteFont m_font;
Expand All @@ -44,7 +44,7 @@ public void update(TimeSpan elapsedTime)

public void render(TimeSpan elapsedTime, SpriteBatch spriteBatch)
{
m_systemRenderer.render(elapsedTime, spriteBatch);
_mSystemWormRenderer.render(elapsedTime, spriteBatch);
}

/// <summary>
Expand All @@ -59,7 +59,7 @@ 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_systemRenderer = new Systems.Renderer(m_systemCamera, graphics, m_font);
_mSystemWormRenderer = new Systems.WormRenderer(m_systemCamera, graphics, m_font);
m_systemNetwork = new Systems.Network();

m_systemNetwork.registerNewEntityHandler(handleNewEntity);
Expand Down Expand Up @@ -137,6 +137,11 @@ private Entity createEntity(Shared.Messages.NewEntity message)
{
entity.add(new ChildId(message.childId));
}

if (message.hasWorm)
{
entity.add(new Worm());
}

if (message.hasName)
{
Expand All @@ -161,7 +166,7 @@ private void addEntity(Entity entity)
m_entities[entity.id] = entity;
m_systemKeyboardInput.add(entity);
m_systemMouseInput.add(entity);
m_systemRenderer.add(entity);
_mSystemWormRenderer.add(entity);
m_systemNetwork.add(entity);
m_systemInterpolation.add(entity);
m_systemCamera.add(entity);
Expand All @@ -178,7 +183,7 @@ private void removeEntity(uint id)
m_systemKeyboardInput.remove(id);
m_systemMouseInput.remove(id);
m_systemNetwork.remove(id);
m_systemRenderer.remove(id);
_mSystemWormRenderer.remove(id);
m_systemInterpolation.remove(id);
m_systemCamera.remove(id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Menu/GamePlayView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override void RegisterCommands()

private bool connectToServer()
{
return MessageQueueClient.instance.initialize("localhost", 3050);
return MessageQueueClient.instance.initialize("localhost", 3000);
}

private void escape(GameTime gameTime, float scale)
Expand Down
6 changes: 5 additions & 1 deletion 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 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.Input))
public KeyboardInput(List<Tuple<Shared.Components.Input.Type, Keys>> mapping, Controls controls) : base(typeof(Shared.Components.Worm))
{
m_controls = controls;
}
Expand All @@ -35,6 +35,10 @@ public override void update(TimeSpan elapsedTime)
// 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>();
if (keyPressed(m_controls.SnakeLeft.key))
{
Expand Down
6 changes: 5 additions & 1 deletion src/Client/Systems/MouseInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MouseInput : Shared.Systems.System
private MouseState previousMouseState = Mouse.GetState();
private Controls m_controls;

public MouseInput(Controls controls) : base(typeof(Shared.Components.Input))
public MouseInput(Controls controls) : base(typeof(Shared.Components.Worm))
{
m_controls = controls;
}
Expand All @@ -30,6 +30,10 @@ public override void update(TimeSpan elapsedTime)
foreach (var entityPair in m_entities)
{
var entity = entityPair.Value;
if (!entity.contains<Input>())
{
continue;
}
// Try to get the Position component safely
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@

namespace Client.Systems;

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

public Renderer(Systems.Camera camera, GraphicsDeviceManager graphics, SpriteFont font) :
public WormRenderer(Systems.Camera camera, GraphicsDeviceManager graphics, SpriteFont font) :
base(
typeof(Client.Components.Sprite),
typeof(Shared.Components.Position),
typeof(Shared.Components.Size)
typeof(Worm)
)
{
m_camera = camera;
Expand Down
21 changes: 18 additions & 3 deletions src/Server/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void handleJoin(int clientId, Shared.Messages.Message message)

private void createNewWorm(int clientId, string name)
{
var startLocation = new Vector2(200, 200);
var startLocation = getLeastDenseStartLocation();
var rotationRate = (float) Math.PI / 1000;
var moveRate = 0.1f;
var headSize = 100;
Expand Down Expand Up @@ -166,15 +166,30 @@ private void createNewWorm(int clientId, string name)
// Step 4: Let all other clients know about this new player entity
// Remove components not needed for "other" players
player.remove<Shared.Components.Input>();
Message message = new NewEntity(player);

// Now send the new entities to all other clients
Message playerMessage = new NewEntity(player);
Message segmentMessage = new NewEntity(segment);
Message tailMessage = new NewEntity(tail);
foreach (int otherId in m_clients)
{
if (otherId != clientId)
{
MessageQueueServer.instance.sendMessage(otherId, message);
MessageQueueServer.instance.sendMessage(otherId, playerMessage);
MessageQueueServer.instance.sendMessage(otherId, segmentMessage);
MessageQueueServer.instance.sendMessage(otherId, tailMessage);
}
}

}

private Vector2 getLeastDenseStartLocation()
{
// We want to start the player in the least dense area of the screen
// For now, we'll just start them randomly generated location

Random random = new Random();
return new Vector2(random.Next(0, 800), random.Next(0, 600));
}
}
}
2 changes: 1 addition & 1 deletion src/Server/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Server": {
"commandName": "Project",
"commandLineArgs": "--port 3050"
"commandLineArgs": "--port 3000"
}
}
}
7 changes: 5 additions & 2 deletions src/Server/Systems/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ private void handleInput(Shared.Messages.Input message)
switch (input)
{
case Shared.Components.Input.Type.SnakeUp:
Shared.Entities.Utility.thrust(entity, message.elapsedTime, m_entities);
m_reportThese.Add(message.entityId);
var snake = Shared.Entities.Utility.thrust(entity, message.elapsedTime, m_entities);
foreach (var part in snake)
{
m_reportThese.Add(part.id);
}
break;
case Shared.Components.Input.Type.RotateLeft:
Shared.Entities.Utility.rotateLeft(entity, message.elapsedTime, m_entities);
Expand Down
6 changes: 6 additions & 0 deletions src/Shared/Components/Worm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Shared.Components;

public class Worm : Component
{

}
7 changes: 4 additions & 3 deletions src/Shared/Entities/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ namespace Shared.Entities;
// NOTE: We will probably move this over to the wormMovement system where behavior lives
public class Utility
{
// Everything that hits these endpoints SHOULD be a WormHead (start of a LinkedList of worm parts)
public static void thrust(Entity entity, TimeSpan elapsedTime, Dictionary<uint, Entity> entities)
// The entity that hits these endpoints should be the head of the worm, with the rest of the worm in the entities
public static List<Entity> thrust(Entity entity, TimeSpan elapsedTime, Dictionary<uint, Entity> entities)
{
var head = getHead(entity, entities);
var snake = getSnakeFromHead(head, entities);
applyThrust(snake, elapsedTime);
return snake;
}

public static void rotateLeft(Entity entity, TimeSpan elapsedTime, Dictionary<uint, Entity> entities)
Expand Down Expand Up @@ -117,7 +118,7 @@ private static void applyThrust(List<Entity> snake, TimeSpan elapsedTime)
}


// We don't need to update the entire snake with these because it will be updated in the next frame when thrust is applied
// We don't need to update the entire worm with these because it will be updated in the next frame when thrust is applied
private static void applyLeftRotation(Entity head, TimeSpan elapsedTime)
{
var position = head.get<Position>();
Expand Down
1 change: 1 addition & 0 deletions src/Shared/Entities/WormHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static Entity create(Vector2 position, float size, float moveRate, float
entity.add(new SpicePower(0));
entity.add(new Head());
entity.add(new Name(name));
entity.add(new Worm());

List<Input.Type> inputs = new List<Input.Type>();
inputs.Add(Input.Type.SnakeUp);
Expand Down
3 changes: 2 additions & 1 deletion src/Shared/Entities/WormSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ public class WormSegment
public static Entity create(Vector2 position, float size, float moveRate, float rotateRate, uint parent)
{
Entity entity = new Entity();
entity.add(new Appearance("Textures/body"));
entity.add(new Position(position));
entity.add(new Appearance("Textures/body"));
entity.add(new Size(new Vector2(size, size)));
entity.add(new Movement(moveRate, rotateRate));
entity.add(new ParentId(parent));
entity.add(new Collision());
entity.add(new Worm());
return entity;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Shared/Entities/WormTail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public static Entity create(Vector2 position, float size, float moveRate, float
{
Entity entity = new Entity();
entity.add(new Position(position));
entity.add(new Appearance("Textures/tail"));
entity.add(new Size(new Vector2(size, size)));
entity.add(new Appearance("Textures/tail"));
entity.add(new ParentId(parent));
entity.add(new Movement(moveRate, rotateRate));
entity.add(new Collision());
entity.add(new Tail());
entity.add(new Worm());
return entity;
}
}
16 changes: 16 additions & 0 deletions src/Shared/Messages/NewEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public NewEntity(Entity entity) : base(Type.NewEntity)
this.hasCollision = true;
}

if (entity.contains<Worm>())
{
this.hasWorm = true;
}

if (entity.contains<Name>())
{
this.hasName = true;
Expand All @@ -106,6 +111,7 @@ public NewEntity() : base(Type.NewEntity)
public uint parentId { get; private set; }
public bool hasChild { get; private set; } = false;
public uint childId { get; private set; }
public bool hasWorm { get; private set; } = false;
public bool hasName { get; private set; } = false;
public string name { get; private set; }

Expand Down Expand Up @@ -147,8 +153,10 @@ public override byte[] serialize()
data.AddRange(BitConverter.GetBytes(hasTail));
serializeParentId(data);
serializeChild(data);
data.AddRange(BitConverter.GetBytes(hasWorm));
serializeName(data); // Make sure this is the last item to serialize


return data.ToArray();
}

Expand All @@ -174,10 +182,18 @@ public override int parse(byte[] data)
offset = parseTail(data, offset);
offset = parseParent(data, offset);
offset = parseChild(data, offset);
offset = parseWorm(data, offset);
offset = parseName(data, offset); // Make sure this is the last item to parse
return offset;
}

private int parseWorm(byte[] data, int offset)
{
this.hasWorm = BitConverter.ToBoolean(data, offset);
offset += sizeof(bool);
return offset;
}

private int parseId(byte[] data, int offset)
{
this.id = BitConverter.ToUInt32(data, offset);
Expand Down

0 comments on commit 03f9261

Please sign in to comment.