Example #1
0
        public void getClosestEnemy(List<Enemy> enemies)
        {
            if (enemies != null)
            {
                if (enemies.Count > 0)
                {
                    _targetEnemy = null;
                    float minDis = _range;
                    int minIndex = 0;

                    for (int i = 0; i < enemies.Count; i++)
                    {
                        float dis = Vector2.Distance(new Vector2((float)_sprite.Left, (float)_sprite.Top),
                            new Vector2((float)enemies[i].Sprite.Left, (float)enemies[i].Sprite.Top));
                        if (dis < minDis && !enemies[i].Kind.Equals("ball"))
                        {
                            minDis = dis;
                            minIndex = i;
                        }
                    }

                    _targetEnemy = enemies[minIndex];
                }
            }
        }
Example #2
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (_targetEnemy != null)
            {
                FaceTarget();

                if (!isInRange(new Vector2((float)(TargetEnemy.Sprite.Left + Global.CELL_WIDTH/2), (float)(TargetEnemy.Sprite.Top + Global.CELL_HEIGHT/2))) || _targetEnemy.isDead)
                {
                    _targetEnemy = null;
                    timer = 0;
                }
            }

            if (first == 0 && _targetEnemy != null)
            {
                Arrow arrow = ArrowManager.CreateArrow(0);
                arrow.Position = new Vector2((float)this.sprite.Left + sprite.Texture.Width / 2, (float)this.sprite.Top + sprite.Texture.Height / 2);
                arrow.Rotation = _rotation;
                arrow.Sprite.Rotation = _rotation;
                arrow.IsFired = true;
                if (Global.SOUNDEFFECT_ENABLE)
                    SoundManager.so_arrowSound.Play();

                arrows.Add(arrow);
                first++;
            }
            if (timer >= (_arrowTime/ Global.GAME_SPEED) && _targetEnemy !=null)
            {
                Arrow arrow = ArrowManager.CreateArrow(0);
                arrow.Position = new Vector2((float)this.sprite.Left + sprite.Texture.Width / 2, (float)this.sprite.Top + sprite.Texture.Height / 2);
                arrow.Rotation = _rotation;
                arrow.Sprite.Rotation = _rotation;
                arrow.IsFired = true;
                if (Global.SOUNDEFFECT_ENABLE)
                    SoundManager.so_arrowSound.Play();

                arrows.Add(arrow);
                timer = 0;
            }

            for (int i=0; i<arrows.Count; i++)
            {
                Arrow arrow = arrows[i];
                if (arrow.IsFired == false)
                    arrow.Rotation = _rotation;
                arrow.Update(gameTime);

                if (_targetEnemy != null && Vector2.Distance(new Vector2(arrow.Sprite.Left + arrow.Sprite.Texture.Width / 2, arrow.Sprite.Top + arrow.Sprite.Texture.Height / 2), new Vector2(_targetEnemy.Sprite.Left + Global.CELL_WIDTH / 2, _targetEnemy.Sprite.Top + Global.CELL_HEIGHT / 2)) < 25)
                {
                    _targetEnemy.CurrentHealth -= arrow.Damage;

                    arrow.disappearArrow();
                }

                if (!isInRange(new Vector2((float)(arrow.Sprite.Left + arrow.Sprite.Texture.Width/2), (float)(arrow.Sprite.Top + arrow.Sprite.Texture.Height/2))))
                {
                    arrow.disappearArrow();
                }

                if (arrow.isNotInRange())
                {
                    arrows.Remove(arrow);
                    i--;
                }

            }
        }