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
{
class GameLogic
{
Game mGame;
public GameLogic(Game game)
{
mGame = game;
}
///
/// Update the game logic.
///
/// timeslice
/// index of the player to center the screen around
public void Update(TimeSpan timespan, int thisPlayer)
{
//Handle projectiles - update and check for wall collisions
for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
{
bool removed = false;
//Check to see if there are any entities in the square with the projectile
if (!mGame.IsCellOpen(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y)))
{
//The projectile has hit something.
IEntity hitEntity = mGame.GetEntityAtCoordinates(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y));
//If it is a monster than decrement monster health and increment the score of the player who shot it.
if (hitEntity is IMonster)
{
IMonster hitMonster = (IMonster)hitEntity;
hitMonster.causeDamageTo(mGame.State.mProjectiles[i].Damage);
if (hitMonster.Health > 0)
{
mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score += mGame.State.HitMonsterScore;
Console.WriteLine(mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score);
}
else
{
mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score += mGame.State.KillMonsterScore;
Console.WriteLine(mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score);
//Remove dead monsters
mGame.State.Entities.Remove(hitEntity);
}
}
mGame.State.mProjectiles.RemoveAt(i);
removed = true;
i--;
}
if (!removed)
mGame.State.mProjectiles[i].Update(timespan);
}
//Check for collisons
for (int j = 0; j < mGame.State.mCharacters.Length; j++)
{
if (mGame.State.mCharacters[j] != null)
for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
{
if (mGame.State.mProjectiles[i].Coordinates.X == mGame.State.mCharacters[j].Coordinates.X && mGame.State.mProjectiles[i].Coordinates.Y == mGame.State.mCharacters[j].Coordinates.Y)
{
mGame.State.mCharacters[j].causeDamageTo(mGame.State.mProjectiles[i].Damage);
Console.WriteLine(mGame.State.mCharacters[j].Health);
mGame.State.mProjectiles.RemoveAt(i);
i--;
}
}
}
//Update input for each player
for (int i = 0; i < mGame.State.NumberOfPlayers; i++)
{
if(mGame.State.mCharacters[i] != null)
mGame.State.mCharacters[i].UpdateInput(timespan, mGame.State.GetKeysDown(i));
}
if (mGame.State.mCharacters[thisPlayer] != null)
{
mGame.State.Map.CenterCell = mGame.State.mCharacters[thisPlayer].Position;
}
//Handle wall collisions of projectiles again...
for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
{
if (!mGame.State.Map.IsCellOpen(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y)))
{
mGame.State.mProjectiles.RemoveAt(i);
i--;
}
}
}
}
}