Skip to content

Commit

Permalink
[KK, KKS] Add GameApi.GetCurrentHeroine and GetTalkScene
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Oct 9, 2021
1 parent deac06f commit 2fc9c93
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
38 changes: 38 additions & 0 deletions src/KKAPI/MainGame/GameApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using HarmonyLib;
using Illusion.Component;
using KKAPI.Studio;
using KKAPI.Utilities;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
Expand Down Expand Up @@ -485,5 +486,42 @@ public static ADVScene GetADVScene()
return ActionControl.initialized ? ActionControl.instance.actionScene?.AdvScene : null;
#endif
}

/// <summary>
/// Gets the TalkScene instance if it's initialized, null otherwise
/// </summary>
public static TalkScene GetTalkScene()
{
return GameObject.FindObjectOfType<TalkScene>();
}

/// <summary>
/// Find heroine that is currently in focus (in a talk or adv scene, leading girl in H scene).
/// null if not found.
/// </summary>
public static SaveData.Heroine GetCurrentHeroine()
{
var advScene = GetADVScene();
if (advScene != null)
{
if (advScene.Scenario != null && advScene.Scenario.currentHeroine != null)
return advScene.Scenario.currentHeroine;
if (advScene.nowScene is TalkScene s && s.targetHeroine != null)
return s.targetHeroine;
}

var hFlag = GameObject.FindObjectOfType<HFlag>();
if (hFlag != null)
return hFlag.GetLeadingHeroine();

var talkScene = GetTalkScene();
if (talkScene != null)
{
var result = talkScene.targetHeroine;
if (result != null) return result;
}

return null;
}
}
}
47 changes: 43 additions & 4 deletions src/KKSAPI/MainGame/GameApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using HarmonyLib;
using Illusion.Component;
using KKAPI.Studio;
using KKAPI.Utilities;
using Manager;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
Expand Down Expand Up @@ -61,13 +63,13 @@ public static partial class GameAPI
/// Runs immediately after all <see cref="GameCustomFunctionController"/> objects trigger their events.
/// </summary>
public static event EventHandler<DayChangeEventArgs> DayChange;

/// <summary>
/// Triggered when the current time of the day changes in story mode.
/// Runs immediately after all <see cref="GameCustomFunctionController"/> objects trigger their events.
/// </summary>
public static event EventHandler<PeriodChangeEventArgs> PeriodChange;

/// <summary>
/// Triggered when a new game is started in story mode.
/// Runs immediately after all <see cref="GameCustomFunctionController"/> objects trigger their events.
Expand Down Expand Up @@ -454,7 +456,7 @@ public PeriodChangeEventArgs(Cycle.Type period)

public Cycle.Type NewPeriod { get; }
}

/// <summary>
/// Gets the ActionControl instance if it's initialized, null otherwise
/// </summary>
Expand All @@ -466,7 +468,7 @@ public static ActionControl GetActionControl()
return ActionControl.initialized ? ActionControl.instance : null;
#endif
}

/// <summary>
/// Gets the ADVScene instance if it's initialized, null otherwise
/// </summary>
Expand All @@ -478,5 +480,42 @@ public static ADVScene GetADVScene()
return ActionControl.initialized ? ActionControl.instance.actionScene?.AdvScene : null;
#endif
}

/// <summary>
/// Gets the TalkScene instance if it's initialized, null otherwise
/// </summary>
public static TalkScene GetTalkScene()
{
return TalkScene.initialized ? TalkScene.instance : null;
}

/// <summary>
/// Find heroine that is currently in focus (in a talk or adv scene, leading girl in H scene).
/// null if not found.
/// </summary>
public static SaveData.Heroine GetCurrentHeroine()
{
var advScene = GetADVScene();
if (advScene != null)
{
if (advScene.Scenario != null && advScene.Scenario.currentHeroine != null)
return advScene.Scenario.currentHeroine;
if (advScene.nowScene is TalkScene s && s.targetHeroine != null)
return s.targetHeroine;
}

var hFlag = GameObject.FindObjectOfType<HFlag>();
if (hFlag != null)
return hFlag.GetLeadingHeroine();

var talkScene = GetTalkScene();
if (talkScene != null)
{
var result = talkScene.targetHeroine;
if (result != null) return result;
}

return null;
}
}
}

0 comments on commit 2fc9c93

Please sign in to comment.