65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
using Encompass;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using PongFE.Components;
|
|
using PongFE.Enums;
|
|
using SpriteFontPlus;
|
|
|
|
namespace PongFE.Renderers
|
|
{
|
|
public class ScoreRenderer : GeneralRenderer
|
|
{
|
|
public SpriteBatch SpriteBatch { get; }
|
|
public DynamicSpriteFont Font { get; }
|
|
public int SpacingFromCenter { get; } = 240;
|
|
public int SpacingFromTop { get; } = 20;
|
|
|
|
public ScoreRenderer(SpriteBatch spriteBatch, DynamicSpriteFont font)
|
|
{
|
|
SpriteBatch = spriteBatch;
|
|
Font = font;
|
|
}
|
|
|
|
public override void Render()
|
|
{
|
|
int? playerOneScore = null;
|
|
int? playerTwoScore = null;
|
|
|
|
foreach (ref readonly var entity in ReadEntities<ScoreComponent>())
|
|
{
|
|
ref readonly var scoreComponent = ref GetComponent<ScoreComponent>(entity);
|
|
ref readonly var playerComponent = ref GetComponent<PlayerComponent>(entity);
|
|
|
|
if (playerComponent.PlayerIndex == Enums.PlayerIndex.One)
|
|
{
|
|
playerOneScore = scoreComponent.Score;
|
|
}
|
|
else if (playerComponent.PlayerIndex == Enums.PlayerIndex.Two)
|
|
{
|
|
playerTwoScore = scoreComponent.Score;
|
|
}
|
|
}
|
|
|
|
if (playerOneScore.HasValue)
|
|
{
|
|
SpriteBatch.DrawString(
|
|
Font,
|
|
playerOneScore.Value.ToString(),
|
|
new Vector2(640 - SpacingFromCenter, SpacingFromTop),
|
|
Color.White
|
|
);
|
|
}
|
|
|
|
if (playerTwoScore.HasValue)
|
|
{
|
|
SpriteBatch.DrawString(
|
|
Font,
|
|
playerTwoScore.Value.ToString(),
|
|
new Vector2(640 + SpacingFromCenter - (Font.Size / 2), SpacingFromTop),
|
|
Color.White
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|