/* public void updateLighting(ref Tile[, ,] tiles, float addedDark = -1) { if (this.tileType == TileType.Empty || this.tileType == TileType.Tree) lightValue = 1.0f; if (lightingHits > 4) return; if (addedDark != -1) this.lightValue += addedDark; lightingHits++; // if (tileY > 0 && this.tileType == TileType.Empty) // tiles[1, tileY - 1, tileX].updateLighting(ref tiles, lightValue / divierAmount); if (tileY+1 < totalHeight) tiles[1, tileY + 1, tileX].updateLighting(ref tiles, lightValue / divierAmount); // if (tileX > 0) // tiles[1, tileY, tileX - 1].updateLighting(ref tiles, lightValue / divierAmount); if (tileX+1 < totalWidth) tiles[1, tileY, tileX + 1].updateLighting(ref tiles, lightValue / divierAmount); // if (tileX > 0 && tileY > 0) // tiles[1, tileY - 1, tileX - 1].updateLighting(ref tiles, lightValue / divierAmount); if (tileX+1 < totalWidth && tileY+1 < totalHeight) tiles[1, tileY + 1, tileX + 1].updateLighting(ref tiles, lightValue / divierAmount); } */ public void updateTile(ref Tile[,,] tiles, float elapsedTime, Vector2 camPos) { if (checkDestructable() == false) return; tileX = getTileCoords("X"); tileY = getTileCoords("Y"); if (this.tileType != TileType.Sand) return; if ((elapsedTime - lastUpdate) < this.updateTime) { return; } lastUpdate = elapsedTime; if (moveNext) { tiles[1, tileY + 1, tileX].tileType = TileType.Sand; tiles[1, tileY, tileX].tileType = TileType.Empty; calculateOrientation(ref tiles); moveNext = false; } if (tileY <= 37) { if (tiles[1, tileY + 1, tileX].tileType == TileType.Empty) { moveNext = true; } else moveNext = false; } }
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 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 calculateOrientation(ref Tile[,,]tiles) { tileX = getTileCoords("X"); tileY = getTileCoords("Y"); int tmpY = grabY(-1); int tmpY2 = grabY(1); int tmpX = grabX(-1); int tmpX2 = grabX(1); if (tmpY == -1 || tmpY2 == -1) return; if (tmpX == -1 || tmpX2 == -1) return; if (tileX == 0) tileOrientation = TileOrientation.Single; if (tiles[1, grabY(-1), tileX].tileType != tileType) topMost = true; else topMost = false; if (tiles[1, grabY(1), tileX].tileType != tileType) bottomMost = true; else bottomMost = false; if (tiles[1, tileY, grabX(1)].tileType != tileType) rightMost = true; else rightMost = false; if (tileX > 0 && tiles[1, tileY, grabX(-1)].tileType != tileType) leftMost = true; else leftMost = false; if (topMost && !rightMost && !leftMost && !bottomMost) tileOrientation = TileOrientation.Top; else if (topMost && rightMost && !leftMost && !bottomMost) tileOrientation = TileOrientation.TopRight; else if (!topMost && rightMost && !leftMost && !bottomMost) tileOrientation = TileOrientation.Right; else if (!topMost && rightMost && !leftMost && bottomMost) tileOrientation = TileOrientation.BottomRight; else if (!topMost && !rightMost && !leftMost && bottomMost) tileOrientation = TileOrientation.Bottom; else if (!topMost && !rightMost && leftMost && bottomMost) tileOrientation = TileOrientation.BottomLeft; else if (!topMost && !rightMost && leftMost && !bottomMost) tileOrientation = TileOrientation.Left; else if (topMost && !rightMost && leftMost && !bottomMost) tileOrientation = TileOrientation.TopLeft; else if (topMost && !rightMost && !leftMost && bottomMost) tileOrientation = TileOrientation.TopBottom; else if (!topMost && rightMost && leftMost && !bottomMost) tileOrientation = TileOrientation.LeftRight; else if (!topMost && !rightMost && !leftMost && !bottomMost) tileOrientation = TileOrientation.Single; else if (topMost && !rightMost && leftMost && bottomMost) tileOrientation = TileOrientation.TopBottomLeft; else if (topMost && rightMost && !leftMost && bottomMost) tileOrientation = TileOrientation.TopBottomRight; else if (topMost && rightMost && leftMost && !bottomMost) tileOrientation = TileOrientation.TopLeftRight; else if (!topMost && rightMost && leftMost && bottomMost) tileOrientation = TileOrientation.BottomLeftRight; else tileOrientation = TileOrientation.Regular; }
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)); }
public void Update(ref Tile[,,] tiles, ref WaterDrop [,] drops, float elapsedTime) { if (this.volume <= 0.0f || (elapsedTime - lastUpdate) < updateTime) { return; } lastUpdate = elapsedTime; if (tiles[1, tileY, tileX].tileType == TileType.Dirt) { this.volume = 0.0f; return; } if (tiles[1, tileY, tileX].tileType == TileType.Sand) waterSand = true; else waterSand = false; if (belowClear() && tiles[1, tileY + 1, tileX].tileType == TileType.Dirt || tiles[1, tileY + 1, tileX].tileType == TileType.Decoration || this.volume >= 1 && drops[grabY(1), tileX].volume >= 16) { if (this.volume > drops[tileY, grabX(-1)].volume && this.volume > drops[tileY, grabX(1)].volume) // Going Left and Right { if (tileX == grabX(-1) || tileX == grabX(1)) return; // If Self, return if (this.volume < 0.01f) { this.volume = 0.0f; return; } float totalVol = drops[tileY, grabX(-1)].volume + this.volume + drops[tileY, grabX(1)].volume; bool tmp = false; int dividerAmount = 1; if (tiles[1, tileY, grabX(-1)].tileType != TileType.Dirt) dividerAmount++; if (tiles[1, tileY, grabX(1)].tileType != TileType.Dirt) dividerAmount++; if (tiles[1, tileY, grabX(-1)].tileType != TileType.Dirt) { drops[tileY, grabX(-1)].volume = (float)Math.Round(totalVol / dividerAmount, 2); tmp = true; } if (tiles[1, tileY, grabX(1)].tileType != TileType.Dirt) { drops[tileY, grabX(1)].volume = (float)Math.Round(totalVol / dividerAmount, 2); tmp = true; } if (tmp) this.volume = (float)Math.Round(totalVol / dividerAmount, 2); } else if (this.volume > drops[tileY, grabX(-1)].volume) // Going Left { if (tileX == grabX(-1)) return; if (this.volume < 0.01f) { this.volume = 0.0f; return; } if (tiles[1, tileY, grabX(-1)].tileType == TileType.Dirt) return; float totalVol = drops[tileY, grabX(-1)].volume + this.volume; totalVol = (float)Math.Round(totalVol / 2, 2); drops[tileY, grabX(-1)].volume = totalVol; this.volume = totalVol; } else if (this.volume > drops[tileY, grabX(1)].volume) // Going Right { if (tileX == grabX(1)) return; if (this.volume < 0.01f) { this.volume = 0.0f; return; } if (tiles[1, tileY, grabX(1)].tileType == TileType.Dirt) return; float totalVol = drops[tileY, grabX(1)].volume + this.volume; totalVol = (float)Math.Round(totalVol / 2,2); drops[tileY, grabX(1)].volume = totalVol; this.volume = totalVol; } } // Check left empty AND left down is empty (then fall) if (belowClear() && tiles[1, tileY + 1, tileX].tileType != TileType.Dirt) { // this.position.Y += 16; // this.tileY = (int)this.position.Y / 16; // if the current volume is more than zero and bottom drop is empty, pour all of water into bottom drop if (this.volume > 0.0 && drops[grabY(1), tileX].volume <= 0.0) { drops[grabY(1), tileX].volume = this.volume; this.volume = 0; } if (drops[grabY(1), tileX].volume < 16) { // if the drop below isnt full, drip into it // Space left to pour into float deltaPour = (16 - drops[grabY(1), tileX].volume); // Delta between deltaPour and vol float amountRemaining = (deltaPour - this.volume); drops[grabY(1), tileX].volume += this.volume; if (amountRemaining > 16) this.volume = (amountRemaining - 16); else this.volume = 0.0f; } // If bottom, left and right drops are full, push water into one above. /* if (this.volume >= 16.0 && drops[grabY(1), tileX].volume >= 16.0f && drops[grabY(0), grabX(-1)].volume >= 16.0f && drops[grabY(0), grabX(1)].volume >= 16.0f && tiles[tileY - 1, tileX].tileType == TileType.Empty) { float deltaPour = (16 - drops[grabY(-1), tileX].volume); // Delta between deltaPour and vol float amountRemaining = (deltaPour - this.volume); drops[grabY(-1), tileX].volume += this.volume; if (amountRemaining > 16) this.volume = (amountRemaining - 16); else this.volume = 0.0f; }*/ } if (this.volume > 16.0f) this.volume = 16.0f; // if (this.volume >= 16.0f && tiles[grabY(-1), tileX].tileType == TileType.Empty) // topMost = true; if (grabY(-1) > 0) { if (drops[grabY(-1), tileX].volume <= 0.0f) topMost = true; else topMost = false; } }
public void updateWater(Camera cam, ref Tile[,,] tiles, float totalElapsed) { if (pauseWater) return; for (int y = cam.getCameraY(0); y < cam.getCameraY(screenHeight); y++) { for (int x = cam.getCameraX(0); x < cam.getCameraX(screenWidth); x++) { if (x < levelWidth - 1 && x > 0 && y <= levelHeight - 1 && y > 0) { droplets[y, x].Update(ref tiles, ref droplets, totalElapsed); } } } }
/* 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); }
private void HandleCollisions(ref Tile[,,] tiles) { // Get the player's bounding rectangle and find neighboring tiles. Rectangle bounds = getBounds(); int leftTile = (int)Math.Floor((float)bounds.Left / 16); int rightTile = (int)Math.Ceiling(((float)bounds.Right / 16)) - 1; int topTile = (int)Math.Floor((float)bounds.Top / 16); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / 16)) - 1; // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { if (x < 0 || x > totalWidth || y < 0 || y > totalHeight) continue; // If this tile is collidable, TileCollision collision = tiles[1, y, x].tileCollision; if (collision != TileCollision.Passable) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = tiles[1,y, x].getBounds(); Vector2 depth = GetIntersectionDepth(bounds, tileBounds); if (depth != Vector2.Zero) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); // Resolve the collision along the shallow axis. if (absDepthY <= absDepthX) { // If we crossed the top of a tile, we are on the ground. if (previousBottom <= tileBounds.Top) isOnGround = true; // Ignore platforms, unless we are on the ground. if (collision == TileCollision.Impassable || vertState == verticalState.Ground) { // Resolve the collision along the Y axis. position = new Vector2(position.X, position.Y + depth.Y); // Perform further collisions with the new bounds. bounds = getBounds(); } } else if (collision == TileCollision.Impassable) // Ignore platforms. { // Resolve the collision along the X axis. position = new Vector2(position.X + depth.X, position.Y); // Perform further collisions with the new bounds. bounds = getBounds(); } } } } } // Save the new bounds bottom. previousBottom = bounds.Bottom; }
public void Update(ref Tile[,,] tiles, KeyboardState keyboard, GameTime gt, Vector2 camPos) { cameraPosition = camPos; prevPos = position; timeGone = (float)gt.ElapsedGameTime.TotalSeconds; tileX = getTileCoords("X"); tileY = getTileCoords("Y"); movement = 0; if (keyboard.IsKeyDown(Keys.Space) || keyboard.IsKeyDown(Keys.W)) isJumping = true; else isJumping = false; if (keyboard.IsKeyDown(Keys.Right) || keyboard.IsKeyDown(Keys.D)) // Move Right { if ( position.X < totalWidth * 16) movement = 1.0f; facingLeft = false; } else if (keyboard.IsKeyDown(Keys.Left) || keyboard.IsKeyDown(Keys.A)) // Skid Left { if (position.X > 0) movement = -1.0f; facingLeft = true; } velocity.X += movement * MoveAcceleration * timeGone; velocity.Y = MathHelper.Clamp(velocity.Y + GravityAcceleration * timeGone, -MaxFallSpeed, MaxFallSpeed); velocity.Y = DoJump(velocity.Y, gt); if (isOnGround) velocity.X *= GroundDragFactor; else velocity.X *= AirDragFactor; velocity.X = MathHelper.Clamp(velocity.X, -MaxMoveSpeed, MaxMoveSpeed); position += velocity * timeGone; position = new Vector2((float)Math.Round(position.X), (float)Math.Round(position.Y)); HandleCollisions(ref tiles); if (position.X == prevPos.X) velocity.X = 0; if (position.Y == prevPos.Y) velocity.Y = 0; movement = 0.0f; isJumping = false; }