Skip to content

Commit

Permalink
Remove appearance string (#115)
Browse files Browse the repository at this point in the history
* Removes appearance strings and adds head on collision

* updates appearnce
  • Loading branch information
MaxEdwards20 authored Apr 19, 2024
1 parent 1000ef8 commit b879093
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 100 deletions.
63 changes: 35 additions & 28 deletions src/Client/GameModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,39 +141,12 @@ private Entity createEntity(Shared.Messages.NewEntity message)

if (message.hasAppearance)
{
var isSpice = message.hasSpicePower && !message.hasWorm;
if (isSpice)
{
createSpiceAppearance(message, entity);
}
else if (message.hasHead)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/sandworm_head");
entity.add(new Components.Sprite(texture));
} else if (message.hasTail)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/sandworm_tail");
entity.add(new Components.Sprite(texture));
} else if (message.hasWorm)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/sandworm_body");
entity.add(new Components.Sprite(texture));
} else if (message.hasWall)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/wall");
entity.add(new Components.Sprite(texture));
}
else
{
Texture2D texture = m_contentManager.Load<Texture2D>(message.texture);
entity.add(new Components.Sprite(texture));
}
createAppearance(message, entity);
}
if (message.hasPosition)
{
entity.add(new Shared.Components.Position(message.position, message.orientation));
}

if (message.hasSize)
{
entity.add(new Shared.Components.Size(message.size));
Expand Down Expand Up @@ -255,6 +228,40 @@ private Entity createEntity(Shared.Messages.NewEntity message)
return entity;
}

private void createAppearance(NewEntity message, Entity entity)
{
var isSpice = message.hasSpicePower && !message.hasWorm;
if (isSpice)
{
createSpiceAppearance(message, entity);
}
else if (message.hasHead)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/sandworm_head");
entity.add(new Components.Sprite(texture));
}
else if (message.hasTail)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/sandworm_tail");
entity.add(new Components.Sprite(texture));
}
else if (message.hasWorm)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/sandworm_body");
entity.add(new Components.Sprite(texture));
}
else if (message.hasWall)
{
Texture2D texture = m_contentManager.Load<Texture2D>("Textures/wall");
entity.add(new Components.Sprite(texture));
}
else
{
Texture2D texture = m_contentManager.Load<Texture2D>(message.texture);
entity.add(new Components.Sprite(texture));
}
}

private void createSpiceAppearance(NewEntity message, Entity entity)
{
int row = 0;
Expand Down
35 changes: 9 additions & 26 deletions src/Server/Systems/CollisionDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,47 +212,30 @@ private void handleHitOtherWorm(Entity head, List<Entity> worm, Entity otherWorm
// Check if we hit head on head
if (otherWormPart.contains<Head>())
{


// The longer worm survives
List<Entity> otherWorm = WormMovement.getWormFromHead(otherWormPart, m_entities);
if (worm.Count > otherWorm.Count)
{
WormKill(head);
// Other worm dies
handleRemoveWormAndGenerateSpice(otherWorm);
// Let everyone know about the collision
MessageQueueServer.instance.broadcastMessage(new Collision(worm[0].id, otherWormPart.id,
Collision.CollisionType.ReceiverDies, worm[0].get<Position>()));
}
else
{
Entity otherHead = WormMovement.getHead(otherWormPart, m_entities);
WormKill(otherHead);

// This worm dies
handleRemoveWormAndGenerateSpice(worm);
MessageQueueServer.instance.broadcastMessage(new Collision(worm[0].id, otherWormPart.id,
Collision.CollisionType.SenderDies, worm[0].get<Position>()));
}
var otherWorm = WormMovement.getWormFromHead(otherWormPart, m_entities);
// Both worms die
// Let everyone know about the collision
MessageQueueServer.instance.broadcastMessage(new Collision(worm[0].id, otherWormPart.id,
Collision.CollisionType.ReceiverDies, worm[0].get<Position>()));
handleRemoveWormAndGenerateSpice(worm);
MessageQueueServer.instance.broadcastMessage(new Collision(otherWormPart.id,worm[0].id,
Collision.CollisionType.SenderDies, worm[0].get<Position>()));
handleRemoveWormAndGenerateSpice(otherWorm);
}
else // Hit the side of other worm, so this worm dies
{
// Let everyone know about the collision
MessageQueueServer.instance.broadcastMessage(new Collision(worm[0].id, otherWormPart.id,
Collision.CollisionType.SenderDies, worm[0].get<Position>()));

Entity otherHead = WormMovement.getHead(otherWormPart, m_entities);
WormKill(otherHead);

handleRemoveWormAndGenerateSpice(worm);
}
}

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

Stats stats = head.get<Stats>();
stats.Kills++;
stats.Score += 100;
Expand Down
6 changes: 1 addition & 5 deletions src/Shared/Components/Appearance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ namespace Shared.Components.Appearance
{
public class Appearance : Component
{
public Appearance(string texture)
{
this.texture = texture;
}
public string texture { get; private set; }

}
}
21 changes: 0 additions & 21 deletions src/Shared/Entities/Background.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Shared/Entities/DeadWormSpice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static Entity create(Vector2 position)
{
float size = 50;
Entity entity = new();
entity.add(new Appearance("Textures/deadWormSpice"));
entity.add(new Appearance());
entity.add(new Position(position));
entity.add(new Size(new Vector2(size, size)));
entity.add(new Collidable());
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Entities/Spice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static Entity create(Vector2 position)
int power = random.Next(1, 10);
float size = 3 * power + 10;
Entity entity = new();
entity.add(new Appearance("Textures/spice"));
entity.add(new Appearance());
entity.add(new Position(position));
entity.add(new Size(new Vector2(size, size)));
entity.add(new Collidable());
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Entities/Wall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Wall
public static Entity create(Vector2 position, int size)
{
Entity entity = new Entity();
entity.add(new Appearance("Textures/dune_wall"));
entity.add(new Appearance());
entity.add(new Position(position));
entity.add(new Size(new Vector2(size, size)));
entity.add(new Collidable());
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Entities/WormHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static Entity create(Vector2 position, string name, int clientId)
{
Entity entity = new();
entity.add(new Position(position));
entity.add(new Appearance("Textures/sandworm_head"));
entity.add(new Appearance());
entity.add(new Size(new Vector2(size, size)));
entity.add(new Movement(moveRate, rotateRate));
entity.add(new ClientId(clientId));
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Entities/WormSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static Entity create(Vector2 position, uint parent, int clientId)
{
Entity entity = new Entity();
entity.add(new Position(position));
entity.add(new Appearance("Textures/sandworm_body"));
entity.add(new Appearance());
entity.add(new Size(new Vector2(size, size)));
entity.add(new Movement(moveRate, rotateRate));
entity.add(new ClientId(clientId));
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/Entities/WormTail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static Entity create(Vector2 position, uint parent, int clientId)
{
Entity entity = new Entity();
entity.add(new Position(position));
entity.add(new Appearance("Textures/sandworm_tail"));
entity.add(new Appearance());
entity.add(new Size(new Vector2(size, size)));
entity.add(new ParentId(parent));
entity.add(new Movement(moveRate, rotateRate));
Expand Down
14 changes: 0 additions & 14 deletions src/Shared/Messages/NewEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public NewEntity(Entity entity) : base(Type.NewEntity)
if (entity.contains<Appearance>())
{
this.hasAppearance = true;
this.texture = entity.get<Appearance>().texture;
}
else
{
Expand Down Expand Up @@ -456,14 +455,6 @@ private int parseAppearance(byte[] data, int offset)
{
this.hasAppearance = BitConverter.ToBoolean(data, offset);
offset += sizeof(bool);
if (hasAppearance)
{
int textureSize = BitConverter.ToInt32(data, offset);
offset += sizeof(Int32);
this.texture = Encoding.UTF8.GetString(data, offset, textureSize);
offset += textureSize;
}

return offset;
}

Expand Down Expand Up @@ -568,11 +559,6 @@ private void serializePosition(List<byte> data)
private void serializeAppearance(List<byte> data)
{
data.AddRange(BitConverter.GetBytes(hasAppearance));
if (hasAppearance)
{
data.AddRange(BitConverter.GetBytes(texture.Length));
data.AddRange(Encoding.UTF8.GetBytes(texture));
}
}
}
}
Expand Down

0 comments on commit b879093

Please sign in to comment.