Beispiel #1
0
        private void HandleCollisions()
        {
            int leftTile   = (int)Math.Floor((float)bounds.Left / Tile.TILE_SIZE);
            int rightTile  = (int)Math.Ceiling((float)bounds.Right / Tile.TILE_SIZE) - 1;
            int topTile    = (int)Math.Floor((float)bounds.Top / Tile.TILE_SIZE);
            int bottomTile = (int)Math.Ceiling((float)bounds.Bottom / Tile.TILE_SIZE) - 1;

            isOnGround = false;

            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    Tile.CollisionType collision = map.getCollisionAtCoordinates(x, y);

                    if (collision != Tile.CollisionType.Passable)
                    {
                        Rectangle tileBounds = map.getTileBounds(x, y);
                        Vector2   depth      = bounds.GetIntersectionDepth(tileBounds);

                        if (depth != Vector2.Zero)
                        {
                            float absDepthX = Math.Abs(depth.X);
                            float absDepthY = Math.Abs(depth.Y);


                            if (absDepthY < absDepthX || collision == Tile.CollisionType.Platform)
                            {
                                if (previousBottom <= tileBounds.Top && collision != Tile.CollisionType.Spikes)
                                {
                                    isOnGround = true;
                                }

                                if (collision == Tile.CollisionType.Impassable || isOnGround)
                                {
                                    position = new Vector2(position.X, position.Y + depth.Y);
                                }
                            }

                            else if (collision == Tile.CollisionType.Spikes && absDepthX >= 30 && absDepthY >= 30)
                            {
                                isAlive = false;
                            }

                            else if (collision == Tile.CollisionType.Impassable)
                            {
                                position = new Vector2(position.X + depth.X / 2, position.Y);
                            }
                        }
                    }
                }
            }
            previousBottom = bounds.Bottom;
        }
Beispiel #2
0
        private void HandleCollisions()
        {
            int leftTile   = (int)Math.Floor((float)bounds.Left / Tile.TILE_SIZE);
            int rightTile  = (int)Math.Ceiling((float)bounds.Right / Tile.TILE_SIZE) - 1;
            int topTile    = (int)Math.Floor((float)bounds.Top / Tile.TILE_SIZE);
            int bottomTile = (int)Math.Ceiling((float)bounds.Bottom / Tile.TILE_SIZE) - 1;

            isOnGround = false;

            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    Tile.CollisionType collision = map.getCollisionAtCoordinates(x, y);

                    if (y == topTile + 1 && x == leftTile + 1)
                    {
                        //Console.WriteLine("bounce");
                        if (collision == Tile.CollisionType.Impassable)
                        {
                            velocity.Y = -Math.Abs(velocity.Y) * 1f;
                        }
                    }
                    if (collision != Tile.CollisionType.Passable)
                    {
                        Rectangle tileBounds = map.getTileBounds(x, y);
                        Vector2   depth      = bounds.GetIntersectionDepth(tileBounds);

                        if (depth != Vector2.Zero)
                        {
                            float absDepthX = Math.Abs(depth.X);
                            float absDepthY = Math.Abs(depth.Y);

                            if (absDepthY < absDepthX || collision == Tile.CollisionType.Platform)
                            {
                                if (previousBottom <= tileBounds.Top)
                                {
                                    isOnGround = true;
                                }

                                if (collision == Tile.CollisionType.Impassable || isOnGround)
                                {
                                    position = new Vector2(position.X, position.Y + depth.Y);
                                    position = new Vector2(position.X + depth.X / 2, position.Y);
                                    if (velocity.X > 0)
                                    {
                                        velocity.X = -Math.Abs(velocity.X) * 0.5f;
                                    }
                                    else if (velocity.X < 0)
                                    {
                                        velocity.X = Math.Abs(velocity.X) * 0.5f;
                                    }
                                }
                            }
                            else if (collision == Tile.CollisionType.Impassable)
                            {
                                position = new Vector2(position.X + depth.X / 2, position.Y);
                                if (velocity.X > 0)
                                {
                                    velocity.X = -Math.Abs(velocity.X) * 0.5f;
                                }
                                else if (velocity.X < 0)
                                {
                                    velocity.X = Math.Abs(velocity.X) * 0.5f;
                                }
                            }
                        }
                    }
                }
            }
            previousBottom = bounds.Bottom;
        }