コード例 #1
0
 public List <Body> SelectAllBodiesOnScreen(Rectangle selectionRectangle, Camera camera)
 {
     return((from component in Components.Values.OfType <Body>()
             let screenPos = camera.Project(component.GlobalTransform.Translation)
                             where selectionRectangle.Contains((int)screenPos.X, (int)screenPos.Y) || selectionRectangle.Intersects(component.GetScreenRect(camera)) && screenPos.Z > 0
                             select component).ToList());
 }
コード例 #2
0
 public List <Body> SelectRootBodiesOnScreen(Rectangle selectionRectangle, Camera camera)
 {
     return((from component in RootComponent.Children.OfType <Body>()
             let screenPos = camera.Project(component.GlobalTransform.Translation)
                             where   screenPos.Z > 0 &&
                             (selectionRectangle.Contains((int)screenPos.X, (int)screenPos.Y) || selectionRectangle.Intersects(component.GetScreenRect(camera))) &&
                             camera.GetFrustrum().Contains(component.GlobalTransform.Translation) != ContainmentType.Disjoint &&
                             !PlayState.ChunkManager.ChunkData.CheckOcclusionRay(camera.Position, component.Position)
                             select component).ToList());
 }
コード例 #3
0
        public void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.Graphics.GraphicsDevice graphicsDevice, DwarfCorp.Shader effect, bool renderingForWater)
        {
            if (GUIObject != null)
            {
                if (Enabled && IsVisible && camera.IsInView(GetBoundingBox()))
                {
                    Vector3 screenPos = camera.Project(GlobalTransform.Translation);
                    GUIObject.LocalBounds = new Rectangle((int)screenPos.X - GUIObject.LocalBounds.Width / 2,
                                                          (int)screenPos.Y - GUIObject.LocalBounds.Height / 2, GUIObject.LocalBounds.Width,
                                                          GUIObject.LocalBounds.Height);

                    GUIObject.IsVisible = true;
                }
                else
                {
                    GUIObject.IsVisible = false;
                }
            }
        }
コード例 #4
0
        public Rectangle GetScreenRect(BoundingBox Box, Camera Camera)
        {
            Vector3 ext    = (Box.Max - Box.Min);
            Vector3 center = Box.Center();

            Vector3 p1 = Camera.Project(Box.Min);
            Vector3 p2 = Camera.Project(Box.Max);
            Vector3 p3 = Camera.Project(Box.Min + new Vector3(ext.X, 0, 0));
            Vector3 p4 = Camera.Project(Box.Min + new Vector3(0, ext.Y, 0));
            Vector3 p5 = Camera.Project(Box.Min + new Vector3(0, 0, ext.Z));
            Vector3 p6 = Camera.Project(Box.Min + new Vector3(ext.X, ext.Y, 0));

            Vector3 min = MathFunctions.Min(p1, p2, p3, p4, p5, p6);
            Vector3 max = MathFunctions.Max(p1, p2, p3, p4, p5, p6);

            return(new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y)));
        }
コード例 #5
0
ファイル: Body.cs プロジェクト: svifylabs/dwarfcorp
        public Rectangle GetScreenRect(Camera camera)
        {
            BoundingBox box = GetBoundingBox();


            Vector3 ext    = (box.Max - box.Min);
            Vector3 center = box.Center();


            Vector3 p1 = camera.Project(box.Min);
            Vector3 p2 = camera.Project(box.Max);
            Vector3 p3 = camera.Project(box.Min + new Vector3(ext.X, 0, 0));
            Vector3 p4 = camera.Project(box.Min + new Vector3(0, ext.Y, 0));
            Vector3 p5 = camera.Project(box.Min + new Vector3(0, 0, ext.Z));
            Vector3 p6 = camera.Project(box.Min + new Vector3(ext.X, ext.Y, 0));


            Vector3 min = MathFunctions.Min(p1, p2, p3, p4, p5, p6);
            Vector3 max = MathFunctions.Max(p1, p2, p3, p4, p5, p6);

            return(new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y)));
        }
コード例 #6
0
ファイル: ParticleEmitter.cs プロジェクト: johan74/dwarfcorp
        public void Update(ParticleManager manager, DwarfTime gameTime, ChunkManager chunks, Camera camera)
        {
            ParticleEmitter._camera = camera;

            List <Particle> toRemove = new List <Particle>();

            TriggerTimer.Update(gameTime);
            if (TriggerTimer.HasTriggered && Data.ParticlesPerFrame > 0)
            {
                Trigger(Data.ParticlesPerFrame, Vector3.Zero, new Color(255, 255, 0));
            }


            bool particlePhysics = GameSettings.Default.ParticlePhysics;

            foreach (Particle p in Particles)
            {
                float vel = p.Velocity.LengthSquared();
                if (Data.EmitsLight && p.Scale > 0.1f)
                {
                    DynamicLight.TempLights.Add(new DynamicLight(10.0f, 255.0f, false)
                    {
                        Position = p.Position
                    });
                }
                p.Position += p.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (Data.RotatesWithVelocity)
                {
                    Vector3 cameraVel   = camera.Project(p.Velocity + camera.Position);
                    float   projectionX = cameraVel.X;
                    float   projectionY = cameraVel.Y;

                    p.Angle = (float)Math.Atan2(projectionY, projectionX);
                }
                else
                {
                    p.Angle += (float)(p.AngularVelocity * gameTime.ElapsedGameTime.TotalSeconds);
                }
                if (!Data.Sleeps || vel > 0.01f)
                {
                    p.Velocity += Data.ConstantAccel * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                p.Velocity        *= Data.LinearDamping;
                p.AngularVelocity *= Data.AngularDamping;


                if (!Data.UseManualControl)
                {
                    p.LifeRemaining -= Data.ParticleDecay * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }
                else if (p.TimeAlive > 60)
                {
                    p.LifeRemaining = 0;
                }

                p.Scale += Data.GrowthSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

                p.Scale = Math.Max(p.Scale, 0.0f);

                var v = new VoxelHandle(chunks.ChunkData,
                                        GlobalVoxelCoordinate.FromVector3(p.Position));

                if (Data.HasLighting)
                {
                    if (v.IsValid)
                    {
                        p.LightRamp = new Color(v.Sunlight ? 255 : 0, 255, 0);
                    }
                }
                else
                {
                    p.LightRamp = new Color(255, 255, 0);
                }

                if (Data.CollidesWorld && particlePhysics && vel > 0.2f)
                {
                    if (v.IsValid && !v.IsEmpty)
                    {
                        BoundingBox     b       = new BoundingBox(p.Position - Vector3.One * p.Scale * 0.5f, p.Position + Vector3.One * p.Scale * 0.5f);
                        BoundingBox     vBox    = v.GetBoundingBox();
                        Physics.Contact contact = new Physics.Contact();
                        if (Physics.TestStaticAABBAABB(b, vBox, ref contact))
                        {
                            p.Position += contact.NEnter * contact.Penetration;
                            Vector3 newVelocity = Vector3.Reflect(p.Velocity, -contact.NEnter);
                            p.Velocity         = newVelocity * Data.Damping;
                            p.AngularVelocity *= 0.5f;
                            if (Data.Sleeps)
                            {
                                p.Velocity        = Vector3.Zero;
                                p.AngularVelocity = 0.0f;
                                vel = 0.0f;
                            }
                            if (!String.IsNullOrEmpty(Data.SpatterType))
                            {
                                var above = VoxelHelpers.GetVoxelAbove(v);
                                if (!above.IsValid || above.IsEmpty)
                                {
                                    float x = MathFunctions.Clamp(p.Position.X, vBox.Min.X + 0.1f, vBox.Max.X - 0.1f);
                                    float z = MathFunctions.Clamp(p.Position.Z, vBox.Min.Z + 0.1f, vBox.Max.Z - 0.1f);
                                    manager.Create(Data.SpatterType,
                                                   VertexNoise.Warp(new Vector3(x, v.RampType == RampType.None ? v.WorldPosition.Y + 1.02f : v.WorldPosition.Y + 0.6f, z)), Vector3.Zero, Color.White, Vector3.Up);
                                }
                                else
                                {
                                    manager.Create(Data.SpatterType, p.Position - contact.NEnter * contact.Penetration * 0.95f, Vector3.Zero, Color.White, contact.NEnter);
                                }
                                p.LifeRemaining = -1.0f;
                            }
                        }
                    }
                }

                if (p.LifeRemaining < 0)
                {
                    if (p.InstanceData != null)
                    {
                        p.InstanceData.ShouldDraw = false;
                        p.InstanceData.Transform  = Matrix.CreateTranslation(camera.Position + new Vector3(-1000, -1000, -1000));
                        Sprites[p.Frame].Remove(p.InstanceData);
                    }

                    toRemove.Add(p);
                }

                else if (p.InstanceData != null)
                {
                    p.TimeAlive += (float)gameTime.ElapsedGameTime.TotalSeconds + MathFunctions.Rand() * 0.01f;
                    int prevFrame = p.Frame;
                    int newFrame  = AnimPlayer.GetFrame(p.TimeAlive);
                    if (vel < 0.2f)
                    {
                        newFrame = prevFrame;
                    }
                    if (newFrame != prevFrame)
                    {
                        p.Frame = newFrame;
                        if (Sprites.Count > 0)
                        {
                            Sprites[prevFrame].Remove(p.InstanceData);
                            Sprites[newFrame].Add(p.InstanceData);
                        }
                        if (/*!Data.Animation.Loops && */ p.Frame == Data.Animation.Frames.Count - 1)
                        {
                            p.LifeRemaining *= 0.1f;
                        }
                    }
                    p.InstanceData.ShouldDraw = true;
                    p.InstanceData.Transform  = MatrixFromParticle(Data, p);
                    p.InstanceData.LightRamp  = p.LightRamp;
                }
            }

            foreach (Particle p in toRemove)
            {
                Particles.Remove(p);
            }


            foreach (var sprites in Sprites)
            {
                sprites.Update(gameTime, camera, GameState.Game.GraphicsDevice, chunks.World.Master.MaxViewingLevel);
            }
        }
コード例 #7
0
        public override void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera)
        {
            ParticleEmitter._camera = camera;

            List <Particle> toRemove = new List <Particle>();

            TriggerTimer.Update(gameTime);
            if (TriggerTimer.HasTriggered && Data.ParticlesPerFrame > 0)
            {
                Trigger(Data.ParticlesPerFrame, Vector3.Zero, Tint);
            }


            bool particlePhysics = GameSettings.Default.ParticlePhysics;

            foreach (Particle p in Particles)
            {
                float vel = p.Velocity.LengthSquared();
                if (!Data.Sleeps || vel > 0.2f)
                {
                    if (Data.EmitsLight && p.Scale > 0.1f)
                    {
                        DynamicLight.TempLights.Add(new DynamicLight(10.0f, 255.0f, false)
                        {
                            Position = p.Position
                        });
                    }
                    p.Position += p.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (Data.RotatesWithVelocity)
                    {
                        Vector3 cameraVel   = camera.Project(p.Velocity + camera.Position);
                        float   projectionX = cameraVel.X;
                        float   projectionY = cameraVel.Y;

                        p.Angle = (float)Math.Atan2(projectionY, projectionX);
                    }
                    else
                    {
                        p.Angle += (float)(p.AngularVelocity * gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    p.Velocity        += Data.ConstantAccel * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    p.Velocity        *= Data.LinearDamping;
                    p.AngularVelocity *= Data.AngularDamping;
                }
                else if (Data.Sleeps && vel < 0.2f)
                {
                    p.Velocity = Vector3.Zero;
                }


                if (!Data.UseManualControl)
                {
                    p.LifeRemaining -= Data.ParticleDecay * (float)gameTime.ElapsedGameTime.TotalSeconds;
                }

                p.Scale += Data.GrowthSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

                p.Scale = Math.Max(p.Scale, 0.0f);

                var v = new VoxelHandle(chunks.ChunkData,
                                        GlobalVoxelCoordinate.FromVector3(p.Position));

                if (Data.HasLighting)
                {
                    if (v.IsValid && v.IsEmpty)
                    {
                        p.Tint = new Color(v.SunColor, 255, 0);
                    }
                }

                if (Data.CollidesWorld && particlePhysics && vel > 0.2f)
                {
                    BoundingBox b = new BoundingBox(p.Position - Vector3.One * p.Scale * 0.5f, p.Position + Vector3.One * p.Scale * 0.5f);
                    if (v.IsValid && !v.IsEmpty)
                    {
                        Physics.Contact contact = new Physics.Contact();
                        if (Physics.TestStaticAABBAABB(b, v.GetBoundingBox(), ref contact))
                        {
                            p.Position += contact.NEnter * contact.Penetration;
                            Vector3 newVelocity = Vector3.Reflect(p.Velocity, -contact.NEnter);
                            p.Velocity         = newVelocity * Data.Damping;
                            p.AngularVelocity *= 0.5f;
                        }
                    }
                }

                if (p.LifeRemaining < 0)
                {
                    if (p.InstanceData != null)
                    {
                        p.InstanceData.ShouldDraw = false;
                        p.InstanceData.Transform  = Matrix.CreateTranslation(camera.Position + new Vector3(-1000, -1000, -1000));
                        Sprites[p.Frame].Remove(p.InstanceData);
                    }

                    toRemove.Add(p);
                }

                else if (p.InstanceData != null)
                {
                    p.TimeAlive += (float)gameTime.ElapsedGameTime.TotalSeconds + MathFunctions.Rand() * 0.01f;
                    int prevFrame = p.Frame;
                    int newFrame  = AnimPlayer.GetFrame(p.TimeAlive);
                    if (newFrame != prevFrame)
                    {
                        p.Frame = newFrame;
                        if (Sprites.Count > 0)
                        {
                            Sprites[prevFrame].Remove(p.InstanceData);
                            Sprites[newFrame].Add(p.InstanceData);
                        }
                        if (/*!Data.Animation.Loops && */ p.Frame == Data.Animation.Frames.Count - 1)
                        {
                            p.LifeRemaining *= 0.1f;
                        }
                    }
                    p.InstanceData.ShouldDraw = true;
                    p.InstanceData.Transform  = MatrixFromParticle(p);
                    p.InstanceData.Color      = p.Tint;
                }
            }

            foreach (Particle p in toRemove)
            {
                Particles.Remove(p);
            }


            foreach (var sprites in Sprites)
            {
                sprites.Update(gameTime, camera, chunks.Graphics, chunks.ChunkData.MaxViewingLevel);
            }
            if (Particles.Count > 0)
            {
                base.Update(gameTime, chunks, camera);
            }
        }
コード例 #8
0
ファイル: WorldGUIObject.cs プロジェクト: scorvi/dwarfcorp
        public override void Render(DwarfTime gameTime, ChunkManager chunks, Camera camera, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, Microsoft.Xna.Framework.Graphics.GraphicsDevice graphicsDevice, Microsoft.Xna.Framework.Graphics.Effect effect, bool renderingForWater)
        {
            if (GUIObject != null)
            {
                if (Enabled && IsVisible && camera.IsInView(GetBoundingBox()))
                {
                    Vector3 screenPos = camera.Project(GlobalTransform.Translation);
                    GUIObject.LocalBounds = new Rectangle((int) screenPos.X - GUIObject.LocalBounds.Width/2,
                        (int) screenPos.Y - GUIObject.LocalBounds.Height/2, GUIObject.LocalBounds.Width,
                        GUIObject.LocalBounds.Height);

                    GUIObject.IsVisible = true;
                }
                else
                {
                    GUIObject.IsVisible = false;
                }
            }
            base.Render(gameTime, chunks, camera, spriteBatch, graphicsDevice, effect, renderingForWater);
        }
コード例 #9
0
ファイル: ParticleEmitter.cs プロジェクト: scorvi/dwarfcorp
        public override void Update(DwarfTime gameTime, ChunkManager chunks, Camera camera)
        {
            ParticleEmitter._camera = camera;

            List<Particle> toRemove = new List<Particle>();

            TriggerTimer.Update(gameTime);
            if(TriggerTimer.HasTriggered && Data.ParticlesPerFrame > 0)
            {
                Trigger(Data.ParticlesPerFrame, Vector3.Zero, Tint);
            }

            bool particlePhysics = GameSettings.Default.ParticlePhysics;
            Voxel v = new Voxel();
            foreach(Particle p in Particles)
            {
                float vel = p.Velocity.LengthSquared();
                if(!Data.Sleeps || vel > 0.2f)
                {
                    if (Data.EmitsLight && p.Scale > 0.1f)
                    {
                        DynamicLight.TempLights.Add(new DynamicLight(10.0f, 255.0f, false) { Position = p.Position});
                    }
                    p.Position += p.Velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (Data.RotatesWithVelocity)
                    {
                        Vector3 cameraVel = camera.Project(p.Velocity + camera.Position);
                        float projectionX = cameraVel.X;
                        float projectionY = cameraVel.Y;

                        p.Angle = (float)Math.Atan2(projectionY, projectionX);
                    }
                    else
                    {
                        p.Angle += (float)(p.AngularVelocity * gameTime.ElapsedGameTime.TotalSeconds);
                    }
                    p.Velocity += Data.ConstantAccel * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    p.Velocity *= Data.LinearDamping;
                    p.AngularVelocity *= Data.AngularDamping;
                }
                else if (Data.Sleeps && vel < 0.2f)
                {
                    p.Velocity = Vector3.Zero;
                }

                p.LifeRemaining -= Data.ParticleDecay * (float)gameTime.ElapsedGameTime.TotalSeconds;
                p.Scale += Data.GrowthSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

                p.Scale = Math.Max(p.Scale, 0.0f);
                bool success = false;

                if (Data.HasLighting)
                {
                    success = chunks.ChunkData.GetVoxel(p.Position, ref v);

                    if (success && v.IsEmpty)
                    {
                        p.Tint = new Color(v.SunColor, 255, 0);
                    }
                }

                if(Data.CollidesWorld && particlePhysics && vel > 0.2f)
                {
                    if (!Data.HasLighting)
                    {
                        success = chunks.ChunkData.GetVoxel(p.Position, ref v);
                    }
                    BoundingBox b = new BoundingBox(p.Position - Vector3.One * p.Scale * 0.5f, p.Position + Vector3.One * p.Scale * 0.5f);
                    if(success && !v.IsEmpty)
                    {
                        Physics.Contact contact = new Physics.Contact();
                        if(Physics.TestStaticAABBAABB(b, v.GetBoundingBox(), ref contact))
                        {
                            p.Position += contact.NEnter * contact.Penetration;
                            Vector3 newVelocity = Vector3.Reflect(p.Velocity, -contact.NEnter);
                            p.Velocity = newVelocity * Data.Damping;
                            p.AngularVelocity *= 0.5f;
                        }
                    }
                }

                if(p.LifeRemaining < 0)
                {
                    if(p.InstanceData != null)
                    {//
                        p.InstanceData.ShouldDraw = false;
                        p.InstanceData.Transform = Matrix.CreateTranslation(camera.Position + new Vector3(-1000, -1000, -1000));
                        Sprites.Remove(p.InstanceData);
                    }

                    toRemove.Add(p);
                }

                else if(p.InstanceData != null)
                {
                    p.InstanceData.ShouldDraw = true;
                    p.InstanceData.Transform = MatrixFromParticle(p);
                    p.InstanceData.Color = p.Tint;
                }
            }

            foreach(Particle p in toRemove)
            {
                Particles.Remove(p);
            }

            Sprites.Update(gameTime, camera, chunks.Graphics);
            base.Update(gameTime, chunks, camera);
        }