using System;
using System.Windows.Forms;
namespace PacMan
{
///
/// Manages the game: Creates grid, pacman and ghost objects, sets order of game mechanics.
///
public class GameEngine
{
//In the classic game, a kibble is worth 10 points. Multiplies the score by 10.
private const int SCORE_MODIFIER = 10;
private Grid dataGrid;
private PacMan pacman;
private Ghost[] ghost;
private Random rand;
private int score;
private bool gameOver;
public GameEngine(Grid dataGrid, Random rand, int tickInterval)
{
this.dataGrid = dataGrid;
this.rand = rand;
dataGrid.Paint += new PaintEventHandler(dataGrid_Paint);
pacman = new PacMan();
ghost = new Ghost[4];
ghost[0] = new Ghost("Purple");
ghost[1] = new Ghost("Red");
ghost[2] = new Ghost("Blue");
ghost[3] = new Ghost("Green");
gameOver = false;
}
///
/// Prompts creatures to be painted on grid
///
///
///
void dataGrid_Paint(object sender, PaintEventArgs e)
{
pacman.Draw(e.Graphics);
for (int i = 0; i < ghost.Length; i++)
{
ghost[i].Draw(e.Graphics);
}
}
///
/// Checks to see if the game is over
///
public void checkGameOverConditions()
{
if ((pacman.Alive == false) || (dataGrid.KibblesRemaining == 0))
{
gameOver = true;
}
}
///
/// Sets the main order of the game.
///
public void RunGame()
{
MoveCreatures();
Draw();
checkGameOverConditions();
}
///
/// Prompts Creatures to move.
///
public void MoveCreatures()
{
pacman.Move(dataGrid);
if (pacman.Eat(dataGrid))
{
foreach (Ghost individualGhost in ghost)
{
individualGhost.BehaviourType = GhostMode.Frightened;
}
}
for (int i = 0; i < ghost.Length; i++)
{
pacman.CheckGhostCollision(ghost[i]);
ghost[i].Move(rand, dataGrid);
ghost[i].UpdateMode();
pacman.CheckGhostCollision(ghost[i]);
}
}
///
/// Prompts everything on the grid to be redrawn
///
public void Draw()
{
dataGrid.Invalidate();
}
///
/// Decides what direction the user is attempting to turn to
///
///
public void GetUserDirection(Keys userKeyStroke)
{
switch (userKeyStroke)
{
case Keys.Up:
pacman.AttemptedDirection = Direction.Up;
break;
case Keys.Left:
pacman.AttemptedDirection = Direction.Left;
break;
case Keys.Down:
pacman.AttemptedDirection = Direction.Down;
break;
case Keys.Right:
pacman.AttemptedDirection = Direction.Right;
break;
default:
break;
}
}
///
/// Calculates the player's score
///
///
public int CalculateScore()
{
return score = ((dataGrid.NStartKibbles - dataGrid.KibblesRemaining) * SCORE_MODIFIER);
}
///
/// Gets/Sets whether or not the game is over
///
public bool GameOver
{
get { return gameOver; }
set { gameOver = value; }
}
///
/// Gets/Sets the Score
///
public int Score
{
get { return score; }
set { score = value; }
}
///
/// Gets/Sets the current number of lives the user has.
///
public int N_Lives
{
get { return pacman.Lives; }
}
}
}