diff --git a/.gitmodules b/.gitmodules index dfa0e5d..6223e3c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "encompass-cs"] path = encompass-cs url = https://gitea.moonside.games/MoonsideGames/encompass-cs.git +[submodule "SpriteFontPlus"] + path = SpriteFontPlus + url = https://github.com/rds1983/SpriteFontPlus.git diff --git a/PongFE/Content/Fonts/SquaredDisplay.ttf b/PongFE/Content/Fonts/SquaredDisplay.ttf new file mode 100755 index 0000000..b285c10 Binary files /dev/null and b/PongFE/Content/Fonts/SquaredDisplay.ttf differ diff --git a/PongFE/Engines/Spawners/GoalBoundarySpawner.cs b/PongFE/Engines/Spawners/GoalBoundarySpawner.cs index 138a49e..4d6f8bd 100644 --- a/PongFE/Engines/Spawners/GoalBoundarySpawner.cs +++ b/PongFE/Engines/Spawners/GoalBoundarySpawner.cs @@ -14,6 +14,7 @@ namespace PongFE.Spawners AddComponent(entity, new CollisionComponent(new MoonTools.Bonk.Rectangle(0, 0, message.Width, message.Height))); AddComponent(entity, new CanDestroyComponent()); AddComponent(entity, new ScoreComponent(0)); + AddComponent(entity, new PlayerComponent(message.PlayerIndex)); } } } diff --git a/PongFE/Messages/GoalBoundarySpawnMessage.cs b/PongFE/Messages/GoalBoundarySpawnMessage.cs index 67f47a0..657ae7f 100644 --- a/PongFE/Messages/GoalBoundarySpawnMessage.cs +++ b/PongFE/Messages/GoalBoundarySpawnMessage.cs @@ -6,12 +6,14 @@ namespace PongFE.Messages { public struct GoalBoundarySpawnMessage : IMessage { + public PlayerIndex PlayerIndex { get; } public Position2D Position { get; } public int Width { get; } public int Height { get; } - public GoalBoundarySpawnMessage(Position2D position, int width, int height) + public GoalBoundarySpawnMessage(PlayerIndex playerIndex, Position2D position, int width, int height) { + PlayerIndex = playerIndex; Position = position; Width = width; Height = height; diff --git a/PongFE/PongFE.Core.csproj b/PongFE/PongFE.Core.csproj index 1b82a06..b75870e 100644 --- a/PongFE/PongFE.Core.csproj +++ b/PongFE/PongFE.Core.csproj @@ -25,6 +25,7 @@ + diff --git a/PongFE/PongFE.Framework.csproj b/PongFE/PongFE.Framework.csproj index 462e657..52582a4 100644 --- a/PongFE/PongFE.Framework.csproj +++ b/PongFE/PongFE.Framework.csproj @@ -30,6 +30,7 @@ + diff --git a/PongFE/PongFEGame.cs b/PongFE/PongFEGame.cs index c55b7b3..2da6adb 100644 --- a/PongFE/PongFEGame.cs +++ b/PongFE/PongFEGame.cs @@ -1,3 +1,4 @@ +using System.IO; using Encompass; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -6,6 +7,7 @@ using PongFE.Enums; using PongFE.Messages; using PongFE.Renderers; using PongFE.Spawners; +using SpriteFontPlus; namespace PongFE { @@ -19,6 +21,8 @@ namespace PongFE SpriteBatch SpriteBatch { get; set; } Texture2D WhitePixel { get; set; } + DynamicSpriteFont ScoreFont { get; set; } + public PongFEGame() { graphics = new GraphicsDeviceManager(this); @@ -39,6 +43,8 @@ namespace PongFE WhitePixel = new Texture2D(GraphicsDevice, 1, 1); WhitePixel.SetData(new Color[] { Color.White }); + ScoreFont = DynamicSpriteFont.FromTtf(File.ReadAllBytes(@"Content/Fonts/SquaredDisplay.ttf"), 128); + WorldBuilder.AddEngine(new InputEngine()); WorldBuilder.AddEngine(new PaddleMovementEngine()); WorldBuilder.AddEngine(new VelocityEngine()); @@ -57,6 +63,7 @@ namespace PongFE WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel)); WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch)); + WorldBuilder.AddGeneralRenderer(new ScoreRenderer(SpriteBatch, ScoreFont), 0); WorldBuilder.SendMessage( new PaddleSpawnMessage( @@ -108,6 +115,7 @@ namespace PongFE // right boundary WorldBuilder.SendMessage( new GoalBoundarySpawnMessage( + Enums.PlayerIndex.One, new MoonTools.Structs.Position2D(1280, 0), 6, 720 @@ -117,6 +125,7 @@ namespace PongFE // left boundary WorldBuilder.SendMessage( new GoalBoundarySpawnMessage( + Enums.PlayerIndex.Two, new MoonTools.Structs.Position2D(-6, 0), 6, 720 diff --git a/PongFE/Renderers/ScoreRenderer.cs b/PongFE/Renderers/ScoreRenderer.cs new file mode 100644 index 0000000..d982af7 --- /dev/null +++ b/PongFE/Renderers/ScoreRenderer.cs @@ -0,0 +1,62 @@ +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 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()) + { + ref readonly var scoreComponent = ref GetComponent(entity); + ref readonly var playerComponent = ref GetComponent(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(400, 20), + Color.White + ); + } + + if (playerTwoScore.HasValue) + { + SpriteBatch.DrawString( + Font, + playerTwoScore.Value.ToString(), + new Vector2(880 - 64, 20), + Color.White + ); + } + } + } +} diff --git a/SpriteFontPlus b/SpriteFontPlus new file mode 160000 index 0000000..0362a40 --- /dev/null +++ b/SpriteFontPlus @@ -0,0 +1 @@ +Subproject commit 0362a4081e41ae6f8b5d78f0ddac7f0240fe8c9f