Exemplo n.º 1
0
        public override void LoadContent()
        {
            base.LoadContent();

            fileReferences = new Texture2D[numberOfFileReferences];
            //load new tiles here
            for (int i = 0; i < fileReferences.Length; i++)
            {
                fileReferences[i] = content.Load<Texture2D>("Sprites\\Files\\Tiles\\File" + i.ToString());
                //0 is block
                //1 is scenery
                //2 is slope
            }

            backgroundFileReferences = new Texture2D[numberOfBackgroundFileReferences];

            for (int i = 0; i < backgroundFileReferences.Length; i++)
            {
                backgroundFileReferences[i] = content.Load<Texture2D>("Backgrounds\\BackgroundTypes\\BackgroundFile" + i.ToString());
                //0 is Normal
                //1 is Other

            }

            tileReferenceBlocks = new Texture2D[numberOfBlockTiles];

            for (int i = 0; i < tileReferenceBlocks.Length; i++)
            {
                tileReferenceBlocks[i] = content.Load<Texture2D>("Sprites\\Block\\Block" + i.ToString());
            }

            tileReferenceSceneries = new Texture2D[numberOfSceneryTiles];

            for (int i = 0; i < tileReferenceSceneries.Length; i++)
            {
                tileReferenceSceneries[i] = content.Load<Texture2D>("Sprites\\Scenery\\Scenery" + i.ToString());
            }

            tileReferenceSlopes = new Texture2D[numberOfSlopeTiles];

            for (int i = 0; i < tileReferenceSlopes.Length; i++)
            {
                tileReferenceSlopes[i] = content.Load<Texture2D>("Sprites\\Slope\\Slope" + i.ToString());
            }

            normalBackgroundReferences = new Texture2D[numberOfNormalBackgroundReferences];

            for (int i = 0; i < normalBackgroundReferences.Length; i++)
            {
                normalBackgroundReferences[i] = content.Load<Texture2D>("Backgrounds\\BackgroundTypes\\Normal\\NormalBackground" + i.ToString());
            }

            otherBackgroundReferences = new Texture2D[numberOfOtherBackgroundReferences];

            for (int i = 0; i < otherBackgroundReferences.Length; i++)
            {
                otherBackgroundReferences[i] = content.Load<Texture2D>("Backgrounds\\BackgroundTypes\\Other\\OtherBackground" + i.ToString());
            }

            tileSelectionBox = content.Load<Texture2D>("Sprites\\tileSelectionBox");

            if (loadingSavedGame)
            {
                if (loadingSavedGameFromText)
                {
                    string path = "Content\\" + LevelName + ".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();
                    }

                }
            }

            if (returningToLevelCreate)
            {
                using (StreamReader sr = new StreamReader(tempPath))
                {

                    while (sr.Peek() >= 0)
                    {
                        string line = sr.ReadLine();

                        string[] parts = line.Split(',');

                        float X = (float)Convert.ToInt32(parts[0]);  //This is the first coordinate
                        float Y = (float)Convert.ToInt32(parts[1]);  //This is the second coordinate
                        int objectNumber = Convert.ToInt32(parts[3]);   //This is which style of the tile
                        int layerNumber = Convert.ToInt32(parts[4]);
                        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), 100, 100));
                            backgroundRectangles[backgroundRectangles.Count - 1] = new Rectangle((int)(Backgrounds[Backgrounds.Count - 1].position.X + scrollOffset.X), (int)(Backgrounds[Backgrounds.Count - 1].position.Y + scrollOffset.Y), 100, 100);

                        }
                    }
                    sr.Close();
                }

                File.Delete(tempPath);
            }
        }
Exemplo n.º 2
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 + scrollOffset.X), (int)(tiles[i].position.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 = .1f;

                            tileRectangle = new Rectangle((int)(tile.position.X + scrollOffset.X), (int)(tile.position.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 + scrollOffset.X), (int)(tile.position.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);

                        }
                    }
                }
            }
        }
Exemplo n.º 3
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))
                {
                    player.velocity.Y = 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
            {

                //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)
                    {
                        //no matter what the velocity should go to 0 when y-axis colliding
                        player.velocity.Y = 0;
                        //CheckImaginaryRectangles();
                        if (depth.Y < 0)
                        {
                            gravityAcceleration = 0;
                            refDepthY = depth.Y;
                            reftileRectangle = tileRectangle;
                            CheckImaginaryRectangles();
                        }

                        //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
                    {
                        //on x-axis collision the player.velocity.Y needs to be the same so he doesn't "stick" to the wall(s)

                        //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);

                    }

                }

            }
        }
Exemplo n.º 4
0
        public override void LoadContent()
        {
            base.LoadContent();

            using (StreamReader sr = new StreamReader(path))
            {
                while (sr.Peek() >= 0)
                {
                    string line = sr.ReadLine();  //read the current line

                    string[] parts = line.Split(',');

                    if (parts[5] == "Tile")
                    {
                        numberOfTiles++;
                    }
                    if (parts[5] == "Background")
                    {
                        numberOfBackgrounds++;
                    }
                }

                tiles = new Tile[numberOfTiles];
                backgrounds = new Background[numberOfBackgrounds];
                sr.Close();
            }

            using (StreamReader sr = new StreamReader(path))
            {

                int i = 0;    //tiles
                int j = 0;    //backgrounds

                while (sr.Peek() >= 0)
                {
                    string line = sr.ReadLine();

                    string[] parts = line.Split(',');

                    float X = (float)Convert.ToInt32(parts[0]);  //This is the first coordinate
                    float Y = (float)Convert.ToInt32(parts[1]);  //This is the second coordinate
                    int objectNumber = Convert.ToInt32(parts[3]);   //This is which style of the tile
                    int layerNumber = Convert.ToInt32(parts[4]);
                    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;

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

                        tiles[i].textureData =
                        new Color[tiles[i].sprite.Width * tiles[i].sprite.Height];
                        tiles[i].sprite.GetData(tiles[i].textureData);

                        i++;
                    }
                    else if (parts[5] == "Background")
                    {

                        if (parts[2] == "Normal")
                            bType = BackgroundType.Normal;
                        if (parts[2] == "Other")
                            bType = BackgroundType.Other;

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

                        j++;
                    }
                }
                sr.Close();
            }

            LevelCreateSession = false;

            Rectangle titleSafeArea = ScreenManager.GraphicsDevice.Viewport.TitleSafeArea;

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

            playerCollisionReference = new Player(content.Load<Texture2D>("Sprites\\playerCollisionReference"));
            playerCollisionReference.textureData =
                        new Color[playerCollisionReference.sprite.Width * playerCollisionReference.sprite.Height];
            playerCollisionReference.sprite.GetData(playerCollisionReference.textureData);

            //start his position on top of the first tile

            player.position = new Vector2(tiles[0].position.X - 50, tiles[0].position.Y - tiles[0].sprite.Height - 100);
        }