center line

main
Evan Hemsley 2020-07-17 21:19:14 -07:00
parent 089f210c88
commit f347780019
2 changed files with 68 additions and 2 deletions

View File

@ -32,8 +32,8 @@ namespace PongFE
public PongFEGame() public PongFEGame()
{ {
graphics = new GraphicsDeviceManager(this); graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1680; graphics.PreferredBackBufferWidth = 1920;
graphics.PreferredBackBufferHeight = 1050; graphics.PreferredBackBufferHeight = 1080;
graphics.PreferMultiSampling = true; graphics.PreferMultiSampling = true;
Content.RootDirectory = "Content"; Content.RootDirectory = "Content";
@ -81,6 +81,7 @@ namespace PongFE
WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel)); WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel));
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch)); WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
WorldBuilder.AddGeneralRenderer(new CenterLineRenderer(SpriteBatch, WhitePixel), 0);
WorldBuilder.AddGeneralRenderer(new ScoreRenderer(SpriteBatch, ScoreFont), 0); WorldBuilder.AddGeneralRenderer(new ScoreRenderer(SpriteBatch, ScoreFont), 0);
WorldBuilder.SendMessage( WorldBuilder.SendMessage(

View File

@ -0,0 +1,65 @@
using System;
using Encompass;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PongFE.Components;
namespace PongFE.Renderers
{
public class CenterLineRenderer : GeneralRenderer
{
public SpriteBatch SpriteBatch { get; }
public Texture2D WhitePixel { get; }
public CenterLineRenderer(SpriteBatch spriteBatch, Texture2D whitePixel)
{
SpriteBatch = spriteBatch;
WhitePixel = whitePixel;
}
public override void Render()
{
ref readonly var playAreaComponent = ref ReadComponent<PlayAreaComponent>();
DrawDottedLine(playAreaComponent.Width / 2, 0, playAreaComponent.Width / 2, playAreaComponent.Height, 20, 20);
}
private void DrawDottedLine(float x1, float y1, float x2, float y2, int dash, int gap)
{
var dx = x2 - x1;
var dy = y2 - y1;
var angle = Math.Atan2(dy, dx);
var st = dash + gap;
var len = Math.Sqrt(dx * dx + dy * dy);
var nm = (len - dash) / st;
SpriteBatch.End();
SpriteBatch.Begin(
SpriteSortMode.Deferred,
null,
null,
null,
null,
null,
Matrix.CreateRotationZ((float)angle) * Matrix.CreateTranslation(x1, y1, 0)
);
for (var i = 0; i < nm; i++)
{
SpriteBatch.Draw(
WhitePixel,
new Rectangle(
(int)(i * st + gap * 0.5),
0,
dash,
1
),
Color.White
);
}
SpriteBatch.End();
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
}
}
}