PongFE/PongFE/PongFEGame.cs

157 lines
5.4 KiB
C#

using System.IO;
using Encompass;
using MoonTools;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PongFE.Components;
using PongFE.Engines;
using PongFE.Enums;
using PongFE.Messages;
using PongFE.Renderers;
using PongFE.Spawners;
using SpriteFontPlus;
namespace PongFE
{
class PongFEGame : Game
{
GraphicsDeviceManager graphics;
WorldBuilder WorldBuilder { get; } = new WorldBuilder();
World World { get; set; }
SpriteBatch SpriteBatch { get; set; }
Texture2D WhitePixel { get; set; }
RenderTarget2D GameRenderTarget { get; set; }
DynamicSpriteFont ScoreFont { get; set; }
DynamicSpriteFont InstructionFont { get; set; }
const int PLAY_AREA_WIDTH = 1280;
const int PLAY_AREA_HEIGHT = 720;
public PongFEGame()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1920;
graphics.PreferredBackBufferHeight = 1080;
graphics.PreferMultiSampling = true;
Content.RootDirectory = "Content";
Window.AllowUserResizing = false;
IsMouseVisible = true;
IsFixedTimeStep = true;
}
protected override void LoadContent()
{
ResolutionScaler.Init(
GraphicsDevice.PresentationParameters.BackBufferWidth,
GraphicsDevice.PresentationParameters.BackBufferHeight,
PLAY_AREA_WIDTH,
PLAY_AREA_HEIGHT
);
SpriteBatch = new SpriteBatch(GraphicsDevice);
WhitePixel = new Texture2D(GraphicsDevice, 1, 1);
WhitePixel.SetData(new Color[] { Color.White });
GameRenderTarget = new RenderTarget2D(GraphicsDevice, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT);
ScoreFont = DynamicSpriteFont.FromTtf(
File.ReadAllBytes(@"Content/Fonts/SquaredDisplay.ttf"),
128
);
InstructionFont = DynamicSpriteFont.FromTtf(
File.ReadAllBytes(@"Content/Fonts/SquaredDisplay.ttf"),
48
);
WorldBuilder.AddEngine(new GameStateEngine(ScoreFont, InstructionFont));
WorldBuilder.AddEngine(new InputEngine());
WorldBuilder.AddEngine(new PaddleMovementEngine());
WorldBuilder.AddEngine(new VelocityEngine());
WorldBuilder.AddEngine(new MotionEngine());
WorldBuilder.AddEngine(new CollisionEngine());
WorldBuilder.AddEngine(new AngledBounceEngine());
WorldBuilder.AddEngine(new BounceEngine());
WorldBuilder.AddEngine(new DestroyEngine());
WorldBuilder.AddEngine(new ScoreEngine());
WorldBuilder.AddEngine(new UpdatePositionEngine());
WorldBuilder.AddEngine(new UpdateVelocityEngine());
WorldBuilder.AddEngine(new ComputerControlEngine());
WorldBuilder.AddEngine(new GameWinEngine(ScoreFont));
WorldBuilder.AddEngine(new BallSpawner(WhitePixel));
WorldBuilder.AddEngine(new BoundarySpawner());
WorldBuilder.AddEngine(new GoalBoundarySpawner());
WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel));
WorldBuilder.AddEngine(new UITextSpawner());
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
WorldBuilder.AddGeneralRenderer(new CenterLineRenderer(SpriteBatch, WhitePixel), 0);
WorldBuilder.AddGeneralRenderer(new ScoreRenderer(SpriteBatch, ScoreFont), 0);
WorldBuilder.AddGeneralRenderer(new UITextRenderer(SpriteBatch), 0);
var playAreaEntity = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(playAreaEntity, new PlayAreaComponent(PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT));
var gameStateEntity = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(gameStateEntity, new GameStateComponent(GameState.Init));
var ballParametersEntity = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(ballParametersEntity, new BallParametersComponent(500, 0.5));
WorldBuilder.SendMessage(new ChangeGameStateMessage(GameState.Title));
World = WorldBuilder.Build();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
protected override void Update(GameTime gameTime)
{
World.Update(gameTime.ElapsedGameTime.TotalSeconds);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(GameRenderTarget);
GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
World.Draw();
SpriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin(
SpriteSortMode.Deferred,
null,
null,
null,
null,
null,
ResolutionScaler.TransformMatrix
);
SpriteBatch.Draw(
GameRenderTarget,
Vector2.Zero,
Color.White
);
SpriteBatch.End();
base.Draw(gameTime);
}
}
}