147 lines
4.6 KiB
C#
147 lines
4.6 KiB
C#
|
using Microsoft.Xna.Framework;
|
|||
|
using Microsoft.Xna.Framework.Graphics;
|
|||
|
using RhythmBullet.Utilities.Camera;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace RhythmBullet.Utilities.ScreenSystem
|
|||
|
{
|
|||
|
public enum ScreenState { EnterTransition, ExitTransition, Normal }
|
|||
|
|
|||
|
public class Screen
|
|||
|
{
|
|||
|
public List<ITransition> Transitions;
|
|||
|
public bool UseRenderTargetForExitTransition;
|
|||
|
public Color BackgroundColor;
|
|||
|
public GraphicsDevice GraphicsDevice { get; private set; }
|
|||
|
public Screen NextScreen { get; set; }
|
|||
|
public Rectangle ScreenSize { get; private set; }
|
|||
|
public bool Initiated { get; private set; }
|
|||
|
public Camera2D Camera { get; private set; }
|
|||
|
public Screen(bool useEnterTransition = false)
|
|||
|
{
|
|||
|
State = useEnterTransition ? ScreenState.EnterTransition : ScreenState.Normal;
|
|||
|
Transitions = new List<ITransition>();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Called only once after initialization to give required parameters.
|
|||
|
/// </summary>
|
|||
|
/// <param name="graphicsDevice"></param>
|
|||
|
/// <param name="screenSize"></param>
|
|||
|
/// <param name="camera"></param>
|
|||
|
public virtual void Initiate(GraphicsDevice graphicsDevice, Rectangle screenSize, Camera2D camera)
|
|||
|
{
|
|||
|
this.Camera = camera;
|
|||
|
this.ScreenSize = screenSize;
|
|||
|
GraphicsDevice = graphicsDevice;
|
|||
|
Initiated = true;
|
|||
|
}
|
|||
|
|
|||
|
public virtual void Update(GameTime gameTime)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public virtual void Draw(SpriteBatch spriteBatch)
|
|||
|
{
|
|||
|
foreach (ITransition transition in Transitions)
|
|||
|
{
|
|||
|
transition.DrawTransition(spriteBatch);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Updates the transition.
|
|||
|
/// </summary>
|
|||
|
/// <param name="delta">Time passed since last frame in seconds.</param>
|
|||
|
/// <param name="assetsLoaded">If waiting on assets.</param>
|
|||
|
/// <param name="previousScreenExitFrame">The previous screen's frame. May be null.</param>
|
|||
|
/// <returns>Only returns true if exit transition is complete. Returns false otherwise.</returns>
|
|||
|
public bool UpdateTransition(double delta, bool assetsLoaded)
|
|||
|
{
|
|||
|
switch (State)
|
|||
|
{
|
|||
|
case ScreenState.EnterTransition:
|
|||
|
EnteringTransition(delta, assetsLoaded);
|
|||
|
break;
|
|||
|
case ScreenState.ExitTransition:
|
|||
|
return ExitingTransition(delta, assetsLoaded);
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
private void EnteringTransition(double delta, bool assetsLoaded)
|
|||
|
{
|
|||
|
bool complete = true;
|
|||
|
for (int transitionID = 0; transitionID < Transitions.Count; transitionID++)
|
|||
|
{
|
|||
|
ITransition transition = Transitions[transitionID];
|
|||
|
if (!transition.EnteringTransition(delta, assetsLoaded))
|
|||
|
{
|
|||
|
complete = false;
|
|||
|
}
|
|||
|
}
|
|||
|
if (complete && State != ScreenState.ExitTransition)
|
|||
|
{
|
|||
|
State = ScreenState.Normal;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private bool ExitingTransition(double delta, bool assetsLoaded)
|
|||
|
{
|
|||
|
bool complete = true;
|
|||
|
foreach (ITransition transition in Transitions)
|
|||
|
{
|
|||
|
if (!transition.ExitingTransition(delta, assetsLoaded))
|
|||
|
{
|
|||
|
complete = false;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
return complete;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Called when the screen is shown.
|
|||
|
/// </summary>
|
|||
|
public virtual void Show()
|
|||
|
{
|
|||
|
foreach (ITransition transition in Transitions)
|
|||
|
{
|
|||
|
transition.InitiateTransition(ScreenSize);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public virtual void Hide()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public virtual void AssetLoadStateChange(bool state)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public ScreenState State { get; private set; }
|
|||
|
|
|||
|
public void StartExitTransition(bool UseRenderTargetForExitTransition = false)
|
|||
|
{
|
|||
|
this.UseRenderTargetForExitTransition = UseRenderTargetForExitTransition;
|
|||
|
State = ScreenState.ExitTransition;
|
|||
|
}
|
|||
|
|
|||
|
public virtual void UpdatePreviousScreenFrame(RenderTarget2D previousScreenFrame)
|
|||
|
{
|
|||
|
foreach (ITransition transition in Transitions)
|
|||
|
{
|
|||
|
transition.UpdatePreviousScreenFrame(previousScreenFrame);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|