#undef SINGLE_TEST
// Define INGAME_ZOOM to allow zooming in and out with
// the PageUp and PageDown keys.
#define INGAME_ZOOM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace CarFire
{
///
/// This class is responsible for controlling what draws to the screen when the game is running.
///
public class Display
{
bool playerChosen = false;
List mProjectiles = new List();
//List mCharacters = new List();
IPlayer[] mCharacters = new IPlayer[4];
Texture2D everything;
Texture2D projectile1;
Game mGame;
#if SINGLE_TEST
List mLastPressedKeys = new List();
#endif
public Display(Game game)
{
mGame = game;
/*
mMap = aMap;
mCharacters = characters;
*/
}
///
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///
public void LoadContent(ContentManager contentManager)
{
everything = contentManager.Load("cs");
projectile1 = contentManager.Load("projectile");
}
///
/// UnloadContent will be called once per game and is the place to unload
/// all content.
///
public void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
/// Provides a snapshot of timing values.
public void Update(TimeSpan timespan, GameState state, int thisPlayer)
{
//INPUT - testing input... has to be through network later
#if SINGLE_TEST
KeyboardState keyState = Keyboard.GetState();
List pressedKeys = new List();
List releasedKeys = new List();
Keys[] pressedKeysArray = keyState.GetPressedKeys();
foreach (Keys k in pressedKeysArray)
{
if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
else mLastPressedKeys.Remove(k);
}
releasedKeys = mLastPressedKeys;
mLastPressedKeys = new List(pressedKeysArray);
//Just apply input for the first player
mCharacters[0].MovePlayer(pressedKeys);
if (pressedKeys.Contains(Keys.Enter) && !releasedKeys.Contains(Keys.Enter))
{
mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5,0), mCharacters[0].GridX +1, mCharacters[0].GridY+1, (int)mCharacters[0].GridX*(int)Map.PixelsToUnitSquares,(int)mCharacters[0].GridY*(int)Map.PixelsToUnitSquares));
}
mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);
#endif
//Handle projectiles - update and check for wall collisions
for (int i = 0; i < mProjectiles.Count; i++ )
{
bool removed = false;
if (!mGame.State.Map.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y)))
{
mProjectiles.RemoveAt(i);
removed = true;
i--;
}
if(!removed)
mProjectiles[i].Update(timespan);
}
//Check for collisons
for (int j = 0; j < mCharacters.Length; j++)
{
if(mCharacters[j] != null)
for (int i = 0; i < mProjectiles.Count; i++)
{
if (mProjectiles[i].Coordinates.X == mCharacters[j].Coordinates.X && mProjectiles[i].Coordinates.Y == mCharacters[j].Coordinates.Y)
{
mCharacters[j].causeDamageTo(mProjectiles[i].Damage);
Console.WriteLine(mCharacters[j].Health);
mProjectiles.RemoveAt(i);
i--;
}
}
}
//Update input for each player
for (int i = 0; i < mGame.State.NumberOfPlayers; i++)
{
//If player has not selected a player yet let them select one.
if (mCharacters[i] == null)
{
if (mGame.State.GetKeysDown(i).Contains(Keys.Enter))
{
mCharacters[i] = new Human(mGame, "", everything, projectile1, this, mGame.State.Map.GetStartingPositionForPlayer(i + 1));
}
}
//Regular player input updates
else
{
mCharacters[i].MovePlayer(timespan, mGame.State.GetKeysDown(i));
}
}
if (mCharacters[thisPlayer] != null)
{
mGame.State.Map.CenterCell = mCharacters[thisPlayer].Position;
}
//Handle wall collisions of projectiles again...
for (int i = 0; i < mProjectiles.Count; i++)
{
if (!mGame.State.Map.IsCellOpen(new Point(mProjectiles[i].Coordinates.X, mProjectiles[i].Coordinates.Y)))
{
mProjectiles.RemoveAt(i);
i--;
}
}
#if INGAME_ZOOM
if (Keyboard.GetState().IsKeyDown(Keys.PageUp)) mGame.State.Map.Zoom = mGame.State.Map.Zoom + 0.5f;
if (Keyboard.GetState().IsKeyDown(Keys.PageDown)) mGame.State.Map.Zoom = mGame.State.Map.Zoom - 0.5f;
#endif
}
///
/// This is called when the game should draw itself.
///
/// Used to draw with
public void Draw(SpriteBatch spriteBatch)
{
mGame.State.Map.Draw(spriteBatch);
mGame.State.Entities.ForEach(delegate(IEntity e) { e.Draw(spriteBatch); });
foreach(Projectile projectile in mProjectiles)
{
projectile.Draw(spriteBatch);
}
for(int i = 0; i < mGame.State.NumberOfPlayers; i++)//IPlayer character in mCharacters)
{
if (mCharacters[i] != null)
{
mCharacters[i].Draw(spriteBatch);
}
}
}
///
/// Add a projectile to the Display.
///
///
public void AddProjectiles(Projectile projectile)
{
mProjectiles.Add(projectile);
}
}
}