private void Split() { int subWidth = (int)(_Bounds.Width / 2); int subHeight = (int)(_Bounds.Height / 2); int x = (int)_Bounds.X; int y = (int)_Bounds.Y; _Nodes[0] = new Quadtree(_Level + 1, new Rectangle(x + subWidth, y, subWidth, subHeight)); _Nodes[1] = new Quadtree(_Level + 1, new Rectangle(x, y, subWidth, subHeight)); _Nodes[2] = new Quadtree(_Level + 1, new Rectangle(x, y + subHeight, subWidth, subHeight)); _Nodes[3] = new Quadtree(_Level + 1, new Rectangle(x + subWidth, y + subHeight, subWidth, subHeight)); }
private Quadtree(int size, int depth, Point center, Quadtree parent) { this.nodeSize = size; this.currentDepth = depth; this.nodeCenter = center; this.nodeParent = parent; //first squre //if (this.currentDepth == 0) //{ // this.nodeBounds = new Rectangle((int)center.X - size, (int)center.Y - size, size * 2, size * 2); //} ////depth >0 means it is divided by previous squre and its size is only 1/4 //else //{ this.nodeBounds = new Rectangle((int)center.X - (size / 2), (int)center.Y - (size / 2), size, size); //} }
protected override void LoadContent() { List <Texture2D> textures = new List <Texture2D>(); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke00")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke01")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke02")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke03")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke04")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke05")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke06")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke07")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke08")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke09")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke10")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke11")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke12")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke13")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke14")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke15")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke16")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke17")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke18")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke19")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke20")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke21")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke22")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke23")); //textures.Add(Content.Load<Texture2D>("Sprites/blackSmoke24")); textures.Add(Content.Load <Texture2D>("Sprites/particle_square")); _Random = new Random(); _SpriteBatch = new SpriteBatch(GraphicsDevice); quad = new Quadtree(0, new Rectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)); quad.CreateTest(); _ParticleEngine = new ParticleEngine(); _ParticleEngine.GenerateNewEmitter(500, textures, new Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2), ParticleEmitter.EmitterTypes.Sequential); _ParticleEngine.GenerateNewEmitter(_Random.Next(0, 500), textures, new Vector2(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 200), ParticleEmitter.EmitterTypes.Sequential); _FrameCounter = new FrameCounter(); }
public override void Update(GameTime gameTime) { CheckToSpawnEnemy(gameTime); //UpdateModels(gameTime); //Quadtree is use for reduce cpu time in relation to //the collisions between bullets and enemy tanks int worldSize = 2400; int maxDepth = 7; int maxNodeObject = 5; Point center = new Point(0, 0); Quadtree quadtree_Enemy = new Quadtree (worldSize,maxDepth,maxNodeObject,center); Quadtree quadtree_obstacles = new Quadtree(worldSize, maxDepth, maxNodeObject, center); //if (pursuitenemy.pathdebug != null) //{ // foreach (Vector3 track in pursuitenemy.pathdebug) // { // pathtrack.Add(new Wallstone(Game.Content.Load<Model>(@"Models/Obstacle/stone"), track)); // } // pursuitenemy.pathdebug = null; // foreach (BasicModel track in pathtrack) // { // models.Add(track); // } // pathtrack.Clear(); //} foreach (BasicModel model in models) { model.Update(gameTime); } currentPosition = tank.CurrentPosition.ToString(); pickPosition = tank.PickPosition.ToString(); foreach (BasicModel model in bullets) { model.Update(gameTime); } foreach (BasicModel model in obstacles) { quadtree_obstacles.Add(model); model.Update(gameTime); } foreach (BasicModel model in enemies) { quadtree_Enemy.Add(model); model.Update(gameTime); } for(int i = 0; i<enemies.Count;i ++) { if (enemies[i].CollidesWith(tank.model, tank.world)) { if (enemies[i] is Human) { enemies.RemoveAt(i); --i; ((Game1)Game).reduceHealth(); ((Game1)Game).DeductPoints(); break; } else { enemies.RemoveAt(i); ((Game1)Game).kill(); --i; ((Game1)Game).reduceHealth(); break; } } } for (int i = 0; i< bullets.Count;i++) { float x = bullets[i].world.Translation.X; float y = bullets[i].world.Translation.Z; Quadtree nearWalls = quadtree_obstacles.GetNodeContaining(x, y); foreach (BasicModel walls in obstacles) { if (bullets[i].CollidesWith(walls.model, walls.world)) { bullets.RemoveAt(i); i--; break; } } } for (int i = 0;i< bullets.Count;i++) { float x= bullets[i].world.Translation.X; float y = bullets[i].world.Translation.Z; //Enemies collides with player (player health -) Quadtree nearEnemies = quadtree_Enemy.GetNodeContaining(x, y); foreach (BasicModel enemy in nearEnemies.models) { if (bullets[i].CollidesWith(enemy.model, enemy.world)) { bullets.RemoveAt(i); ((Game1)Game).soundHit.Play(); if (enemy is Human) { ((Game1)Game).DeductPoints(); ((Game1)Game).reduceHealth(); } else { ((Game1)Game).AddPoints(); } enemies.Remove(enemy); --i; break; } } } Quadtree nearObstacles = quadtree_obstacles.GetNodeContaining(tank.translation.Translation.X, tank.translation.Translation.Z); foreach (BasicModel model in nearObstacles.models ) { if (model.CollidesWith(tank.model,tank.world)) { tank.CurrentPosition = tank.CurrentPosition - tank.velocity*(gameTime.ElapsedGameTime.Milliseconds) /100; tank.velocity = Vector3.Zero; //tank.tankDirection = -tank.tankDirection; //tank.PickPosition = tank.CurrentPosition; } } updateShots(gameTime); base.Update(gameTime); }