Chris Gregan
8 years ago
committed by
GitHub
12 changed files with 1505 additions and 596 deletions
@ -0,0 +1,126 @@ |
|||||||
|
using System.Collections; |
||||||
|
using System.Collections.Generic; |
||||||
|
using UnityEngine; |
||||||
|
using Fungus; |
||||||
|
using MoonSharp.Interpreter; |
||||||
|
|
||||||
|
namespace Fungus |
||||||
|
{ |
||||||
|
public class LuaCondition : Condition |
||||||
|
{ |
||||||
|
[Tooltip("Lua Environment to use to execute this Lua script (null for global)")] |
||||||
|
[SerializeField] protected LuaEnvironment luaEnvironment; |
||||||
|
|
||||||
|
[Tooltip("The lua comparison string to run; implicitly prepends 'return' onto this")] |
||||||
|
[TextArea] |
||||||
|
public string luaCompareString; |
||||||
|
protected bool initialised; |
||||||
|
protected string friendlyName = ""; |
||||||
|
protected Closure luaFunction; |
||||||
|
|
||||||
|
protected override bool EvaluateCondition() |
||||||
|
{ |
||||||
|
bool condition = false; |
||||||
|
luaEnvironment.RunLuaFunction(luaFunction, false, (returnValue) => { |
||||||
|
if( returnValue != null ) |
||||||
|
{ |
||||||
|
condition = returnValue.Boolean; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Debug.LogWarning("No return value from " + friendlyName); |
||||||
|
} |
||||||
|
}); |
||||||
|
return condition; |
||||||
|
} |
||||||
|
|
||||||
|
protected override bool HasNeededProperties() |
||||||
|
{ |
||||||
|
return !string.IsNullOrEmpty(luaCompareString); |
||||||
|
} |
||||||
|
|
||||||
|
protected virtual void Start() |
||||||
|
{ |
||||||
|
InitExecuteLua(); |
||||||
|
} |
||||||
|
|
||||||
|
protected virtual string GetLuaString() |
||||||
|
{ |
||||||
|
return "return not not (" + luaCompareString + ")"; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Initialises the Lua environment and compiles the Lua string for execution later on. |
||||||
|
/// </summary> |
||||||
|
protected virtual void InitExecuteLua() |
||||||
|
{ |
||||||
|
if (initialised) |
||||||
|
{ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
// Cache a descriptive name to use in Lua error messages |
||||||
|
friendlyName = gameObject.name + "." + ParentBlock.BlockName + "." + this.GetType().ToString() + " #" + CommandIndex.ToString(); |
||||||
|
|
||||||
|
Flowchart flowchart = GetFlowchart(); |
||||||
|
|
||||||
|
// See if a Lua Environment has been assigned to this Flowchart |
||||||
|
if (luaEnvironment == null) |
||||||
|
{ |
||||||
|
luaEnvironment = flowchart.LuaEnv; |
||||||
|
} |
||||||
|
|
||||||
|
// No Lua Environment specified so just use any available or create one. |
||||||
|
if (luaEnvironment == null) |
||||||
|
{ |
||||||
|
luaEnvironment = LuaEnvironment.GetLua(); |
||||||
|
} |
||||||
|
|
||||||
|
string s = GetLuaString(); |
||||||
|
luaFunction = luaEnvironment.LoadLuaFunction(s, friendlyName); |
||||||
|
|
||||||
|
// Add a binding to the parent flowchart |
||||||
|
if (flowchart.LuaBindingName != "") |
||||||
|
{ |
||||||
|
Table globals = luaEnvironment.Interpreter.Globals; |
||||||
|
if (globals != null) |
||||||
|
{ |
||||||
|
globals[flowchart.LuaBindingName] = flowchart; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Always initialise when playing in the editor. |
||||||
|
// Allows the user to edit the Lua script while the game is playing. |
||||||
|
if ( !(Application.isPlaying && Application.isEditor) ) |
||||||
|
{ |
||||||
|
initialised = true; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
#region Public members |
||||||
|
|
||||||
|
public override string GetSummary() |
||||||
|
{ |
||||||
|
if (string.IsNullOrEmpty(luaCompareString)) |
||||||
|
{ |
||||||
|
return "Error: no lua compare string provided"; |
||||||
|
} |
||||||
|
|
||||||
|
return luaCompareString; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
public override bool OpenBlock() |
||||||
|
{ |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override Color GetButtonColor() |
||||||
|
{ |
||||||
|
return new Color32(253, 253, 150, 255); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: 75ddfadd68d3d4713803a6b170cb0b51 |
||||||
|
timeCreated: 1493078204 |
||||||
|
licenseType: Pro |
||||||
|
MonoImporter: |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
||||||
|
assetBundleName: |
||||||
|
assetBundleVariant: |
@ -0,0 +1,38 @@ |
|||||||
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). |
||||||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
||||||
|
|
||||||
|
using UnityEngine; |
||||||
|
|
||||||
|
namespace Fungus |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Marks the start of a command block to be executed when the preceding If statement is False and the test expression is true. |
||||||
|
/// </summary> |
||||||
|
[CommandInfo("Flow", |
||||||
|
"Lua Else If", |
||||||
|
"Marks the start of a command block to be executed when the preceding If statement is False and the test expression is true.")] |
||||||
|
[AddComponentMenu("")] |
||||||
|
public class LuaElseIf : LuaCondition |
||||||
|
{ |
||||||
|
protected override bool IsElseIf { get { return true; } } |
||||||
|
|
||||||
|
#region Public members |
||||||
|
|
||||||
|
public override bool OpenBlock() |
||||||
|
{ |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool CloseBlock() |
||||||
|
{ |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public override Color GetButtonColor() |
||||||
|
{ |
||||||
|
return new Color32(253, 253, 150, 255); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: 9739de3269e5246b399e3a1cd41b94de |
||||||
|
timeCreated: 1493078204 |
||||||
|
licenseType: Pro |
||||||
|
MonoImporter: |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
||||||
|
assetBundleName: |
||||||
|
assetBundleVariant: |
@ -0,0 +1,26 @@ |
|||||||
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). |
||||||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
||||||
|
|
||||||
|
using UnityEngine; |
||||||
|
|
||||||
|
namespace Fungus |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// If the test expression is true, execute the following command block. |
||||||
|
/// </summary> |
||||||
|
[CommandInfo("Flow", |
||||||
|
"Lua If", |
||||||
|
"If the test expression is true, execute the following command block.")] |
||||||
|
[AddComponentMenu("")] |
||||||
|
public class LuaIf : LuaCondition |
||||||
|
{ |
||||||
|
#region Public members |
||||||
|
|
||||||
|
public override Color GetButtonColor() |
||||||
|
{ |
||||||
|
return new Color32(253, 253, 150, 255); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: a8d396bcbf372485cad471c0bb64bb44 |
||||||
|
timeCreated: 1493078204 |
||||||
|
licenseType: Pro |
||||||
|
MonoImporter: |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
||||||
|
assetBundleName: |
||||||
|
assetBundleVariant: |
@ -0,0 +1,110 @@ |
|||||||
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). |
||||||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
||||||
|
|
||||||
|
using UnityEngine; |
||||||
|
|
||||||
|
namespace Fungus |
||||||
|
{ |
||||||
|
public abstract class VariableCondition : Condition |
||||||
|
{ |
||||||
|
[Tooltip("The type of comparison to be performed")] |
||||||
|
[SerializeField] protected CompareOperator compareOperator; |
||||||
|
|
||||||
|
[Tooltip("Variable to use in expression")] |
||||||
|
[VariableProperty(typeof(BooleanVariable), |
||||||
|
typeof(IntegerVariable), |
||||||
|
typeof(FloatVariable), |
||||||
|
typeof(StringVariable))] |
||||||
|
[SerializeField] protected Variable variable; |
||||||
|
|
||||||
|
[Tooltip("Boolean value to compare against")] |
||||||
|
[SerializeField] protected BooleanData booleanData; |
||||||
|
|
||||||
|
[Tooltip("Integer value to compare against")] |
||||||
|
[SerializeField] protected IntegerData integerData; |
||||||
|
|
||||||
|
[Tooltip("Float value to compare against")] |
||||||
|
[SerializeField] protected FloatData floatData; |
||||||
|
|
||||||
|
[Tooltip("String value to compare against")] |
||||||
|
[SerializeField] protected StringDataMulti stringData; |
||||||
|
|
||||||
|
protected override bool EvaluateCondition() |
||||||
|
{ |
||||||
|
BooleanVariable booleanVariable = variable as BooleanVariable; |
||||||
|
IntegerVariable integerVariable = variable as IntegerVariable; |
||||||
|
FloatVariable floatVariable = variable as FloatVariable; |
||||||
|
StringVariable stringVariable = variable as StringVariable; |
||||||
|
|
||||||
|
bool condition = false; |
||||||
|
|
||||||
|
if (booleanVariable != null) |
||||||
|
{ |
||||||
|
condition = booleanVariable.Evaluate(compareOperator, booleanData.Value); |
||||||
|
} |
||||||
|
else if (integerVariable != null) |
||||||
|
{ |
||||||
|
condition = integerVariable.Evaluate(compareOperator, integerData.Value); |
||||||
|
} |
||||||
|
else if (floatVariable != null) |
||||||
|
{ |
||||||
|
condition = floatVariable.Evaluate(compareOperator, floatData.Value); |
||||||
|
} |
||||||
|
else if (stringVariable != null) |
||||||
|
{ |
||||||
|
condition = stringVariable.Evaluate(compareOperator, stringData.Value); |
||||||
|
} |
||||||
|
|
||||||
|
return condition; |
||||||
|
} |
||||||
|
|
||||||
|
protected override bool HasNeededProperties() |
||||||
|
{ |
||||||
|
return (variable != null); |
||||||
|
} |
||||||
|
|
||||||
|
#region Public members |
||||||
|
|
||||||
|
public override string GetSummary() |
||||||
|
{ |
||||||
|
if (variable == null) |
||||||
|
{ |
||||||
|
return "Error: No variable selected"; |
||||||
|
} |
||||||
|
|
||||||
|
string summary = variable.Key + " "; |
||||||
|
summary += Condition.GetOperatorDescription(compareOperator) + " "; |
||||||
|
|
||||||
|
if (variable.GetType() == typeof(BooleanVariable)) |
||||||
|
{ |
||||||
|
summary += booleanData.GetDescription(); |
||||||
|
} |
||||||
|
else if (variable.GetType() == typeof(IntegerVariable)) |
||||||
|
{ |
||||||
|
summary += integerData.GetDescription(); |
||||||
|
} |
||||||
|
else if (variable.GetType() == typeof(FloatVariable)) |
||||||
|
{ |
||||||
|
summary += floatData.GetDescription(); |
||||||
|
} |
||||||
|
else if (variable.GetType() == typeof(StringVariable)) |
||||||
|
{ |
||||||
|
summary += stringData.GetDescription(); |
||||||
|
} |
||||||
|
|
||||||
|
return summary; |
||||||
|
} |
||||||
|
|
||||||
|
public override bool HasReference(Variable variable) |
||||||
|
{ |
||||||
|
return (variable == this.variable); |
||||||
|
} |
||||||
|
|
||||||
|
public override Color GetButtonColor() |
||||||
|
{ |
||||||
|
return new Color32(253, 253, 150, 255); |
||||||
|
} |
||||||
|
|
||||||
|
#endregion |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
fileFormatVersion: 2 |
||||||
|
guid: b065f7dff8779442ab5841ccc6ae375b |
||||||
|
timeCreated: 1493077787 |
||||||
|
licenseType: Pro |
||||||
|
MonoImporter: |
||||||
|
serializedVersion: 2 |
||||||
|
defaultReferences: [] |
||||||
|
executionOrder: 0 |
||||||
|
icon: {instanceID: 0} |
||||||
|
userData: |
||||||
|
assetBundleName: |
||||||
|
assetBundleVariant: |
Loading…
Reference in new issue