//public void BuildRandomMap(int mapWidth, int mapHeight) //{ // var rand = new Random(); // //WorldDetails.WordWidthInPixles = mapWidth * (int)WorldDetails.ScaledUnitSize; // //WorldDetails.WordHeightInPixles = mapHeight * (int)WorldDetails.ScaledUnitSize; // int[] tiles = new int[mapWidth * mapHeight]; // Tile[] tileData = new Tile[mapWidth * mapHeight]; // for (int i = 0; i < tiles.Length; i++) // { // if (rand.Next() % 5 != 0) // { // tiles[i] = 0; // tileData[i] = new Tile { IsVisable = false, SpriteTile = 0 }; // } // else // { // tiles[i] = 40; // tileData[i] = new Tile { IsVisable = true, SpriteTile = 40 }; // } // } // _map = new BasicMap // { // Data = tiles, // Width = mapWidth, // Height = mapHeight, // TileData = tileData // }; //} public void BuildRandom2DMap(int mapWidth, int mapHeight) { var rand = new Random(); //WorldDetails.WordWidthInPixles = mapWidth * (int)WorldDetails.ScaledUnitSize; //WorldDetails.WordHeightInPixles = mapHeight * (int)WorldDetails.ScaledUnitSize; int[] tiles = new int[mapWidth * mapHeight]; Tile[,] tileData = new Tile[mapWidth, mapHeight]; for (int i = 0; i < tileData.GetUpperBound(0); i++) { for (int j = 0; j < tileData.GetUpperBound(1); j++) { if (rand.Next() % 133 != 0) { //tiles[i] = 0; tileData[i, j] = new Tile { IsVisable = false, SpriteTile = 0, Position = new Vector2(i, j) }; } else { //tiles[i] = 40; tileData[i, j] = new Tile { IsVisable = true, SpriteTile = 40, Position = new Vector2(i, j) }; } } } _map = new BasicMap { Data = tiles, Width = mapWidth, Height = mapHeight, TileData = tileData }; }
public static void UpdatePosition(Vector2 pos, BasicMap map) { Vector2 topLeft = new Vector2((pos.X + 0) / WU, (pos.Y + 0) / WU); Vector2 middleLeft = new Vector2((pos.X + 0) / WU, (pos.Y + 4) / WU); Vector2 bottomLeft = new Vector2((pos.X + 0) / WU, (pos.Y + 8) / WU); Vector2 topMiddle = new Vector2((pos.X + 4) / WU, (pos.Y + 0) / WU); Vector2 center = new Vector2((pos.X + 4) / WU, (pos.Y + 4) / WU); Vector2 bottomMiddle = new Vector2((pos.X + 4) / WU, (pos.Y + 8) / WU); Vector2 topRight = new Vector2((pos.X + 8) / WU, (pos.Y + 0) / WU); Vector2 middleRight = new Vector2((pos.X + 8) / WU, (pos.Y + 4) / WU); Vector2 bottomRight = new Vector2((pos.X + 8) / WU, (pos.Y + 8) / WU); //Vector2 nextOriginBlock = new Vector2(pos.X / WU * BasicCamera.Zoom, pos.Y / WU * BasicCamera.Zoom); //if ( // map.TileData[(int)nextOriginBlock.X, (int)nextOriginBlock.Y].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X + 1, (int)nextOriginBlock.Y].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X - 1, (int)nextOriginBlock.Y].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X, (int)nextOriginBlock.Y + 1].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X, (int)nextOriginBlock.Y - 1].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X + 1, (int)nextOriginBlock.Y + 1].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X - 1, (int)nextOriginBlock.Y - 1].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X + 1, (int)nextOriginBlock.Y - 1].SpriteTile == 0 && // map.TileData[(int)nextOriginBlock.X - 1, (int)nextOriginBlock.Y + 1].SpriteTile == 0 // ) //{ // position = pos; //} Vector2 tilePos = map.TileData[(int)topLeft.X, (int)topLeft.Y].Position * WU; Vector2 overlap = pos - tilePos; Console.WriteLine(); Console.WriteLine(tilePos.ToString()); Console.WriteLine(pos.ToString()); Console.WriteLine(overlap); Console.WriteLine(); if (pos.X > position.X) { // Get all collidable tiles to the right of player // check for right side tile collisions bool collidedRight = false; if (map.TileData[(int)topRight.X, (int)topRight.Y].SpriteTile == 40) { // do per pixel check // get tile overlap // set collidedRight //collidedRight = true; } if (!collidedRight) { if (map.TileData[(int)bottomRight.X, (int)bottomRight.Y].SpriteTile == 40) { // do per pixel check // set collidedRight //collidedRight = true; } } if (!collidedRight) { position.X = pos.X; } } // player moving left else if (pos.X < position.X) { // check for right side tile collisions bool collidedLeft = false; if (map.TileData[(int)topLeft.X, (int)topLeft.Y].SpriteTile == 40) { // create new color arrays Color[,] tileOverlap = new Color[(int)overlap.X, (int)overlap.Y]; int i = 0; int j = 0; for (int x = (int)overlap.X; x < WU; x++) { for (int y = (int)overlap.Y; y < WU; y++) { tileOverlap[i, j] = BasicMap.TileColorArray[x, y]; j++; } i++; } ; //collidedLeft = true; } if (!collidedLeft) { if (map.TileData[(int)bottomLeft.X, (int)bottomLeft.Y].SpriteTile == 40) { // do per pixel check // set collidedLeft } } if (!collidedLeft) { position.X = pos.X; } } if (pos.Y > position.Y) { // check for right side tile collisions bool collidedBottom = false; if (map.TileData[(int)bottomRight.X, (int)bottomRight.Y].SpriteTile == 40) { // do per pixel check // get tile overlap // set collidedRight //collidedBottom = true; } if (!collidedBottom) { if (map.TileData[(int)bottomLeft.X, (int)bottomLeft.Y].SpriteTile == 40) { // do per pixel check // set collidedRight //collidedBottom = true; } } if (!collidedBottom) { position.Y = pos.Y; } } // player moving left else if (pos.Y < position.Y) { // check for right side tile collisions bool collidedTop = false; if (map.TileData[(int)topRight.X, (int)topRight.Y].SpriteTile == 40) { // do per pixel check // set collidedLeft //collidedTop = true; } if (!collidedTop) { if (map.TileData[(int)topLeft.X, (int)topLeft.Y].SpriteTile == 40) { // do per pixel check // set collidedLeft //collidedTop = true; } } if (!collidedTop) { position.Y = pos.Y; } } }