public bool Tick(bool autopilotEnabled) { if (ExecuteUntil > DateTime.UtcNow && CurrentAction != null) { CurrentAction.Execute(); return(true); } if (LastActionExecuted + Cooldown <= DateTime.UtcNow) { IEnumerable <IIdleAction> filteredActions = IdleActions.Where(e => (!e.AutopilotOnly || autopilotEnabled) && !LastActions.Any(e => e.Value == e.GetType()) || LastActions.Where(x => x.Value == e.GetType() && (DateTime.UtcNow - x.Key).TotalMilliseconds > Rnd.Next(CurrentAction.MinCooldown, CurrentAction.MaxCooldown)).Any()); if (filteredActions.Any()) { CurrentAction = filteredActions.ElementAtOrDefault(Rnd.Next(0, filteredActions.Count())); if (CurrentAction != null && CurrentAction.Enter()) { LastActionExecuted = DateTime.UtcNow; Cooldown = TimeSpan.FromMilliseconds(Rnd.Next(MinCooldown, MaxCooldown)); ExecuteUntil = LastActionExecuted + TimeSpan.FromMilliseconds(Rnd.Next(CurrentAction.MinDuration, CurrentAction.MaxDuration)); LastActions.Add(new KeyValuePair <DateTime, Type>(LastActionExecuted, CurrentAction.GetType())); CurrentAction.Execute(); return(true); } } } return(false); }
public virtual void UpdateTask() { if (Actions.Count == 0) { return; // No More Actions } if (CurrentAction.IsComplete) // If Action is complete, pop from queue { //Debug.Log("Ship [" + gameObject.name + "] doing [" + GetType().Name + "] completed action [" + Actions.Peek().GetType().Name + "]."); Actions.Dequeue().Exit(); if (Actions.Count > 0) { //Debug.Log("Ship [" + gameObject.name + "] doing [" + GetType().Name + "] completed action [" + Actions.Peek().GetType().Name + "]."); CurrentAction.Enter(); } else { //SDebug.Log("Ship [" + gameObject.name + "] doing [" + GetType().Name + "] completed ALL actions."); } stageBegan = Time.realtimeSinceStartup; } if (CurrentAction != null) { CurrentAction.UpdateAction(); } }
public bool Tick(bool autopilotEnabled) { if (ExecuteUntil > DateTime.UtcNow && CurrentAction != null) { CurrentAction.Execute(); return(true); } // cleanup old events LastActions.RemoveAll(e => e.Key < e.Value.Cooldown); if (LastActionExecuted + Cooldown <= DateTime.UtcNow) { IEnumerable <IIdleAction> filteredActions = IdleActions.Where ( e => Config.IdleActionsEnabled.TryGetValue(e.ToString(), out bool b) && b && (!e.AutopilotOnly || autopilotEnabled) && DateTime.Now > e.Cooldown ); if (filteredActions.Any()) { CurrentAction = filteredActions.ElementAtOrDefault(Rnd.Next(0, filteredActions.Count())); if (CurrentAction != null && CurrentAction.Enter()) { LastActionExecuted = DateTime.UtcNow; Cooldown = TimeSpan.FromMilliseconds(Rnd.Next(MinActionCooldown, MaxActionCooldown)); CurrentAction.Cooldown = DateTime.Now + TimeSpan.FromMilliseconds(Rnd.Next(CurrentAction.MinCooldown, CurrentAction.MaxCooldown)); ExecuteUntil = LastActionExecuted + TimeSpan.FromMilliseconds(Rnd.Next(CurrentAction.MinDuration, CurrentAction.MaxDuration)); LastActions.Add(new(LastActionExecuted, CurrentAction)); CurrentAction.Execute(); return(true); } } } return(false); }