/// <summary> /// Called once when a map is unloaded. /// </summary> public override void MapUnloaded() { _quadTree.Clear(); _quadPool.PutObject(_quadTree); _quadTree = null; base.MapUnloaded(); }
public void Update() { Profiler.BeginSample("Cleat quad tree"); UnitList.Clear(); Objects.Clear(); quad.Clear(); Profiler.EndSample(); Profiler.BeginSample("Fill quad tree"); FiilObjectsList(); FillQuadTree(); Profiler.EndSample(); if (FirstUpdate) { Grid.instance.CreateGrid(); FirstUpdate = false; } Profiler.BeginSample("Detection collistion"); BaseCollision(); UnitCollision(); TowerCollision(); Profiler.EndSample(); }
void BuildQuadtree() { Quadtree.Clear(); if (agents.Count > 0) { //GG //Rect bounds = Rect.MinMaxRect(agents[0].position.x, agents[0].position.y, agents[0].position.x, agents[0].position.y); VRect bounds = VRect.MinMaxRect(agents[0].position.x, agents[0].position.y, agents[0].position.x, agents[0].position.y); for (int i = 1; i < agents.Count; i++) { //GG /*Vector2 p = agents[i].position; * bounds = Rect.MinMaxRect(Mathf.Min(bounds.xMin, p.x), Mathf.Min(bounds.yMin, p.y), Mathf.Max(bounds.xMax, p.x), Mathf.Max(bounds.yMax, p.y));*/ VInt2 p = agents[i].position; bounds = VRect.MinMaxRect(Mathf.Min(bounds.xMin, p.x), Mathf.Min(bounds.yMin, p.y), Mathf.Max(bounds.xMax, p.x), Mathf.Max(bounds.yMax, p.y)); } Quadtree.SetBounds(bounds); for (int i = 0; i < agents.Count; i++) { Quadtree.Insert(agents[i]); } //quadtree.DebugDraw (); } Quadtree.CalculateSpeeds(); }
public static void Reset() { for (int i = 0; i < StaticObjects.Length; i++) { StaticObjects[i].Active = false; } StaticObjectTree.Clear(); }
public void MoveTest() { Quadtree <Q> quadtree = new Quadtree <Q>(2, Vector2Int.Zero); Q q = new Q(0, 0); quadtree.Add(q); quadtree.Center = new Vector2Int(1, 1); Assert.AreEqual(new Vector2Int(0, 0), quadtree.GetData(new Vector2Int(1, 1)).Position); quadtree.Clear(); quadtree.Add(q); Assert.AreEqual(new Vector2Int(0, 0), quadtree.GetData(new Vector2Int(0, 0)).Position); }
public static void QuadtreeNonAllocTest() { float topBoundry = float.MinValue; float bottomBoundry = float.MaxValue; float leftBoundry = float.MaxValue; float rightBoundry = float.MinValue; foreach (var s in ShapeSystem.shapes) { if (s.center.x > rightBoundry) { rightBoundry = s.center.x; } if (s.center.x < leftBoundry) { leftBoundry = s.center.x; } if (s.center.y > topBoundry) { topBoundry = s.center.y; } if (s.center.y < bottomBoundry) { bottomBoundry = s.center.y; } } q.Resize( (rightBoundry + leftBoundry) / 2, (topBoundry + bottomBoundry) / 2, rightBoundry - leftBoundry, topBoundry - bottomBoundry); q.Clear(); foreach (var s in ShapeSystem.shapes) { q.Insert(s); } q.ClearEmptySubdivisions(); if (Settings.debugDrawTree) { q.Draw(); } foreach (var s in ShapeSystem.shapes) { q.Search(s); } }
public void Transform(Func <Vector2, Vector2> transform) { //Get all vertices and clear the quadtree var vertices = _vertices.Select(a => a.Value).ToArray(); _vertices.Clear(); //Now modify all the vertices and insert back into quadtree foreach (var vertex in vertices) { vertex.Transform(transform); _vertices.Insert(new BoundingRectangle(vertex.Position, vertex.Position).Inflate(0.1f), vertex); } }
public void Clear() { shapesCounter = 0; for (int i = 0; i < shapes.Length; i++) { shapes[i] = null; } if (subdivided) { tl.Clear(); tr.Clear(); bl.Clear(); br.Clear(); } }
public void Update(GameTime gameTime) { quad.Clear(); for (int i = entities.Count - 1; i >= 0; i--) { quad.Insert(entities[i]); } List <Entity> objects = new List <Entity>(); for (int i = entities.Count - 1; i >= 0; i--) { entities[i].Update(gameTime); if (!entities[i].Hitbox.Intersects(RoomBounds)) { entities[i].Alive = false; } objects = quad.Retreive(objects, entities[i]); for (int j = 0; j < objects.Count; j++) { if (entities[i] != objects[j] && entities[i].Hitbox.Intersects(objects[j].Hitbox)) { entities[i].OnEntityHit(objects[j]); } } if (!entities[i].Alive) { if (typeof(PlayerBase).GetTypeInfo().IsAssignableFrom(entities[i].GetType().GetTypeInfo())) { playerIndex = -1; } entities[i].OnDeath(); entities[i] = null; entities.RemoveAt(i); } } }
private void UseQuadtree(Sprite[] hurtboxSprites, IEnumerable <Sprite> hitboxes) { _quad.Clear(); foreach (var sprite in hurtboxSprites) { _quad.Insert(sprite); } foreach (var sprite in hitboxes) { _quad.Insert(sprite); } foreach (var spriteA in hurtboxSprites) { _returnSprites.Clear(); _quad.Retrieve(_returnSprites, spriteA); foreach (var spriteB in _returnSprites) { if (!(spriteB is IDamageable)) { continue; } if (spriteA == spriteB) { continue; } if (spriteA.Parent is Room && spriteB.Parent is Room) { if (!(spriteA is Enemy && spriteB is Enemy)) // Enemies can collide with each other { continue; } } spriteA.Intersects(spriteB); } foreach (var spriteC in _returnSprites) { if (!(spriteC is Hitbox)) { continue; } if (spriteA == spriteC) { continue; } if (spriteC.Rectangle.Intersects(spriteA.Hurtbox)) { ((IDamageable)spriteA).OnCollide((Hitbox)spriteC); if (spriteA is Hitbox && spriteC is Enemy) { _score++; _bw = new BinaryWriter(new FileStream("Score.bin", FileMode.OpenOrCreate, FileAccess.Write)); _bw.Write(_score); _bw.Close(); } } } spriteA.Position += spriteA.Velocity; } }
private void UseQuadtree(Sprite[] hurtboxSprites, IEnumerable <Sprite> hitboxes) { _quad.Clear(); foreach (var sprite in hurtboxSprites) { _quad.Insert(sprite); } foreach (var sprite in hitboxes) { _quad.Insert(sprite); } foreach (var spriteA in hurtboxSprites) { _returnSprites.Clear(); _quad.Retrieve(_returnSprites, spriteA); foreach (var spriteB in _returnSprites) { if (!(spriteB is IDamageable)) { continue; } if (spriteA == spriteB) { continue; } if (spriteA.Parent is Room && spriteB.Parent is Room) { if (!(spriteA is Enemy && spriteB is Enemy)) // Enemies can collide with each other { continue; } } spriteA.Intersects(spriteB); } foreach (var spriteC in _returnSprites) { if (!(spriteC is Hitbox)) { continue; } if (spriteA == spriteC) { continue; } if (spriteC.Rectangle.Intersects(spriteA.Hurtbox)) { ((IDamageable)spriteA).OnCollide((Hitbox)spriteC); } } spriteA.Position += spriteA.Velocity; } }