got basic screen transitions working

This commit is contained in:
Dylan Wilson
2018-04-10 22:40:45 +10:00
parent 332ee260d5
commit bc65dab9ac
3 changed files with 87 additions and 11 deletions
+2 -2
View File
@@ -169,7 +169,7 @@ namespace Pong.Screens
_ball.Position = new Vector2(ScreenWidth / 2f, ScreenHeight / 2f);
_ball.Velocity = new Vector2(_random.Next(2, 5) * -100, 100);
_leftScore++;
ScreenManager.LoadScreen(new MagentaScreen(_game));
ScreenManager.LoadScreen(new MagentaScreen(_game), new Transition(GraphicsDevice, Color.Black));
}
if (_ball.Position.X < -halfWidth && _ball.Velocity.X < 0)
@@ -177,7 +177,7 @@ namespace Pong.Screens
_ball.Position = new Vector2(ScreenWidth / 2f, ScreenHeight / 2f);
_ball.Velocity = new Vector2(_random.Next(2, 5) * 100, 100);
_rightScore++;
ScreenManager.LoadScreen(new MagentaScreen(_game));
ScreenManager.LoadScreen(new MagentaScreen(_game), new Transition(GraphicsDevice, Color.Black));
}
}
+2 -7
View File
@@ -1,7 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended;
using MonoGame.Extended.Screens;
namespace Pong.Screens
@@ -27,16 +26,12 @@ namespace Pong.Screens
var mouseState = Mouse.GetState();
if (mouseState.LeftButton == ButtonState.Pressed)
ScreenManager.LoadScreen(new GameScreen(_game));
ScreenManager.LoadScreen(new GameScreen(_game), new Transition(_game.GraphicsDevice, Color.Black));
}
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();
_game.GraphicsDevice.Clear(Color.Magenta);
}
}
}
@@ -1,7 +1,69 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MonoGame.Extended.Screens
{
public class Transition : IDisposable
{
private readonly GraphicsDevice _graphicsDevice;
private readonly SpriteBatch _spriteBatch;
private readonly Color _color;
private float _value;
private TransitionState _state = TransitionState.Out;
private enum TransitionState { In, Out }
public Transition(GraphicsDevice graphicsDevice, Color color)
{
_graphicsDevice = graphicsDevice;
_color = color;
_spriteBatch = new SpriteBatch(graphicsDevice);
}
public event EventHandler StateChanged;
public void Dispose()
{
}
public bool Update(GameTime gameTime)
{
var elapsedSeconds = gameTime.GetElapsedSeconds();
switch (_state)
{
case TransitionState.Out:
_value += elapsedSeconds;
if (_value >= 1.0f)
{
_state = TransitionState.In;
StateChanged?.Invoke(this, EventArgs.Empty);
}
break;
case TransitionState.In:
_value -= elapsedSeconds;
if (_value <= 0.0f)
return false;
break;
default:
throw new ArgumentOutOfRangeException();
}
return true;
}
public void Draw(GameTime gameTime)
{
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
_spriteBatch.FillRectangle(_graphicsDevice.Viewport.Bounds, _color * MathHelper.Clamp(_value, 0, 1));
_spriteBatch.End();
}
}
public class ScreenManager : SimpleDrawableGameComponent
{
public ScreenManager()
@@ -11,6 +73,13 @@ namespace MonoGame.Extended.Screens
private Screen _activeScreen;
private bool _isInitialized;
private bool _isLoaded;
private Transition _activeTransition;
public void LoadScreen(Screen screen, Transition transition)
{
_activeTransition = transition;
_activeTransition.StateChanged += (sender, args) => LoadScreen(screen);
}
public void LoadScreen(Screen screen)
{
@@ -51,12 +120,24 @@ namespace MonoGame.Extended.Screens
public override void Update(GameTime gameTime)
{
_activeScreen?.Update(gameTime);
if (_activeTransition != null)
{
if (!_activeTransition.Update(gameTime))
{
_activeTransition.Dispose();
_activeTransition = null;
}
}
else
{
_activeScreen?.Update(gameTime);
}
}
public override void Draw(GameTime gameTime)
{
_activeScreen.Draw(gameTime);
_activeScreen?.Draw(gameTime);
_activeTransition?.Draw(gameTime);
}
}
}