using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using StardewValley; namespace StardustCore.Events { public class EventManager { /// /// The list of events that this event manager is holding. /// public Dictionary events; /// /// Event logic that occurs when the specialized command appears. /// public Dictionary> customEventLogic; public Dictionary concurrentEventActions; public EventManager() { this.events = new Dictionary(); this.customEventLogic = new Dictionary>(); this.concurrentEventActions = new Dictionary(); this.customEventLogic.Add("Omegasis.EventFramework.AddObjectToPlayersInventory", ExtraEventActions.addObjectToPlayerInventory); this.customEventLogic.Add("Omegasis.EventFramework.ViewportLerp", ExtraEventActions.ViewportLerp); this.customEventLogic.Add("Omegasis.EventFramework.AddInJunimoActor", ExtraEventActions.AddInJumimoActorForEvent); this.customEventLogic.Add("Omegasis.EventFramework.FlipJunimoActor", ExtraEventActions.FlipJunimoActor); this.customEventLogic.Add("Omegasis.EventFramework.SetUpAdvanceJunimoMovement", ExtraEventActions.SetUpAdvanceJunimoMovement); this.customEventLogic.Add("Omegasis.EventFramework.FinishAdvanceJunimoMovement", ExtraEventActions.FinishAdvanceJunimoMovement); this.customEventLogic.Add("Omegasis.EventFramework.AddInJunimoAdvanceMove", ExtraEventActions.AddInJunimoAdvanceMove); this.customEventLogic.Add("Omegasis.EventFramework.RemoveJunimoAdvanceMove", ExtraEventActions.RemoveAdvanceJunimoMovement); } /// /// Adds an event to the dictionary of events. /// /// public void addEvent(EventHelper Event) { this.events.Add(Event.eventName, Event); } /// /// Adds in custom code that happens when the game's current event sees the given command name. /// /// /// public void addCustomEventLogic(string CommandName,Action Function) { this.customEventLogic.Add(CommandName, Function); } public void addConcurrentEvent(ConcurrentEventInformation EventInfo) { this.concurrentEventActions.Add(EventInfo.id, EventInfo); } /// /// Hooked into the game's update tick. /// public virtual void update() { if (Game1.CurrentEvent == null) { if (this.concurrentEventActions.Count > 0) { this.concurrentEventActions.Clear(); } return; } string commandName = this.getGameCurrentEventCommandStringName(); //HappyBirthday.ModMonitor.Log("Current event command name is: " + commandName, StardewModdingAPI.LogLevel.Info); List _eventGC = new List(); foreach (KeyValuePair eventInfo in this.concurrentEventActions) { if (eventInfo.Value.finished) { _eventGC.Add(eventInfo.Key); } eventInfo.Value.invokeIfNotFinished(); } foreach(string garbage in _eventGC) { this.concurrentEventActions.Remove(garbage); } if (string.IsNullOrEmpty(commandName)) return; if (this.customEventLogic.ContainsKey(commandName)){ this.customEventLogic[commandName].Invoke(this,this.getGameCurrentEventCommandString()); } } public virtual void finishConcurrentEvent(string Key) { if (this.concurrentEventActions.ContainsKey(Key)) { this.concurrentEventActions[Key].finish(); } } /// /// Gets the event from this list of events. /// /// /// public EventHelper getEvent(string Name) { if (this.events.ContainsKey(Name)) { return this.events[Name]; } else { return null; } } /// /// Starts the event with the given id if possible. /// /// public virtual void startEventAtLocationIfPossible(string EventName) { if (this.events.ContainsKey(EventName)) { if (Game1.eventUp == true) return; this.concurrentEventActions.Clear(); //Clean all old parallel actions before starting a new event. this.events[EventName].startEventAtLocationifPossible(); } } /// /// Clears the event from the farmer. /// /// public void clearEventFromFarmer(string EventName) { this.events.TryGetValue(EventName, out EventHelper e); if (e == null) return; Game1.player.eventsSeen.Remove(e.getEventID()); } /// /// Gets the current command and all of it's data. /// /// public virtual string getGameCurrentEventCommandString() { if (Game1.CurrentEvent != null) { return Game1.CurrentEvent.currentCommandString(); } else { return ""; } } /// /// Gets the name of the action for the current command in the string of event commands. /// /// public virtual string getGameCurrentEventCommandStringName() { if (Game1.CurrentEvent != null) { return Game1.CurrentEvent.currentCommandStringName(); } else { return ""; } } } }