예제 #1
0
        public void InstantiatePool(ClassType type, Game game, int poolSize, string poolTag)
        //where T : DrawableSprite, new()
        {
            //I have know idea if this is how this would work.
            //Queue<DrawableSprite> pool = new Queue<DrawableSprite>();
            //for (int i = 0; i < poolSize; i++)
            //{
            //    var instance = Activator.CreateInstance(typeof(T), new DrawableSprite[] { game, null });
            //    pool.Enqueue(instance);
            //}
            try
            {
                if (poolDictionary[poolTag] == null)
                {
                    poolDictionary.Remove(poolTag);
                }
            }
            catch (KeyNotFoundException k)
            {
                Queue <DrawableSprite> queue = new Queue <DrawableSprite>();

                for (int i = 0; i < poolSize; i++)
                {
                    switch (type)
                    {
                    case ClassType.Enemy:
                        Enemies.BasicEnemy e = new Enemies.BasicEnemy(game);
                        e.Initialize();
                        e.Enabled = false;
                        queue.Enqueue(e);
                        break;

                    case ClassType.Shot:
                        Shot s = new Shot(game);
                        s.Initialize();
                        s.Enabled = false;
                        queue.Enqueue(s);
                        break;

                    case ClassType.Pickup:
                        Pickups.PickUp p = new Pickups.PickUp(game);
                        p.Initialize();
                        p.Enabled = false;
                        queue.Enqueue(p);
                        break;

                    default:
                        break;
                    }
                }

                Pool pool = new Pool(game, queue);

                poolDictionary.Add(poolTag, pool);
            }
        }
        //checks to see if the enemy collides with various things
        private void CheckCollision(GameTime gameTime)
        {
            foreach (Enemy enemy in poolManager.PoolDictionary[enemyPoolTag].objectPool)
            {
                if (enemy.Enabled)
                {
                    //enemy and bullet collision
                    foreach (Shot shot in poolManager.PoolDictionary["Shots"].objectPool)
                    {
                        if (shot.Enabled)
                        {
                            if (enemy.Intersects(shot))
                            {
                                if (enemy.PerPixelCollision(shot))
                                {
                                    //enemy takes damage and destroys the shot
                                    enemy.TakeDamage(shot.damage);
                                    shot.Dies(gameTime);

                                    if (enemy.Health < 0)
                                    {
                                        Pickups.PickUp p = (Pickups.PickUp)poolManager.PoolDictionary["PickUps"].SpawnFromPool(enemy.Location, new Vector2(0, 0));
                                        p.type = (Pickups.PickUp.PickUpType)r.Next(0, Enum.GetNames(typeof(Pickups.PickUp.PickUpType)).Length);
                                        enemy.Dies(gameTime);
                                        scoreManager.score += 10;
                                    }

                                    numOfEnemiesKilled++;
                                }
                            }
                        }
                    }

                    //enemy and player collision
                    if (enemy.Intersects(player))
                    {
                        if (enemy.PerPixelCollision(player))
                        {
                            player.TakeDamage(enemy.damage);
                            enemy.Dies(gameTime);
                            numOfEnemiesKilled++;
                        }
                    }


                    //enemy and enemy collision
                    foreach (Enemy other in poolManager.PoolDictionary[enemyPoolTag].objectPool)
                    {
                        if (other.Enabled && other.type != "Ranged" || other.type == enemy.type)
                        {
                            //code barrowed form my old sim and serious homeworks. makes the enemies bounce off of one another
                            var dist  = Vector2.Distance(enemy.Location, other.Location);
                            var sum   = new Vector2();
                            int count = 0;
                            //if (enemy.Intersects(other))
                            if (dist > 0 && dist < other.spriteTexture.Width)
                            {
                                var diff = Vector2.Subtract(enemy.Location, other.Location);
                                diff = Vector2.Divide(diff, dist);
                                sum  = Vector2.Add(sum, diff);
                                count++;
                            }
                            if (count > 0)
                            {
                                sum = Vector2.Divide(sum, count);
                                sum.Normalize();
                                sum *= (enemy.Speed * gameTime.ElapsedGameTime.Milliseconds / 1000);
                                var steer = Vector2.Subtract(sum, enemy.velocity);

                                //freezes enemy in place but its better looking than the vibrations i get now
                                //enemy.Location = enemy.lastLocation;

                                enemy.AddForce(steer);
                            }
                        }
                    }
                }
            }
        }