72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using System;
|
|
using Encompass;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using PongFE.Components;
|
|
using PongFE.Enums;
|
|
|
|
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 gameStateComponent = ref ReadComponent<GameStateComponent>();
|
|
|
|
if (gameStateComponent.GameState == GameState.Game)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|