Skip to content

Commit

Permalink
Remove standard library implementation methods
Browse files Browse the repository at this point in the history
(they're now part of the core library)
  • Loading branch information
desplesda committed Nov 22, 2023
1 parent b7463f6 commit 1cb2dc0
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 112 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fixed a bug where the Rounded Views sample wouldn't import correctly.
- Fixed Minimal Dialogue Runner sample that was using obsolete methods.
- Fixed a bug where TMPShim wasn't being detected.
- Standard library functions (like `random`, `dice`, `round_places`, etc) have been moved to the core Yarn Spinner library.

### Removed

Expand Down
112 changes: 0 additions & 112 deletions Runtime/Commands/DefaultActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ public static void RegisterActions(IActionRegistration target)
{
// Register the built-in methods and commands from Yarn Spinner for Unity.
target.AddCommandHandler<float>("wait", Wait);

target.AddFunction<float>("random", Random);
target.AddFunction<float, float, float>("random_range", RandomRange);
target.AddFunction<int, int>("dice", Dice);
target.AddFunction<float, int>("round", Round);
target.AddFunction<float, int, float>("round_places", RoundPlaces);
target.AddFunction<float, int>("floor", Floor);
target.AddFunction<float, int>("ceil", Ceil);
target.AddFunction<float, int>("inc", Inc);
target.AddFunction<float, int>("dec", Dec);
target.AddFunction<float, float>("decimal", Decimal);
target.AddFunction<float, int>("int", Int);
}

#region Commands
Expand All @@ -49,105 +37,5 @@ public static IEnumerator Wait(float duration)
yield return new WaitForSeconds(duration);
}
#endregion

#region Functions
[YarnFunction("random")]
public static float Random()
{
return RandomRange(0, 1);
}

[YarnFunction("random_range")]
public static float RandomRange(float minInclusive, float maxInclusive)
{
return UnityEngine.Random.Range(minInclusive, maxInclusive);
}

/// <summary>
/// Pick an integer in the given range.
/// </summary>
/// <param name="sides">Dice range.</param>
/// <returns>A number between <c>[1, <paramref name="sides"/>]</c>.
/// </returns>
[YarnFunction("dice")]
public static int Dice(int sides)
{
return UnityEngine.Random.Range(1, sides + 1);
}

[YarnFunction("round")]
public static int Round(float num)
{
return (int)RoundPlaces(num, 0);
}

[YarnFunction("round_places")]
public static float RoundPlaces(float num, int places)
{
return (float)Math.Round(num, places);
}

[YarnFunction("floor")]
public static int Floor(float num)
{
return Mathf.FloorToInt(num);
}

[YarnFunction("ceil")]
public static int Ceil(float num)
{
return Mathf.CeilToInt(num);
}

/// <summary>
/// Increment if integer, otherwise go to next integer.
/// </summary>
[YarnFunction("inc")]
public static int Inc(float num)
{
if (Decimal(num) != 0)
{
return Mathf.CeilToInt(num);
}
return (int)num + 1;
}

/// <summary>
/// Decrement if integer, otherwise go to previous integer.
/// </summary>
[YarnFunction("dec")]
public static int Dec(float num)
{
if (Decimal(num) != 0)
{
return Mathf.FloorToInt(num);
}
return (int)num - 1;
}

/// <summary>
/// The decimal portion of the given number.
/// </summary>
/// <param name="num">Number to get the decimal portion of.</param>
/// <returns><c>[0, 1)</c></returns>
[YarnFunction("decimal")]
public static float Decimal(float num)
{
return num - Int(num);
}

/// <summary>
/// Truncates the number into an int. This is different to
/// <see cref="floor(float)"/> because it rounds to zero rather than
/// <see cref="Mathf.NegativeInfinity"/>.
/// </summary>
/// <param name="num">Number to truncate.</param>
/// <returns>Truncated float value as int.</returns>
[YarnFunction("int")]
public static int Int(float num)
{
return (int)Math.Truncate(num);
}
#endregion
}
}

0 comments on commit 1cb2dc0

Please sign in to comment.