2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
16 public GameLogic(Game game)
21 /// Update the game logic.
23 /// <param name="timespan">timeslice</param>
24 /// <param name="thisPlayer">index of the player to center the screen around</param>
25 public void Update(TimeSpan timespan, int thisPlayer)
27 //Handle projectiles - update and check for wall collisions
28 for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
31 //Check to see if there are any entities in the square with the projectile
32 if (!mGame.IsCellOpen(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y)))
34 //The projectile has hit something.
35 IEntity hitEntity = mGame.GetEntityAtCoordinates(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y));
36 //If it is a monster than decrement monster health and increment the score of the player who shot it.
37 if (hitEntity is IMonster)
39 IMonster hitMonster = (IMonster)hitEntity;
40 hitMonster.causeDamageTo(mGame.State.mProjectiles[i].Damage);
41 if (hitMonster.Health > 0)
43 mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score += mGame.State.HitMonsterScore;
44 Console.WriteLine(mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score);
48 mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score += mGame.State.KillMonsterScore;
49 Console.WriteLine(mGame.State.mCharacters[mGame.State.mProjectiles[i].CharacterIndex].Score);
50 //Remove dead monsters
51 mGame.State.Entities.Remove(hitEntity);
54 mGame.State.mProjectiles.RemoveAt(i);
59 mGame.State.mProjectiles[i].Update(timespan);
63 for (int j = 0; j < mGame.State.mCharacters.Length; j++)
66 if (mGame.State.mCharacters[j] != null)
67 for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
69 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)
71 mGame.State.mCharacters[j].causeDamageTo(mGame.State.mProjectiles[i].Damage);
72 Console.WriteLine(mGame.State.mCharacters[j].Health);
73 mGame.State.mProjectiles.RemoveAt(i);
78 //Update input for each player
79 for (int i = 0; i < mGame.State.NumberOfPlayers; i++)
81 if(mGame.State.mCharacters[i] != null)
82 mGame.State.mCharacters[i].UpdateInput(timespan, mGame.State.GetKeysDown(i));
84 if (mGame.State.mCharacters[thisPlayer] != null)
86 mGame.State.Map.CenterCell = mGame.State.mCharacters[thisPlayer].Position;
88 //Handle wall collisions of projectiles again...
89 for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
91 if (!mGame.State.Map.IsCellOpen(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y)))
93 mGame.State.mProjectiles.RemoveAt(i);