using System;
using System.Threading;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace DodgeEm.Model
{
///
/// Manages the entire game.
///
public class GameManager
{
#region Data members
private readonly double backgroundHeight;
private readonly double backgroundWidth;
private Canvas gameBoard;
private WaveManager newManager;
private DispatcherTimer timer;
private PlayerManager playerManager;
private int timeUntilPlayerWins;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
/// Precondition: backgroundHeight > 0 AND backgroundWidth > 0
///
/// The backgroundHeight of the game play window.
/// The backgroundWidth of the game play window.
public GameManager(double backgroundHeight, double backgroundWidth, Canvas gameBoard)
{
if (backgroundHeight <= 0)
{
throw new ArgumentOutOfRangeException(nameof(backgroundHeight));
}
if (backgroundWidth <= 0)
{
throw new ArgumentOutOfRangeException(nameof(backgroundWidth));
}
if (gameBoard == null)
{
throw new ArgumentNullException(nameof(gameBoard));
}
this.backgroundHeight = backgroundHeight;
this.backgroundWidth = backgroundWidth;
this.playerManager = new PlayerManager(backgroundHeight, backgroundWidth);
this.gameBoard = gameBoard;
this.timer = new DispatcherTimer();
this.timeUntilPlayerWins = 22;
this.setUpTimer();
}
#endregion
#region Methods
///
/// Initializes the game placing player in the game
/// Precondition: background != null
/// Postcondition: Game is initialized and ready for play.
///
public void InitializeGame()
{
this.playerManager.PlacePlayer(this.gameBoard);
}
///
/// Moves the player to the left.
/// Precondition: none
/// Postcondition: The player has moved left.
///
public void HandlePlayerLeftMovement()
{
this.playerManager.MovePlayerLeft();
}
///
/// Moves the player to the right.
/// Precondition: none
/// Postcondition: The player has moved right.
///
public void HandlePlayerRightMovement()
{
this.playerManager.MovePlayerRight();
}
///
/// Moves the player up
/// Precondition: none
/// Postcondition: The player has moved up.
///
public void HandlePlayerUpMovement()
{
this.playerManager.MovePlayerUp();
}
///
/// Moves the player down
/// Precondition: none
/// Postcondition: The player has moved down.
///
public void HandlePlayerDownMovement()
{
this.playerManager.MovePlayerDown();
}
private void setUpTimer()
{
this.timer.Interval = TimeSpan.FromSeconds(this.timeUntilPlayerWins);
this.timer.Tick += this.timerOnTick;
this.timer.Start();
}
private void timerOnTick(object sender, object e)
{
if (this.timeUntilPlayerWins == 15)
{
this.newManager.createEnemy();
}
this.timeUntilPlayerWins--;
System.Diagnostics.Debug.WriteLine(this.timeUntilPlayerWins);
}
#endregion
}
}