Пример #1
0
        private void ResolveCollisions(Tile tile, Player player)
        {
            if (tile.type == TileType.Slope)
            {

                tileTransform =
                        Matrix.CreateTranslation(new Vector3(-tile.center, 0.0f)) *
                        Matrix.CreateRotationZ(tile.rotation) *
                        Matrix.CreateTranslation(new Vector3(tile.position + scrollOffset, 0.0f));

                tileRectangle = GameObject.CalculateBoundingRectangle(
                    new Rectangle(0, 0, tile.sprite.Width, tile.sprite.Height),
                    tileTransform);

                if (GameObject.IntersectPixels(playerTransform, player.sprite.Width, player.sprite.Height, playerCollisionReference.textureData,
                                               tileTransform, tile.sprite.Width, tile.sprite.Height, tile.textureData))
                {
                    gravityAcceleration = 0;

                    if (player.velocity.X > 0)
                    {
                        player.position.Y -= player.velocity.X;
                    }
                    else if (player.velocity.X < 0 && (playerRectangle.Right) <= (tileRectangle.Right))
                    {
                        player.position.Y += player.velocity.X;
                    }
                }

            }
            else
            {
                gravityAcceleration = 2;
                //get the signed depth of the intersection to account for the correction vector
                Vector2 depth = RectangleExtensions.GetIntersectionDepth(playerRectangle, tileRectangle);

                //if the depth isn't zero...probably just a redundant check because it is assumed that the depth is not zero due to the check above on collision
                if (depth != Vector2.Zero)
                {
                    //get the absolute values to check if its a side-on collision or a bottom-top collision
                    float absDepthX = Math.Abs(depth.X);
                    float absDepthY = Math.Abs(depth.Y);

                    // Resolve the collision along the y-axis.
                    if (absDepthY < absDepthX)
                    {
                        //simply give it a new vector with its X staying the same but correcting the Y position the exact amount it collided inwards with the other rectangle
                        player.position = new Vector2(player.position.X, player.position.Y + depth.Y);
                        //reassign the rectangle a value (probably redundant since it checks again when the method is called
                        playerRectangle = new Rectangle((int)(player.position.X - player.center.X + scrollOffset.X), (int)(player.position.Y - player.center.Y + scrollOffset.Y), player.sprite.Width, player.sprite.Height);

                    }
                    // or Resolve the collision along the x-axis
                    else
                    {
                        //same here but in the side-wards direction
                        player.position = new Vector2(player.position.X + depth.X, player.position.Y);
                        //reassign the rectangle a value (probably redundant since it checks again when the method is called
                        playerRectangle = new Rectangle((int)(player.position.X - player.center.X + scrollOffset.X), (int)(player.position.Y - player.center.Y + scrollOffset.Y), player.sprite.Width, player.sprite.Height);

                    }
                }
            }
        }
Пример #2
0
        public override void LoadContent()
        {
            string path = "Content\\Levels\\Level" + LbKStorage.Level.ToString() + ".txt";

            using (StreamReader sr = new StreamReader(path))
            {
                //Taken from a text file formated with 4 pieces of info.  coor1,coor2,type,style,layer,object
                //                                                       (float,float,TileType,int32,int32,string)
                while (sr.Peek() >= 0)  //apparently this keep going until the stream reader peeks and sees nothing on the line
                {

                    string line = sr.ReadLine();  //read the current line

                    string[] parts = line.Split(',');  //split anything that has commas to separate parts of a string array

                    float X = (float)Convert.ToInt32(parts[0]);  //This is the first coordinate of the tile/background
                    float Y = (float)Convert.ToInt32(parts[1]);  //This is the second coordinate of the tile/background
                    int objectNumber = Convert.ToInt32(parts[3]);   //This is which style of the tile/background
                    int layerNumber = Convert.ToInt32(parts[4]);  //This is the layer of the tile/background
                    TileType type = TileType.Block;
                    BackgroundType bType = BackgroundType.Normal;
                    if (parts[5] == "Tile")
                    {
                        if (parts[2] == "Block")
                            type = TileType.Block;
                        if (parts[2] == "Slope")
                            type = TileType.Slope;
                        if (parts[2] == "Scenery")
                            type = TileType.Scenery;

                        tempLoadedTile = new Tile(content.Load<Texture2D>("Sprites\\" + parts[2] + "\\" + parts[2] + objectNumber.ToString()));
                        tempLoadedTile.position = new Vector2(X, Y);
                        tempLoadedTile.type = type;
                        tempLoadedTile.objectNumber = objectNumber;
                        tempLoadedTile.layerNumber = layerNumber;

                        if (parts[2] == "Scenery")
                        {
                            tempLoadedTile.textureData =
                                new Color[tempLoadedTile.sprite.Width * tempLoadedTile.sprite.Height];
                            tempLoadedTile.sprite.GetData(tempLoadedTile.textureData);
                        }

                        Tiles.Add(tempLoadedTile);

                        tileRectangles.Add(new Rectangle((int)(Tiles[Tiles.Count - 1].position.X + scrollOffset.X), (int)(Tiles[Tiles.Count - 1].position.Y + scrollOffset.Y), 100, 100));
                        tileRectangles[tileRectangles.Count - 1] = new Rectangle((int)(Tiles[Tiles.Count - 1].position.X + scrollOffset.X), (int)(Tiles[Tiles.Count - 1].position.Y + scrollOffset.Y), 100, 100);

                    }
                    else if (parts[5] == "Background")
                    {
                        if (parts[2] == "Normal")
                            bType = BackgroundType.Normal;
                        if (parts[2] == "Other")
                            bType = BackgroundType.Other;

                        tempLoadedBackground = new Background(content.Load<Texture2D>("Backgrounds\\BackgroundTypes\\" + parts[2] + "\\" + parts[2] + "Background" + objectNumber.ToString()));
                        tempLoadedBackground.position = new Vector2(X, Y);
                        tempLoadedBackground.type = bType;
                        tempLoadedBackground.objectNumber = objectNumber;
                        tempLoadedBackground.layerNumber = layerNumber;
                        Backgrounds.Add(tempLoadedBackground);

                        backgroundRectangles.Add(new Rectangle((int)(Backgrounds[Backgrounds.Count - 1].position.X + scrollOffset.X), (int)(Backgrounds[Backgrounds.Count - 1].position.Y + scrollOffset.Y), Backgrounds[Backgrounds.Count - 1].sprite.Width, Backgrounds[Backgrounds.Count - 1].sprite.Height));
                        backgroundRectangles[backgroundRectangles.Count - 1] = new Rectangle((int)(Backgrounds[Backgrounds.Count - 1].position.X + scrollOffset.X), (int)(Backgrounds[Backgrounds.Count - 1].position.Y + scrollOffset.Y), Backgrounds[Backgrounds.Count - 1].sprite.Width, Backgrounds[Backgrounds.Count - 1].sprite.Height);
                    }
                }
                sr.Close();
            }

            tiles = new Tile[Tiles.Count];
            for (int i = 0; i < Tiles.Count; i++)
            {
                tiles[i] = Tiles[i];
            }
            backgrounds = new Background[Backgrounds.Count];
            for (int i = 0; i < Tiles.Count; i++)
            {
                backgrounds[i] = Backgrounds[i];
            }

            player = new Player(content.Load<Texture2D>("Sprites\\player2.0"));

            if (LbKStorage.Level == 1)
            {
                if (LbKStorage.CheckPoint == 1)
                {
                    player.position = new Vector2(100.0f);
                }
            }

            base.LoadContent();
        }
Пример #3
0
        private void ResolveCollisionErrors(Tile tile, Player player)
        {
            if (player.velocity.X < 0)
            {
                for (int i = 0; i < tiles.Length; i++)
                {
                    if (tiles[i].type == TileType.Slope)
                    {
                        tileRectangle = new Rectangle((int)(tiles[i].position.X - tiles[i].center.X + scrollOffset.X), (int)(tiles[i].position.Y - tiles[i].center.Y + scrollOffset.Y), tiles[i].sprite.Width, tiles[i].sprite.Height);
                        //where would we be five frames in the future
                        player.position.Y += 10;
                        playerRectangle = new Rectangle((int)(player.position.X - player.center.X + scrollOffset.X), (int)(player.position.Y - player.center.Y + scrollOffset.Y), player.sprite.Width, player.sprite.Height);

                        if (player.Collision(playerRectangle, tileRectangle))
                        {

                            player.position.Y -= 10;
                            gravityAcceleration = 2;
                            tileRectangle = new Rectangle((int)(tile.position.X - tile.center.X + scrollOffset.X), (int)(tile.position.Y - tile.center.Y + scrollOffset.Y), tile.sprite.Width, tile.sprite.Height);
                            playerRectangle = new Rectangle((int)(player.position.X - player.center.X + scrollOffset.X), (int)(player.position.Y - player.center.Y + scrollOffset.Y), player.sprite.Width, player.sprite.Height);

                        }
                        else
                        {
                            player.position.Y -= 10;
                            tileRectangle = new Rectangle((int)(tile.position.X - tile.center.X + scrollOffset.X), (int)(tile.position.Y - tile.center.Y + scrollOffset.Y), tile.sprite.Width, tile.sprite.Height);
                            playerRectangle = new Rectangle((int)(player.position.X - player.center.X + scrollOffset.X), (int)(player.position.Y - player.center.Y + scrollOffset.Y), player.sprite.Width, player.sprite.Height);

                        }
                    }
                }
            }
        }