예제 #1
0
        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);
        }
예제 #2
0
        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));
            }
        }
예제 #3
0
        private static void drawInterior(SpriteBatch spriteBatch, GameTime gameTime, Interior interior)
        {
            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);

            topLeft.X = Math.Max(0, topLeft.X);
            topLeft.Y = Math.Max(0, topLeft.Y);

            bottomRight.X = Math.Min(interior.Dimensions.X, bottomRight.X);
            bottomRight.Y = Math.Min(interior.Dimensions.Y, bottomRight.Y);

            for (int blockX = topLeft.X; blockX < bottomRight.X; blockX++)
            {
                for (int blockY = topLeft.Y; blockY < bottomRight.Y; blockY++)
                {
                    BlockRenderer.Draw(spriteBatch, blockX * WorldGrid.BlockSize.X, blockY * WorldGrid.BlockSize.Y, interior.GetBlockType(blockX, blockY));
                }
            }

            if (interior.AmbientObjects != null)
            {
                foreach (AmbientObject ambientObject in interior.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 (interior.ContainedObjects != null)
            {
                foreach (var containedObject in interior.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 (interior.ContainedEffects != null)
            {
                foreach (var effectItem in interior.ContainedEffects)
                {
                    EffectRenderer.Draw(spriteBatch, gameTime, effectItem.Value);
                }
            }

            if (SimulationGame.IsDebug && interior.WorldLinks != null)
            {
                foreach (var worldLinkItem in interior.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);
                    }
                }
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        public static bool IsRectBlockedFast(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++)
                    {
                        if (!SimulationGame.World.WalkableGrid.IsBlockWalkable(blockX, blockY))
                        {
                            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);
            }
        }