Esempio n. 1
0
            public WeaponFSM(string name, BaseWeapon weapon) : base(name, weapon)
            {
                if (!weapon)
                {
                    // TODO: Add log
                    LogHalt("BaseWeapon object null!");
                    Halt();
                }

                idleState     = new IdleState(this, EWeaponState.IDLE, "STATE_IDLE", weapon);
                windupState   = new WindupState(this, EWeaponState.WINDUP, "STATE_WINDUP", weapon);
                fireState     = new FireState(this, EWeaponState.FIRING, "STATE_FIRING", weapon);
                cooldownState = new CooldownState(this, EWeaponState.COOLDOWN, "STATE_COOLDOWN", weapon);

                idleState.AddTransition(new Tran_IdleToWindup(windupState, weapon));

                windupState.AddTransition(new Tran_WindupToIdle(idleState, weapon));
                windupState.AddTransition(new Tran_WindupToFire(fireState, weapon));

                fireState.AddTransition(new Tran_FireToCooldown(cooldownState, weapon));
                fireState.AddTransition(new Tran_FireToIdle(idleState, weapon));
                fireState.AddTransition(new Tran_FireToFire(fireState, weapon));

                AddState(idleState);
                AddState(windupState);
                AddState(fireState);
                AddState(cooldownState);
            }
Esempio n. 2
0
 private void Initialize()
 {
     // Set initial states.
     _groundState   = new GroundState.OnGroundState(this);
     _actionState   = new ActionState.WalkState(this);
     _cooldownState = new CooldownState.ReadyState(this);
 }
Esempio n. 3
0
    void Start()
    {
        var idle = new IdleState <Feed>();

        shooting = new ShootingState <Feed>(this.transform, target, this);
        var cooldown = new CooldownState <Feed>();

        idle.AddTransition(Feed.EnemigoEntraEnLOS, shooting);

        shooting.AddTransition(Feed.EnemigoSaleDeLOS, idle);
        shooting.AddTransition(Feed.ArmaRecalentada, cooldown);

        cooldown.AddTransition(Feed.ArmaEnfriada, shooting);
        cooldown.AddTransition(Feed.EnemigoSaleDeLOS, idle);

        stateMachine = new FSM <Feed>(idle);

        shootState = true;
    }
Esempio n. 4
0
 private void Initialize()
 {
     // Set initial states.
     _groundState   = new GroundState.OnGroundState (this);
     _actionState   = new ActionState.WalkState (this);
     _cooldownState = new CooldownState.ReadyState (this);
 }
        /// <summary>
        /// Performs the action with cooldown for the given id or hash.
        /// For example:
        ///     var cdm = new CooldownManager();
        ///     cdm.PerformActionWithCooldown(error.Message, () => {
        ///         CreateErrorTicket();
        ///     });
        /// </summary>
        /// <param name="idOrHash">The identifier or hash.</param>
        /// <param name="action">The action.</param>
        /// <param name="force">if set to <c>true</c> [force].</param>
        public void PerformActionWithCooldown(string idOrHash, Action action, bool force = false)
        {
            // Force the action?
            if (force == true)
            {
                action();
            }
            else
            {
                // House keeping...
                RemoveOldStates();

                // Get the state
                CooldownState state = null;
                lock (_cooldownLock) {
                    if (!_cooldownStates.ContainsKey(idOrHash))
                    {
                        // New
                        state = new CooldownState();
                        _cooldownStates.Add(idOrHash, state);
                    }
                    else
                    {
                        // Exists
                        state = _cooldownStates[idOrHash];
                    }
                }

                // Cooldown?
                var minActionTimespan = new TimeSpan(0, 0, (int)Math.Pow(this.MinActionIntervalSeconds, state.FalloffFactor));
                //_log("ts=" + minActionTimespan.TotalSeconds+"   -- is="+this.MinActionIntervalSeconds+"   -- ff="+ state.FalloffFactor+" s="+Math.Pow(this.MinActionIntervalSeconds,state.FalloffFactor));
                if (DateTime.UtcNow < state.Timestamp + minActionTimespan)
                {
                    // Cool down
                    state.CooldownHits++;
                    //_log("cooldown f="+state.FalloffFactor+" h="+state.CooldownHits + " ts="+minActionTimespan.TotalSeconds);
                }
                else
                {
                    if (state.CooldownHits > 0)
                    {
                        state.FalloffFactor += this.FalloffFactorStep;
                    }
                    else
                    {
                        state.FalloffFactor -= this.FalloffFactorStep;
                    }
                    if (state.FalloffFactor < 1)
                    {
                        state.FalloffFactor = 1;
                    }
                    if (state.FalloffFactor > this.MaxFalloffFactor)
                    {
                        state.FalloffFactor = this.MaxFalloffFactor;
                    }

                    state.Timestamp    = DateTime.UtcNow;
                    state.CooldownHits = 0;

                    // Do action
                    action();
                    //_log("action f="+state.FalloffFactor);
                }
            }
        }