Beispiel #1
0
 public abstract void Update( LevelMap levelMap, GameTime gameTime );
Beispiel #2
0
 /// <summary>
 /// Checks if the rectangle collides with an existing game object, and if so
 /// removes it from the map returning the removed object. Returns null if 
 /// no collision exists.
 /// </summary>
 /// <param name="rectangle"></param>
 /// <returns></returns>
 public GameActor CheckHitAndRemove( LevelMap map, Rectangle rectangle, bool remove = true )
 {
     for ( int i = 0; i < map.ActorObjects.Count; ++i ) {
         GameActor actor = map.ActorObjects[i];
         if ( actor != this &&
             actor.BoundingBox.Intersects( rectangle ) ) {
             if ( remove )
                 map.ActorObjects.RemoveAt( i );
             return actor;
         }
     }
     return null;
 }
Beispiel #3
0
 private bool IsNewPositionOK( LevelMap levelMap, Rectangle rectangle )
 {
     return ( levelMap.CheckRectangleBounds( rectangle ) &&
             CheckHitAndRemove( levelMap, rectangle, false ) == null );
 }
Beispiel #4
0
        public override void Update( LevelMap levelMap, GameTime gameTime )
        {
            if ( ( gameTime.TotalGameTime.TotalMilliseconds - _lastTimeMoved ) > 10 ) {
                _lastTimeMoved = ( int ) gameTime.TotalGameTime.TotalMilliseconds;

                // move randomly
                int steps = 0; // bound the maximum times to retry
                Vector2 displacement = new Vector2( );
                while ( steps < 10 ) {
                    if ( steps == 0 && _lastDisplacementUsedTimes < 7 ) {
                        // try last displacement
                        displacement = _lastDisplacement;
                        ++_lastDisplacementUsedTimes;
                    }
                    else {
                        displacement = levelMap.ThePlayer.Position - Position;
                        displacement.X = MathHelper.Clamp( displacement.X, -1, 1 );
                        displacement.Y = MathHelper.Clamp( displacement.Y, -1, 1 );

                        displacement.X *= _random.Next( 4 ) - 1;
                        displacement.Y *= ( displacement.X == 0 ? _random.Next( 4 ) - 1 : 0 );

                        _lastDisplacementUsedTimes = 0;
                    }

                    Rectangle newBoundingBox = BoundingBox;
                    newBoundingBox.Offset( ( int ) displacement.X, ( int ) displacement.Y );
                    if ( IsNewPositionOK( levelMap, newBoundingBox ) ) {
                        Position += displacement;

                        if ( levelMap.ThePlayer.BoundingBox.Intersects( newBoundingBox ) ) {
                            levelMap.ThePlayer.IsDead = true;
                        }
                        break;
                    }
                    else {
                        // discard the last used displacement
                        _lastDisplacementUsedTimes = 500;
                    }
                    ++steps;
                }

                // save displacement
                _lastDisplacement = displacement;

                if ( displacement.Y == 0 ) {
                    if ( displacement.X < 0 ) FacingDirection = CardinalDirection.WEST;
                    else FacingDirection = CardinalDirection.EAST;
                }

                if ( displacement.X == 0 ) {
                    if ( displacement.Y < 0 ) FacingDirection = CardinalDirection.NORTH;
                    else FacingDirection = CardinalDirection.SOUTH;
                }
                ReplaceCurrentAnimation( );
            }

            if ( CurrentAnimation != null ) {
                CurrentAnimation.Update( gameTime );
            }
        }
Beispiel #5
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( );
                    }
                }
            }
        }