Ejemplo n.º 1
0
        internal TurretAIUpdate(GameObject gameObject, TurretAIUpdateModuleData moduleData)
        {
            _gameObject = gameObject;
            _moduleData = moduleData;

            _gameObject.TurretYaw   = MathUtility.ToRadians(_moduleData.NaturalTurretAngle);
            _gameObject.TurretPitch = MathUtility.ToRadians(_moduleData.NaturalTurretPitch);

            _turretAIstate = _moduleData.InitiallyDisabled ? TurretAIStates.Disabled : TurretAIStates.ScanningForTargets;
        }
Ejemplo n.º 2
0
        internal void Update(BehaviorUpdateContext context, BitArray <AutoAcquireEnemiesType> autoAcquireEnemiesWhenIdle)
        {
            var deltaTime = (float)context.Time.DeltaTime.TotalSeconds;

            var   target = _gameObject.CurrentWeapon?.CurrentTarget;
            float targetYaw;

            if (_gameObject.ModelConditionFlags.Get(ModelConditionFlag.Moving))
            {
                _turretAIstate = TurretAIStates.Recentering;
                _gameObject.CurrentWeapon?.SetTarget(null);
            }

            switch (_turretAIstate)
            {
            case TurretAIStates.Disabled:
                break;     // TODO: how does it get enabled?

            case TurretAIStates.Idle:
                if (target != null)
                {
                    _turretAIstate = TurretAIStates.Turning;
                    _currentTarget = target;
                }
                else if (context.Time.TotalTime > _waitUntil && (autoAcquireEnemiesWhenIdle?.Get(AutoAcquireEnemiesType.Yes) ?? true))
                {
                    _turretAIstate = TurretAIStates.ScanningForTargets;
                }
                break;

            case TurretAIStates.ScanningForTargets:
                if (target == null)
                {
                    if (!FoundTargetWhileScanning(context, autoAcquireEnemiesWhenIdle))
                    {
                        var scanInterval =
                            context.GameContext.Random.NextDouble() *
                            (_moduleData.MaxIdleScanInterval - _moduleData.MinIdleScanInterval) +
                            _moduleData.MinIdleScanInterval;
                        _waitUntil     = context.Time.TotalTime + TimeSpan.FromMilliseconds(scanInterval);
                        _turretAIstate = TurretAIStates.Idle;
                        break;
                    }
                }

                if (!_moduleData.FiresWhileTurning)
                {
                    _gameObject.ModelConditionFlags.Set(ModelConditionFlag.Attacking, false);
                }

                _turretAIstate = TurretAIStates.Turning;
                break;

            case TurretAIStates.Turning:
                if (target == null)
                {
                    _waitUntil     = context.Time.TotalTime + TimeSpan.FromMilliseconds(_moduleData.RecenterTime);
                    _turretAIstate = TurretAIStates.Recentering;
                    break;
                }

                var directionToTarget = (target.TargetPosition - _gameObject.Translation).Vector2XY();
                targetYaw = MathUtility.GetYawFromDirection(directionToTarget) - _gameObject.Yaw;

                if (Rotate(targetYaw, deltaTime))
                {
                    break;
                }

                if (!_moduleData.FiresWhileTurning)
                {
                    _gameObject.ModelConditionFlags.Set(ModelConditionFlag.Attacking, true);
                }

                _turretAIstate = TurretAIStates.Attacking;
                break;

            case TurretAIStates.Attacking:
                if (target == null)
                {
                    _waitUntil     = context.Time.TotalTime + TimeSpan.FromMilliseconds(_moduleData.RecenterTime);
                    _turretAIstate = TurretAIStates.Recentering;
                }
                else if (target != _currentTarget)
                {
                    _turretAIstate = TurretAIStates.Turning;
                    _currentTarget = target;
                }
                break;

            case TurretAIStates.Recentering:
                if (context.Time.TotalTime > _waitUntil)
                {
                    targetYaw = MathUtility.ToRadians(_moduleData.NaturalTurretAngle);
                    if (!Rotate(targetYaw, deltaTime))
                    {
                        _turretAIstate = TurretAIStates.Idle;
                    }
                }
                break;
            }
        }