Added way to run events concurrently so that things like viewport panning can happen at the same time as something else.

This commit is contained in:
JoshuaNavarro 2019-12-05 01:40:29 -08:00
parent 1b1b6d227a
commit b184c654f4
4 changed files with 47 additions and 10 deletions

View File

@ -23,7 +23,7 @@ namespace Omegasis.HappyBirthday.Framework
EventHelper e = new EventHelper("CommunityCenterBirthday",19950, conditions, new EventStartData(EventStartData.MusicToPlayType.Continue, 32, 16, new EventStartData.FarmerData(32, 22, EventHelper.FacingDirection.Up),new List<EventStartData.NPCData>())); EventHelper e = new EventHelper("CommunityCenterBirthday",19950, conditions, new EventStartData(EventStartData.MusicToPlayType.Continue, 32, 16, new EventStartData.FarmerData(32, 22, EventHelper.FacingDirection.Up),new List<EventStartData.NPCData>()));
e.globalFadeIn(); e.globalFadeIn();
e.moveFarmerUp(6, EventHelper.FacingDirection.Up, false); e.moveFarmerUp(6, EventHelper.FacingDirection.Up, false);
e.ViewportLerpTileOffset(new Microsoft.Xna.Framework.Point(0,-6),60*6); e.ViewportLerpTileOffset(new Microsoft.Xna.Framework.Point(0,-6),60*6,true);
e.addObjectToPlayersInventory(64, 22,true); e.addObjectToPlayersInventory(64, 22,true);
e.showMessage("Community center birthday here."); e.showMessage("Community center birthday here.");

View File

@ -58,7 +58,7 @@ namespace StardustCore.Events
/// <param name="EventHelper"></param> /// <param name="EventHelper"></param>
/// <param name="NewTilePositionOffset"></param> /// <param name="NewTilePositionOffset"></param>
/// <param name="Speed">How many frames (aka update ticks) it takes to finish. Aka 60~=1 second</param> /// <param name="Speed">How many frames (aka update ticks) it takes to finish. Aka 60~=1 second</param>
public static void ViewportLerpTileOffset(this EventHelper EventHelper, Point NewTilePositionOffset, int Frames=60) public static void ViewportLerpTileOffset(this EventHelper EventHelper, Point NewTilePositionOffset, int Frames=60,bool Concurrent=false)
{ {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
b.Append("Omegasis.EventFramework.ViewportLerp "); b.Append("Omegasis.EventFramework.ViewportLerp ");
@ -67,6 +67,8 @@ namespace StardustCore.Events
b.Append((NewTilePositionOffset.Y * Game1.tileSize)); b.Append((NewTilePositionOffset.Y * Game1.tileSize));
b.Append(" "); b.Append(" ");
b.Append(Frames); b.Append(Frames);
b.Append(" ");
b.Append(Concurrent);
EventHelper.add(b); EventHelper.add(b);
} }

View File

@ -17,16 +17,18 @@ namespace StardustCore.Events
/// <summary> /// <summary>
/// Event logic that occurs when the specialized command appears. /// Event logic that occurs when the specialized command appears.
/// </summary> /// </summary>
public Dictionary<string, Action<string>> customEventLogic; public Dictionary<string, Action<EventManager,string>> customEventLogic;
public Dictionary<string, Action<EventManager,string>> concurrentEventActions;
public bool eventStarted; public bool eventStarted;
public EventManager() public EventManager()
{ {
this.events = new Dictionary<string, EventHelper>(); this.events = new Dictionary<string, EventHelper>();
this.customEventLogic = new Dictionary<string, Action<string>>(); this.customEventLogic = new Dictionary<string, Action<EventManager,string>>();
this.concurrentEventActions = new Dictionary<string, Action<EventManager,string>>();
this.customEventLogic.Add("Omegasis.EventFramework.AddObjectToPlayersInventory", ExtraEventActions.addObjectToPlayerInventory); this.customEventLogic.Add("Omegasis.EventFramework.AddObjectToPlayersInventory", ExtraEventActions.addObjectToPlayerInventory);
this.customEventLogic.Add("Omegasis.EventFramework.ViewportLerp", ExtraEventActions.ViewportLerp); this.customEventLogic.Add("Omegasis.EventFramework.ViewportLerp", ExtraEventActions.ViewportLerp);
@ -46,11 +48,17 @@ namespace StardustCore.Events
/// </summary> /// </summary>
/// <param name="CommandName"></param> /// <param name="CommandName"></param>
/// <param name="Function"></param> /// <param name="Function"></param>
public void addCustomEventLogic(string CommandName,Action<string> Function) public void addCustomEventLogic(string CommandName,Action<EventManager,string> Function)
{ {
this.customEventLogic.Add(CommandName, Function); this.customEventLogic.Add(CommandName, Function);
} }
public void addConcurrentEvent(string EventData,Action<EventManager,string> Function)
{
this.concurrentEventActions.Add(EventData, Function);
}
/// <summary> /// <summary>
/// Hooked into the game's update tick. /// Hooked into the game's update tick.
/// </summary> /// </summary>
@ -59,9 +67,23 @@ namespace StardustCore.Events
if (Game1.CurrentEvent == null) return; if (Game1.CurrentEvent == null) return;
string commandName = this.getGameCurrentEventCommandStringName(); string commandName = this.getGameCurrentEventCommandStringName();
//HappyBirthday.ModMonitor.Log("Current event command name is: " + commandName, StardewModdingAPI.LogLevel.Info); //HappyBirthday.ModMonitor.Log("Current event command name is: " + commandName, StardewModdingAPI.LogLevel.Info);
foreach(KeyValuePair<string,Action<EventManager,string>> pair in this.concurrentEventActions)
{
pair.Value.Invoke(this,pair.Key);
}
if (string.IsNullOrEmpty(commandName)) return; if (string.IsNullOrEmpty(commandName)) return;
if (this.customEventLogic.ContainsKey(commandName)){ if (this.customEventLogic.ContainsKey(commandName)){
this.customEventLogic[commandName].Invoke(this.getGameCurrentEventCommandString()); this.customEventLogic[commandName].Invoke(this,this.getGameCurrentEventCommandString());
}
}
public virtual void finishConcurrentEvent(string Key)
{
if (this.concurrentEventActions.ContainsKey(Key))
{
this.concurrentEventActions.Remove(Key);
} }
} }

View File

@ -22,7 +22,7 @@ namespace StardustCore.Events
/// Adds the item from Game1.ObjectInformation to the player's inventory from the given event string. /// Adds the item from Game1.ObjectInformation to the player's inventory from the given event string.
/// </summary> /// </summary>
/// <param name="EventData"></param> /// <param name="EventData"></param>
public static void addObjectToPlayerInventory(string EventData) public static void addObjectToPlayerInventory(EventManager EventManager,string EventData)
{ {
string[] splits = EventData.Split(' '); string[] splits = EventData.Split(' ');
string name = splits[0]; string name = splits[0];
@ -34,7 +34,7 @@ namespace StardustCore.Events
Game1.CurrentEvent.CurrentCommand++; Game1.CurrentEvent.CurrentCommand++;
} }
public static void ViewportLerp(string EventData) public static void ViewportLerp(EventManager EventManager,string EventData)
{ {
string[] splits = EventData.Split(' '); string[] splits = EventData.Split(' ');
string name = splits[0]; string name = splits[0];
@ -42,6 +42,15 @@ namespace StardustCore.Events
int xEndPosition =Convert.ToInt32(splits[1]); int xEndPosition =Convert.ToInt32(splits[1]);
int yEndPosition = Convert.ToInt32(splits[2]); int yEndPosition = Convert.ToInt32(splits[2]);
int frames = Convert.ToInt32(splits[3]); int frames = Convert.ToInt32(splits[3]);
bool concurrent = Convert.ToBoolean(splits[4]);
if (concurrent)
{
if (EventManager.concurrentEventActions.ContainsKey(EventData)==false)
{
EventManager.addConcurrentEvent(EventData, ViewportLerp);
++Game1.CurrentEvent.CurrentCommand; //I've been told ++<int> is more efficient than <int>++;
}
}
if (StartedLerp==false) if (StartedLerp==false)
{ {
@ -58,7 +67,11 @@ namespace StardustCore.Events
OldViewportPosition = new Point(0, 0); OldViewportPosition = new Point(0, 0);
CurrentViewportLerpAmount = 0; CurrentViewportLerpAmount = 0;
StartedLerp = false; StartedLerp = false;
++Game1.CurrentEvent.CurrentCommand; //I've been told ++<int> is more efficient than <int>++; if(concurrent==false)++Game1.CurrentEvent.CurrentCommand; //I've been told ++<int> is more efficient than <int>++;
else
{
EventManager.finishConcurrentEvent(EventData);
}
return; return;
} }
Vector2 currentLerp = Vector2.Lerp(new Vector2(OldViewportPosition.X, OldViewportPosition.Y), new Vector2(OldViewportPosition.X + xEndPosition, OldViewportPosition.Y + yEndPosition), (float)((float)CurrentViewportLerpAmount/(float)frames)); Vector2 currentLerp = Vector2.Lerp(new Vector2(OldViewportPosition.X, OldViewportPosition.Y), new Vector2(OldViewportPosition.X + xEndPosition, OldViewportPosition.Y + yEndPosition), (float)((float)CurrentViewportLerpAmount/(float)frames));