Multiple players visible in cell

This commit is contained in:
Michele 2021-02-04 23:18:08 +00:00
parent 5434766207
commit 4619844607
2 changed files with 182 additions and 15 deletions

View File

@ -1,4 +1,5 @@
using System;
using System.Security.Cryptography;
namespace Sledgemapper.Shared.Entities
{
@ -22,6 +23,64 @@ namespace Sledgemapper.Shared.Entities
public class Tile : BaseMapEntity
{
public bool Equals(Tile other)
{
if (other == null) return false;
return (X == other.X && Y == other.Y);
}
public static bool operator ==(Tile a, Tile b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.X == b.X && a.Y == b.Y;
}
public static bool operator !=(Tile a, Tile b)
{
return !(a == b);
}
public override bool Equals(object obj)
{
//
// See the full list of guidelines at
// http://go.microsoft.com/fwlink/?LinkID=85237
// and also the guidance for operator== at
// http://go.microsoft.com/fwlink/?LinkId=85238
//
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return Equals(obj as Tile);
}
// override object.GetHashCode
public override int GetHashCode()
{
unchecked
{
int hash = 13;
hash = (hash * 7) + X + Y;
return hash;
}
}
}
public class Line : BaseMapEntity

View File

@ -1304,22 +1304,130 @@ namespace Sledgemapper
private void DrawPlayers()
{
for (int i = 0; i < _sessionData.Players.Count; i++)
var ffont = _fonts.FirstOrDefault(m => int.Parse(m.Key.Replace("font", "")) > _state.TileSize).Value ?? _fonts.Last().Value;
var fscale = _state.TileSize / ((float)ffont.LineSpacing * 3f);
var playerCells = _sessionData.Players.Select(m => m.Position).Distinct().ToList();
foreach (var cell in playerCells)
{
Player player = _sessionData.Players[i];
var color = player.Color.ToColor();
_spriteBatch.DrawRectangle(new Rectangle(player.Position.X * _state.TileSize - 4, player.Position.Y * _state.TileSize - 4, _state.TileSize + 7, _state.TileSize + 7), color, 2);
var ffont = _fonts.FirstOrDefault(m => int.Parse(m.Key.Replace("font", "")) > _state.TileSize).Value ?? _fonts.Last().Value;
var fscale = _state.TileSize / ((float)ffont.LineSpacing * 2);
_spriteBatch.DrawString(ffont,
player.Initials,
new Vector2(player.Position.X * _state.TileSize + 2, player.Position.Y * _state.TileSize + _state.TileSize - 2 - ffont.LineSpacing * fscale),
color,
0,
Vector2.Zero,
fscale,
SpriteEffects.None,
0);
var playersInCell = _sessionData.Players.Where(m => m.Position == cell).ToList();
var i = 0;
foreach (var player in playersInCell)
{
var color = player.Color.ToColor();
var rectangle = new Rectangle();
var stringPosition = new Vector2();
var measure = ffont.MeasureString(player.Initials);
var maxSize = Math.Max(measure.X, measure.Y);
if (playersInCell.Count == 1)
{
fscale = (_state.TileSize - 2) / maxSize;
rectangle = new Rectangle(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize,
_state.TileSize - 1,
_state.TileSize - 1);
stringPosition = new Vector2(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize + _state.TileSize - measure.Y * fscale);
}
else if (playersInCell.Count == 2)
{
fscale = (_state.TileSize / 2 - 2) / maxSize;
if (i == 0)
{
rectangle = new Rectangle(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize,
_state.TileSize / 2 - 1,
_state.TileSize - 1);
stringPosition = new Vector2(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize + _state.TileSize - measure.Y * fscale);
}
else
{
rectangle = new Rectangle(
player.Position.X * _state.TileSize + _state.TileSize / 2,
player.Position.Y * _state.TileSize,
_state.TileSize / 2 - 1,
_state.TileSize - 1);
stringPosition = new Vector2(
player.Position.X * _state.TileSize + _state.TileSize / 2,
player.Position.Y * _state.TileSize + _state.TileSize - measure.Y * fscale);
}
i++;
}
else if (playersInCell.Count >= 3)
{
fscale = (_state.TileSize / 2 - 2) / maxSize;
switch (i)
{
case 0:
rectangle = new Rectangle(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize,
_state.TileSize / 2 - 1,
_state.TileSize / 2 - 1);
stringPosition = new Vector2(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize + _state.TileSize / 2 - measure.Y * fscale);
break;
case 1:
rectangle = new Rectangle(
player.Position.X * _state.TileSize + _state.TileSize / 2,
player.Position.Y * _state.TileSize,
_state.TileSize / 2 - 1,
_state.TileSize / 2 - 1);
stringPosition = new Vector2(
player.Position.X * _state.TileSize + _state.TileSize / 2,
player.Position.Y * _state.TileSize + _state.TileSize / 2 - measure.Y * fscale);
break;
case 2:
rectangle = new Rectangle(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize + _state.TileSize / 2,
_state.TileSize / 2 - 1,
_state.TileSize / 2 - 1);
stringPosition = new Vector2(
player.Position.X * _state.TileSize,
player.Position.Y * _state.TileSize + _state.TileSize - measure.Y * fscale);
break;
case 3:
default:
rectangle = new Rectangle(
player.Position.X * _state.TileSize + _state.TileSize / 2,
player.Position.Y * _state.TileSize + _state.TileSize / 2,
_state.TileSize / 2 - 1,
_state.TileSize / 2 - 1);
stringPosition = new Vector2(
player.Position.X * _state.TileSize + _state.TileSize / 2,
player.Position.Y * _state.TileSize + _state.TileSize - measure.Y * fscale);
break;
}
i++;
}
_spriteBatch.DrawRectangle(rectangle, color, 2);
_spriteBatch.DrawString(ffont,
player.Initials,
stringPosition,
color,
0,
Vector2.Zero,
fscale,
SpriteEffects.None,
0);
}
}
foreach (var player in _sessionData.Players)