public void calculateTiles(Camera cam, Player player, bool initialLoad = false) { for (int y = 0; y < currentLevel.Height; y++) { for (int x = 0; x < currentLevel.Width; x++) { tileLayers[1, y, x].calculateOrientation(ref tileLayers); } } tileLayers[1, 0, 0].updateLight(ref tileLayers, player, initialLoad); }
public void updateLight(ref Tile[,,] tiles, Player player, bool initialLoad = false) { resetLights(ref tiles, player); if (initialLoad) { for (int y = 0; y < totalHeight; y++) { for (int x = 0; x < totalWidth; x++) { if (x >= totalWidth || x < 0 || y >= totalHeight || y < 0) continue; if (tiles[1, y, x].tileType == TileType.Empty || tiles[1, y, x].tileType == TileType.Decoration || tiles[1, y, x].tileType == TileType.Tree) { tiles[1, y, x].lightValue = 1; tiles[1, y, x].lightRad = 5; lightRadius(ref tiles, x, y, player); } } } } else for (int x = (int)(player.position.X / size.X) - 50; x < (int)(player.position.X / size.X) + 50; x++) { for (int y = (int)(player.position.Y / size.Y) - 50; y < (int)(player.position.Y / size.Y) + 50; y++) { if (x >= totalWidth || x < 0 || y >= totalHeight || y < 0) continue; if (tiles[1, y, x].tileType == TileType.Empty || tiles[1, y, x].tileType == TileType.Decoration || tiles[1, y, x].tileType == TileType.Tree) { tiles[1, y, x].lightValue = 1; tiles[1, y, x].lightRad = 5; lightRadius(ref tiles, x, y, player); } } } }
public void resetLights(ref Tile[,,] tiles, Player player) { for (int x = (int)(player.position.X / size.X) - 50; x < (int)(player.position.X / size.X) + 50; x++) for (int y = (int)(player.position.Y / size.Y) - 50; y < (int)(player.position.Y / size.Y) + 50; y++) { if (x >= totalWidth|| x < 0 || y >= totalHeight || y < 0) continue; //if (tiles[1, y, x].tileType == TileType.Torch) // continue; tiles[1, y, x].lightValue = 0.1f; } player.lightValue = 0; }
public void lightRadius(ref Tile[,,] tiles, int tileX, int tileY, Player player) { int radius = tiles[1, tileY, tileX].lightRad; float masterLight = tiles[1, tileY, tileX].lightValue; for (int x = tileX - radius; x < tileX + radius + 1; x++) { for (int y = tileY - radius; y < tileY + radius + 1; y++) { //Make sure we don't exceed the boundaries of the world array. if (x < totalWidth - 1 && x > 0 && y <= totalHeight -1 && y > 0) { if (x == tileX && y == tileY || tiles[1,y,x].tileType == TileType.Decoration) // || tiles[1, y,x].tileType == TileType.Torch) continue; //Checks that we're still within the radius of the original tile. if ((new Vector2(tileX, tileY) - new Vector2(x, y)).Length() <= radius) { float tmpLightLevel = 0; //If the tile isn't solid. // if (tiles[1, y, x].tileType == TileType.Empty) // { // tmpLightLevel = (short)(tiles[1, y, x].lightValue - ((new Vector2(tileX, tileY) - new Vector2(x, y)).Length() * 10)); // else //else if the tile is solid. tmpLightLevel = Math.Abs(masterLight - ((new Vector2(tileX, tileY) - new Vector2(x, y)).Length()) / 5); // } if (tiles[1, y, x].lightValue < tmpLightLevel) tiles[1, y, x].lightValue = tmpLightLevel; if (tiles[1, y, x].lightValue < 0.0) tiles[1, y, x].lightValue = 0.1f; } } } } //Apply light to the player if he is within the light radius. if ((new Vector2(tileX, tileY) - (player.position / size.X)).Length() < radius) if (player.lightValue < tiles[1, tileY, tileX].lightValue / (int)((new Vector2(tileX, tileY) - (player.position / size.X)).Length() + 1)) player.lightValue = (ushort)(tiles[1, tileY, tileX].lightValue / (int)((new Vector2(tileX, tileY) - (player.position / size.X)).Length() + 1)); }
protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); level.loadTextures(Content); player = new Player(screenRectangle, level.levelHeight, level.levelWidth); player.texture = Content.Load<Texture2D>("player"); level.setUpTile("Levels/levelOne.png", player, camera); level.dropManager = new DropManager(level.levelTileWidth, level.levelTileHeight, screenWidth, screenHeight); level.dropManager.setupDrops(); level.dropManager.loadTextures(Content); layers[0] = new Layer(Content, "backgrounds/clouds", new Vector2(0.1f,0.01f), new Vector2(2000, 1101), true, 60); }
/* public void createLighting(Camera cam) { for (int y = 0; y < currentLevel.Height; y++) { for (int x = 0; x < currentLevel.Width; x++) { if (x < 0 || y < 0 || x >= levelTileWidth || y >= levelTileHeight) continue; if (tileLayers[1, y, x] != null && tileLayers[1, y, x].render) { tileLayers[1, y, x].calculateOrientation(ref tileLayers); } } } }*/ /* public void resetLightingCount(Camera cam) { for (int y = 0; y < currentLevel.Height; y++) { for (int x = 0; x < currentLevel.Width; x++) { if (x < 0 || y < 0 || x >= levelTileWidth || y >= levelTileHeight) continue; if (tileLayers[1, y, x] != null && tileLayers[1, y, x].render) { tileLayers[1, y, x].lightingHits = 0; tileLayers[1, y, x].lightValue = 0; } } } } */ public void setUpTile(string path, Player player, Camera cam) { Color[] levelData = new Color[currentLevel.Height * currentLevel.Width]; levelWidth = currentLevel.Width * tileSize; levelHeight = currentLevel.Height * tileSize; levelTileWidth = currentLevel.Width; levelTileHeight = currentLevel.Height; player.totalWidth = levelTileWidth; player.totalHeight = levelTileHeight; tileLayers = new Tile[2, currentLevel.Height, currentLevel.Width]; currentLevel.GetData<Color>(levelData); rand = new Random(); int runningTotal = 0; for (int y = 0; y < currentLevel.Height; y++) { for (int x = 0; x < currentLevel.Width; x++) { if (runningTotal >= levelData.Length) break; if (levelData[runningTotal] == Color.Lime) tileLayers[1, y, x] = new Tile(TileType.Decoration, x, y, levelTileWidth, levelTileHeight, rand, 0); else if (levelData[runningTotal] == Color.Blue) tileLayers[1, y, x] = new Tile(TileType.Dirt, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Impassable); else if (levelData[runningTotal] == Color.White) tileLayers[1, y, x] = new Tile(TileType.Empty, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Passable); else if (levelData[runningTotal] == Color.Yellow) tileLayers[1, y, x] = new Tile(TileType.Sand, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Impassable); else if (levelData[runningTotal] == new Color(204,204,204)) tileLayers[1, y, x] = new Tile(TileType.Decoration, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Passable, 1); else if (levelData[runningTotal] == new Color(53, 34, 18)) // Tree Foot tileLayers[1, y, x] = new Tile(TileType.Tree, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Passable, 0); else if (levelData[runningTotal] == new Color(109, 69, 35)) // Tree Truck tileLayers[1, y, x] = new Tile(TileType.Tree, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Passable, 1); else if (levelData[runningTotal] == new Color(24, 100, 12)) // Tree Top tileLayers[1, y, x] = new Tile(TileType.Tree, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Passable, 2); else if (levelData[runningTotal] == new Color(119, 119, 119)) // Metal tileLayers[1, y, x] = new Tile(TileType.Metal, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Impassable); else if (levelData[runningTotal] == Color.Black) // Indestructable tileLayers[1, y, x] = new Tile(TileType.Indestructable, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Impassable); else if (levelData[runningTotal] == new Color(255, 0, 0)) // Player start { tileLayers[1, y, x] = new Tile(TileType.Empty, x, y, levelTileWidth, levelTileHeight, rand, TileCollision.Passable); player.setPosition(x * tileSize, y * tileSize); } runningTotal++; } } calculateTiles(cam, player, true); }
public void setTileHealth(Camera camera, Player player) { int mouse_x = Mouse.GetState().X - 4; int mouse_y = Mouse.GetState().Y - 4; TileType tmpBlock; Vector2 delta; if (mouse_x > 0 && mouse_x < screenWidth && mouse_y > 0 && mouse_y < screenHeight) { int mouseX = (mouse_x + (int)camera.position.X) / 16; int mouseY = (mouse_y + (int)camera.position.Y) / 16; if (mouseY > levelHeight) return; if (tileLayers[1, grabY(mouseY, -1), mouseX].tileType == TileType.Tree) { return; } tmpBlock = tileLayers[1, mouseY, mouseX].tileType; if (tileLayers[1,mouseY, mouseX].doDamage() == true) { calculateTiles(camera, player); delta = player.getMiddle() - tileLayers[1, mouseY, mouseX].getMiddle(); if (Math.Abs(delta.X) <= 32 && Math.Abs(delta.Y) <= 32) // Player in range collect block { if (tmpBlock == TileType.Dirt) player.addInventory(1, Player.InventoryType.DirtTile); else if (tmpBlock == TileType.Metal) player.addInventory(1, Player.InventoryType.MetalTile); } } } }
public bool setTile(TileType tileIn, Camera camera, Player player, int decoration = -1) { int mouse_x = Mouse.GetState().X - 4 + (int)camera.position.X; int mouse_y = Mouse.GetState().Y - 4 + (int)camera.position.Y; if (mouse_x > 0 && mouse_x < levelWidth && mouse_y > 0 && mouse_y < levelHeight) { if (tileLayers[1, mouse_y / 16, mouse_x / 16].tileType == TileType.Empty) { tileLayers[1, mouse_y / 16, mouse_x / 16].tileType = tileIn; tileLayers[1, mouse_y / 16, mouse_x / 16].decorationValue = decoration; if (tileIn == TileType.Decoration || tileIn == TileType.Empty || tileIn == TileType.Tree) tileLayers[1, mouse_y / 16, mouse_x / 16].tileCollision = TileCollision.Passable; else tileLayers[1, mouse_y / 16, mouse_x / 16].tileCollision = TileCollision.Impassable; calculateTiles(camera, player); return true; } else return false; } else return false; }