mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-25 08:22:15 +00:00
working on new screen management stuff
This commit is contained in:
@@ -1,252 +1,42 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Audio;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using Microsoft.Xna.Framework.Media;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.BitmapFonts;
|
||||
using MonoGame.Extended.Sprites;
|
||||
using MonoGame.Extended.Screens;
|
||||
using Pong.Screens;
|
||||
|
||||
namespace Pong
|
||||
{
|
||||
public class GameObject
|
||||
{
|
||||
public Vector2 Position;
|
||||
public Sprite Sprite;
|
||||
public RectangleF BoundingRectangle => Sprite.GetBoundingRectangle(Position, 0, Vector2.One);
|
||||
}
|
||||
|
||||
public class Paddle : GameObject
|
||||
{
|
||||
}
|
||||
|
||||
public class Ball : GameObject
|
||||
{
|
||||
public Vector2 Velocity;
|
||||
}
|
||||
|
||||
public class GameMain : Game
|
||||
{
|
||||
// ReSharper disable once NotAccessedField.Local
|
||||
private GraphicsDeviceManager _graphics;
|
||||
private SpriteBatch _spriteBatch;
|
||||
|
||||
private const int _screenWidth = 800;
|
||||
private const int _screenHeight = 480;
|
||||
private Paddle _bluePaddle;
|
||||
private Paddle _redPaddle;
|
||||
private Ball _ball;
|
||||
private Texture2D _court;
|
||||
private BitmapFont _font;
|
||||
private int _leftScore;
|
||||
private int _rightScore;
|
||||
private readonly FastRandom _random = new FastRandom();
|
||||
|
||||
private SoundEffect _plopSoundEffect;
|
||||
|
||||
public GameMain()
|
||||
{
|
||||
_graphics = new GraphicsDeviceManager(this)
|
||||
{
|
||||
PreferredBackBufferWidth = _screenWidth,
|
||||
PreferredBackBufferHeight = _screenHeight,
|
||||
PreferredBackBufferWidth = 800,
|
||||
PreferredBackBufferHeight = 480,
|
||||
SynchronizeWithVerticalRetrace = false
|
||||
};
|
||||
|
||||
Content.RootDirectory = "Content";
|
||||
IsFixedTimeStep = true;
|
||||
TargetElapsedTime = TimeSpan.FromSeconds(1f / 60f);
|
||||
}
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
|
||||
_plopSoundEffect = Content.Load<SoundEffect>("pip");
|
||||
|
||||
_font = Content.Load<BitmapFont>("kenney-rocket-square");
|
||||
|
||||
_court = Content.Load<Texture2D>("court");
|
||||
|
||||
_bluePaddle = new Paddle
|
||||
{
|
||||
Position = new Vector2(50, _screenWidth / 2f),
|
||||
Sprite = new Sprite(Content.Load<Texture2D>("paddleBlue"))
|
||||
};
|
||||
|
||||
_redPaddle = new Paddle
|
||||
{
|
||||
Position = new Vector2(_screenWidth - 50, _screenHeight / 2f),
|
||||
Sprite = new Sprite(Content.Load<Texture2D>("paddleRed"))
|
||||
};
|
||||
|
||||
_ball = new Ball
|
||||
{
|
||||
Position = new Vector2(_screenWidth / 2f, _screenHeight / 2f),
|
||||
Sprite = new Sprite(Content.Load<Texture2D>("ballGrey")),
|
||||
Velocity = new Vector2(250, 200)
|
||||
};
|
||||
var screenManager = Components.Add<ScreenManager>();
|
||||
screenManager.LoadScreen(new GameScreen(this));
|
||||
}
|
||||
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
var elapsedSeconds = gameTime.GetElapsedSeconds();
|
||||
var keyboardState = Keyboard.GetState();
|
||||
var mouseState = Mouse.GetState();
|
||||
|
||||
if (keyboardState.IsKeyDown(Keys.Escape))
|
||||
Exit();
|
||||
|
||||
MovePaddlePlayer(mouseState);
|
||||
|
||||
MovePaddleAi(_redPaddle, elapsedSeconds);
|
||||
|
||||
ConstrainPaddle(_bluePaddle);
|
||||
ConstrainPaddle(_redPaddle);
|
||||
|
||||
MoveBall(elapsedSeconds);
|
||||
|
||||
if (BallHitPaddle(_ball, _bluePaddle))
|
||||
{
|
||||
// TODO: Play pong sound
|
||||
// TODO: Change the angle of the bounce
|
||||
_ball.Velocity *= 1.05f;
|
||||
_plopSoundEffect.Play(1.0f, _random.NextSingle(0.5f, 1.0f), -1f);
|
||||
}
|
||||
|
||||
if (BallHitPaddle(_ball, _redPaddle))
|
||||
{
|
||||
// TODO: Play ping sound
|
||||
// TODO: Change the angle of the bounce
|
||||
_ball.Velocity *= 1.05f;
|
||||
_plopSoundEffect.Play(1f, _random.NextSingle(-1f, 1f), 1f);
|
||||
}
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
private void MovePaddlePlayer(MouseState mouseState)
|
||||
{
|
||||
_bluePaddle.Position.Y = mouseState.Position.Y;
|
||||
}
|
||||
|
||||
private static bool BallHitPaddle(Ball ball, Paddle paddle)
|
||||
{
|
||||
if (ball.BoundingRectangle.Intersects(paddle.BoundingRectangle))
|
||||
{
|
||||
if (ball.BoundingRectangle.Left < paddle.BoundingRectangle.Left)
|
||||
ball.Position.X = paddle.BoundingRectangle.Left - ball.BoundingRectangle.Width / 2;
|
||||
|
||||
if (ball.BoundingRectangle.Right > paddle.BoundingRectangle.Right)
|
||||
ball.Position.X = paddle.BoundingRectangle.Right + ball.BoundingRectangle.Width / 2;
|
||||
|
||||
ball.Velocity.X = -ball.Velocity.X;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void MoveBall(float elapsedSeconds)
|
||||
{
|
||||
_ball.Position += _ball.Velocity * elapsedSeconds;
|
||||
|
||||
var halfHeight = _ball.BoundingRectangle.Height / 2;
|
||||
var halfWidth = _ball.BoundingRectangle.Width / 2;
|
||||
|
||||
// top and bottom walls
|
||||
// TODO: Play 'tink' sound
|
||||
if (_ball.Position.Y - halfHeight < 0)
|
||||
{
|
||||
_ball.Position.Y = halfHeight;
|
||||
_ball.Velocity.Y = -_ball.Velocity.Y;
|
||||
}
|
||||
|
||||
if (_ball.Position.Y + halfHeight > _screenHeight)
|
||||
{
|
||||
_ball.Position.Y = _screenHeight - halfHeight;
|
||||
_ball.Velocity.Y = -_ball.Velocity.Y;
|
||||
}
|
||||
|
||||
// left and right is out of bounds
|
||||
// TODO: Play sound and update score
|
||||
// TODO: Reset ball to default velocity
|
||||
if (_ball.Position.X > _screenWidth + halfWidth && _ball.Velocity.X > 0)
|
||||
{
|
||||
_ball.Position = new Vector2(_screenWidth / 2f, _screenHeight / 2f);
|
||||
_ball.Velocity = new Vector2(_random.Next(2, 5) * -100, 100);
|
||||
_leftScore++;
|
||||
}
|
||||
|
||||
if (_ball.Position.X < -halfWidth && _ball.Velocity.X < 0)
|
||||
{
|
||||
_ball.Position = new Vector2(_screenWidth / 2f, _screenHeight / 2f);
|
||||
_ball.Velocity = new Vector2(_random.Next(2, 5) * 100, 100);
|
||||
_rightScore++;
|
||||
}
|
||||
}
|
||||
|
||||
private static void ConstrainPaddle(Paddle paddle)
|
||||
{
|
||||
if (paddle.BoundingRectangle.Left < 0)
|
||||
paddle.Position.X = paddle.BoundingRectangle.Width / 2f;
|
||||
|
||||
if (paddle.BoundingRectangle.Right > _screenWidth)
|
||||
paddle.Position.X = _screenWidth - paddle.BoundingRectangle.Width / 2f;
|
||||
|
||||
if (paddle.BoundingRectangle.Top < 0)
|
||||
paddle.Position.Y = paddle.BoundingRectangle.Height / 2f;
|
||||
|
||||
if (paddle.BoundingRectangle.Bottom > _screenHeight)
|
||||
paddle.Position.Y = _screenHeight - paddle.BoundingRectangle.Height / 2f;
|
||||
}
|
||||
|
||||
private void MovePaddleAi(Paddle paddle, float elapsedSeconds)
|
||||
{
|
||||
const float difficulty = 0.80f;
|
||||
var paddleSpeed = Math.Abs(_ball.Velocity.Y) * difficulty;
|
||||
|
||||
if (paddleSpeed < 0)
|
||||
paddleSpeed = -paddleSpeed;
|
||||
|
||||
//ball moving down
|
||||
if (_ball.Velocity.Y > 0)
|
||||
{
|
||||
if (_ball.Position.Y > paddle.Position.Y)
|
||||
paddle.Position.Y += paddleSpeed * elapsedSeconds;
|
||||
else
|
||||
paddle.Position.Y -= paddleSpeed * elapsedSeconds;
|
||||
}
|
||||
|
||||
//ball moving up
|
||||
if (_ball.Velocity.Y < 0)
|
||||
{
|
||||
if (_ball.Position.Y < paddle.Position.Y)
|
||||
paddle.Position.Y -= paddleSpeed * elapsedSeconds;
|
||||
else
|
||||
paddle.Position.Y += paddleSpeed * elapsedSeconds;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||
_spriteBatch.Draw(_court, new Rectangle(0, 0, _screenWidth, _screenHeight), Color.White);
|
||||
|
||||
DrawScores();
|
||||
|
||||
_spriteBatch.Draw(_redPaddle.Sprite, _redPaddle.Position);
|
||||
_spriteBatch.Draw(_bluePaddle.Sprite, _bluePaddle.Position);
|
||||
_spriteBatch.Draw(_ball.Sprite, _ball.Position);
|
||||
_spriteBatch.End();
|
||||
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
|
||||
private void DrawScores()
|
||||
{
|
||||
_spriteBatch.DrawString(_font, $"{_leftScore:00}", new Vector2(172, 10), new Color(0.2f, 0.2f, 0.2f), 0, Vector2.Zero, Vector2.One * 4f, SpriteEffects.None, 0);
|
||||
_spriteBatch.DrawString(_font, $"{_rightScore:00}", new Vector2(430, 10), new Color(0.2f, 0.2f, 0.2f), 0, Vector2.Zero, Vector2.One * 4f, SpriteEffects.None, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace Pong.GameObjects
|
||||
{
|
||||
public class Ball : GameObject
|
||||
{
|
||||
public Vector2 Velocity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.Sprites;
|
||||
|
||||
namespace Pong.GameObjects
|
||||
{
|
||||
public class GameObject
|
||||
{
|
||||
public Vector2 Position;
|
||||
public Sprite Sprite;
|
||||
public RectangleF BoundingRectangle => Sprite.GetBoundingRectangle(Position, 0, Vector2.One);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Pong.GameObjects
|
||||
{
|
||||
public class Paddle : GameObject
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -42,9 +42,14 @@
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="GameObjects\Ball.cs" />
|
||||
<Compile Include="GameMain.cs" />
|
||||
<Compile Include="GameObjects\GameObject.cs" />
|
||||
<Compile Include="GameObjects\Paddle.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Screens\GameScreen.cs" />
|
||||
<Compile Include="Screens\MagentaScreen.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MonoGame.Framework">
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Audio;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.BitmapFonts;
|
||||
using MonoGame.Extended.Screens;
|
||||
using MonoGame.Extended.Sprites;
|
||||
using Pong.GameObjects;
|
||||
|
||||
namespace Pong.Screens
|
||||
{
|
||||
public class GameScreen : Screen
|
||||
{
|
||||
private readonly Game _game;
|
||||
private SpriteBatch _spriteBatch;
|
||||
private Paddle _bluePaddle;
|
||||
private Paddle _redPaddle;
|
||||
private Ball _ball;
|
||||
private Texture2D _court;
|
||||
private BitmapFont _font;
|
||||
private SoundEffect _plopSoundEffect;
|
||||
private int _leftScore;
|
||||
private int _rightScore;
|
||||
private readonly FastRandom _random = new FastRandom();
|
||||
|
||||
public GameScreen(Game game)
|
||||
{
|
||||
_game = game;
|
||||
}
|
||||
|
||||
public ContentManager Content => _game.Content;
|
||||
public GraphicsDevice GraphicsDevice => _game.GraphicsDevice;
|
||||
public int ScreenWidth => _game.GraphicsDevice.Viewport.Width;
|
||||
public int ScreenHeight => _game.GraphicsDevice.Viewport.Height;
|
||||
|
||||
public override void LoadContent()
|
||||
{
|
||||
base.LoadContent();
|
||||
|
||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
|
||||
_plopSoundEffect = Content.Load<SoundEffect>("pip");
|
||||
|
||||
_font = Content.Load<BitmapFont>("kenney-rocket-square");
|
||||
|
||||
_court = Content.Load<Texture2D>("court");
|
||||
|
||||
_bluePaddle = new Paddle
|
||||
{
|
||||
Position = new Vector2(50, ScreenWidth / 2f),
|
||||
Sprite = new Sprite(Content.Load<Texture2D>("paddleBlue"))
|
||||
};
|
||||
|
||||
_redPaddle = new Paddle
|
||||
{
|
||||
Position = new Vector2(ScreenWidth - 50, ScreenHeight / 2f),
|
||||
Sprite = new Sprite(Content.Load<Texture2D>("paddleRed"))
|
||||
};
|
||||
|
||||
_ball = new Ball
|
||||
{
|
||||
Position = new Vector2(ScreenWidth / 2f, ScreenHeight / 2f),
|
||||
Sprite = new Sprite(Content.Load<Texture2D>("ballGrey")),
|
||||
Velocity = new Vector2(250, 200)
|
||||
};
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
var elapsedSeconds = gameTime.GetElapsedSeconds();
|
||||
var mouseState = Mouse.GetState();
|
||||
|
||||
MovePaddlePlayer(mouseState);
|
||||
|
||||
MovePaddleAi(_redPaddle, elapsedSeconds);
|
||||
|
||||
ConstrainPaddle(_bluePaddle);
|
||||
ConstrainPaddle(_redPaddle);
|
||||
|
||||
MoveBall(elapsedSeconds);
|
||||
|
||||
if (BallHitPaddle(_ball, _bluePaddle))
|
||||
{
|
||||
// TODO: Play pong sound
|
||||
// TODO: Change the angle of the bounce
|
||||
_ball.Velocity *= 1.05f;
|
||||
_plopSoundEffect.Play(1.0f, _random.NextSingle(0.5f, 1.0f), -1f);
|
||||
}
|
||||
|
||||
if (BallHitPaddle(_ball, _redPaddle))
|
||||
{
|
||||
// TODO: Play ping sound
|
||||
// TODO: Change the angle of the bounce
|
||||
_ball.Velocity *= 1.05f;
|
||||
_plopSoundEffect.Play(1f, _random.NextSingle(-1f, 1f), 1f);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Draw(GameTime gameTime)
|
||||
{
|
||||
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||
_spriteBatch.Draw(_court, new Rectangle(0, 0, ScreenWidth, ScreenHeight), Color.White);
|
||||
|
||||
DrawScores();
|
||||
|
||||
_spriteBatch.Draw(_redPaddle.Sprite, _redPaddle.Position);
|
||||
_spriteBatch.Draw(_bluePaddle.Sprite, _bluePaddle.Position);
|
||||
_spriteBatch.Draw(_ball.Sprite, _ball.Position);
|
||||
_spriteBatch.End();
|
||||
}
|
||||
|
||||
private void DrawScores()
|
||||
{
|
||||
_spriteBatch.DrawString(_font, $"{_leftScore:00}", new Vector2(172, 10), new Color(0.2f, 0.2f, 0.2f), 0, Vector2.Zero, Vector2.One * 4f, SpriteEffects.None, 0);
|
||||
_spriteBatch.DrawString(_font, $"{_rightScore:00}", new Vector2(430, 10), new Color(0.2f, 0.2f, 0.2f), 0, Vector2.Zero, Vector2.One * 4f, SpriteEffects.None, 0);
|
||||
}
|
||||
|
||||
private void MovePaddlePlayer(MouseState mouseState)
|
||||
{
|
||||
_bluePaddle.Position.Y = mouseState.Position.Y;
|
||||
}
|
||||
|
||||
private static bool BallHitPaddle(Ball ball, Paddle paddle)
|
||||
{
|
||||
if (ball.BoundingRectangle.Intersects(paddle.BoundingRectangle))
|
||||
{
|
||||
if (ball.BoundingRectangle.Left < paddle.BoundingRectangle.Left)
|
||||
ball.Position.X = paddle.BoundingRectangle.Left - ball.BoundingRectangle.Width / 2;
|
||||
|
||||
if (ball.BoundingRectangle.Right > paddle.BoundingRectangle.Right)
|
||||
ball.Position.X = paddle.BoundingRectangle.Right + ball.BoundingRectangle.Width / 2;
|
||||
|
||||
ball.Velocity.X = -ball.Velocity.X;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void MoveBall(float elapsedSeconds)
|
||||
{
|
||||
_ball.Position += _ball.Velocity * elapsedSeconds;
|
||||
|
||||
var halfHeight = _ball.BoundingRectangle.Height / 2;
|
||||
var halfWidth = _ball.BoundingRectangle.Width / 2;
|
||||
|
||||
// top and bottom walls
|
||||
// TODO: Play 'tink' sound
|
||||
if (_ball.Position.Y - halfHeight < 0)
|
||||
{
|
||||
_ball.Position.Y = halfHeight;
|
||||
_ball.Velocity.Y = -_ball.Velocity.Y;
|
||||
}
|
||||
|
||||
if (_ball.Position.Y + halfHeight > ScreenHeight)
|
||||
{
|
||||
_ball.Position.Y = ScreenHeight - halfHeight;
|
||||
_ball.Velocity.Y = -_ball.Velocity.Y;
|
||||
}
|
||||
|
||||
// left and right is out of bounds
|
||||
// TODO: Play sound and update score
|
||||
// TODO: Reset ball to default velocity
|
||||
if (_ball.Position.X > ScreenWidth + halfWidth && _ball.Velocity.X > 0)
|
||||
{
|
||||
_ball.Position = new Vector2(ScreenWidth / 2f, ScreenHeight / 2f);
|
||||
_ball.Velocity = new Vector2(_random.Next(2, 5) * -100, 100);
|
||||
_leftScore++;
|
||||
ScreenManager.LoadScreen(new MagentaScreen(_game));
|
||||
}
|
||||
|
||||
if (_ball.Position.X < -halfWidth && _ball.Velocity.X < 0)
|
||||
{
|
||||
_ball.Position = new Vector2(ScreenWidth / 2f, ScreenHeight / 2f);
|
||||
_ball.Velocity = new Vector2(_random.Next(2, 5) * 100, 100);
|
||||
_rightScore++;
|
||||
ScreenManager.LoadScreen(new MagentaScreen(_game));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void ConstrainPaddle(Paddle paddle)
|
||||
{
|
||||
if (paddle.BoundingRectangle.Left < 0)
|
||||
paddle.Position.X = paddle.BoundingRectangle.Width / 2f;
|
||||
|
||||
if (paddle.BoundingRectangle.Right > ScreenWidth)
|
||||
paddle.Position.X = ScreenWidth - paddle.BoundingRectangle.Width / 2f;
|
||||
|
||||
if (paddle.BoundingRectangle.Top < 0)
|
||||
paddle.Position.Y = paddle.BoundingRectangle.Height / 2f;
|
||||
|
||||
if (paddle.BoundingRectangle.Bottom > ScreenHeight)
|
||||
paddle.Position.Y = ScreenHeight - paddle.BoundingRectangle.Height / 2f;
|
||||
}
|
||||
|
||||
private void MovePaddleAi(Paddle paddle, float elapsedSeconds)
|
||||
{
|
||||
const float difficulty = 0.80f;
|
||||
var paddleSpeed = Math.Abs(_ball.Velocity.Y) * difficulty;
|
||||
|
||||
if (paddleSpeed < 0)
|
||||
paddleSpeed = -paddleSpeed;
|
||||
|
||||
//ball moving down
|
||||
if (_ball.Velocity.Y > 0)
|
||||
{
|
||||
if (_ball.Position.Y > paddle.Position.Y)
|
||||
paddle.Position.Y += paddleSpeed * elapsedSeconds;
|
||||
else
|
||||
paddle.Position.Y -= paddleSpeed * elapsedSeconds;
|
||||
}
|
||||
|
||||
//ball moving up
|
||||
if (_ball.Velocity.Y < 0)
|
||||
{
|
||||
if (_ball.Position.Y < paddle.Position.Y)
|
||||
paddle.Position.Y -= paddleSpeed * elapsedSeconds;
|
||||
else
|
||||
paddle.Position.Y += paddleSpeed * elapsedSeconds;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using MonoGame.Extended;
|
||||
using MonoGame.Extended.Screens;
|
||||
|
||||
namespace Pong.Screens
|
||||
{
|
||||
public class MagentaScreen : Screen
|
||||
{
|
||||
private readonly Game _game;
|
||||
private SpriteBatch _spriteBatch;
|
||||
|
||||
public MagentaScreen(Game game)
|
||||
{
|
||||
_game = game;
|
||||
}
|
||||
|
||||
public override void LoadContent()
|
||||
{
|
||||
base.LoadContent();
|
||||
_spriteBatch = new SpriteBatch(_game.GraphicsDevice);
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
var mouseState = Mouse.GetState();
|
||||
|
||||
if (mouseState.LeftButton == ButtonState.Pressed)
|
||||
ScreenManager.LoadScreen(new GameScreen(_game));
|
||||
}
|
||||
|
||||
public override void Draw(GameTime gameTime)
|
||||
{
|
||||
_game.GraphicsDevice.Clear(Color.Black);
|
||||
|
||||
_spriteBatch.Begin();
|
||||
_spriteBatch.FillRectangle(new RectangleF(100, 100, 100, 100), Color.Magenta);
|
||||
_spriteBatch.End();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public static class GameComponentCollectionExtensions
|
||||
{
|
||||
public static T Add<T>(this GameComponentCollection collection)
|
||||
where T : IGameComponent, new()
|
||||
{
|
||||
var gameComponent = new T();
|
||||
collection.Add(gameComponent);
|
||||
return gameComponent;
|
||||
}
|
||||
|
||||
public static T Add<T>(this GameComponentCollection collection, Func<T> createGameComponent)
|
||||
where T : IGameComponent
|
||||
{
|
||||
var gameComponent = createGameComponent();
|
||||
collection.Add(gameComponent);
|
||||
return gameComponent;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,9 +46,11 @@
|
||||
<Compile Include="ColorExtensions.cs" />
|
||||
<Compile Include="Entities\EntityComponentAttribute.cs" />
|
||||
<Compile Include="FloatHelper.cs" />
|
||||
<Compile Include="GameComponentCollectionExtensions.cs" />
|
||||
<Compile Include="HslColor.cs" />
|
||||
<Compile Include="IRectangular.cs" />
|
||||
<Compile Include="Point3.cs" />
|
||||
<Compile Include="Screens\ScreenManager.cs" />
|
||||
<Compile Include="Serialization\BaseTypeJsonConverter.cs" />
|
||||
<Compile Include="Serialization\HslColorJsonConverter.cs" />
|
||||
<Compile Include="Serialization\JsonReaderExtensions.cs" />
|
||||
|
||||
@@ -5,69 +5,81 @@ namespace MonoGame.Extended.Screens
|
||||
{
|
||||
public abstract class Screen : IDisposable
|
||||
{
|
||||
protected Screen()
|
||||
{
|
||||
}
|
||||
public ScreenManager ScreenManager { get; internal set; }
|
||||
|
||||
public IScreenManager ScreenManager { get; internal set; }
|
||||
public bool IsInitialized { get; private set; }
|
||||
public bool IsVisible { get; set; }
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public T FindScreen<T>() where T : Screen
|
||||
{
|
||||
return ScreenManager?.FindScreen<T>();
|
||||
}
|
||||
|
||||
public void Show<T>(bool hideThis) where T : Screen
|
||||
{
|
||||
var screen = FindScreen<T>();
|
||||
screen.Show();
|
||||
|
||||
if (hideThis)
|
||||
Hide();
|
||||
}
|
||||
|
||||
public void Show<T>() where T : Screen
|
||||
{
|
||||
Show<T>(true);
|
||||
}
|
||||
|
||||
public void Show()
|
||||
{
|
||||
if (!IsInitialized)
|
||||
Initialize();
|
||||
|
||||
IsVisible = true;
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
IsVisible = false;
|
||||
}
|
||||
|
||||
public virtual void Initialize()
|
||||
{
|
||||
IsInitialized = true;
|
||||
}
|
||||
|
||||
public virtual void LoadContent()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void UnloadContent()
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Update(GameTime gameTime)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void Draw(GameTime gameTime)
|
||||
{
|
||||
}
|
||||
public virtual void Dispose() { }
|
||||
public virtual void Initialize() { }
|
||||
public virtual void LoadContent() { }
|
||||
public virtual void UnloadContent() { }
|
||||
public abstract void Update(GameTime gameTime);
|
||||
public abstract void Draw(GameTime gameTime);
|
||||
}
|
||||
|
||||
//public abstract class Screen : IDisposable
|
||||
//{
|
||||
// protected Screen()
|
||||
// {
|
||||
// }
|
||||
|
||||
// public IScreenManager ScreenManager { get; internal set; }
|
||||
// public bool IsInitialized { get; private set; }
|
||||
// public bool IsVisible { get; set; }
|
||||
|
||||
// public virtual void Dispose()
|
||||
// {
|
||||
// }
|
||||
|
||||
// public T FindScreen<T>() where T : Screen
|
||||
// {
|
||||
// return ScreenManager?.FindScreen<T>();
|
||||
// }
|
||||
|
||||
// public void Show<T>(bool hideThis) where T : Screen
|
||||
// {
|
||||
// var screen = FindScreen<T>();
|
||||
// screen.Show();
|
||||
|
||||
// if (hideThis)
|
||||
// Hide();
|
||||
// }
|
||||
|
||||
// public void Show<T>() where T : Screen
|
||||
// {
|
||||
// Show<T>(true);
|
||||
// }
|
||||
|
||||
// public void Show()
|
||||
// {
|
||||
// if (!IsInitialized)
|
||||
// Initialize();
|
||||
|
||||
// IsVisible = true;
|
||||
// }
|
||||
|
||||
// public void Hide()
|
||||
// {
|
||||
// IsVisible = false;
|
||||
// }
|
||||
|
||||
// public virtual void Initialize()
|
||||
// {
|
||||
// IsInitialized = true;
|
||||
// }
|
||||
|
||||
// public virtual void LoadContent()
|
||||
// {
|
||||
// }
|
||||
|
||||
// public virtual void UnloadContent()
|
||||
// {
|
||||
// }
|
||||
|
||||
// public virtual void Update(GameTime gameTime)
|
||||
// {
|
||||
// }
|
||||
|
||||
// public virtual void Draw(GameTime gameTime)
|
||||
// {
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -5,89 +5,89 @@ using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Screens
|
||||
{
|
||||
public interface IScreenManager
|
||||
{
|
||||
T FindScreen<T>() where T : Screen;
|
||||
}
|
||||
//public interface IScreenManager
|
||||
//{
|
||||
// T FindScreen<T>() where T : Screen;
|
||||
//}
|
||||
|
||||
public class ScreenGameComponent : DrawableGameComponent, IScreenManager
|
||||
{
|
||||
private readonly List<Screen> _screens;
|
||||
//public class ScreenGameComponent : DrawableGameComponent, IScreenManager
|
||||
//{
|
||||
// private readonly List<Screen> _screens;
|
||||
|
||||
public ScreenGameComponent(Game game, IEnumerable<Screen> screens)
|
||||
: this(game)
|
||||
{
|
||||
foreach (var screen in screens)
|
||||
Register(screen);
|
||||
}
|
||||
// public ScreenGameComponent(Game game, IEnumerable<Screen> screens)
|
||||
// : this(game)
|
||||
// {
|
||||
// foreach (var screen in screens)
|
||||
// Register(screen);
|
||||
// }
|
||||
|
||||
public ScreenGameComponent(Game game)
|
||||
: base(game)
|
||||
{
|
||||
_screens = new List<Screen>();
|
||||
}
|
||||
// public ScreenGameComponent(Game game)
|
||||
// : base(game)
|
||||
// {
|
||||
// _screens = new List<Screen>();
|
||||
// }
|
||||
|
||||
public T FindScreen<T>() where T : Screen
|
||||
{
|
||||
var screen = _screens.OfType<T>().FirstOrDefault();
|
||||
// public T FindScreen<T>() where T : Screen
|
||||
// {
|
||||
// var screen = _screens.OfType<T>().FirstOrDefault();
|
||||
|
||||
if (screen == null)
|
||||
throw new InvalidOperationException($"{typeof(T).Name} not registered");
|
||||
// if (screen == null)
|
||||
// throw new InvalidOperationException($"{typeof(T).Name} not registered");
|
||||
|
||||
return screen;
|
||||
}
|
||||
// return screen;
|
||||
// }
|
||||
|
||||
public T Register<T>(T screen)
|
||||
where T : Screen
|
||||
{
|
||||
screen.ScreenManager = this;
|
||||
_screens.Add(screen);
|
||||
// public T Register<T>(T screen)
|
||||
// where T : Screen
|
||||
// {
|
||||
// screen.ScreenManager = this;
|
||||
// _screens.Add(screen);
|
||||
|
||||
if (_screens.Count == 1)
|
||||
_screens[0].Show();
|
||||
// if (_screens.Count == 1)
|
||||
// _screens[0].Show();
|
||||
|
||||
return screen;
|
||||
}
|
||||
// return screen;
|
||||
// }
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
foreach (var screen in _screens)
|
||||
if (!screen.IsInitialized) // Check if screen has already been Initialized from the register
|
||||
screen.Initialize();
|
||||
// public override void Initialize()
|
||||
// {
|
||||
// foreach (var screen in _screens)
|
||||
// if (!screen.IsInitialized) // Check if screen has already been Initialized from the register
|
||||
// screen.Initialize();
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
// base.Initialize();
|
||||
// }
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
base.LoadContent();
|
||||
// protected override void LoadContent()
|
||||
// {
|
||||
// base.LoadContent();
|
||||
|
||||
foreach (var screen in _screens)
|
||||
screen.LoadContent();
|
||||
}
|
||||
// foreach (var screen in _screens)
|
||||
// screen.LoadContent();
|
||||
// }
|
||||
|
||||
protected override void UnloadContent()
|
||||
{
|
||||
base.UnloadContent();
|
||||
// protected override void UnloadContent()
|
||||
// {
|
||||
// base.UnloadContent();
|
||||
|
||||
foreach (var screen in _screens)
|
||||
screen.UnloadContent();
|
||||
}
|
||||
// foreach (var screen in _screens)
|
||||
// screen.UnloadContent();
|
||||
// }
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
base.Update(gameTime);
|
||||
// public override void Update(GameTime gameTime)
|
||||
// {
|
||||
// base.Update(gameTime);
|
||||
|
||||
foreach (var screen in _screens.Where(s => s.IsVisible))
|
||||
screen.Update(gameTime);
|
||||
}
|
||||
// foreach (var screen in _screens.Where(s => s.IsVisible))
|
||||
// screen.Update(gameTime);
|
||||
// }
|
||||
|
||||
public override void Draw(GameTime gameTime)
|
||||
{
|
||||
base.Draw(gameTime);
|
||||
// public override void Draw(GameTime gameTime)
|
||||
// {
|
||||
// base.Draw(gameTime);
|
||||
|
||||
foreach (var screen in _screens.Where(s => s.IsVisible))
|
||||
screen.Draw(gameTime);
|
||||
}
|
||||
}
|
||||
// foreach (var screen in _screens.Where(s => s.IsVisible))
|
||||
// screen.Draw(gameTime);
|
||||
// }
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Screens
|
||||
{
|
||||
public class ScreenManager : SimpleDrawableGameComponent
|
||||
{
|
||||
public ScreenManager()
|
||||
{
|
||||
}
|
||||
|
||||
private Screen _activeScreen;
|
||||
private bool _isInitialized;
|
||||
private bool _isLoaded;
|
||||
|
||||
public void LoadScreen(Screen screen)
|
||||
{
|
||||
_activeScreen?.UnloadContent();
|
||||
_activeScreen?.Dispose();
|
||||
|
||||
screen.ScreenManager = this;
|
||||
|
||||
if (_isInitialized)
|
||||
screen.Initialize();
|
||||
|
||||
if (_isLoaded)
|
||||
screen.LoadContent();
|
||||
|
||||
_activeScreen = screen;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_activeScreen?.Initialize();
|
||||
_isInitialized = true;
|
||||
}
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
base.LoadContent();
|
||||
_activeScreen?.LoadContent();
|
||||
_isLoaded = true;
|
||||
}
|
||||
|
||||
protected override void UnloadContent()
|
||||
{
|
||||
base.UnloadContent();
|
||||
_activeScreen?.UnloadContent();
|
||||
_isLoaded = false;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
_activeScreen?.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(GameTime gameTime)
|
||||
{
|
||||
_activeScreen.Draw(gameTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,13 +5,11 @@ namespace MonoGame.Extended
|
||||
{
|
||||
public abstract class SimpleDrawableGameComponent : SimpleGameComponent, IDrawable
|
||||
{
|
||||
private int _drawOrder;
|
||||
private bool _isVisible;
|
||||
|
||||
protected SimpleDrawableGameComponent()
|
||||
{
|
||||
}
|
||||
|
||||
private bool _isVisible = true;
|
||||
public bool Visible
|
||||
{
|
||||
get { return _isVisible; }
|
||||
@@ -27,6 +25,7 @@ namespace MonoGame.Extended
|
||||
|
||||
bool IDrawable.Visible => _isVisible;
|
||||
|
||||
private int _drawOrder;
|
||||
public int DrawOrder
|
||||
{
|
||||
get { return _drawOrder; }
|
||||
@@ -44,13 +43,5 @@ namespace MonoGame.Extended
|
||||
public event EventHandler<EventArgs> VisibleChanged;
|
||||
|
||||
public abstract void Draw(GameTime gameTime);
|
||||
|
||||
protected virtual void LoadContent()
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual void UnloadContent()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,15 +3,24 @@ using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended
|
||||
{
|
||||
public abstract class SimpleGameComponent : IGameComponent, IUpdateable
|
||||
public abstract class SimpleGameComponent : IGameComponent, IUpdateable, IDisposable, IComparable<GameComponent>, IComparable<SimpleGameComponent>
|
||||
{
|
||||
private bool _isEnabled = true;
|
||||
private int _updateOrder;
|
||||
private bool _isInitialized;
|
||||
|
||||
protected SimpleGameComponent()
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_isInitialized)
|
||||
{
|
||||
UnloadContent();
|
||||
_isInitialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _isEnabled = true;
|
||||
public bool IsEnabled
|
||||
{
|
||||
get { return _isEnabled; }
|
||||
@@ -27,10 +36,19 @@ namespace MonoGame.Extended
|
||||
|
||||
public virtual void Initialize()
|
||||
{
|
||||
if (!_isInitialized)
|
||||
{
|
||||
LoadContent();
|
||||
_isInitialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void LoadContent() { }
|
||||
protected virtual void UnloadContent() { }
|
||||
|
||||
bool IUpdateable.Enabled => _isEnabled;
|
||||
|
||||
private int _updateOrder;
|
||||
public int UpdateOrder
|
||||
{
|
||||
get { return _updateOrder; }
|
||||
@@ -48,5 +66,15 @@ namespace MonoGame.Extended
|
||||
public event EventHandler<EventArgs> UpdateOrderChanged;
|
||||
|
||||
public abstract void Update(GameTime gameTime);
|
||||
|
||||
public int CompareTo(GameComponent other)
|
||||
{
|
||||
return other.UpdateOrder - UpdateOrder;
|
||||
}
|
||||
|
||||
public int CompareTo(SimpleGameComponent other)
|
||||
{
|
||||
return other.UpdateOrder - UpdateOrder;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user