]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/GameLogic.cs
ee5535a433e3274da0484ee5af51404459c3d25e
[chaz/carfire] / CarFire / CarFire / CarFire / GameLogic.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
9
10 namespace CarFire
11 {
12 class GameLogic
13 {
14
15 Game mGame;
16 public GameLogic(Game game)
17 {
18 mGame = game;
19 }
20 /// <summary>
21 /// Update the game logic.
22 /// </summary>
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)
26 {
27 //Handle projectiles - update and check for wall collisions
28 for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
29 {
30 bool removed = false;
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)))
33 {
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)
38 {
39 IMonster hitMonster = (IMonster)hitEntity;
40 hitMonster.causeDamageTo(mGame.State.mProjectiles[i].Damage);
41 if (hitMonster.Health > 0)
42 {
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);
45 }
46 else
47 {
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);
52 }
53 }
54 mGame.State.mProjectiles.RemoveAt(i);
55 removed = true;
56 i--;
57 }
58 if (!removed)
59 mGame.State.mProjectiles[i].Update(timespan);
60
61 }
62 //Check for collisons
63 for (int j = 0; j < mGame.State.mCharacters.Length; j++)
64 {
65
66 if (mGame.State.mCharacters[j] != null)
67 for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
68 {
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)
70 {
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);
74 i--;
75 }
76 }
77 }
78 //Update input for each player
79 for (int i = 0; i < mGame.State.NumberOfPlayers; i++)
80 {
81 if(mGame.State.mCharacters[i] != null)
82 mGame.State.mCharacters[i].Update(timespan, mGame.State.GetKeysDown(i));
83 }
84 if (mGame.State.mCharacters[thisPlayer] != null)
85 {
86 mGame.State.Map.CenterCell = mGame.State.mCharacters[thisPlayer].Position;
87 }
88 //Handle wall collisions of projectiles again...
89 for (int i = 0; i < mGame.State.mProjectiles.Count; i++)
90 {
91 if (!mGame.State.Map.IsCellOpen(new Point(mGame.State.mProjectiles[i].Coordinates.X, mGame.State.mProjectiles[i].Coordinates.Y)))
92 {
93 mGame.State.mProjectiles.RemoveAt(i);
94 i--;
95 }
96
97 }
98 }
99 }
100 }
This page took 0.035873 seconds and 3 git commands to generate.