Пример #1
0
        protected override void UpdateInternal(GameTime time)
        {
            var turretRotation = BasicHelpers.NormalizeAngle(TankHelper.ConstrainTurretRotation(
                                                                 null, null,
                                                                 Components["turret"].Rotation + Rotation,
                                                                 InputState.LookDirection,
                                                                 1.5f * (float)time.ElapsedGameTime.TotalSeconds
                                                                 ) - Rotation);

            //Network optimization - check if turret rotation changed without accounting for object rotation
            var uncorrected = BasicHelpers.NormalizeAngle(TankHelper.ConstrainTurretRotation(
                                                              null, null,
                                                              _uncorrectedRot,
                                                              InputState.LookDirection,
                                                              1.5f * (float)time.ElapsedGameTime.TotalSeconds
                                                              ));

            _uncorrectedRot = uncorrected;
            ComponentGroups["turret"].Rotation = turretRotation;

            if (Authoritative && MathHelper.Distance(_lastStateChangeRotation, uncorrected) > 0.05)
            {
                RaiseStateChangeEvent(a => a.Write(turretRotation));
                _lastStateChangeRotation = uncorrected;
            }
            Animations["death_explosion"].Mask = ColorMask;
            base.UpdateInternal(time);
        }
Пример #2
0
        public static float ConstrainTurretRotation(float?minRotation, float?maxRotation, float currentRotation, float targetRotation, float maxSpeed)
        {
            if (minRotation != null && maxRotation != null)
            {
                if (targetRotation < minRotation)
                {
                    targetRotation = minRotation.Value;
                }
                if (targetRotation > maxRotation)
                {
                    targetRotation = maxRotation.Value;
                }
            }

            currentRotation = BasicHelpers.NormalizeAngle(currentRotation);
            targetRotation  = BasicHelpers.NormalizeAngle(targetRotation);

            var shortestDistance = BasicHelpers.NormalizeAngle(
                targetRotation - currentRotation + MathHelper.Pi) - MathHelper.Pi;

            if (shortestDistance < 0)
            {
                return(currentRotation + Math.Max(shortestDistance, -maxSpeed));
            }

            return(currentRotation + Math.Min(shortestDistance, maxSpeed));
        }