Exemplo n.º 1
0
 private bool IsNewPositionOK( LevelMap levelMap, Rectangle rectangle )
 {
     return ( levelMap.CheckRectangleBounds( rectangle ) &&
             CheckHitAndRemove( levelMap, rectangle, false ) == null );
 }
Exemplo n.º 2
0
        public override void Update( LevelMap levelMap, GameTime gameTime )
        {
            KeyboardState keys = Keyboard.GetState( );
            if ( !_attacking && keys.IsKeyDown2( Keys.Space ) ) {
                _attacking = true;
                TheStory.SOUND.Play2D( "Content/sfx/bow.ogg" );
            }
            else if ( _attacking && CurrentAnimation.Finished ) {
                _attacking = false;

                // create new projectile
                AnimatedSprite projectileAnim = null;

                switch ( FacingDirection ) {
                    case CardinalDirection.EAST:
                        projectileAnim = ProjectileEastAnim;
                        break;
                    case CardinalDirection.WEST:
                        projectileAnim = ProjectileEastAnim;
                        break;
                    case CardinalDirection.SOUTH:
                        projectileAnim = ProjectileSouthAnim;
                        break;
                    case CardinalDirection.NORTH:
                        projectileAnim = ProjectileNorthAnim;
                        break;
                }

                if ( projectileAnim != null )
                    _projectilesShot.Enqueue( new Projectile( ) {
                        Animation = projectileAnim,
                        Position = Position + CurrentAnimation.FrameBoundingBox.Size( ) / 2,
                        Velocity = FacingDirection.ToVelocity( )
                    } );
            }

            _walking = false;
            // cannot move while attacking
            if ( !_attacking ) {
                if ( keys.IsKeyDown( Keys.Left ) ) {
                    FacingDirection = CardinalDirection.WEST;
                    _walking = true;
                }
                else if ( keys.IsKeyDown( Keys.Right ) ) {
                    FacingDirection = CardinalDirection.EAST;
                    _walking = true;
                }
                else if ( keys.IsKeyDown( Keys.Down ) ) {
                    FacingDirection = CardinalDirection.SOUTH;
                    _walking = true;
                }
                else if ( keys.IsKeyDown( Keys.Up ) ) {
                    FacingDirection = CardinalDirection.NORTH;
                    _walking = true;
                }

                if ( _walking ) {
                    // check if we can move there.
                    Vector2 newPosition = ( Position + FacingDirection.ToVelocity( ) * 2 );
                    Rectangle newBoundingBox = CurrentAnimation.FrameBoundingBox;
                    newBoundingBox.Width -= 15;
                    newBoundingBox.Height -= 15;
                    newBoundingBox.Offset( ( int ) newPosition.X + 5, ( int ) newPosition.Y + 5 );

                    if ( levelMap.CheckRectangleBounds( newBoundingBox ) ) {
                        // check collision with other objects
                        bool collides = false;
                        foreach ( GameActor actor in levelMap.ActorObjects ) {
                            if ( actor.BoundingBox.Intersects( newBoundingBox ) ) {
                                if ( actor.IsEnemy ) {
                                    this.IsDead = true;
                                }
                                else {
                                    collides = true;
                                }
                                break;
                            }
                        }

                        if ( !collides ) {
                            Position = newPosition;
                        }
                    }
                }
            }
            ReplaceCurrentAnimation( );

            if ( CurrentAnimation != null ) {
                if ( !_attacking ) {
                    CurrentAnimation.Playing = _walking;
                }

                CurrentAnimation.Update( gameTime );
            }

            Projectile[] tmpArray = _projectilesShot.ToArray( );

            for ( int i = 0; i < tmpArray.Length; ++i ) {
                Projectile projectile = tmpArray[i];
                projectile.Animation.Update( gameTime );
                projectile.Position += projectile.Velocity * 5;

                Rectangle projectileBox = projectile.Animation.FrameBoundingBox;
                projectileBox.Offset( ( int ) projectile.Position.X, ( int ) projectile.Position.Y );

                if ( !levelMap.Mask.Bounds.Contains( ref projectile.Position ) ) {
                    // remove this
                    _projectilesShot.Dequeue( );
                }
                else if ( !levelMap.CheckRectangleBounds( projectileBox, true ) ) {
                    //TODO: add hit animation
                    _projectilesShot.Dequeue( );
                }
                else {
                    // check collision with other objects
                    if ( CheckHitAndRemove( levelMap, projectileBox ) != null ) {
                        _projectilesShot.Dequeue( );
                    }
                }
            }
        }