PongFE/PongFE/PongFEGame.cs

153 lines
4.6 KiB
C#

using Encompass;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PongFE.Engines;
using PongFE.Enums;
using PongFE.Messages;
using PongFE.Renderers;
using PongFE.Spawners;
namespace PongFE
{
class PongFEGame : Game
{
GraphicsDeviceManager graphics;
WorldBuilder WorldBuilder { get; } = new WorldBuilder();
World World { get; set; }
SpriteBatch SpriteBatch { get; set; }
Texture2D WhitePixel { get; set; }
public PongFEGame()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
graphics.PreferMultiSampling = true;
Content.RootDirectory = "Content";
Window.AllowUserResizing = true;
IsMouseVisible = true;
IsFixedTimeStep = true;
}
protected override void LoadContent()
{
SpriteBatch = new SpriteBatch(GraphicsDevice);
WhitePixel = new Texture2D(GraphicsDevice, 1, 1);
WhitePixel.SetData(new Color[] { Color.White });
WorldBuilder.AddEngine(new InputEngine());
WorldBuilder.AddEngine(new PaddleMovementEngine());
WorldBuilder.AddEngine(new VelocityEngine());
WorldBuilder.AddEngine(new MotionEngine());
WorldBuilder.AddEngine(new CollisionEngine());
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 BallSpawner(WhitePixel));
WorldBuilder.AddEngine(new BoundarySpawner());
WorldBuilder.AddEngine(new GoalBoundarySpawner());
WorldBuilder.AddEngine(new PaddleSpawner(WhitePixel));
WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch));
WorldBuilder.SendMessage(
new PaddleSpawnMessage(
new MoonTools.Structs.Position2D(5, 5),
Enums.PlayerIndex.One,
PaddleControl.Player,
20,
80
)
);
WorldBuilder.SendMessage(
new PaddleSpawnMessage(
new MoonTools.Structs.Position2D(1255, 5),
Enums.PlayerIndex.Two,
PaddleControl.Computer,
20,
80
)
);
WorldBuilder.SendMessage(
new BallSpawnMessage(
new MoonTools.Structs.Position2D(640, 360),
300,
16,
16
)
);
// top boundary
WorldBuilder.SendMessage(
new BoundarySpawnMessage(
new MoonTools.Structs.Position2D(0, -6),
1280,
6
)
);
// bottom boundary
WorldBuilder.SendMessage(
new BoundarySpawnMessage(
new MoonTools.Structs.Position2D(0, 720),
1280,
6
)
);
// right boundary
WorldBuilder.SendMessage(
new GoalBoundarySpawnMessage(
new MoonTools.Structs.Position2D(1280, 0),
6,
720
)
);
// left boundary
WorldBuilder.SendMessage(
new GoalBoundarySpawnMessage(
new MoonTools.Structs.Position2D(-6, 0),
6,
720
)
);
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.Clear(Color.Black);
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
World.Draw();
SpriteBatch.End();
base.Draw(gameTime);
}
}
}