aboutsummaryrefslogtreecommitdiff
path: root/V3/Quadtree.cs
diff options
context:
space:
mode:
authorThomas Leyh <leyh.thomas@web.de>2016-07-24 08:14:18 +0200
committerThomas Leyh <leyh.thomas@web.de>2016-07-24 08:14:18 +0200
commitced3d03bdb3ce866d832e03fb212865140905a9a (patch)
tree2a16c2063a46d3c354ce1585029dda3124f6ad93 /V3/Quadtree.cs
parent0394dccaf06e1009e591a6ff4d645895574724c1 (diff)
downloadV3-release.tar.gz
V3-release.tar.bz2
Add project files.v1.0release
Diffstat (limited to 'V3/Quadtree.cs')
-rw-r--r--V3/Quadtree.cs87
1 files changed, 87 insertions, 0 deletions
diff --git a/V3/Quadtree.cs b/V3/Quadtree.cs
new file mode 100644
index 0000000..dfc088f
--- /dev/null
+++ b/V3/Quadtree.cs
@@ -0,0 +1,87 @@
+using System.Collections.Generic;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.Graphics;
+using V3.Objects;
+
+namespace V3
+{
+ public sealed class Quadtree
+ {
+ private Node mRoot; // The first "Node" of the Quadtree
+ private ContentManager mContentManager;
+ private readonly Point mMaxSize;
+
+ /// <summary>
+ /// Generates Quadtree
+ /// </summary>
+ /// <param name="maxSize">Gives the biggest/first Rectangle of the Quadtree. This should be the sice of the howl map.</param>
+ public Quadtree(Point maxSize)
+ {
+ mRoot = new Node(new Rectangle(new Point(-128, -128), maxSize), null);
+ mMaxSize = maxSize;
+ }
+
+ /// <summary>
+ /// Updates the Quadtree. This is importent for the movements of the Objects.
+ /// </summary>
+ public void Update()
+ {
+ mRoot.Update1();
+ }
+
+ /// <summary>
+ /// You call this method if you want to Inster an Object to the Quadtree.
+ /// </summary>
+ /// <param name="item">Type of Creature including their position.</param>
+ public void Insert(IGameObject item)
+ {
+ mRoot.AddtoSubNode(item);
+ }
+
+ /// <summary>
+ /// DMakes the Rectangles of the Quadtree visible.
+ /// </summary>
+ /// <param name="spriteBatch"></param>
+ public void Draw(SpriteBatch spriteBatch)
+ {
+ mRoot.DrawQuadtree(spriteBatch, Texture);
+ }
+
+ public List<IGameObject> GetObjectsInRectangle(Rectangle rectangle)
+ {
+ List<IGameObject> objectList = new List<IGameObject>();
+ return mRoot.GetObjectsInRectangle(rectangle, objectList);
+ }
+
+ /// <summary>
+ /// Deletes an Object out of the Quadtree.
+ /// </summary>
+ /// <param name="item">Type of Creature including their position.</param>
+ public void RemoveItem(IGameObject item)
+ {
+ mRoot.Delete(item);
+ }
+
+ /// <summary>
+ /// Loads the content to draw the Rectangels of the Quadtree.
+ /// </summary>
+ /// <param name="contentManager"></param>
+ public void LoadContent(ContentManager contentManager)
+ {
+ mContentManager = contentManager;
+ Texture = mContentManager.Load<Texture2D>("Sprites/WhiteRectangle");
+ }
+
+ /// <summary>
+ /// Deletes all elements from the quadtree.
+ /// </summary>
+ public void Clear()
+ {
+ mRoot?.Clear();
+ mRoot = new Node(new Rectangle(new Point(-50, -50), mMaxSize), null);
+ }
+
+ private Texture2D Texture { get; set; }
+ }
+}