public static bool IsHitableBlockHitted(Rect hitboxBounds, string interiorID = null) { ThreadingUtils.assertMainThread(); if (interiorID == Interior.Outside) { // Check if blocks are of type blocking Point topLeft = GeometryUtils.GetChunkPosition(hitboxBounds.Left, hitboxBounds.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); Point bottomRight = GeometryUtils.GetChunkPosition(hitboxBounds.Right, hitboxBounds.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++) { for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++) { Point chunkPos = GeometryUtils.GetChunkPosition(blockX, blockY, WorldGrid.WorldChunkBlockSize.X, WorldGrid.WorldChunkBlockSize.Y); WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkPos.X, chunkPos.Y); int blockType = worldGridChunk.GetBlockType(blockX, blockY); if (IsBlockHitable(blockType)) { return(true); } } } } else { Interior interior = SimulationGame.World.InteriorManager.Get(interiorID); // Check if blocks are of type blocking Point topLeft = GeometryUtils.GetChunkPosition(hitboxBounds.Left, hitboxBounds.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); Point bottomRight = GeometryUtils.GetChunkPosition(hitboxBounds.Right, hitboxBounds.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++) { for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++) { int blockType = interior.GetBlockType(blockX, blockY); if (IsBlockHitable(blockType)) { return(true); } } } } return(false); }
public static bool IsBlockHitable(int blockX, int blockY, string interiorID) { if (interiorID == Interior.Outside) { Point chunkPos = GeometryUtils.GetChunkPosition(blockX, blockY, WorldGrid.WorldChunkBlockSize.X, WorldGrid.WorldChunkBlockSize.Y); WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkPos.X, chunkPos.Y); int blockType = worldGridChunk.GetBlockType(blockX, blockY); return(IsBlockHitable(blockType)); } else { Interior interior = SimulationGame.World.InteriorManager.Get(interiorID); int blockType = interior.GetBlockType(blockX, blockY); return(IsBlockHitable(blockType)); } }
private static void drawOutside(SpriteBatch spriteBatch, GameTime gameTime) { Point topLeft = GeometryUtils.GetChunkPosition(SimulationGame.VisibleArea.Left, SimulationGame.VisibleArea.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); Point bottomRight = GeometryUtils.GetChunkPosition(SimulationGame.VisibleArea.Right, SimulationGame.VisibleArea.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); for (int blockX = topLeft.X; blockX < bottomRight.X; blockX++) { for (int blockY = topLeft.Y; blockY < bottomRight.Y; blockY++) { Point worldGridChunkPosition = GeometryUtils.GetChunkPosition(blockX, blockY, WorldGrid.WorldChunkBlockSize.X, WorldGrid.WorldChunkBlockSize.Y); WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(worldGridChunkPosition.X, worldGridChunkPosition.Y); BlockRenderer.Draw(spriteBatch, blockX * WorldGrid.BlockSize.X, blockY * WorldGrid.BlockSize.Y, worldGridChunk.GetBlockType(blockX, blockY)); } } Point chunkTopLeft = GeometryUtils.GetChunkPosition(SimulationGame.VisibleArea.Left, SimulationGame.VisibleArea.Top, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y); Point chunkBottomRight = GeometryUtils.GetChunkPosition(SimulationGame.VisibleArea.Right, SimulationGame.VisibleArea.Bottom, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y); for (int chunkX = chunkTopLeft.X; chunkX <= chunkBottomRight.X; chunkX++) { for (int chunkY = chunkTopLeft.Y; chunkY <= chunkBottomRight.Y; chunkY++) { WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkX, chunkY); if (SimulationGame.IsDebug) { SimulationGame.PrimitiveDrawer.Rectangle(new Rectangle(chunkX * WorldGrid.WorldChunkPixelSize.X, chunkY * WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y), worldGridChunk.IsPersistent ? Color.Blue : Color.Red); } if (worldGridChunk.AmbientObjects != null) { foreach (AmbientObject ambientObject in worldGridChunk.AmbientObjects) { if (SimulationGame.VisibleArea.Contains(ambientObject.Position) && ambientObject.InteriorID == SimulationGame.Player.InteriorID) { if (ambientObject.CustomRenderer != null) { ambientObject.CustomRenderer.Draw(spriteBatch, gameTime); } else { AmbientObjectRenderer.Draw(spriteBatch, gameTime, ambientObject); } } } } if (worldGridChunk.ContainedObjects != null) { foreach (var containedObject in worldGridChunk.ContainedObjects) { if (SimulationGame.VisibleArea.Contains(containedObject.Position) && containedObject.InteriorID == SimulationGame.Player.InteriorID) { if (containedObject.CustomRenderer != null) { containedObject.CustomRenderer.Draw(spriteBatch, gameTime); } else { if (containedObject is LivingEntity) { LivingEntityRenderer.Draw(spriteBatch, gameTime, (LivingEntity)containedObject); } else if (containedObject is AmbientHitableObject) { AmbientHitableObjectRenderer.Draw(spriteBatch, gameTime, (AmbientHitableObject)containedObject); } } } } } if (worldGridChunk.ContainedEffects != null) { foreach (var effectItem in worldGridChunk.ContainedEffects) { EffectRenderer.Draw(spriteBatch, gameTime, effectItem.Value); } } if (SimulationGame.IsDebug && worldGridChunk.WorldLinks != null) { foreach (var worldLinkItem in worldGridChunk.WorldLinks) { if (SimulationGame.VisibleArea.Contains(new Point(worldLinkItem.Value.FromBlock.X * WorldGrid.BlockSize.X, worldLinkItem.Value.FromBlock.Y * WorldGrid.BlockSize.Y))) { SimulationGame.PrimitiveDrawer.Rectangle(new Rectangle(worldLinkItem.Value.FromBlock.X * WorldGrid.BlockSize.X, worldLinkItem.Value.FromBlock.Y * WorldGrid.BlockSize.Y, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y), Color.DarkBlue); } } } } } }
public static bool IsRectBlockedAccurate(HitableObject origin, Rect rect) { ThreadingUtils.assertMainThread(); if (origin.InteriorID == Interior.Outside) { // Check if blocks are of type blocking Point topLeft = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); Point bottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++) { for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++) { Point chunkPos = GeometryUtils.GetChunkPosition(blockX, blockY, WorldGrid.WorldChunkBlockSize.X, WorldGrid.WorldChunkBlockSize.Y); WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkPos.X, chunkPos.Y); int blockType = worldGridChunk.GetBlockType(blockX, blockY); if (IsBlockBlocking(blockType)) { return(true); } } } // Check collision with interactive && contained objects Point chunkTopLeft = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y); Point chunkBottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y); for (int chunkX = chunkTopLeft.X; chunkX <= chunkBottomRight.X; chunkX++) { for (int chunkY = chunkTopLeft.Y; chunkY <= chunkBottomRight.Y; chunkY++) { WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkX, chunkY); if (worldGridChunk.OverlappingObjects != null) { foreach (HitableObject hitableObject in worldGridChunk.OverlappingObjects) { if (hitableObject.IsBlocking() && hitableObject != origin && hitableObject.BlockingBounds.Intersects(rect)) { return(true); } } } if (worldGridChunk.ContainedObjects != null) { foreach (var hitableObject in worldGridChunk.ContainedObjects) { if (hitableObject.IsBlocking() && hitableObject != origin && hitableObject.BlockingBounds.Intersects(rect)) { return(true); } } } } } return(false); } else { Interior interior = SimulationGame.World.InteriorManager.Get(origin.InteriorID); // Check if blocks are of type blocking Point topLeft = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); Point bottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y); for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++) { for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++) { if (blockX < 0 || blockX >= interior.Dimensions.X) { return(true); } if (blockY < 0 || blockY >= interior.Dimensions.Y) { return(true); } int blockType = interior.GetBlockType(blockX, blockY); if (IsBlockBlocking(blockType)) { return(true); } } } foreach (var hitableObject in interior.ContainedObjects) { if (hitableObject.IsBlocking() && hitableObject != origin && hitableObject.BlockingBounds.Intersects(rect)) { return(true); } } return(false); } }