Tweaks for setting weather
This commit is contained in:
@@ -106,9 +106,16 @@ public interface IBattle : IScriptSource, IDeepCloneable
|
||||
bool TrySetChoice(ITurnChoice choice);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current weather for the battle. If null is passed, this clears the weather.
|
||||
/// The script that handles the current weather of the battle.
|
||||
/// </summary>
|
||||
void SetWeather(StringKey? weatherName);
|
||||
IReadOnlyScriptContainer WeatherScript { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current weather for the battle. If null is passed, this clears the weather.
|
||||
/// A duration can be passed to set the duration of the weather in turns. This duration can be modified by
|
||||
/// other scripts before the weather is set through the <see cref="Script.ChangeWeatherDuration"/> script hook.
|
||||
/// </summary>
|
||||
bool SetWeather(StringKey? weatherName, int duration);
|
||||
|
||||
public IScriptSet Volatile { get; }
|
||||
|
||||
@@ -343,28 +350,37 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
EventHook.Invoke(new EndTurnEvent());
|
||||
}
|
||||
|
||||
private readonly ScriptContainer _weatherScript = new();
|
||||
private ScriptContainer _weatherScript = new();
|
||||
public IReadOnlyScriptContainer WeatherScript => _weatherScript;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void SetWeather(StringKey? weatherName)
|
||||
public bool SetWeather(StringKey? weatherName, int duration)
|
||||
{
|
||||
if (weatherName.HasValue)
|
||||
{
|
||||
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Weather, weatherName.Value, null, out var script))
|
||||
throw new InvalidOperationException($"Weather script {weatherName} not found.");
|
||||
|
||||
if (script is IWeatherScript weatherScript)
|
||||
{
|
||||
this.RunScriptHook(x => x.ChangeWeatherDuration(weatherName.Value, ref duration));
|
||||
weatherScript.SetTurns(duration);
|
||||
}
|
||||
|
||||
_weatherScript.Set(script);
|
||||
}
|
||||
else
|
||||
{
|
||||
_weatherScript.Clear();
|
||||
}
|
||||
return true;
|
||||
// TODO: Trigger weather change script hooks
|
||||
}
|
||||
|
||||
public IScriptSet Volatile { get; } = new ScriptSet();
|
||||
|
||||
/// <inheritdoc />
|
||||
public StringKey? WeatherName => _weatherScript.Script?.Name;
|
||||
public StringKey? WeatherName => WeatherScript.Script?.Name;
|
||||
|
||||
private readonly ScriptContainer _terrainScript = new();
|
||||
|
||||
@@ -415,7 +431,7 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
/// <inheritdoc />
|
||||
public override void GetOwnScripts(List<IEnumerable<ScriptContainer>> scripts)
|
||||
{
|
||||
scripts.Add(_weatherScript);
|
||||
scripts.Add(WeatherScript);
|
||||
scripts.Add(_terrainScript);
|
||||
scripts.Add(Volatile);
|
||||
}
|
||||
|
||||
@@ -814,6 +814,7 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
public void SuppressAbility()
|
||||
{
|
||||
AbilitySuppressed = true;
|
||||
AbilityScript.Clear();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -1130,6 +1131,15 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
public void ChangeAbility(IAbility ability)
|
||||
{
|
||||
OverrideAbility = ability;
|
||||
if (Library.ScriptResolver.TryResolve(ScriptCategory.Ability, ability.Name, ability.Parameters,
|
||||
out var abilityScript))
|
||||
{
|
||||
AbilityScript.Set(abilityScript);
|
||||
}
|
||||
else
|
||||
{
|
||||
AbilityScript.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
6
PkmnLib.Dynamic/ScriptHandling/IWeatherScript.cs
Normal file
6
PkmnLib.Dynamic/ScriptHandling/IWeatherScript.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
|
||||
public interface IWeatherScript
|
||||
{
|
||||
public void SetTurns(int turns);
|
||||
}
|
||||
@@ -559,4 +559,8 @@ public abstract class Script : IDeepCloneable
|
||||
ref int modifiedAccuracy)
|
||||
{
|
||||
}
|
||||
|
||||
public virtual void ChangeWeatherDuration(StringKey weatherName, ref int duration)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,24 @@ using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
|
||||
public interface IReadOnlyScriptContainer : IEnumerable<ScriptContainer>, IDeepCloneable
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether this container is empty.
|
||||
/// </summary>
|
||||
public bool IsEmpty { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The script in this container.
|
||||
/// </summary>
|
||||
public Script? Script { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A holder class for a script. This is used so we can cache a list of these, and iterate over them, even when
|
||||
/// the underlying script changes.
|
||||
/// </summary>
|
||||
public class ScriptContainer : IEnumerable<ScriptContainer>, IDeepCloneable
|
||||
public class ScriptContainer : IReadOnlyScriptContainer
|
||||
{
|
||||
/// <inheritdoc cref="ScriptContainer"/>
|
||||
public ScriptContainer()
|
||||
@@ -21,15 +34,11 @@ public class ScriptContainer : IEnumerable<ScriptContainer>, IDeepCloneable
|
||||
Script = script;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether this container is empty.
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
[MemberNotNullWhen(false, nameof(ScriptHandling.Script))]
|
||||
public bool IsEmpty => Script is null;
|
||||
|
||||
/// <summary>
|
||||
/// The script in this container.
|
||||
/// </summary>
|
||||
/// <inheritdoc />
|
||||
public Script? Script { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user