Exemplo n.º 1
0
        /// <summary>
        /// Go right if it's going right, else go left
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            int tileX = (int)Math.Floor(Position.X / Tile.width);
            int tileY = (int)Math.Floor(Position.Y / Tile.height);

            if (Level.GetCollision(tileX, tileY) == TileCollision.Impassable ||
                Level.GetCollision(tileX, tileY) == TileCollision.Platform)
            {
                this.Die();
            }
            else
            {
                Position += mDirection * mSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Detects and resolves all collisions between the player and his neighboring
        /// tiles. When a collision is detected, the player is pushed away along one
        /// axis to prevent overlapping. There is some special logic for the Y axis to
        /// handle platforms which behave differently depending on direction of movement.
        /// </summary>


        private void HandleCollisions()
        {
            // Get the player's bounding rectangle and find neighboring tiles.
            Rectangle bounds     = BoundingRectangle;
            int       leftTile   = (int)Math.Floor((float)bounds.Left / Tile.width);
            int       rightTile  = (int)Math.Ceiling(((float)bounds.Right / Tile.width)) - 1;
            int       topTile    = (int)Math.Floor((float)bounds.Top / Tile.height);
            int       bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.height)) - 1;

            // Reset flag to search for ground collision.
            isOnGround = false;

            // For each potentially colliding tile,
            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    // If this tile is collidable,
                    TileCollision collision = Level.GetCollision(x, y);
                    if (collision != TileCollision.Passable)
                    {
                        // Determine collision depth (with direction) and magnitude.
                        Rectangle tileBounds = Level.GetBounds(x, y);
                        Vector2   depth      = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
                        if (depth != Vector2.Zero)
                        {
                            float absDepthX = Math.Abs(depth.X);
                            float absDepthY = Math.Abs(depth.Y);

                            // Resolve the collision along the shallow axis.
                            if (absDepthY < absDepthX || collision == TileCollision.Platform)
                            {
                                // If we crossed the top of a tile, we are on the ground.
                                if (previousBottom <= tileBounds.Top)
                                {
                                    isOnGround = true;
                                }

                                // Ignore platforms, unless we are on the ground.
                                if (collision == TileCollision.Impassable || IsOnGround)
                                {
                                    // Resolve the collision along the Y axis.
                                    Position = new Vector2(Position.X, Position.Y + depth.Y);

                                    // Perform further collisions with the new bounds.
                                    bounds = BoundingRectangle;
                                }
                            }
                            else if (collision == TileCollision.Impassable) // Ignore platforms.
                            {
                                // Resolve the collision along the X axis.
                                Position = new Vector2(Position.X + depth.X, Position.Y);

                                // Perform further collisions with the new bounds.
                                bounds = BoundingRectangle;
                            }
                        }
                    }
                }
            }

            // Save the new bounds bottom.
            previousBottom = bounds.Bottom;
        }
Exemplo n.º 3
0
        public void Update(GameScreen screen, GameTime gameTime, List <Fireball> fireballs, Bobo player)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (IsAlive)
            {
                // Calculate tile position based on the side we are walking towards.
                float posX  = Position.X + localBounds.Width / 2 * (int)direction;
                int   tileX = (int)Math.Floor(posX / Tile.width) - (int)direction;
                int   tileY = (int)Math.Floor(Position.Y / Tile.height);
                if (waitTime > 0)
                {
                    // Wait for some amount of time.
                    waitTime = Math.Max(0.0f, waitTime - (float)gameTime.ElapsedGameTime.TotalSeconds);
                    if (waitTime <= 0.0f)
                    {
                        // Then turn around.
                        direction = (FaceDirection)(-(int)direction);
                    }
                }
                else
                {
                    // If we are about to run into a wall or off a cliff, start waiting.
                    if (Level.GetCollision(tileX + (int)direction, tileY - 1) == TileCollision.Impassable ||
                        Level.GetCollision(tileX + (int)direction, tileY) == TileCollision.Passable)
                    {
                        waitTime = MaxWaitTime;
                    }
                    else
                    {
                        if (enemy == "enemy")
                        {
                            // Move in the current direction.
                            Vector2 velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f);
                            position = position + velocity;
                        }
                        else
                        {
                            waitTime = Math.Min(0.0f, waitTime - elapsed);
                            if (-1.3f < waitTime && waitTime < -1.0f)
                            {
                                if (flip == 1)
                                {
                                    direction = (FaceDirection)(-(int)direction);
                                }
                                flip = 0;
                            }
                            else
                            {
                                if (flip == 0)
                                {
                                    flip      = 1;
                                    direction = (FaceDirection)(-(int)direction);
                                }

                                // Move in the current direction.
                                Vector2 velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f);
                                position = position + velocity;
                            }
                        }
                    }
                    foreach (Fireball f in fireballs)
                    {
                        if (this.BoundingRectangle.Intersects(f.BoundingRectangle) && !f.isDead)
                        {
                            OnEnemyKilled(f);
                            level.AddScore(100);
                            f.Die();
                        }
                    }
                }
                if (enemy == "enemy")
                {
                    UpdateFireball(screen, gameTime, player);
                }
            }
        }