Example #1
0
 public SpawnPoint(Vector2 spawnPosition, float spawnInterval, float startAfterSeconds, int numEnemies, Enemy e)
 {
     this.spawnInterval = spawnInterval;
     spawned = 0;
     this.startAfterSeconds = startAfterSeconds;
     this.enemiesToSpawn = numEnemies;
     elapsedSinceSpawn = spawnInterval+0.000001f;
     totalElapsed = 0;
     this.e = e;
     this.spawnPosition = spawnPosition;
 }
Example #2
0
        private void LoadAssetsFromXml()
        {
            XmlNode towerDefenceNode = doc["TowerDefence"];
            XmlNode spritesNode = towerDefenceNode["Sprites"];

            Texture2D tmp;
            foreach (XmlNode node in spritesNode.SelectNodes("Sprite"))
            {
                int id = int.Parse(node.Attributes["id"].InnerText);
                bool animated = node.Attributes["animated"] == null ? false : bool.Parse(node.Attributes["animated"].InnerText);
                string filename = node["resourceName"].InnerText;
                tmp = content.Load<Texture2D>(@"Textures/" + filename);
                int width = node["width"] == null ? tmp.Width : int.Parse(node["width"].InnerText);
                int height = node["height"] == null ? tmp.Height : int.Parse(node["height"].InnerText);
                Sprite s;
                if (animated)
                {
                    float spritesPerSecond = float.Parse(node["spritesPerSecond"].InnerText);
                    s = new AnimatedSprite(tmp, width, height, spritesPerSecond);

                }
                else
                {
                    int positionX = node["positionX"] == null ? 0 : int.Parse(node["positionX"].InnerText);
                    int positionY = node["positionY"] == null ? 0 : int.Parse(node["positionY"].InnerText);
                    Vector2 position = new Vector2(positionX, positionY);
                    s = new Sprite(tmp, width, height, position);
                }
                spriteDict.Add(id, s);
            }
            foreach (XmlNode node in towerDefenceNode["Projectiles"].SelectNodes("Projectile"))
            {
                int id = int.Parse(node.Attributes["id"].InnerText);
                int speed = int.Parse(node["speed"].InnerText);
                int spriteid = int.Parse(node["Sprite"].InnerText);
                Sprite s = spriteDict[spriteid];
                Projectile proj = new Projectile(speed, s);
                projectileDict.Add(id, proj);
            }

            foreach (XmlNode node in towerDefenceNode["Towers"].SelectNodes("Tower"))
            {
                int id = int.Parse(node.Attributes["id"].InnerText);
                String name = node["name"].InnerText;
                int spriteid = int.Parse(node["Sprite"].InnerText);
                int projid = int.Parse(node["Projectile"].InnerText);
                int range = int.Parse(node["range"].InnerText);
                int damage = int.Parse(node["damage"].InnerText);
                float shootspeed = float.Parse(node["shootSpeed"].InnerText);
                bool walkable = bool.Parse(node["walkable"].InnerText);
                int cost = int.Parse(node["cost"].InnerText);
                Sprite s = spriteDict[spriteid];
                Projectile proj = projectileDict[projid];
                Tower tow = new Tower(Vector2.Zero, range, shootspeed, walkable, name, 48, s, cost, proj, damage);
                towerDict.Add(id, tow);
            }
            foreach (XmlNode node in towerDefenceNode["Enemies"].SelectNodes("Enemy"))
            {
                int id = int.Parse(node.Attributes["id"].InnerText);
                String name = node["name"].InnerText;
                int spriteid = int.Parse(node["Sprite"].InnerText);
                int health = int.Parse(node["health"].InnerText);
                int speed = int.Parse(node["speed"].InnerText);
                float rotation = float.Parse(node["rotation"].InnerText);
                int drawSize = int.Parse(node["drawSize"].InnerText);
                Sprite s = spriteDict[spriteid];
                Enemy e = new Enemy(health, name, s, drawSize, rotation, speed);
                enemyDict.Add(id, e);
            }

            foreach (XmlNode node in towerDefenceNode.SelectNodes("SpawnPoint"))
            {
                int spawnId = int.Parse(node.Attributes["id"].InnerText);
                float interval = float.Parse(node["interval"].InnerText);
                float delay = float.Parse(node["delay"].InnerText);
                int numToSpawn = int.Parse(node["numToSpawn"].InnerText);
                int enemyId = int.Parse(node["Enemy"].InnerText);
                int posx = int.Parse(node["positionX"].InnerText);
                int posy = int.Parse(node["positionY"].InnerText);
                Vector2 position = new Vector2(posx, posy);
                Enemy s = enemyDict[enemyId];
                SpawnPoint sp = new SpawnPoint(position, interval, delay, numToSpawn, s);
                spawnPointDict.Add(spawnId, sp);
            }

            foreach (XmlNode levelNode in towerDefenceNode.SelectNodes("Level"))
            {
                int id = int.Parse(levelNode.Attributes["id"].InnerText);
                int columns = int.Parse(levelNode["map"]["columns"].InnerText);
                int rows = int.Parse(levelNode["map"]["rows"].InnerText);
                Point end = readPointFromXml(levelNode["end"]);

                List<Wave> waves = new List<Wave>();
                foreach (XmlNode waveNove in levelNode.SelectNodes("Wave"))
                {
                    List<SpawnPoint> spawns = new List<SpawnPoint>();
                    foreach (XmlNode spawnNode in waveNove.SelectNodes("SpawnPoint"))
                    {
                        int spawnId = int.Parse(spawnNode.InnerText);
                        spawns.Add(spawnPointDict[spawnId]);
                    }
                    waves.Add(new Wave(spawns));
                }
                Level lev = new Level(game, 48, rows, columns, end, waves, id);

                foreach (XmlNode towerNode in levelNode.SelectNodes("Tower"))
                {
                    int towid = int.Parse(towerNode.InnerText);
                    lev.towerManager.towerList.Add(towerDict[towid]);
                }

                levelDict.Add(lev);
            }
        }
Example #3
0
        public void DrawHealthBar(SpriteBatch spriteBatch, Enemy enemy, Point position, int width, int height)
        {
            float heightMultiplier = (float)height / game.health.Height;
            float widthMuliplier = (float)44 / game.health.Width;
            //Draw the negative space for the health bar
            spriteBatch.Draw(game.health, new Rectangle(position.X,
            position.Y, (int)(game.health.Width * widthMuliplier), (int)(44 * heightMultiplier)), new Rectangle(0, 45, game.health.Width, 44), Color.Gray);
            //Draw the current health level based on the current Health
            spriteBatch.Draw(game.health, new Rectangle(position.X,
                 position.Y, (int)(game.health.Width * widthMuliplier * ((double)enemy.health / enemy.MaxHealth)), (int)(44 * heightMultiplier)),
                 new Rectangle(0, 45, game.health.Width, 44), Color.Red);

            //Draw the box around the health bar
            spriteBatch.Draw(game.health, new Rectangle(position.X,
                position.Y, (int)(game.health.Width *widthMuliplier), (int)(44 * heightMultiplier)), new Rectangle(0, 0, game.health.Width, 44), Color.White);
        }
Example #4
0
 public void testTowerGetsEnemy()
 {
     Tower t = new Tower("t", 0, 0, 0, 0);
     Enemy e = new Enemy(0, 0.0, "e", 0);
     t.AddNearbyEnemy(e);
 }
Example #5
0
 public void AddNearbyEnemy(Enemy enemy)
 {
     nearbyEnemies.Add(enemy);
 }
Example #6
0
 public object Clone()
 {
     Enemy e = new Enemy(health, name, (Sprite)animated.Clone(), drawSize, 0, speed);
     return e;
 }
Example #7
0
 /// <summary>
 /// Returns distance beetwen enemy and tower
 /// </summary>
 /// <param name="Tower"></param>
 /// <param name="Enemy"></param>
 /// <returns></returns>
 public double Radius(Tower tower, Enemy enemy)
 {
     return Math.Sqrt(Math.Pow(enemy.position.X - tower.position.X, 2) + Math.Pow(enemy.position.Y - tower.position.Y, 2));
 }