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

            bluebox = new Player(content.Load<Texture2D>("Sprites\\spritesheet"));

            bluebox.textureData =
                    new Color[bluebox.sprite.Width * bluebox.sprite.Height];

            bluebox.sprite.GetData(bluebox.textureData);
        }
Ejemplo 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);

                        }
                    }
                }
            }
        }
Ejemplo 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);

                    }

                }

            }
        }
Ejemplo 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);
        }