Commits

John McDonald committed 245f724

Adding a power grid rendering system, and a quad tree rendering system.

Comments (0)

Files changed (11)

Asteroid Outpost.csproj

     <Compile Include="Systems\AccumulationSystem.cs" />
     <Compile Include="Systems\AnimationSystem.cs" />
     <Compile Include="Systems\PhysicsSystem.cs" />
+    <Compile Include="Systems\RenderPowerGridSystem.cs" />
+    <Compile Include="Systems\RenderQuadTreeSystem.cs" />
     <Compile Include="Systems\RenderSystem.cs" />
     <Compile Include="TextureDictionary.cs" />
     <Compile Include="Screens\HeadsUpDisplay\Radar.cs" />

Components/PowerLinker.cs

 
 		public override void Draw(SpriteBatch spriteBatch, float scaleModifier, Color tint)
 		{
-			foreach (var powerLink in world.PowerGrid(owningForce).GetAllPowerLinks(relatedPowerNode))
+			foreach (var powerLink in world.PowerGrid[owningForce.ID].GetAllPowerLinks(relatedPowerNode))
 			{
 				Color linkColor;
-				if (world.PowerGrid(owningForce).IsPowerRoutableBetween(relatedPowerNode, powerLink.Value))
+				if (world.PowerGrid[owningForce.ID].IsPowerRoutableBetween(relatedPowerNode, powerLink.Value))
 				{
 					linkColor = Color.Yellow;
 				}

Entities/Structures/ConstructableEntity.cs

 			base.PostDeserializeLink(world);
 
 			// Hook me into the grid
-			world.PowerGrid(owningForce).ConnectToPowerGrid(this);
+			world.PowerGrid[owningForce.ID].ConnectToPowerGrid(this);
 		}
 
 
 				int delta;
 
 				// Check that we have enough power in the grid
-				if(world.PowerGrid(owningForce).HasPower(this, powerToUse))
+				if(world.PowerGrid[owningForce.ID].HasPower(this, powerToUse))
 				{
 					// Check to see if the mineralsLeftToConstruct would pass an integer boundary
 					delta = (int)Math.Ceiling(mineralsLeftToConstruct) - (int)Math.Ceiling(mineralsLeftToConstruct - mineralsToUse);
 						if (owningForce.GetMinerals() >= delta)
 						{
 							// Consume the resources
-							world.PowerGrid(owningForce).GetPower(this, powerToUse);
+							world.PowerGrid[owningForce.ID].GetPower(this, powerToUse);
 							SetMineralsLeftToConstruct(mineralsLeftToConstruct - mineralsToUse);
 
 							// Set the force's minerals
 						mineralsLeftToConstruct -= mineralsToUse;
 
 						// We should consume our little tidbit of power though:
-						world.PowerGrid(owningForce).GetPower(this, powerToUse);
+						world.PowerGrid[owningForce.ID].GetPower(this, powerToUse);
 					}
 				}
 			}
 				float mineralsToUse = mineralUsageRate * (float)deltaTime.TotalSeconds;
 
 				// BUG: There is a disconnect between the check for minerals (below) and the actual consumption of minerals. Could cause weird behaviour
-				if (owningForce.GetMinerals() > mineralsToUse && world.PowerGrid(owningForce).GetPower(this, powerToUse))
+				if (owningForce.GetMinerals() > mineralsToUse && world.PowerGrid[owningForce.ID].GetPower(this, powerToUse))
 				{
 					// Use some minerals toward my upgrade
 					int temp = (int)mineralsLeftToConstruct;
 			IsUpgrading = false;
 
 			// Hook me into the grid
-			world.PowerGrid(owningForce).ConnectToPowerGrid(this);
+			world.PowerGrid[owningForce.ID].ConnectToPowerGrid(this);
 		}
 	}
 }

Entities/Structures/LaserMiner.cs

 
 				if (currentAsteroid != null)
 				{
-					if (world.PowerGrid(owningForce).GetPower(this, powerToUse))
+					if (world.PowerGrid[owningForce.ID].GetPower(this, powerToUse))
 					{
 						if (currentAsteroid.GetMinerals() < mineralsToExtract)
 						{

Entities/Structures/LaserTower.cs

 			{
 				// TODO:  Change this to server-side code
 				float powerToUse = (float)(5.0 * deltaTime.TotalSeconds);
-				if (world.PowerGrid(owningForce).GetPower(this, powerToUse))
+				if (world.PowerGrid[owningForce.ID].GetPower(this, powerToUse))
 				{
 					ScanForTarget();
 					if (target != null)
 		private World world;
 
 		public const int PowerConductingDistance = 220;
-		private readonly Dictionary<IPowerGridNode, List<IPowerGridNode>> powerNodes = new Dictionary<IPowerGridNode, List<IPowerGridNode>>(32);
+		internal readonly Dictionary<IPowerGridNode, List<IPowerGridNode>> powerNodes = new Dictionary<IPowerGridNode, List<IPowerGridNode>>(32);
 
-		private List<Tuple<IPowerGridNode, IPowerGridNode>> recentlyActiveLinks = new List<Tuple<IPowerGridNode, IPowerGridNode>>();
+		internal List<Tuple<IPowerGridNode, IPowerGridNode>> recentlyActiveLinks = new List<Tuple<IPowerGridNode, IPowerGridNode>>();
 
 
 		public PowerGrid(World world)
 
 			return path;
 		}
-
-
-		
-
-		public void Draw(SpriteBatch spriteBatch)
-		{
-			Color color;
-
-			// TODO: This allocates a bunch of memory each draw, fix this!
-			List<Tuple<IPowerGridNode, IPowerGridNode>> linksAlreadyDrawn = new List<Tuple<IPowerGridNode, IPowerGridNode>>(powerNodes.Count * 6);
-
-			// Draw all of the active links first
-			foreach (var linkToDraw in recentlyActiveLinks)
-			{
-				if (!linksAlreadyDrawn.Contains(linkToDraw))
-				{
-					color = new Color((int)(150 + world.Scale(50)), (int)(150 + world.Scale(50)), 0, (int)(150 + world.Scale(50)));
-
-					spriteBatch.DrawLine(world.WorldToScreen(linkToDraw.Item1.PowerLinkPointAbsolute),
-					                         world.WorldToScreen(linkToDraw.Item2.PowerLinkPointAbsolute),
-					                         color);
-
-					linksAlreadyDrawn.Add(linkToDraw);
-					linksAlreadyDrawn.Add(new Tuple<IPowerGridNode, IPowerGridNode>(linkToDraw.Item2, linkToDraw.Item1));
-				}
-			}
-
-			// Wipe the recent list
-			recentlyActiveLinks.Clear();
-
-
-			foreach (var nodeA in powerNodes.Keys)
-			{
-				foreach (var nodeB in powerNodes[nodeA])
-				{
-					var linkToDraw = new Tuple<IPowerGridNode, IPowerGridNode>(nodeA, nodeB);
-					if (!linksAlreadyDrawn.Contains(linkToDraw))
-					{
-						if (nodeA.PowerStateActive && nodeB.PowerStateActive)
-						{
-							color = new Color((int)(70 + world.Scale(50)), (int)(70 + world.Scale(50)), 0, (int)(70 + world.Scale(50)));
-						}
-						else
-						{
-							color = new Color((int)(80 + world.Scale(50)), (int)(0 + world.Scale(50)), 0, (int)(0 + world.Scale(50)));
-						}
-
-						spriteBatch.DrawLine(world.WorldToScreen(nodeA.PowerLinkPointAbsolute),
-						                     world.WorldToScreen(nodeB.PowerLinkPointAbsolute),
-						                     color);
-
-						linksAlreadyDrawn.Add(linkToDraw);
-						linksAlreadyDrawn.Add(new Tuple<IPowerGridNode, IPowerGridNode>(nodeB, nodeA));
-					}
-				}
-			}
-		}
 	}
 
 }

Scenarios/RandomScenario.cs

 			{
 				Force force = new Force(world, world.GetNextForceID(), initialMinerals, (Team)iPlayer);
 				world.AddForce(force);
-				world.CreatePowerGrid(force);
+				world.PowerGrid.Add(force.ID, new PowerGrid(world));
 				Vector2 focusPoint = CreateStartingBase(force);
 
 

Scenarios/TutorialScenario.cs

 			Controller aiController = new AIController(world, world, aiForce);
 			world.AddForce(aiForce);
 			world.AddController(aiController);
-			
 
-			world.CreatePowerGrid(localForce);
+			world.PowerGrid.Add(localForce.ID, new PowerGrid(world));
 
 
 			world.HUD.FocusWorldPoint = new Vector2(world.MapWidth / 2f, world.MapHeight / 2f);
 		private Scenario scenario;
 		private PhysicsSystem physicsSystem;
 		private RenderSystem renderSystem;
+		private RenderQuadTreeSystem renderQuadTreeSystem;
+		private RenderPowerGridSystem renderPowerGridSystem;
 
 		private bool paused;
-		private bool drawQuadTree = false;
 
 		private bool isServer = true;
 		private AONetwork network;
 			hud = new AOHUD(game, this);
 			physicsSystem = new PhysicsSystem(game, this);
 			renderSystem = new RenderSystem(game, this);
+			renderQuadTreeSystem = new RenderQuadTreeSystem(game, this);
+			renderPowerGridSystem = new RenderPowerGridSystem(game, this);
 			awesomium = game.Awesomium;
 
 			// TODO: Create this on the server, then send the size to the clients
 			game.Components.Add(physicsSystem);
 			game.Components.Add(hud);
 			game.Components.Add(renderSystem);
+			game.Components.Add(renderQuadTreeSystem);
+			game.Components.Add(renderPowerGridSystem);
 		}
 
 
 			}
 		}
 
+
+		// TODO: Not sure I should have this helper methos here to pass a message to a system. Think about removing this
 		public bool DrawQuadTree
 		{
 			get
 			{
-				return drawQuadTree;
+				return renderQuadTreeSystem.DrawQuadTree;
 			}
 			set
 			{
-				drawQuadTree = value;
+				renderQuadTreeSystem.DrawQuadTree = value;
 			}
 		}
 
 
-		internal PowerGrid PowerGrid(Force force)
+		internal Dictionary<int, PowerGrid> PowerGrid
 		{
-			return powerGrid[force.ID];
+			get
+			{
+				return powerGrid;
+			}
 		}
 
-		internal PowerGrid PowerGrid(int forceID)
-		{
-			return powerGrid[forceID];
-		}
+		//internal PowerGrid PowerGrid(int forceID)
+		//{
+		//    return powerGrid[forceID];
+		//}
 
 
-		public void CreatePowerGrid(Force force)
-		{
-			powerGrid.Add(force.ID, new PowerGrid(this));
-			/*
-			if(isServer)
-			{
-				network.EnqueueMessage(new AOReflectiveOutgoingMessage(ID,
-				                                                       "CreatePowerGrid",
-				                                                       new object[]{ force }));
-			}
-			*/
-		}
+		//public void CreatePowerGrid(Force force)
+		//{
+		//    powerGrid.Add(force.ID, new PowerGrid(this));
+		//}
 
 
 		/// <summary>
 		public override void Draw(GameTime gameTime)
 		{
 			spriteBatch.Begin();
-			if(drawQuadTree)
-			{
-				DrawQuad(spriteBatch, quadTree, 0);
-			}
 
 			// Draw the back of the HUD
 			hud.DrawBack(spriteBatch, Color.White);
 
-			//// Draw all the visible entities
-			//List<Entity> visible = quadTree.GetObjects(hud.FocusScreen);
-			//foreach (Entity entity in visible)
+			//foreach (var grid in powerGrid.Values)
 			//{
-			//    entity.Draw(spriteBatch, 1, Color.White);
+			//    grid.Draw(spriteBatch);
 			//}
-
-			foreach (var grid in powerGrid.Values)
-			{
-				grid.Draw(spriteBatch);
-			}
 			
 			spriteBatch.End();
 
 		}
 
 
-		#region Draw QuadTree
-
-		private void DrawQuad(SpriteBatch spriteBatch, QuadTree<Entity> quad, int depth)
-		{
-			if (quad != null)
-			{
-				DrawQuad(spriteBatch, quad.RootQuad, depth);
-			}
-		}
-
-		private void DrawQuad(SpriteBatch spriteBatch, QuadTreeNode<Entity> quad, int depth)
-		{
-			if (quad != null)
-			{
-
-				Rectangle rect = quad.QuadRect;
-
-				Color drawColor;
-				switch (depth)
-				{
-				default:
-					goto case 0;
-				case 0:
-					drawColor = Color.White;
-					break;
-				case 1:
-					drawColor = Color.Red;
-					break;
-				case 2:
-					drawColor = Color.Green;
-					break;
-				case 3:
-					drawColor = Color.Blue;
-					break;
-				case 4:
-					drawColor = Color.Gray;
-					break;
-				case 5:
-					drawColor = Color.DarkRed;
-					break;
-				case 6:
-					drawColor = Color.DarkGreen;
-					break;
-				case 7:
-					drawColor = Color.DarkBlue;
-					break;
-				}
-
-				Vector2 screenTopLeft = WorldToScreen(rect.X, rect.Y);
-				Vector2 screenBottomLeft = WorldToScreen(rect.Right, rect.Bottom);
-
-				rect = new Rectangle((int)screenTopLeft.X,
-				                     (int)screenTopLeft.Y,
-				                     (int)(screenBottomLeft.X - screenTopLeft.X),
-				                     (int)(screenBottomLeft.Y - screenTopLeft.Y));
-				spriteBatch.DrawRectangle(rect, drawColor, 1);
-
-				DrawQuad(spriteBatch, quad.TopLeftChild, depth + 1);
-				DrawQuad(spriteBatch, quad.TopRightChild, depth + 1);
-				DrawQuad(spriteBatch, quad.BottomLeftChild, depth + 1);
-				DrawQuad(spriteBatch, quad.BottomRightChild, depth + 1);
-			}
-		}
-		#endregion
-
-
 
 		public void SetScene(LayeredStarField starField)
 		{
 			for (int iGrid = 0; iGrid < powerGridCount; iGrid++)
 			{
 				int owningForceID = br.ReadInt32();
-				CreatePowerGrid(GetForce(owningForceID));
+				PowerGrid.Add(owningForceID, new PowerGrid(this));
 			}
 
 

Systems/RenderPowerGridSystem.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using AsteroidOutpost.Interfaces;
+using AsteroidOutpost.Screens;
+using C3.XNA;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace AsteroidOutpost.Systems
+{
+	class RenderPowerGridSystem : DrawableGameComponent
+	{
+		private World world;
+		private SpriteBatch spriteBatch;
+
+
+		public RenderPowerGridSystem(AOGame game, World world)
+			: base(game)
+		{
+			this.world = world;
+			spriteBatch = new SpriteBatch(game.GraphicsDevice);
+		}
+
+		public override void Draw(GameTime gameTime)
+		{
+			spriteBatch.Begin();
+			foreach (var grid in world.PowerGrid.Values)
+			{
+				Color color;
+
+				// TODO: This allocates a bunch of memory each draw, fix this!
+				List<Tuple<IPowerGridNode, IPowerGridNode>> linksAlreadyDrawn = new List<Tuple<IPowerGridNode, IPowerGridNode>>(grid.powerNodes.Count * 6);
+
+				// Draw all of the active links first
+				foreach (var linkToDraw in grid.recentlyActiveLinks)
+				{
+					if (!linksAlreadyDrawn.Contains(linkToDraw))
+					{
+						color = new Color((int)(150 + world.Scale(50)), (int)(150 + world.Scale(50)), 0, (int)(150 + world.Scale(50)));
+
+						spriteBatch.DrawLine(world.WorldToScreen(linkToDraw.Item1.PowerLinkPointAbsolute),
+						                     world.WorldToScreen(linkToDraw.Item2.PowerLinkPointAbsolute),
+						                     color);
+
+						linksAlreadyDrawn.Add(linkToDraw);
+						linksAlreadyDrawn.Add(new Tuple<IPowerGridNode, IPowerGridNode>(linkToDraw.Item2, linkToDraw.Item1));
+					}
+				}
+
+				// Wipe the recent list
+				grid.recentlyActiveLinks.Clear();
+
+
+				foreach (var nodeA in grid.powerNodes.Keys)
+				{
+					foreach (var nodeB in grid.powerNodes[nodeA])
+					{
+						var linkToDraw = new Tuple<IPowerGridNode, IPowerGridNode>(nodeA, nodeB);
+						if (!linksAlreadyDrawn.Contains(linkToDraw))
+						{
+							if (nodeA.PowerStateActive && nodeB.PowerStateActive)
+							{
+								color = new Color((int)(70 + world.Scale(50)), (int)(70 + world.Scale(50)), 0, (int)(70 + world.Scale(50)));
+							}
+							else
+							{
+								color = new Color((int)(80 + world.Scale(50)), (int)(0 + world.Scale(50)), 0, (int)(0 + world.Scale(50)));
+							}
+
+							spriteBatch.DrawLine(world.WorldToScreen(nodeA.PowerLinkPointAbsolute),
+							                     world.WorldToScreen(nodeB.PowerLinkPointAbsolute),
+							                     color);
+
+							linksAlreadyDrawn.Add(linkToDraw);
+							linksAlreadyDrawn.Add(new Tuple<IPowerGridNode, IPowerGridNode>(nodeB, nodeA));
+						}
+					}
+				}
+			}
+
+			spriteBatch.End();
+			base.Draw(gameTime);
+		}
+	}
+}

Systems/RenderQuadTreeSystem.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using AsteroidOutpost.Entities;
+using AsteroidOutpost.Screens;
+using C3.XNA;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace AsteroidOutpost.Systems
+{
+	class RenderQuadTreeSystem : DrawableGameComponent
+	{
+		private World world;
+		private SpriteBatch spriteBatch;
+
+		private Color[] colorPalette = {
+		                               	Color.White,
+		                               	Color.Red,
+		                               	Color.Green,
+		                               	Color.Blue,
+		                               	Color.Gray,
+		                               	Color.DarkRed,
+		                               	Color.DarkGreen,
+		                               	Color.DarkBlue
+		                               };
+
+		protected internal bool DrawQuadTree { get; set; }
+
+		public RenderQuadTreeSystem(AOGame game, World world)
+			: base(game)
+		{
+			this.world = world;
+			spriteBatch = new SpriteBatch(game.GraphicsDevice);
+		}
+
+		public override void Draw(GameTime gameTime)
+		{
+			spriteBatch.Begin();
+			if(DrawQuadTree && world.QuadTree != null)
+			{
+				DrawQuad(spriteBatch, world.QuadTree.RootQuad, 0);
+			}
+			spriteBatch.End();
+			base.Draw(gameTime);
+		}
+
+		private void DrawQuad(SpriteBatch spriteBatch, QuadTreeNode<Entity> quad, int depth)
+		{
+			if (quad != null)
+			{
+
+				Rectangle rect = quad.QuadRect;
+
+				Color drawColor = colorPalette[depth % colorPalette.Length];
+
+				Vector2 screenTopLeft = world.WorldToScreen(rect.X, rect.Y);
+				Vector2 screenBottomLeft = world.WorldToScreen(rect.Right, rect.Bottom);
+
+				rect = new Rectangle((int)screenTopLeft.X,
+				                     (int)screenTopLeft.Y,
+				                     (int)(screenBottomLeft.X - screenTopLeft.X),
+				                     (int)(screenBottomLeft.Y - screenTopLeft.Y));
+				spriteBatch.DrawRectangle(rect, drawColor, 1);
+
+				DrawQuad(spriteBatch, quad.TopLeftChild, depth + 1);
+				DrawQuad(spriteBatch, quad.TopRightChild, depth + 1);
+				DrawQuad(spriteBatch, quad.BottomLeftChild, depth + 1);
+				DrawQuad(spriteBatch, quad.BottomRightChild, depth + 1);
+			}
+		}
+	}
+}