private void OldBulletHit(int cmdSeq, DefaultBulletSegment segment, ICompensationWorld world)
        {
            RaycastHit camDirHit;

            if (world.Raycast(segment.RaySegment, out camDirHit, _hitboxLayerMask))
            {
                _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntity, camDirHit, world);
            }
        }
        private void OldRaycast()
        {
            ICompensationWorld lastCompensationWorld = null;

            foreach (var segment in _allThrowingSegments)
            {
                if (!segment.IsValid)
                {
                    continue;
                }
                if (lastCompensationWorld != null)
                {
                    if (lastCompensationWorld.ServerTime != segment.ServerTime)
                    {
                        lastCompensationWorld.Release();
                        lastCompensationWorld = _compensationWorldFactory.CreateCompensationWorld(segment.ServerTime);
                    }
                }
                else
                {
                    lastCompensationWorld = _compensationWorldFactory.CreateCompensationWorld(segment.ServerTime);
                    if (lastCompensationWorld == null)
                    {
                        _logger.ErrorFormat("create compensation world at time {0}, FAILED", segment.ServerTime);
                    }
                    else
                    {
                        if (_logger.IsDebugEnabled)
                        {
                            _logger.DebugFormat("create compensation world at time {0}, SUCC", segment.ServerTime);
                        }
                    }
                }

                if (lastCompensationWorld != null)
                {
                    RaycastHit hit;
                    lastCompensationWorld.Self = segment.ThrowingEntity.ownerId.Value;
                    lastCompensationWorld.ExcludePlayerList = segment.ExcludePlayerList;
                    if (lastCompensationWorld.Raycast(segment.RaySegment, out hit, _layerMask))
                    {
                        CollisionHandler(segment, hit);
                    }
                }
            }
            if (lastCompensationWorld != null)
            {
                lastCompensationWorld.Release();
            }
        }
示例#3
0
        public virtual void OnHit(int cmdSeq, IBulletEntity bulletEntity, RaycastHit hit, ICompensationWorld compensationWorld)
        {
            bulletEntity.HitPoint = hit.point;
            Collider collider = hit.collider;

            if (collider == null)
            {
                _logger.ErrorFormat("bullet hit unknown collier {0}", bulletEntity.OwnerEntityKey);
                return;
            }

            PlayerEntity srcPlayer = _contexts.player.GetEntityWithEntityKey(bulletEntity.OwnerEntityKey);

            if (srcPlayer == null)
            {
                _logger.WarnFormat("bullet from unkown {0} hit environment {1}, collier {2}",
                                   bulletEntity.OwnerEntityKey, hit.point, collider.name);
                return;
            }

            PlayerEntity  targetPlayer  = null;
            VehicleEntity targetVehicle = null;

            var comp = hit.collider.transform.gameObject.GetComponent <HitBoxOwnerComponent>();

            if (comp != null)
            {
                targetPlayer  = _contexts.player.GetEntityWithEntityKey(comp.OwnerEntityKey);
                targetVehicle = _contexts.vehicle.GetEntityWithEntityKey(comp.OwnerEntityKey);
            }

            if (targetPlayer != null)
            {
                try
                {
                    _OnHitPlayer.BeginProfileOnlyEnableProfile();
                    Vector3 pos;
                    if (compensationWorld.TryGetEntityPosition(targetPlayer.entityKey.Value, out pos))
                    {
                        OnHitPlayer(_contexts, srcPlayer, targetPlayer, bulletEntity, hit, pos, cmdSeq);
                    }
                    else
                    {
                        _logger.ErrorFormat("cant get player compensation position with key {0}", targetPlayer.entityKey.Value);
                        OnHitPlayer(_contexts, srcPlayer, targetPlayer, bulletEntity, hit, targetPlayer.position.Value, cmdSeq);
                    }
                    bulletEntity.HitType = EHitType.Player;
                }
                finally
                {
                    _OnHitPlayer.EndProfileOnlyEnableProfile();
                }
                return;
            }

            if (targetVehicle != null)
            {
                try
                {
                    _OnHitVehicle.BeginProfileOnlyEnableProfile();
                    OnHitVehicle(srcPlayer, targetVehicle, bulletEntity, hit);
                    bulletEntity.HitType = EHitType.Vehicle;
                }
                finally
                {
                    _OnHitVehicle.EndProfileOnlyEnableProfile();
                }
                return;
            }

            try
            {
                _OnHitEnvironment.BeginProfileOnlyEnableProfile();
                OnHitEnvironment(srcPlayer, bulletEntity, hit);
                bulletEntity.HitType = EHitType.Environment;
            }
            finally
            {
                _OnHitEnvironment.EndProfileOnlyEnableProfile();
            }
        }
        private void NewBulletHit(int cmdSeq, DefaultBulletSegment segment, ICompensationWorld world)
        {
            RaycastHit camDirHit;

            segment.BulletEntity.IsNew = false;
            RaycastHit gunDirHit;
            var        camRaySegment       = segment.RaySegment;
            bool       checkGunDirObstacle = false;

            while (segment.BulletEntity.IsValid &&
                   world.Raycast(camRaySegment, out camDirHit, _hitboxLayerMask))
            {
                if (!checkGunDirObstacle)
                {
                    checkGunDirObstacle = true;
                    //如果击中物体,从枪口向击中位置做检测,如果有物体,则使用枪口方向的结果
                    var startPosition     = segment.BulletEntity.GunEmitPosition;
                    var target            = camDirHit.point;
                    var dir               = target - startPosition;
                    var blockCheckSegment = new RaySegment()
                    {
                        Length = Vector3.Distance(target, startPosition) - RaycastStepOffset,
                        Ray    = new Ray(startPosition, dir.normalized),
                    };

                    while (segment.BulletEntity.IsValid &&
                           world.Raycast(blockCheckSegment, out gunDirHit,
                                         _hitboxLayerMask))
                    {
                        _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntity, gunDirHit, world);
                        blockCheckSegment.Ray.origin =
                            gunDirHit.point + blockCheckSegment.Ray.direction * RaycastStepOffset;
                    }
                }

                if (segment.BulletEntity.IsValid)
                {
                    _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntity, camDirHit, world);
                    camRaySegment.Ray.origin =
                        camDirHit.point + camRaySegment.Ray.direction * RaycastStepOffset;
                }
            }

            if (segment.BulletEntity.IsValid)
            {
                if (!checkGunDirObstacle)
                {
                    //如果没有击中物体,从枪口向第一帧末子弹到达的位置做检测,如果有物体,使用枪口方向的结果
                    var startPosition = segment.BulletEntity.GunEmitPosition;
                    var target        = segment.RaySegment.Ray.direction * segment.RaySegment.Length +
                                        segment.RaySegment.Ray.origin;
                    var dir = target - startPosition;
                    var blockCheckSegment = new RaySegment()
                    {
                        Length = Vector3.Distance(target, startPosition) - RaycastStepOffset,
                        Ray    = new Ray(startPosition, dir.normalized),
                    };
                    while (segment.BulletEntity.IsValid &&
                           world.Raycast(blockCheckSegment, out gunDirHit,
                                         _hitboxLayerMask))
                    {
                        checkGunDirObstacle = true;
                        _bulletHitHandler.OnHit(cmdSeq, segment.BulletEntity, gunDirHit, world);
                        blockCheckSegment.Ray.origin =
                            gunDirHit.point + blockCheckSegment.Ray.direction * RaycastStepOffset;
                    }
                }
            }
        }