Esempio n. 1
0
        public enemy1(Vector2 position, int width, int height, Vector2 velocity, float rotation, Vector2 origin, Vector2 scale, Color color, bool islive)
        {
            _texture       = MyContentManager.GetInstance().LoadContent <Texture2D>("pig1_230x85");
            _idleAnimation = new Animation();
            _idleAnimation.AddFrame(new Rectangle(0, 0, 115, 85), TimeSpan.FromSeconds(.25));
            _idleAnimation.AddFrame(new Rectangle(115, 0, 115, 85), TimeSpan.FromSeconds(.25));
            isTurnright = true;

            _upray    = new Ray2D(new Vector2(position.X + width / 2, position.Y), new Vector2(0, -0.01f));
            _downray  = new Ray2D(new Vector2(position.X + width / 2, position.Y + height), new Vector2(0, 0.01f));
            _rightray = new Ray2D(new Vector2(position.X + width, position.Y + height / 2), new Vector2(0.01f, 0));
            _leftray  = new Ray2D(new Vector2(position.X, position.Y + height / 2), new Vector2(-0.01f, 0));

            _canUp    = true;
            _canDown  = true;
            _canRight = true;
            _canLeft  = true;

            _speed      = 2;
            Health      = 10;
            _isfindHero = false;
            _state      = EnemyPigState.XL;

            changetime  = 2.0f;
            changetimer = 0.0f;
            rushtime    = 0.7f;
            rushtimer   = 0.0f;
            stoptime    = 2.0f;
            stoptimer   = 0.0f;

            rnd    = new Random();
            rndnum = 0;

            EnemySprite = new Sprite(position, width, height, velocity, rotation, origin, scale, color, islive);
        }
Esempio n. 2
0
        public override void Update(GameTime gameTime)
        {
            CheckCanMove();

            if (_state == EnemyPigState.XL)
            {
                changetimer += gameTime.ElapsedGameTime.TotalSeconds;
                if (changetimer >= changetime)
                {
                    changetimer = 0.0f;
                    if (!_isfindHero)
                    {
                        rndnum = rnd.Next(0, 4);
                    }
                }
                if ((GameManager.GetInstance().Player.Position - EnemySprite.Position).Length() <= 400)
                {
                    _isfindHero = true;
                    _state      = EnemyPigState.FINDENEMY;
                }
            }
            else
            {
                if (_state == EnemyPigState.FINDENEMY)
                {
                    stoptimer += gameTime.ElapsedGameTime.TotalSeconds;
                    if (stoptimer >= stoptime)
                    {
                        stoptimer = 0.0f;
                        changedir = true;
                        _state    = EnemyPigState.RUSH;
                    }
                }
                else if (_state == EnemyPigState.RUSH)
                {
                    rushtimer += gameTime.ElapsedGameTime.TotalSeconds;
                    if (rushtimer >= rushtime)
                    {
                        rushtimer = 0;
                        _state    = EnemyPigState.FINDENEMY;
                    }
                }
            }

            switch (_state)
            {
            case EnemyPigState.XL:
                MoveXL();
                break;

            case EnemyPigState.FINDENEMY:
                Move();
                break;

            case EnemyPigState.RUSH:
                //SoundManager.Getinstance().PlaySoundEffect("pigAttack");
                MoveRush();
                break;
            }



            if (Health <= 0)
            {
                EnemySprite.IsLive = false;
            }
            _currentAnimation = _idleAnimation;
            _currentAnimation.Update(gameTime);
            EnemySprite.Update();
        }