Пример #1
0
        void Blocks_ItemAdded(int index, Entity.Handle e)
        {
            PhysicsBlock block = e.Target.Get <PhysicsBlock>();

            block.Add(new CommandBinding <BEPUphysics.BroadPhaseEntries.Collidable, BEPUphysics.NarrowPhaseSystems.Pairs.ContactCollection>(block.Collided, this.Collided));
            this.blocks.Add(block);
        }
Пример #2
0
        public override Entity Create(Main main)
        {
            Entity result = new Entity(main, "MapBoundary");
            result.Add("Transform", new Transform());
            PhysicsBlock block = new PhysicsBlock();
            block.Size.Value = new Vector3(100.0f, 50.0f, 2.0f);
            result.Add("PhysicsBlock", block);

            return result;
        }
Пример #3
0
        public void Update(float dt)
        {
            if (this.active)
            {
                if (this.Type.Value != Voxel.t.Empty && this.Blocks.Length == 0)
                {
                    SceneryBlockFactory factory         = Factory.Get <SceneryBlockFactory>();
                    Vector3             blockSpawnPoint = this.Position;
                    for (int i = 0; i < TotalBlocks; i++)
                    {
                        Entity block = factory.CreateAndBind(main);
                        block.Get <Transform>().Position.Value = blockSpawnPoint + new Vector3(((float)BlockCloud.random.NextDouble() - 0.5f) * 2.0f, ((float)BlockCloud.random.NextDouble() - 0.5f) * 2.0f, ((float)BlockCloud.random.NextDouble() - 0.5f) * 2.0f);
                        SceneryBlock sceneryBlock = block.Get <SceneryBlock>();
                        sceneryBlock.Type.Value  = this.Type;
                        sceneryBlock.Scale.Value = this.Scale;
                        block.Get <PhysicsBlock>().Box.LinearDamping = 0.75f;
                        this.Blocks.Add(block);
                        main.Add(block);
                    }
                }

                Vector3 avg = Vector3.Zero;
                for (int i = 0; i < this.blocks.Count; i++)
                {
                    PhysicsBlock block = this.blocks[i];
                    if (block.Active)
                    {
                        if (!block.Suspended)
                        {
                            Vector3 toCenter = this.Position - block.Box.Position;
                            if (toCenter.Length() > 20.0f)
                            {
                                block.Box.Position = this.Position + Vector3.Normalize(toCenter) * -20.0f;
                            }

                            float   offset = i + this.main.TotalTime;
                            Vector3 force  = toCenter + new Vector3(this.noise.Sample(new Vector3(offset)), this.noise.Sample(new Vector3(offset + 64)), noise.Sample(new Vector3(offset + 128))) * 10.0f * this.Scale;
                            force *= main.ElapsedTime * forceMultiplier / this.Scale;
                            block.Box.ApplyLinearImpulse(ref force);

                            avg += block.Box.Position;
                        }
                    }
                    else
                    {
                        this.Blocks.RemoveAt(i);
                        i--;
                    }
                }
                avg /= this.blocks.Count;
                this.AveragePosition.Value = avg;
            }
        }
Пример #4
0
        public override Entity Create(Main main)
        {
            Entity result = new Entity(main, "Block");

            Transform transform = new Transform();
            result.Add("Transform", transform);

            PhysicsBlock physics = new PhysicsBlock();
            result.Add("Physics", physics);

            Property<string> cue = new Property<string> { Value = "ConcreteRubble" };
            result.Add("CollisionSoundCue", cue);

            ModelInstance model = new ModelInstance();
            result.Add("Model", model);
            model.Scale.Value = new Vector3(0.5f);

            return result;
        }
Пример #5
0
        private static void explode(Main main, Voxel map, Voxel.Coord coord, Vector3 pos, int radius, float physicsRadius)
        {
            float distanceToCamera = (main.Camera.Position.Value - pos).Length();

            // Kaboom
            Sound.PostEvent(distanceToCamera < physicsRadius * 1.5f ? AK.EVENTS.PLAY_EXPLOSION_CLOSE : AK.EVENTS.PLAY_EXPLOSION, pos);

            Entity lightEntity = Factory.Get <PointLightFactory>().CreateAndBind(main);

            lightEntity.Serialize = false;
            PointLight light = lightEntity.Get <PointLight>();

            light.Color.Value       = new Vector3(1.3f, 1.1f, 0.9f);
            light.Attenuation.Value = 20.0f;
            light.Position.Value    = pos;
            lightEntity.Add(new Animation
                            (
                                new Animation.FloatMoveTo(light.Attenuation, 0.0f, 1.0f),
                                new Animation.Execute(light.Delete)
                            ));
            main.Add(lightEntity);

            SmokeFactory smokeFactory = Factory.Get <SmokeFactory>();

            for (int i = 0; i < 5; i++)
            {
                Entity smoke = smokeFactory.CreateAndBind(main);
                smoke.Get <Transform>().Position.Value = pos;
                main.Add(smoke);
            }

            ParticleEmitter.Emit(main, "Smoke", pos, physicsRadius * 0.4f, 250);

            Entity player = PlayerFactory.Instance;

            if (player != null && player.Active)
            {
                player.Get <CameraController>().Shake.Execute(pos, 50.0f);
            }

            const float physicsImpulse         = 70.0f;
            const float minPlayerDamage        = 0.1f;
            const float playerDamageMultiplier = 2.0f;

            // Remove the cells
            BlockFactory blockFactory = Factory.Get <BlockFactory>();

            foreach (Voxel m in Voxel.ActiveVoxels.ToList())
            {
                List <Voxel.Coord> removals = new List <Voxel.Coord>();

                Voxel.Coord c           = m.GetCoordinate(pos);
                Vector3     relativePos = m.GetRelativePosition(c);

                Quaternion quat = m.Entity.Get <Transform>().Quaternion;

                for (Voxel.Coord x = c.Move(Direction.NegativeX, radius - 1); x.X < c.X + radius; x.X++)
                {
                    for (Voxel.Coord y = x.Move(Direction.NegativeY, radius - 1); y.Y < c.Y + radius; y.Y++)
                    {
                        for (Voxel.Coord z = y.Move(Direction.NegativeZ, radius - 1); z.Z < c.Z + radius; z.Z++)
                        {
                            Voxel.State s = m[z];
                            if (s.ID == 0 || s.Permanent)
                            {
                                continue;
                            }

                            Vector3 cellPos = m.GetRelativePosition(z);
                            if ((cellPos - relativePos).Length() < radius - 1)
                            {
                                removals.Add(z);
                                if (random.NextDouble() > 0.5)
                                {
                                    Entity    block          = blockFactory.CreateAndBind(main);
                                    Transform blockTransform = block.Get <Transform>();
                                    blockTransform.Position.Value   = m.GetAbsolutePosition(cellPos);
                                    blockTransform.Quaternion.Value = Quaternion.CreateFromYawPitchRoll(((float)random.NextDouble() - 0.5f) * 2.0f * (float)Math.PI, ((float)random.NextDouble() - 0.5f) * 2.0f * (float)Math.PI, ((float)random.NextDouble() - 0.5f) * 2.0f * (float)Math.PI);
                                    s.ApplyToBlock(block);
                                    main.Add(block);
                                }
                            }
                        }
                    }
                }
                if (removals.Count > 0)
                {
                    m.Empty(removals);
                    m.Regenerate();
                }
            }

            // Damage the player
            if (player != null && player.Active)
            {
                Vector3 toPlayer = player.Get <Transform>().Position - pos;
                float   d        = toPlayer.Length();
                if (d < physicsRadius)
                {
                    float attenuation = 1.0f;
                    if (d > 0)
                    {
                        Voxel.GlobalRaycast(pos, toPlayer / d, d, delegate(int x, Voxel.t c)
                        {
                            Voxel.State s = Voxel.States.All[c];
                            if (s.Permanent)
                            {
                                attenuation = 0.0f;
                                return(true);
                            }
                            else if (s.Hard)
                            {
                                attenuation -= 0.6f;
                            }
                            else
                            {
                                attenuation -= 0.35f;
                            }
                            return(false);
                        });
                        attenuation = Math.Max(0, attenuation);
                    }
                    player.Get <Agent>().Damage.Execute(attenuation * (minPlayerDamage + (1.0f - (d / physicsRadius)) * playerDamageMultiplier));
                }
            }

            // Apply impulse to dynamic maps
            foreach (Voxel m in Voxel.ActiveVoxels)
            {
                DynamicVoxel dm = m as DynamicVoxel;
                if (dm == null)
                {
                    continue;
                }

                Vector3 toMap         = dm.Transform.Value.Translation - pos;
                float   distanceToMap = toMap.Length();
                toMap /= distanceToMap;

                toMap *= Math.Max(0.0f, 1.0f - (distanceToMap / physicsRadius)) * Math.Min(200.0f, dm.PhysicsEntity.Mass) * physicsImpulse;

                dm.PhysicsEntity.ApplyImpulse(dm.Transform.Value.Translation + new Vector3(((float)random.NextDouble() - 0.5f) * 2.0f, ((float)random.NextDouble() - 0.5f) * 2.0f, ((float)random.NextDouble() - 0.5f) * 2.0f), toMap);
            }

            // Apply impulse to physics blocks
            foreach (Entity b in main.Get("Block"))
            {
                PhysicsBlock block         = b.Get <PhysicsBlock>();
                Vector3      fromExplosion = b.Get <Transform>().Position.Value - pos;
                float        distance      = fromExplosion.Length();
                if (distance > 0.0f && distance < physicsRadius)
                {
                    float blend = 1.0f - (distance / physicsRadius);
                    block.Box.LinearVelocity  += fromExplosion * blend * 10.0f / distance;
                    block.Box.AngularVelocity += new Vector3(((float)random.NextDouble() - 0.5f) * 2.0f, ((float)random.NextDouble() - 0.5f) * 2.0f, ((float)random.NextDouble() - 0.5f) * 2.0f) * blend;
                }
            }
        }
Пример #6
0
 public static void CancelPlayerCollisions(PhysicsBlock block)
 {
     block.Box.CollisionInformation.CollisionRules.Group = Util.Character.NoCollideGroup;
 }
Пример #7
0
        public override Entity Create(Main main)
        {
            Entity result = new Entity(main, "Blast");

            Transform transform = new Transform();
            result.Add("Transform", transform);

            PhysicsBlock physics = new PhysicsBlock();
            result.Add("Physics", physics);

            Model model = new Model();
            result.Add("Model", model);
            model.Filename.Value = "Models\\blast";
            model.Color.Value = new Vector3(0.75f, 2.0f, 0.75f);

            PointLight light = new PointLight();
            light.Shadowed.Value = true;
            light.Color.Value = new Vector3(model.Color.Value.X, model.Color.Value.Y, model.Color.Value.Z);
            light.Attenuation.Value = 10.0f;
            result.Add("Light", light);

            if (ParticleSystem.Get(main, "Sparks") == null)
            {
                ParticleSystem.Add(main, "Sparks",
                new ParticleSystem.ParticleSettings
                {
                    TextureName = "Particles\\spark",
                    MaxParticles = 1000,
                    Duration = TimeSpan.FromSeconds(1.0f),
                    MinHorizontalVelocity = 0.0f,
                    MaxHorizontalVelocity = 0.0f,
                    MinVerticalVelocity = 0.0f,
                    MaxVerticalVelocity = 0.0f,
                    Gravity = new Vector3(0.0f, 0.0f, 0.0f),
                    EndVelocity = 0.0f,
                    MinRotateSpeed = -20.0f,
                    MaxRotateSpeed = 20.0f,
                    MinStartSize = 0.5f,
                    MaxStartSize = 0.4f,
                    MinEndSize = 0.2f,
                    MaxEndSize = 0.1f,
                    BlendState = Microsoft.Xna.Framework.Graphics.BlendState.Additive,
                    MinColor = new Vector4(0.75f, 2.0f, 0.75f, 1.0f),
                    MaxColor = new Vector4(0.75f, 2.0f, 0.75f, 1.0f),
                });
            }

            ParticleEmitter emitter = new ParticleEmitter();
            emitter.ParticleType.Value = "Sparks";
            emitter.ParticlesPerSecond.Value = 200;
            emitter.Jitter.Value = new Vector3(0.25f);
            result.Add("Particles", emitter);

            Sound loopSound = new Sound();
            result.Add("LoopSound", loopSound);
            loopSound.Cue.Value = "Blast Loop";

            return result;
        }
Пример #8
0
        public bool BreakWalls(Vector3 forward, Vector3 right)
        {
            BlockFactory blockFactory = Factory.Get <BlockFactory>();
            Vector3      basePos      = this.Position + new Vector3(0, 0.2f + (this.Height * -0.5f) - this.SupportHeight, 0) + forward * -1.0f;
            bool         broke        = false;

            foreach (Voxel map in Voxel.ActivePhysicsVoxels.ToList())
            {
                List <Voxel.Coord> removals      = new List <Voxel.Coord>();
                Quaternion         mapQuaternion = Quaternion.CreateFromRotationMatrix(map.Transform);
                Voxel.Coord        top           = map.GetCoordinate(basePos + new Vector3(0, this.Height + this.SupportHeight + 0.5f, 0));
                Direction          upDir         = map.GetRelativeDirection(Vector3.Up);
                Direction          rightDir      = map.GetRelativeDirection(right);
                Direction          forwardDir    = map.GetRelativeDirection(forward);
                Voxel.Coord        center        = map.GetCoordinate(basePos);
                for (Voxel.Coord y = center.Clone(); y.GetComponent(upDir) <= top.GetComponent(upDir); y = y.Move(upDir))
                {
                    int minZ = center.GetComponent(rightDir) - 10;
                    int maxZ = minZ + 20;
                    foreach (Voxel.Coord x in this.spreadFromCenter(y, rightDir))
                    {
                        Voxel.Coord z = x.Clone();
                        for (int i = 0; i < 4; i++)
                        {
                            Voxel.State state           = map[z];
                            int         zRightDimension = z.GetComponent(rightDir);
                            if (zRightDimension > minZ && zRightDimension < maxZ && state.ID != 0 && !removals.Contains(z))
                            {
                                if (state.Permanent || state.Hard)
                                {
                                    if (zRightDimension >= center.GetComponent(rightDir))
                                    {
                                        maxZ = zRightDimension;
                                    }
                                    else
                                    {
                                        minZ = zRightDimension;
                                    }
                                    break;
                                }
                                else
                                {
                                    broke = true;
                                    removals.Add(z);
                                    Vector3   cellPos        = map.GetAbsolutePosition(z);
                                    Vector3   toCell         = cellPos - basePos;
                                    Entity    block          = blockFactory.CreateAndBind(this.main);
                                    Transform blockTransform = block.Get <Transform>();
                                    blockTransform.Position.Value   = cellPos;
                                    blockTransform.Quaternion.Value = mapQuaternion;
                                    state.ApplyToBlock(block);
                                    toCell += forward * 4.0f;
                                    toCell.Normalize();
                                    PhysicsBlock physicsBlock = block.Get <PhysicsBlock>();
                                    physicsBlock.LinearVelocity.Value  = toCell * 15.0f;
                                    physicsBlock.AngularVelocity.Value = new Vector3(((float)this.random.NextDouble() - 0.5f) * 2.0f, ((float)this.random.NextDouble() - 0.5f) * 2.0f, ((float)this.random.NextDouble() - 0.5f) * 2.0f);
                                    main.Add(block);
                                }
                            }
                            z = z.Move(forwardDir);
                        }
                    }
                }

                if (removals.Count > 0)
                {
                    map.Empty(removals);
                    map.Regenerate();
                }
            }
            return(broke);
        }
Пример #9
0
 public static void CancelPlayerCollisions(PhysicsBlock block)
 {
     block.Box.CollisionInformation.CollisionRules.Group = Util.Character.NoCollideGroup;
 }