From 17bed9c60b2fa64cb9f366afd18b012c65b7a968 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Thu, 16 Jul 2020 15:07:42 -0700 Subject: [PATCH] score renderer --- .gitmodules | 3 + PongFE/Content/Fonts/SquaredDisplay.ttf | Bin 0 -> 14648 bytes .../Engines/Spawners/GoalBoundarySpawner.cs | 1 + PongFE/Messages/GoalBoundarySpawnMessage.cs | 4 +- PongFE/PongFE.Core.csproj | 1 + PongFE/PongFE.Framework.csproj | 1 + PongFE/PongFEGame.cs | 9 +++ PongFE/Renderers/ScoreRenderer.cs | 62 ++++++++++++++++++ SpriteFontPlus | 1 + 9 files changed, 81 insertions(+), 1 deletion(-) create mode 100755 PongFE/Content/Fonts/SquaredDisplay.ttf create mode 100644 PongFE/Renderers/ScoreRenderer.cs create mode 160000 SpriteFontPlus 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 0000000000000000000000000000000000000000..b285c10af7cf25996ad0d4a4e741e278ee96d21e GIT binary patch literal 14648 zcmeHO31D2+b-jQ7%>T1#7A+o+WWkKbk~dkBEo6D$kc~kGjO93^00tomOU9CgWm`7b z#36xz!lH!4kj6oY{!ZHQmI zq4DhW`TN^eA%8WI=lQno*52kL_bw+2uOV{z+jeeGP>G%(D*Qgm7hc-CsoQ8d(0~Tg zsH1OFYhN$L$VW7P8uBBXw(P!i>a!E@Y~c~2$)|O;ckgOG{~_f2kZ(-s>}YL2^4!f~ zJUa>H=XN5&v)a{#_rbarpwwdV_s~;yO6=Xet&f&bf^Jtl zr0F2Cgad6*I87k>rd^!xazSF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U^7`z^|?bJX1x$$)#zaPg>?ZjXCwu9FNcmb$}`sGuNS6e!$6aDLA(Mmg1eMN%#ju v6W$g&6Yuz(he26ID=|=KVMx<>KWGh|?WykBv1JR`W0&8BpUO1+TT%Zl@Mk09 literal 0 HcmV?d00001 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