PongFE/PongFE/PongFEGame.cs

157 lines
5.4 KiB
C#
Raw Permalink Normal View History

2020-07-16 22:07:42 +00:00
using System.IO;
2020-07-11 21:36:18 +00:00
using Encompass;
2020-07-18 03:16:19 +00:00
using MoonTools;
2020-07-11 21:36:18 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2020-07-17 20:37:54 +00:00
using PongFE.Components;
2020-07-12 21:54:08 +00:00
using PongFE.Engines;
2020-07-15 20:08:58 +00:00
using PongFE.Enums;
2020-07-12 21:54:08 +00:00
using PongFE.Messages;
2020-07-12 01:25:43 +00:00
using PongFE.Renderers;
2020-07-12 21:54:08 +00:00
using PongFE.Spawners;
2020-07-16 22:07:42 +00:00
using SpriteFontPlus;
2020-07-11 21:36:18 +00:00
namespace PongFE
{
class PongFEGame : Game
{
GraphicsDeviceManager graphics;
WorldBuilder WorldBuilder { get; } = new WorldBuilder();
World World { get; set; }
2020-07-12 01:25:43 +00:00
SpriteBatch SpriteBatch { get; set; }
Texture2D WhitePixel { get; set; }
2020-07-17 20:37:54 +00:00
RenderTarget2D GameRenderTarget { get; set; }
2020-07-12 01:25:43 +00:00
2020-07-16 22:07:42 +00:00
DynamicSpriteFont ScoreFont { get; set; }
2020-07-18 21:41:32 +00:00
DynamicSpriteFont InstructionFont { get; set; }
2020-07-16 22:07:42 +00:00
2020-07-17 20:37:54 +00:00
const int PLAY_AREA_WIDTH = 1280;
const int PLAY_AREA_HEIGHT = 720;
2020-07-11 21:36:18 +00:00
public PongFEGame()
{
graphics = new GraphicsDeviceManager(this);
2020-07-18 04:19:14 +00:00
graphics.PreferredBackBufferWidth = 1920;
graphics.PreferredBackBufferHeight = 1080;
2020-07-11 21:36:18 +00:00
graphics.PreferMultiSampling = true;
Content.RootDirectory = "Content";
2020-07-17 20:37:54 +00:00
Window.AllowUserResizing = false;
2020-07-11 21:36:18 +00:00
IsMouseVisible = true;
2020-07-12 21:54:08 +00:00
IsFixedTimeStep = true;
2020-07-11 21:36:18 +00:00
}
protected override void LoadContent()
{
2020-07-18 03:16:19 +00:00
ResolutionScaler.Init(
2020-07-17 20:37:54 +00:00
GraphicsDevice.PresentationParameters.BackBufferWidth,
GraphicsDevice.PresentationParameters.BackBufferHeight,
PLAY_AREA_WIDTH,
PLAY_AREA_HEIGHT
);
2020-07-12 01:25:43 +00:00
SpriteBatch = new SpriteBatch(GraphicsDevice);
WhitePixel = new Texture2D(GraphicsDevice, 1, 1);
WhitePixel.SetData(new Color[] { Color.White });
2020-07-17 20:37:54 +00:00
GameRenderTarget = new RenderTarget2D(GraphicsDevice, PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT);
ScoreFont = DynamicSpriteFont.FromTtf(
File.ReadAllBytes(@"Content/Fonts/SquaredDisplay.ttf"),
128
);
2020-07-16 22:07:42 +00:00
2020-07-18 21:41:32 +00:00
InstructionFont = DynamicSpriteFont.FromTtf(
File.ReadAllBytes(@"Content/Fonts/SquaredDisplay.ttf"),
48
);
2020-07-20 03:16:15 +00:00
WorldBuilder.AddEngine(new GameStateEngine(ScoreFont, InstructionFont));
2020-07-12 21:54:08 +00:00
WorldBuilder.AddEngine(new InputEngine());
WorldBuilder.AddEngine(new PaddleMovementEngine());
2020-07-13 18:49:20 +00:00
WorldBuilder.AddEngine(new VelocityEngine());
2020-07-12 21:54:08 +00:00
WorldBuilder.AddEngine(new MotionEngine());
2020-07-13 18:49:20 +00:00
WorldBuilder.AddEngine(new CollisionEngine());
2020-07-18 05:53:22 +00:00
WorldBuilder.AddEngine(new AngledBounceEngine());
2020-07-13 18:49:20 +00:00
WorldBuilder.AddEngine(new BounceEngine());
2020-07-15 21:00:55 +00:00
WorldBuilder.AddEngine(new DestroyEngine());
2020-07-16 21:39:36 +00:00
WorldBuilder.AddEngine(new ScoreEngine());
2020-07-13 18:49:20 +00:00
WorldBuilder.AddEngine(new UpdatePositionEngine());
WorldBuilder.AddEngine(new UpdateVelocityEngine());
2020-07-15 20:08:58 +00:00
WorldBuilder.AddEngine(new ComputerControlEngine());
2020-07-20 03:16:15 +00:00
WorldBuilder.AddEngine(new GameWinEngine(ScoreFont));
2020-07-13 18:49:20 +00:00
2020-07-15 20:08:58 +00:00
WorldBuilder.AddEngine(new BallSpawner(WhitePixel));
2020-07-15 01:53:55 +00:00
WorldBuilder.AddEngine(new BoundarySpawner());
2020-07-15 21:00:55 +00:00
WorldBuilder.AddEngine(new GoalBoundarySpawner());
2020-07-15 20:08:58 +00:00
WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel));
2020-07-20 03:16:15 +00:00
WorldBuilder.AddEngine(new UITextSpawner());
2020-07-13 18:49:20 +00:00
2020-07-12 01:25:43 +00:00
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
2020-07-18 04:19:14 +00:00
WorldBuilder.AddGeneralRenderer(new CenterLineRenderer(SpriteBatch, WhitePixel), 0);
2020-07-16 22:07:42 +00:00
WorldBuilder.AddGeneralRenderer(new ScoreRenderer(SpriteBatch, ScoreFont), 0);
2020-07-20 03:16:15 +00:00
WorldBuilder.AddGeneralRenderer(new UITextRenderer(SpriteBatch), 0);
2020-07-12 21:54:08 +00:00
2020-07-17 20:37:54 +00:00
var playAreaEntity = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(playAreaEntity, new PlayAreaComponent(PLAY_AREA_WIDTH, PLAY_AREA_HEIGHT));
2020-07-18 21:41:32 +00:00
var gameStateEntity = WorldBuilder.CreateEntity();
2020-07-20 03:16:15 +00:00
WorldBuilder.SetComponent(gameStateEntity, new GameStateComponent(GameState.Init));
2020-07-18 21:41:32 +00:00
2020-07-20 03:16:15 +00:00
var ballParametersEntity = WorldBuilder.CreateEntity();
WorldBuilder.SetComponent(ballParametersEntity, new BallParametersComponent(500, 0.5));
WorldBuilder.SendMessage(new ChangeGameStateMessage(GameState.Title));
2020-07-11 21:36:18 +00:00
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)
{
2020-07-17 20:37:54 +00:00
GraphicsDevice.SetRenderTarget(GameRenderTarget);
2020-07-12 01:25:43 +00:00
GraphicsDevice.Clear(Color.Black);
2020-07-11 21:36:18 +00:00
2020-07-12 01:25:43 +00:00
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
2020-07-11 21:36:18 +00:00
World.Draw();
2020-07-12 01:25:43 +00:00
SpriteBatch.End();
2020-07-11 21:36:18 +00:00
2020-07-17 20:37:54 +00:00
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin(
SpriteSortMode.Deferred,
null,
null,
null,
null,
null,
2020-07-18 03:16:19 +00:00
ResolutionScaler.TransformMatrix
2020-07-17 20:37:54 +00:00
);
SpriteBatch.Draw(
GameRenderTarget,
Vector2.Zero,
Color.White
);
SpriteBatch.End();
2020-07-11 21:36:18 +00:00
base.Draw(gameTime);
}
}
}