예제 #1
0
        private void ShieldIntersect(MyEntity ent)
        {
            var grid = ent as MyCubeGrid;

            if (grid == null)
            {
                return;
            }

            if (GridInside(grid, MyOrientedBoundingBoxD.CreateFromBoundingBox(grid.PositionComp.WorldAABB)))
            {
                return;
            }
            ShieldGridComponent shieldComponent;

            grid.Components.TryGet(out shieldComponent);
            if (shieldComponent?.DefenseShields == null)
            {
                return;
            }

            var ds = shieldComponent.DefenseShields;

            if (!ds.WasOnline)
            {
                EntIntersectInfo entInfo;
                WebEnts.TryRemove(ent, out entInfo);
            }
            var dsVerts     = ds.ShieldComp.PhysicsOutside;
            var dsMatrixInv = ds.DetectMatrixOutsideInv;
            var myGrid      = Shield.CubeGrid;

            var insidePoints = new List <Vector3D>();

            if (_isServer)
            {
                CustomCollision.ShieldX2PointsInside(dsVerts, dsMatrixInv, ShieldComp.PhysicsOutside, DetectMatrixOutsideInv, insidePoints);
            }
            else
            {
                CustomCollision.ClientShieldX2PointsInside(dsVerts, dsMatrixInv, ShieldComp.PhysicsOutsideLow, DetectMatrixOutsideInv, insidePoints);
            }

            var bPhysics = ((IMyCubeGrid)grid).Physics;
            var sPhysics = myGrid.Physics;

            var bMass = grid.GetCurrentMass();
            var sMass = ((MyCubeGrid)myGrid).GetCurrentMass();

            if (bMass <= 0)
            {
                bMass = int.MaxValue;
            }
            if (sMass <= 0)
            {
                sMass = int.MaxValue;
            }

            var bVel           = bPhysics.LinearVelocity;
            var bVelLen        = bVel.Length();
            var momentum       = (bMass * bVel) + (sMass * sPhysics.LinearVelocity);
            var resultVelocity = momentum / (bMass + sMass);

            var collisionAvg      = Vector3D.Zero;
            var numOfPointsInside = insidePoints.Count;

            for (int i = 0; i < numOfPointsInside; i++)
            {
                collisionAvg += insidePoints[i];
            }

            collisionAvg /= numOfPointsInside;

            if (numOfPointsInside > 0 && !bPhysics.IsStatic)
            {
                var ejectorAccel = numOfPointsInside > 10 ? numOfPointsInside : 10;
                var impulseData  = new MyImpulseData {
                    MyGrid = grid, Direction = (resultVelocity - bVel) * bMass, Position = bPhysics.CenterOfMassWorld
                };
                var forceData = new MyAddForceData {
                    MyGrid = grid, Force = (bPhysics.CenterOfMassWorld - collisionAvg) * bMass * ejectorAccel, MaxSpeed = MathHelper.Clamp(bVelLen, 1f, 50f)
                };
                ImpulseData.Enqueue(impulseData);
                ForceData.Enqueue(forceData);
            }
            if (!_isServer || numOfPointsInside <= 0)
            {
                return;
            }

            var shieldMaxChargeRate = ds._shieldMaxChargeRate;
            var damage = ((shieldMaxChargeRate * ConvToHp) * DsState.State.ModulateKinetic) * 0.01666666666f;

            if (_mpActive)
            {
                if (_isServer)
                {
                    AddShieldHit(ds.Shield.EntityId, damage, Session.Instance.MPEnergy, null, false, collisionAvg);
                }
            }
            else
            {
                EnergyHit           = true;
                WorldImpactPosition = collisionAvg;

                ds.EnergyHit           = true;
                ds.WorldImpactPosition = collisionAvg;

                Absorb    += damage;
                ImpactSize = damage;
                WebDamage  = true;
            }
        }