コード例 #1
0
        public override void Initialize()
        {
            base.Initialize();
            dinosaurState         = DinosaurState.WALK;
            previousDinosaurState = dinosaurState;
            facingDirection       = startFacingDirection;
            if (facingDirection == Direction.RIGHT)
            {
                currentAnimationName = "WALK_RIGHT";
            }
            else if (facingDirection == Direction.LEFT)
            {
                currentAnimationName = "WALK_LEFT";
            }
            airGroundState = AirGroundState.GROUND;

            // every 2 seconds, the fireball will be shot out
            shootTimer.SetWaitTime(2000);
        }
コード例 #2
0
        public override void Update(Player player)
        {
            float startBound = startLocation.X;
            float endBound   = endLocation.X;

            // if shoot timer is up and dinosaur is not currently shooting, set its state to SHOOT
            if (shootTimer.IsTimeUp() && dinosaurState != DinosaurState.SHOOT)
            {
                dinosaurState = DinosaurState.SHOOT;
            }

            base.Update(player);

            // if dinosaur is walking, determine which direction to walk in based on facing direction
            if (dinosaurState == DinosaurState.WALK)
            {
                if (facingDirection == Direction.RIGHT)
                {
                    currentAnimationName = "WALK_RIGHT";
                    MoveXHandleCollision(movementSpeed);
                }
                else
                {
                    currentAnimationName = "WALK_LEFT";
                    MoveXHandleCollision(-movementSpeed);
                }

                // if dinosaur reaches the start or end location, it turns around
                // dinosaur may end up going a bit past the start or end location depending on movement speed
                // this calculates the difference and pushes the enemy back a bit so it ends up right on the start or end location
                if (GetX1() + GetScaledWidth() >= endBound)
                {
                    float difference = endBound - (GetScaledX2());
                    MoveXHandleCollision(-difference);
                    facingDirection = Direction.LEFT;
                }
                else if (GetX1() <= startBound)
                {
                    float difference = startBound - GetX1();
                    MoveXHandleCollision(difference);
                    facingDirection = Direction.RIGHT;
                }

                // if dinosaur is shooting, it first turns read for 1 second
                // then the fireball is actually shot out
            }
            else if (dinosaurState == DinosaurState.SHOOT)
            {
                if (previousDinosaurState == DinosaurState.WALK)
                {
                    shootTimer.SetWaitTime(1000);
                    currentAnimationName = facingDirection == Direction.RIGHT ? "SHOOT_RIGHT" : "SHOOT_LEFT";
                }
                else if (shootTimer.IsTimeUp())
                {
                    // define where fireball will spawn on map (x location) relative to dinosaur enemy's location
                    // and define its movement speed
                    int   fireballX;
                    float movementSpeed;
                    if (facingDirection == Direction.RIGHT)
                    {
                        fireballX     = GetX().Round() + GetScaledWidth();
                        movementSpeed = 1.5f;
                    }
                    else
                    {
                        fireballX     = GetX().Round();
                        movementSpeed = -1.5f;
                    }

                    // define where fireball will spawn on the map (y location) relative to dinosaur enemy's location
                    int fireballY = GetY().Round() + 4;

                    // create Fireball enemy
                    Fireball fireball = new Fireball(new Point(fireballX, fireballY), movementSpeed, 1000);

                    // add fireball enemy to the map for it to offically spawn in the level
                    map.AddEnemy(fireball);

                    // change dinosaur back to its WALK state after shooting, reset shootTimer to wait another 2 seconds before shooting again
                    dinosaurState = DinosaurState.WALK;
                    shootTimer.SetWaitTime(2000);
                }
            }
            previousDinosaurState = dinosaurState;
        }