Example #1
0
        public void update(GameTime elapsedTime, List <Tower> towers, List <Trap> traps, HomeBase homeTarget, List <Enemy> enemies, int levelCount)
        {
            timeAttack += elapsedTime.ElapsedGameTime.Milliseconds;

            float     currx       = location.X;
            bool      blocked     = false;
            Boolean   isAttacking = false;
            EnemyType value       = enemyType;

            int multiplier = 1;

            if (levelCount >= 1)
            {
                multiplier = 2;
            }

            if (!stunned)
            {
                if (enemyType == EnemyType.Grunt)
                {
                    if (goal == null)
                    {
                        Random rand = new Random(0);
                        int    x    = rand.Next(229, 229 + 185);
                        int    y    = 403;
                        goal = new Vector2(x, y);
                    }

                    if (timeAttack > 3000f)
                    {
                        damage     = 3;
                        timeAttack = 0;
                    }
                    else
                    {
                        damage = 0;
                    }
                    Vector2 current       = new Vector2(location.X, location.Y - 1);
                    Vector2 northNeighbor = new Vector2(location.X, location.Y - 1);
                    Vector2 southNeighbor = new Vector2(location.X, location.Y + 1);
                    Vector2 eastNeighbor  = new Vector2(location.X + 1, location.Y);
                    Vector2 westNeighbor  = new Vector2(location.X - 1, location.Y);

                    Vector2 relative = goal - current;

                    foreach (Tower t in towers)
                    {
                        Rectangle targRectangle = new Rectangle((int)t.location.X, (int)t.location.Y, 64, 64);
                        if (bounds.Intersects(targRectangle) && location.X < 320)
                        {
                            location.X -= gruntMovementSpeed * multiplier;
                            blocked     = true;
                        }
                        else if (bounds.Intersects(targRectangle) && location.X >= 320)
                        {
                            location.X += gruntMovementSpeed * multiplier;
                            blocked     = true;
                        }
                    }
                    if (bounds.Intersects(homeTarget.bounds))
                    {
                        isAttacking = true;
                        homeTarget.collide(this);
                    }


                    if (!blocked && !isAttacking)
                    {
                        if (relative.X > 0)
                        {
                            facingRight = true;
                            location.X += gruntMovementSpeed * multiplier;
                        }
                        if (relative.X < 0)
                        {
                            facingLeft  = true;
                            location.X -= gruntMovementSpeed * multiplier;
                        }
                        if (relative.Y > 0)
                        {
                            facingDown  = true;
                            location.Y += gruntMovementSpeed * multiplier;
                        }
                        if (relative.Y < 0)
                        {
                            facingUp    = true;
                            location.Y -= gruntMovementSpeed * multiplier;
                        }
                    }
                }
                target = null;

                if (value == EnemyType.Tank)
                {
                    if (timeAttack > 300f)
                    {
                        damage     = 15;
                        timeAttack = 0;
                    }
                    else
                    {
                        damage = 0;
                    }
                    if (!(findClosestTower(towers) == null))
                    {
                        target = findClosestTower(towers);
                        Rectangle targetRectangle = new Rectangle((int)target.location.X, (int)target.location.Y, 64, 64);
                        goal = new Vector2(target.location.X, target.location.Y);

                        if (bounds.Intersects(targetRectangle))
                        {
                            target.collide(this);
                            isAttacking = true;
                        }
                    }
                    else
                    {
                        Random rand = new Random();
                        //Rectangle targetRectangle = new Rectangle((int)target.location.X, (int)target.location.Y, 64, 64);
                        int x = rand.Next(229, 229 + 185);
                        int y = 403;
                        goal = new Vector2(x, y);
                        if (timeAttack > 1000f)
                        {
                            damage = 5;
                        }

                        if (bounds.Intersects(homeTarget.bounds))
                        {
                            homeTarget.collide(this);
                            isAttacking = true;
                        }
                    }


                    Vector2 current       = new Vector2(location.X, location.Y - 1);
                    Vector2 northNeighbor = new Vector2(location.X, location.Y - 1);
                    Vector2 southNeighbor = new Vector2(location.X, location.Y + 1);
                    Vector2 eastNeighbor  = new Vector2(location.X + 1, location.Y);
                    Vector2 westNeighbor  = new Vector2(location.X - 1, location.Y);

                    Vector2 relative = goal - current;
                    if (!isAttacking)
                    {
                        if (relative.X > 0)
                        {
                            facingRight = true;
                            location.X += tankMovementSpeed * multiplier;
                        }
                        if (relative.X < 0)
                        {
                            facingLeft  = true;
                            location.X -= tankMovementSpeed * multiplier;
                        }
                        if (relative.Y > 0)
                        {
                            facingDown  = true;
                            location.Y += tankMovementSpeed * multiplier;
                        }
                        if (relative.Y < 0)
                        {
                            facingUp    = true;
                            location.Y -= tankMovementSpeed * multiplier;
                        }
                    }
                }

                if (value == EnemyType.Assassin)
                {
                    Vector2 current       = new Vector2(location.X, location.Y - 1);
                    Vector2 northNeighbor = new Vector2(location.X, location.Y - 1);
                    Vector2 southNeighbor = new Vector2(location.X, location.Y + 1);
                    Vector2 eastNeighbor  = new Vector2(location.X + 1, location.Y);
                    Vector2 westNeighbor  = new Vector2(location.X - 1, location.Y);

                    Vector2 relative = goal - current;

                    if (timeAttack > 3000f)
                    {
                        damage     = 3;
                        timeAttack = 0;
                    }
                    else
                    {
                        damage = 0;
                    }

                    foreach (Tower t in towers)
                    {
                        Rectangle targRectangle = new Rectangle((int)t.location.X, (int)t.location.Y, 64, 64);
                        if (bounds.Intersects(targRectangle) && location.X < 320)
                        {
                            location.X -= assassinMovementSpeed * multiplier;
                            blocked     = true;
                        }
                        else if (bounds.Intersects(targRectangle) && location.X >= 320)
                        {
                            location.X += assassinMovementSpeed * multiplier;
                            blocked     = true;
                        }
                    }


                    if (bounds.Intersects(homeTarget.bounds))
                    {
                        isAttacking = true;
                        homeTarget.collide(this);
                    }

                    if (!blocked && !isAttacking)
                    {
                        if (relative.X > 0)
                        {
                            facingRight = true;
                            location.X += assassinMovementSpeed * multiplier;
                        }
                        if (relative.X < 0)
                        {
                            facingLeft  = true;
                            location.X -= assassinMovementSpeed * multiplier;
                        }
                        if (relative.Y > 0)
                        {
                            facingDown  = true;
                            location.Y += assassinMovementSpeed * multiplier;
                        }
                        if (relative.Y < 0)
                        {
                            facingUp    = true;
                            location.Y -= assassinMovementSpeed * multiplier;
                        }
                    }
                }

                bounds = new Rectangle((int)location.X, (int)location.Y, sprite.frame.Width, sprite.frame.Height);
                foreach (Enemy e in enemies)
                {
                    if (bounds.Intersects(e.bounds) && (location.X != e.location.X && location.Y != e.location.Y))
                    {
                        location.X = currx;
                    }
                }

                foreach (Trap t in traps)
                {
                    if (bounds.Intersects(t.bounds) && t.activate())
                    {
                        timeSinceStun = 0.0f;
                        stunned       = true;
                    }
                }
            }
            else
            {
                timeSinceStun += elapsedTime.ElapsedGameTime.Milliseconds;
                if (timeSinceStun >= maxTimeStunned)
                {
                    stunned = false;
                }
            }

            timeSinceFrameChange += elapsedTime.ElapsedGameTime.Milliseconds;
            if (timeSinceFrameChange >= timeBetweenFrames)
            {
                timeSinceFrameChange = 0.0f;
                frame++;
                frame %= textures.Count;
            }
        }
Example #2
0
        public World(Texture2D gruntSprite, Texture2D assassinSprite, Texture2D tankSprite, Texture2D bulletSprite, Texture2D towerSprite, Texture2D trapSprite,
                     Texture2D gr1, Texture2D gr2, Texture2D ar1, Texture2D ar2, Texture2D tr1, Texture2D tr2, SpriteFont font, Texture2D healthBar, Texture2D ti, Texture2D tii,
                     Texture2D tri, Texture2D trii, Texture2D towerSpriteIn, Texture2D trapSpriteIn)
        {
            traps   = new List <Trap>();
            towers  = new List <Tower>();
            enemies = new List <Enemy>();

            gruntList    = new List <Texture2D>();
            assassinList = new List <Texture2D>();
            tankList     = new List <Texture2D>();

            //static tower
            towers.Add(new Tower(Movespeed.Zero, ArmorValue.High, Damage.High, 100, new Sprite(towerSprite), new Vector2(300, 200), bulletSprite, healthBar));

            List <Level> hordes = new List <Level>();

            //levels = new List<Level>();
            //levels.Add(new Level(10, 5, 2, this, hordes));
            levels = new List <Level>();
            levels.Add(new Level(10, 5, 0, this, hordes));
            levels.Add(new Level(18, 2, 0, this, hordes));
            levels.Add(new Level(18, 5, 2, this, hordes));

            hordes.Add(new Level(10, 3, 2, this, new List <Level>()));

            levels.Add(new Level(10, 4, 1, this, hordes));

            hordes = new List <Level>();
            hordes.Add(new Level(5, 5, 5, this, new List <Level>()));
            hordes.Add(new Level(1, 6, 3, this, new List <Level>()));

            levels.Add(new Level(8, 5, 2, this, hordes));

            hordes = new List <Level>();
            hordes.Add(new Level(3, 6, 3, this, new List <Level>()));
            hordes.Add(new Level(8, 7, 4, this, new List <Level>()));

            levels.Add(new Level(6, 10, 4, this, hordes));

            hordes = new List <Level>();
            hordes.Add(new Level(2, 9, 1, this, new List <Level>()));
            hordes.Add(new Level(1, 4, 2, this, new List <Level>()));
            hordes.Add(new Level(2, 16, 3, this, new List <Level>()));

            levels.Add(new Level(7, 6, 1, this, hordes));

            this.gruntSprite    = gruntSprite;
            this.bulletSprite   = bulletSprite;
            this.towerSprite    = towerSprite;
            this.tankSprite     = tankSprite;
            this.assassinSprite = assassinSprite;
            this.trapSprite     = trapSprite;
            this.healthBar      = healthBar;
            this.gr1            = gr1;
            this.gr2            = gr2;
            this.ar1            = ar1;
            this.ar2            = ar2;
            this.ti             = ti;
            this.tii            = tii;
            this.tri            = tri;
            this.trii           = trii;
            this.trapSpriteIn   = trapSpriteIn;
            this.towerSpriteIn  = towerSpriteIn;

            gruntList.Add(gruntSprite);
            gruntList.Add(gr1);
            gruntList.Add(gruntSprite);
            gruntList.Add(gr2);

            assassinList.Add(assassinSprite);
            assassinList.Add(ar1);
            assassinList.Add(assassinSprite);
            assassinList.Add(ar2);

            tankList.Add(tankSprite);
            tankList.Add(tr1);
            tankList.Add(tankSprite);
            tankList.Add(tr2);

            this.font = font;

            player = new HomeBase(Movespeed.Zero, ArmorValue.VeryHigh, Damage.Zero, 500, new Sprite(gruntSprite), Vector2.Zero);

            //enemies.Add(new Enemy(Movespeed.Slow, ArmorValue.Low, Damage.High, 1, new Sprite(tankSprite), new Vector2(0,10), EnemyType.Tank, tankList));
            //enemies.Add(new Enemy(Movespeed.Slow, ArmorValue.Low, Damage.High, 1, new Sprite(gruntSprite), new Vector2(0,30), EnemyType.Grunt, gruntList));
            //enemies.Add(new Enemy(Movespeed.Slow, ArmorValue.Low, Damage.High, 1, new Sprite(gruntSprite), new Vector2(0, 50), EnemyType.Grunt, gruntList));
            //enemies.Add(new Enemy(Movespeed.Slow, ArmorValue.Low, Damage.High, 1, new Sprite(gruntSprite), new Vector2(0, 70), EnemyType.Grunt, gruntList));
            //tow = new Tower(Movespeed.Zero, ArmorValue.Medium, Damage.High, 100, new Sprite(placeHolder), new Vector2(640/2, 480/2), s);
            //towers.Add(tow);

            levels[0].startLevel();
        }