示例#1
0
        public void GetObjectsIntersecting <TObject>(BoundingFrustum frustum, HashSet <TObject> set, CollisionType queryType) where TObject : IBoundedObject
        {
            List <SpatialHash <IBoundedObject> > hashes = new List <SpatialHash <IBoundedObject> >();

            switch ((int)queryType)
            {
            case (int)CollisionType.Static:
            case (int)CollisionType.Dynamic:
                hashes.Add(Hashes[queryType]);
                break;

            case ((int)CollisionType.Static | (int)CollisionType.Dynamic):
                hashes.Add(Hashes[CollisionType.Static]);
                hashes.Add(Hashes[CollisionType.Dynamic]);
                break;
            }

            BoundingBox frustumBox = MathFunctions.GetBoundingBox(frustum.GetCorners());

            foreach (var obj in
                     from hash
                     in hashes
                     from pair
                     in hash.HashMap
                     where pair.Value != null && frustumBox.Contains(pair.Key.ToVector3()) == ContainmentType.Contains
                     from obj in pair.Value
                     where obj is TObject && !set.Contains((TObject)obj) && obj.GetBoundingBox().Intersects(frustum)
                     select obj)
            {
                set.Add((TObject)obj);
            }
        }
示例#2
0
        public void RenderShadowmap(Shader effect,
                                    GraphicsDevice graphicsDevice,
                                    ShadowRenderer shadowRenderer,
                                    Matrix worldMatrix,
                                    Texture2D tilemap)
        {
            Vector3[] corners    = new Vector3[8];
            Camera    tempCamera = new Camera(World, camera.Target, camera.Position, camera.FOV, camera.AspectRatio, camera.NearPlane, 30);

            tempCamera.GetFrustrum().GetCorners(corners);
            BoundingBox cameraBox = MathFunctions.GetBoundingBox(corners);

            cameraBox          = cameraBox.Expand(1.0f);
            effect.World       = worldMatrix;
            effect.MainTexture = tilemap;
            shadowRenderer.SetupViewProj(cameraBox);
            shadowRenderer.PrepareEffect(effect, false);
            shadowRenderer.BindShadowmapEffect(effect);
            shadowRenderer.BindShadowmap(graphicsDevice);

            List <VoxelChunk> renderListCopy = RenderList.ToArray().ToList();

            foreach (VoxelChunk chunk in renderListCopy)
            {
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    chunk.Render(Graphics);
                }
            }
            shadowRenderer.UnbindShadowmap(graphicsDevice);
            effect.CurrentTechnique        = effect.Techniques[Shader.Technique.Textured];
            effect.SelfIlluminationEnabled = false;
        }
示例#3
0
        public void GetChunksIntersecting(BoundingFrustum frustum, HashSet<VoxelChunk> chunks)
        {
            chunks.Clear();
            BoundingBox frustumBox = MathFunctions.GetBoundingBox(frustum.GetCorners());
            GetChunksIntersecting(frustumBox, chunks);

            chunks.RemoveWhere(chunk => frustum.Contains(chunk.GetBoundingBox()) == ContainmentType.Disjoint);
        }
示例#4
0
 public IEnumerable <VoxelChunk> EnumerateChunksInBounds(BoundingFrustum Frustum)
 {
     return(EnumerateChunksInBounds(MathFunctions.GetBoundingBox(Frustum.GetCorners()))
            .Where(c =>
     {
         var min = new GlobalVoxelCoordinate(c.ID, new LocalVoxelCoordinate(0, 0, 0));
         var box = new BoundingBox(min.ToVector3(), min.ToVector3() + new Vector3(VoxelConstants.ChunkSizeX, VoxelConstants.ChunkSizeY, VoxelConstants.ChunkSizeZ));
         var r = Frustum.Contains(box) != ContainmentType.Disjoint;
         return r;
     }));
 }
示例#5
0
        public void UpdateBounds()
        {
            var boundingBoxes = chunkData.GetChunkEnumerator().Select(c => c.GetBoundingBox());

            Bounds = MathFunctions.GetBoundingBox(boundingBoxes);
        }
示例#6
0
        private void RecalculateBounds()
        {
            List <BoundingBox> boxes = ChunkData.GetChunkEnumerator().Select(c => c.GetBoundingBox()).ToList();

            Bounds = MathFunctions.GetBoundingBox(boxes);
        }
示例#7
0
        public bool Verify(
            List <VoxelHandle> Voxels,
            Faction Faction,
            WorldManager World)
        {
            if (Voxels.Count == 0)
            {
                return(false);
            }

            if (Faction.GetRooms().Where(room => room.RoomData.Name == this.Name).Count() + 1 > MaxNumRooms)
            {
                World.ShowToolPopup(String.Format("We can only build {0} {1}. Destroy the existing to build a new one.", MaxNumRooms, Name));
                return(false);
            }

            // Todo: Lift into helper function that uses better algorithm.
            List <BoundingBox> boxes = Voxels.Select(voxel => voxel.GetBoundingBox()).ToList();
            BoundingBox        box   = MathFunctions.GetBoundingBox(boxes);

            Vector3 extents = box.Max - box.Min;

            float maxExtents = Math.Max(extents.X, extents.Z);
            float minExtents = Math.Min(extents.X, extents.Z);

            if (maxExtents < MinimumSideLength || minExtents < MinimumSideWidth)
            {
                World.ShowToolPopup("Room is too small (minimum is " + MinimumSideLength + " x " + MinimumSideWidth + ")!");
                return(false);
            }

            int  height   = Voxels[0].Coordinate.Y;
            bool allEmpty = true;

            foreach (var voxel in Voxels)
            {
                if (voxel.IsEmpty)
                {
                    continue;
                }

                var above = VoxelHelpers.GetVoxelAbove(voxel);
                allEmpty &= (above.IsValid && above.IsEmpty);

                if (voxel.Type.IsInvincible)
                {
                    continue;
                }

                if (height != (int)voxel.Coordinate.Y && !CanBuildOnMultipleLevels)
                {
                    World.ShowToolPopup("Room must be on flat ground!");
                    return(false);
                }

                if (MustBeBuiltOnSoil && !voxel.Type.IsSoil)
                {
                    World.ShowToolPopup("Room must be built on soil!");
                    return(false);
                }

                if (!CanBuildAboveGround && voxel.Sunlight)
                {
                    World.ShowToolPopup("Room can't be built aboveground!");
                    return(false);
                }

                if (!CanBuildBelowGround && !voxel.Sunlight)
                {
                    World.ShowToolPopup("Room can't be built belowground!");
                    return(false);
                }
            }

            if (!allEmpty)
            {
                World.ShowToolPopup("Room must be built in free space.");
                return(false);
            }

            return(true);
        }
示例#8
0
        public static List <Body> GenerateRoomComponentsTemplate(RoomData roomData, List <Voxel> voxels, ComponentManager componentManager,
                                                                 Microsoft.Xna.Framework.Content.ContentManager content, GraphicsDevice graphics)
        {
            List <Body> components = new List <Body>();

            RoomTile[,] currentTiles = RoomTemplate.CreateFromRoom(voxels, componentManager.World.ChunkManager);
            float[,] rotations       = new float[currentTiles.GetLength(0), currentTiles.GetLength(1)];
            foreach (RoomTemplate myTemp in roomData.Templates)
            {
                RoomTemplate template = new RoomTemplate(myTemp)
                {
                    Rotation = 0
                };
                for (int r = -2; r < currentTiles.GetLength(0) + 1; r++)
                {
                    for (int c = -2; c < currentTiles.GetLength(1) + 1; c++)
                    {
                        for (int rotation = 0; rotation < 5; rotation++)
                        {
                            template.PlaceTemplate(ref currentTiles, ref rotations, r, c);
                            template.RotateClockwise(1);
                        }
                    }
                }
            }

            BoundingBox box        = MathFunctions.GetBoundingBox(voxels);
            int         thingsMade = 0;

            for (int r = 0; r < currentTiles.GetLength(0); r++)
            {
                for (int c = 0; c < currentTiles.GetLength(1); c++)
                {
                    RoomTile tile             = currentTiles[r, c];
                    Body     createdComponent = null;
                    Vector3  noise            =
                        VertexNoise.GetNoiseVectorFromRepeatingTexture(box.Min +
                                                                       new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1));
                    switch (tile)
                    {
                    case RoomTile.Barrel:
                        createdComponent = EntityFactory.CreateEntity <Body>("Barrel", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Wheat:
                        createdComponent = EntityFactory.CreateEntity <Body>("Wheat", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Mushroom:
                        createdComponent = EntityFactory.CreateEntity <Body>("Mushroom", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Table:
                        createdComponent = EntityFactory.CreateEntity <Body>("Table", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Stove:
                        createdComponent = EntityFactory.CreateEntity <Body>("Stove", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.KitchenTable:
                        createdComponent = EntityFactory.CreateEntity <Body>("Kitchen Table", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Lamp:
                        createdComponent = EntityFactory.CreateEntity <Body>("Lamp", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Flag:
                        createdComponent = EntityFactory.CreateEntity <Body>("Flag", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Chair:
                        createdComponent = EntityFactory.CreateEntity <Body>("Chair", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Books:
                        createdComponent = EntityFactory.CreateEntity <Body>(MathFunctions.RandEvent(0.5f) ? "Books" : "Potions", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Anvil:
                        createdComponent = EntityFactory.CreateEntity <Body>("Anvil", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Forge:
                        createdComponent = EntityFactory.CreateEntity <Body>("Forge", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Target:
                        createdComponent = EntityFactory.CreateEntity <Body>("Target", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Strawman:
                        createdComponent = EntityFactory.CreateEntity <Body>("Strawman", box.Min + new Vector3(r + 0.5f - 1, 1.5f, c + 0.5f - 1) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.BookShelf:
                        createdComponent = EntityFactory.CreateEntity <Body>("Bookshelf", box.Min + new Vector3(r - 1 + 0.5f, 1.5f, c - 1 + 0.5f) + noise);
                        thingsMade++;
                        break;

                    case RoomTile.Pillow:

                        for (int dx = -1; dx < 2; dx++)
                        {
                            for (int dy = -1; dy < 2; dy++)
                            {
                                if (Math.Abs(dx) + Math.Abs(dy) != 1 || r + dx < 0 || r + dx >= currentTiles.GetLength(0) || c + dy < 0 || c + dy >= currentTiles.GetLength(1))
                                {
                                    continue;
                                }

                                if (currentTiles[r + dx, c + dy] != RoomTile.Bed)
                                {
                                    continue;
                                }

                                createdComponent = EntityFactory.CreateEntity <Body>("Bed", box.Min + new Vector3(r - 1 + 0.5f, 1.5f, c - 1 + 0.5f) + noise);
                                break;
                            }
                        }


                        thingsMade++;
                        break;

                    default:
                        break;
                    }

                    if (createdComponent == null)
                    {
                        continue;
                    }
                    createdComponent.LocalTransform = Matrix.CreateRotationY(-(rotations[r, c] + (float)Math.PI * 0.5f)) * createdComponent.LocalTransform;
                    components.Add(createdComponent);
                }
            }
            return(components);
        }