Esempio n. 1
0
        private void PlaceVoxel(VoxelHandle Vox, VoxelType Type, WorldManager World)
        {
            Vox.IsPlayerBuilt = true;
            Vox.Type          = Type;
            Vox.QuickSetLiquid(LiquidType.None, 0);

            for (int i = 0; i < 4; i++)
            {
                World.ParticleManager.Trigger("puff", MathFunctions.RandVector3Box(Vox.GetBoundingBox().Expand(0.25f)), Color.White, 5);
            }

            // Todo: Should this be handled by the chunk manager while processing voxel update events?
            foreach (var phys in World.EnumerateIntersectingObjects(Vox.GetBoundingBox(), CollisionType.Dynamic).OfType <Physics>())
            {
                phys.ApplyForce((phys.GlobalTransform.Translation - (Vox.WorldPosition + new Vector3(0.5f, 0.5f, 0.5f))) * 100, 0.01f);
                BoundingBox     box     = Vox.GetBoundingBox();
                Physics.Contact contact = new Physics.Contact();
                Physics.TestStaticAABBAABB(box, phys.GetBoundingBox(), ref contact);

                if (!contact.IsIntersecting)
                {
                    continue;
                }

                Vector3 diff = contact.NEnter * contact.Penetration;
                Matrix  m    = phys.LocalTransform;
                m.Translation      += diff;
                phys.LocalTransform = m;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a screenshot of the game and saves it to a file.
        /// </summary>
        /// <param name="filename">The file to save the screenshot to</param>
        /// <param name="resolution">The width/height of the image</param>
        /// <returns>True if the screenshot could be taken, false otherwise</returns>
        public bool TakeScreenshot(string filename, Point resolution)
        {
            try
            {
                using (
                    RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, resolution.X, resolution.Y, false,
                                                                     SurfaceFormat.Color, DepthFormat.Depth24))
                {
                    var frustum     = Camera.GetDrawFrustum();
                    var renderables = World.EnumerateIntersectingObjects(frustum)
                                      .Where(r => r.IsVisible && !World.ChunkManager.IsAboveCullPlane(r.GetBoundingBox()));

                    var    oldProjection    = Camera.ProjectionMatrix;
                    Matrix projectionMatrix = Matrix.CreatePerspectiveFieldOfView(Camera.FOV, ((float)resolution.X) / resolution.Y, Camera.NearPlane, Camera.FarPlane);
                    Camera.ProjectionMatrix = projectionMatrix;
                    GraphicsDevice.SetRenderTarget(renderTarget);
                    DrawSky(new DwarfTime(), Camera.ViewMatrix, 1.0f, Color.CornflowerBlue);
                    Draw3DThings(new DwarfTime(), DefaultShader, Camera.ViewMatrix);

                    DefaultShader.View       = Camera.ViewMatrix;
                    DefaultShader.Projection = Camera.ProjectionMatrix;

                    ComponentRenderer.Render(renderables, new DwarfTime(), World.ChunkManager, Camera,
                                             DwarfGame.SpriteBatch, GraphicsDevice, DefaultShader,
                                             ComponentRenderer.WaterRenderType.None, 0);
                    InstanceRenderer.Flush(GraphicsDevice, DefaultShader, Camera,
                                           InstanceRenderMode.Normal);


                    GraphicsDevice.SetRenderTarget(null);
                    renderTarget.SaveAsPng(new FileStream(filename, FileMode.Create), resolution.X, resolution.Y);
                    GraphicsDevice.Textures[0] = null;
                    GraphicsDevice.Indices     = null;
                    GraphicsDevice.SetVertexBuffer(null);
                    Camera.ProjectionMatrix = oldProjection;
                }
            }
            catch (IOException e)
            {
                Console.Error.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
 private GameComponent GetBodyAt(VoxelHandle voxel, WorldManager World, string tag)
 {
     return(World.EnumerateIntersectingObjects(voxel.GetBoundingBox(),
                                               CollisionType.Static).OfType <GameComponent>().FirstOrDefault(component => component.Tags.Contains(tag)));
 }
Esempio n. 4
0
        public static bool IsValidPlacement(
            VoxelHandle Location,
            ResourceType CraftType,
            WorldManager World,
            GameComponent PreviewBody,
            String Verb,
            String PastParticple)
        {
            if (CraftType == null)
            {
                return(false);
            }

            switch (CraftType.Placement_PlacementRequirement)
            {
            case ResourceType.PlacementRequirement.NearWall:
            {
                var neighborFound = VoxelHelpers.EnumerateManhattanNeighbors2D(Location.Coordinate)
                                    .Select(c => new VoxelHandle(World.ChunkManager, c))
                                    .Any(v => v.IsValid && !v.IsEmpty);

                if (!neighborFound)
                {
                    World.UserInterface.ShowTooltip("Must be " + PastParticple + " next to wall!");
                    return(false);
                }

                break;
            }

            case ResourceType.PlacementRequirement.OnGround:
            {
                var below = VoxelHelpers.GetNeighbor(Location, new GlobalVoxelOffset(0, -1, 0));

                if (!below.IsValid || below.IsEmpty)
                {
                    World.UserInterface.ShowTooltip("Must be " + PastParticple + " on solid ground!");
                    return(false);
                }
                break;
            }
            }

            if (PreviewBody != null)
            {
                // Just check for any intersecting body in octtree.

                var previewBox = PreviewBody.GetRotatedBoundingBox();
                var sensorBox  = previewBox;

                GenericVoxelListener sensor;
                if (PreviewBody.GetComponent <GenericVoxelListener>().HasValue(out sensor))
                {
                    sensorBox = sensor.GetRotatedBoundingBox();
                }

                if (Debugger.Switches.DrawToolDebugInfo)
                {
                    Drawer3D.DrawBox(sensorBox, Color.Yellow, 0.1f, false);
                }

                foreach (var intersectingObject in World.EnumerateIntersectingObjects(sensorBox, CollisionType.Static))
                {
                    if (Object.ReferenceEquals(intersectingObject, sensor))
                    {
                        continue;
                    }
                    var objectRoot = intersectingObject.GetRoot() as GameComponent;
                    if (objectRoot is WorkPile)
                    {
                        continue;
                    }
                    if (objectRoot == PreviewBody)
                    {
                        continue;
                    }
                    if (objectRoot.IsDead)
                    {
                        continue;
                    }
                    if (objectRoot != null && objectRoot.GetRotatedBoundingBox().Intersects(previewBox))
                    {
                        World.UserInterface.ShowTooltip("Can't " + Verb + " here: intersects " + objectRoot.Name);
                        return(false);
                    }
                }

                bool intersectsWall = VoxelHelpers.EnumerateCoordinatesInBoundingBox
                                          (PreviewBody.GetRotatedBoundingBox().Expand(-0.1f)).Any(
                    v =>
                {
                    var tvh = new VoxelHandle(World.ChunkManager, v);
                    return(tvh.IsValid && !tvh.IsEmpty);
                });

                var  current    = new VoxelHandle(World.ChunkManager, GlobalVoxelCoordinate.FromVector3(PreviewBody.Position));
                bool underwater = current.IsValid && current.LiquidType != LiquidType.None;

                if (underwater)
                {
                    World.UserInterface.ShowTooltip("Can't " + Verb + " here: underwater or in lava.");
                    return(false);
                }

                if (intersectsWall && CraftType.Placement_PlacementRequirement != ResourceType.PlacementRequirement.NearWall)
                {
                    World.UserInterface.ShowTooltip("Can't " + Verb + " here: intersects wall.");
                    return(false);
                }
            }
            World.UserInterface.ShowTooltip("");
            return(true);
        }