Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add head check #90

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ private void addEntity(Entity entity)
private void removeEntity(uint id)
{
// NOTE: Update the systems we use here
if (!m_entities.ContainsKey(id))
return;
m_entities.Remove(id);
m_systemKeyboardInput.remove(id);
m_systemGrowthHandler.remove(id);
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Systems/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void renderEntity(TimeSpan elapsedTime, SpriteBatch spriteBatch, Entity
if (entity.contains<Invincible>())
{
var invincible = entity.get<Invincible>();
color = invincible.duration < 1000 ? Color.Coral : Colors.displayColor;
color = Color.Coral;
}

if (entity.contains<SpicePower>() && !entity.contains<Worm>())
Expand Down
2 changes: 2 additions & 0 deletions src/Server/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ private void addEntity(Entity entity)
/// </summary>
private void removeEntity(uint id)
{
if (!m_entities.ContainsKey(id))
return;
m_entities.Remove(id);
m_systemNetwork.remove(id);
m_systemCollisionDetection.remove(id);
Expand Down
4 changes: 2 additions & 2 deletions src/Server/Systems/CollisionDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

public static int POWER_TO_GROW = 50;

public CollisionDetection() :

Check warning on line 21 in src/Server/Systems/CollisionDetection.cs

View workflow job for this annotation

GitHub Actions / buildProject

Non-nullable field 'm_removeEntity' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 21 in src/Server/Systems/CollisionDetection.cs

View workflow job for this annotation

GitHub Actions / buildProject

Non-nullable field 'm_addEntity' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 21 in src/Server/Systems/CollisionDetection.cs

View workflow job for this annotation

GitHub Actions / buildProject

Non-nullable field 'm_updateEntity' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
base(
typeof(Shared.Components.Collidable), typeof(Shared.Components.Position), typeof(Shared.Components.Size)
)
Expand Down Expand Up @@ -249,6 +249,8 @@

private void WormKill(Entity head)
{
if (!head.contains<Stats>()) return;

Stats stats = head.get<Stats>();
stats.Kills++;
stats.Score += 100;
Expand All @@ -257,8 +259,6 @@

private void handleWormHitWall(List<Entity> worm, Entity wall)
{


// There was a collision let everyone know about it
MessageQueueServer.instance.broadcastMessage(new Collision(worm[0].id, wall.id,
Collision.CollisionType.HeadToWall, wall.get<Position>()));
Expand Down
14 changes: 11 additions & 3 deletions src/Shared/Systems/WormMovement.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Shared.Components;
using Shared.Entities;
Expand Down Expand Up @@ -197,11 +198,17 @@ public static Entity getHead(Entity entity, Dictionary<uint, Entity> entities)

public static List<Entity> getWormFromHead(Entity head, Dictionary<uint, Entity> entities)
{
var snakeEntities = new List<Entity>();
// make sure we are at the head
while (head.contains<ParentId>() && entities.ContainsKey(head.get<ParentId>().id))
{
head = entities[head.get<ParentId>().id];
}

var worm = new List<Entity>();
var current = head;
while (current != null)
{
snakeEntities.Add(current);
worm.Add(current);
if (current.contains<ChildId>() && entities.ContainsKey(current.get<ChildId>().id))
{
current = entities[current.get<ChildId>().id];
Expand All @@ -211,7 +218,8 @@ public static List<Entity> getWormFromHead(Entity head, Dictionary<uint, Entity>
current = null;
}
}
return snakeEntities;
Debug.Assert(worm[0].contains<Head>(), "The first entity in the list should be the head");
return worm;
}

public static List<Entity> getSnakeFromTail(Entity tail, Dictionary<uint, Entity> entities)
Expand Down
Loading