public override bool Combine(Operator op, bool a, object b, out StoryVar result) { result = default(StoryVar); if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool bBool; if (!StoryVar.TryConvertTo <bool>(b, out bBool)) { return(false); } switch (op) { case Operator.LogicalAnd: result = a && bBool; break; case Operator.LogicalOr: result = a || bBool; break; default: break; } return(true); } double aDouble; return(ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Combine(op, aDouble, b, out result)); }
// ............................ public override bool Compare(Operator op, double a, object b, out bool result) { result = false; double bDouble; if (!StoryVar.TryConvertTo <double>(b, out bDouble)) { return(false); } switch (op) { case Operator.Equals: result = a == bDouble; break; case Operator.GreaterThan: result = a > bDouble; break; case Operator.GreaterThanOrEquals: result = a >= bDouble; break; case Operator.LessThan: result = a < bDouble; break; case Operator.LessThanOrEquals: result = a <= bDouble; break; default: return(false); // comparison not possible } return(true); }
public StoryVar GetMember(StoryVar member) { if (Value == null) { throw new VarTypeMemberException("Cannot get members of an empty story variable."); } if (member.Value == null) { throw new VarTypeMemberException("Cannot treat an empty variable as a member."); } IVarTypeService service = GetTypeService(this.Value.GetType()); if (service != null) { return(service.GetMember(this.Value, member)); } if (this.Value is IVarType) { return(((IVarType)this.Value).GetMember(member)); } throw new VarTypeMemberException(string.Format("Cannot get member of a story variable of type {0}.", this.Value.GetType().Name)); }
public override bool Combine(Operator op, int a, object b, out StoryVar result) { result = default(StoryVar); // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Combine(op, aBool, b, out result)); } // Always use double combinations double aDouble; if (!ConvertTo <double>(a, out aDouble) || !StoryVar.GetTypeService <double>(true).Combine(op, aDouble, b, out result)) { return(false); } // Convert result back to int if it is whole number (or close to it in precision) double resultDouble = (double)result.Value; if (Math.Round(resultDouble) == Math.Round(resultDouble, DecimalPrecision)) { result.Value = Convert.ToInt32(resultDouble); } return(true); }
public override bool Combine(Operator op, string a, object b, out StoryVar result) { result = default(StoryVar); // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Combine(op, aBool, b, out result)); } // To comply with Javascript comparison, concat if b is a string if (op == Operator.Add && b is string) { result = a + (string)b; return(true); } // For other operators, try numeric comabinations if possible (in strict mode this won't work) double aDouble; if (ConvertTo <double>(a, out aDouble)) { return(StoryVar.GetTypeService <double>(true).Combine(op, aDouble, b, out result)); } else { return(false); } }
string PrintInv() { var str = ""; items = twinePlayer.Story.Vars["inv"]; if (items["NOTE"] == true) { str += "\nNOTE"; } if (items["COAT"] == true) { str += "\nCOAT"; } if (items["GUN"] == true) { str += "\nGUN"; } if (items["FOB"] == true) { str += "\nFOB"; } if (items["CURE"] == true) { str += "\nCURE"; } return(str); }
public StoryStyle(StoryVar booleanExpression) { // When giving a single value if (booleanExpression.ConvertValueTo <bool>()) { this[EvaluatedValContextOption] = booleanExpression; } }
// ............................ public override bool Compare(Operator op, int a, object b, out bool result) { // Always use double comparison result = false; double aDouble; return(ConvertTo(a, out aDouble) && StoryVar.GetTypeService <double>(true).Compare(op, aDouble, b, out result)); }
public override bool Unary(Operator op, bool a, out StoryVar result) { result = default(StoryVar); double aDouble; return(ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Unary(op, aDouble, out result)); }
public Story() { StoryVar.RegisterTypeService <bool>(new BoolService()); StoryVar.RegisterTypeService <int>(new IntService()); StoryVar.RegisterTypeService <double>(new DoubleService()); StoryVar.RegisterTypeService <string>(new StringService()); this.Passages = new Dictionary <string, StoryPassage>(); this.PassageHistory = new List <string>(); }
// .............. // MEMBERS public StoryVar this[StoryVar memberName] { get { return(GetMember(memberName)); } set { SetMember(memberName, value); } }
// ............................ public override bool Compare(Operator op, string a, object b, out bool result) { result = false; // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Compare(op, aBool, b, out result)); } // Try numeric comparison (in strict mode this won't work) double aDouble; if (ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Compare(op, aDouble, b, out result)) { return(true); } string bString; if (!StoryVar.TryConvertTo <string>(b, out bString)) { return(false); } switch (op) { case Operator.Equals: result = a == bString; break; case Operator.GreaterThan: result = String.Compare(a, bString) > 0; break; case Operator.GreaterThanOrEquals: result = String.Compare(a, bString) >= 0; break; case Operator.LessThan: result = String.Compare(a, bString) < 0; break; case Operator.LessThanOrEquals: result = String.Compare(a, bString) <= 0; break; case Operator.Contains: result = a.Contains(bString); break; default: return(false); } return(true); }
public override bool Unary(Operator op, string a, out StoryVar result) { result = default(StoryVar); // Try numeric unary if possible (in strict mode this won't work) double aDouble; if (ConvertTo(a, out aDouble)) { return(StoryVar.GetTypeService <double>(true).Unary(op, aDouble, out result)); } else { return(false); } }
public override bool Unary(Operator op, double a, out StoryVar result) { result = default(StoryVar); switch (op) { case Operator.Increment: result = ++a; break; case Operator.Decrement: result = --a; break; default: return(false); } return(true); }
public override bool Combine(Operator op, object b, out StoryVar result) { result = default(StoryVar); if (!(b is StoryStyle) || op != Operator.Add) { return(false); } var bStyle = (StoryStyle)b; var combined = this.GetCopy(); foreach (var entry in bStyle._settings) { combined[entry.Key] = entry.Value; } result = combined; return(true); }
public override bool Combine(Operator op, double a, object b, out StoryVar result) { result = default(StoryVar); // Logical operators, treat as bool if (op == Operator.LogicalAnd || op == Operator.LogicalOr) { bool aBool; return(ConvertTo <bool>(a, out aBool) && StoryVar.GetTypeService <bool>(true).Combine(op, aBool, b, out result)); } double bDouble; if (!StoryVar.TryConvertTo <double>(b, out bDouble)) { return(false); } switch (op) { case Operator.Add: result = a + bDouble; break; case Operator.Subtract: result = a - bDouble; break; case Operator.Multiply: result = a * bDouble; break; case Operator.Divide: result = a / bDouble; break; case Operator.Modulo: result = a % bDouble; break; default: return(false); // combination not possible } return(true); }
public static bool TryCombine(Operator op, object left, object right, out StoryVar result) { object a = GetInnerValue(left); object b = GetInnerValue(right); if (a != null || b != null) { IVarTypeService service; if (a == null && b != null) { // Handle uninitialized left operand - use the right operand's type to convert (fix for issue #39) object aConverted; if ( _typeServices.TryGetValue(b.GetType(), out service) && service.ConvertFrom(a, out aConverted, StoryVar.StrictMode) && service.Combine(op, aConverted, b, out result) ) { return(true); } } // Handle other combinations else if (a != null && _typeServices.TryGetValue(a.GetType(), out service) && service.Combine(op, a, b, out result)) { return(true); } } // No type service managed to covert so try the var type method directly, if available if (a is IVarType) { if ((a as IVarType).Combine(op, b, out result)) { return(true); } } result = default(StoryVar); return(false); }
public override bool Unary(Operator op, int a, out StoryVar result) { result = default(StoryVar); // Always use double operations double aDouble; if (!ConvertTo(a, out aDouble) || !StoryVar.GetTypeService <double>(true).Unary(op, aDouble, out result)) { return(false); } // Convert result back to int if it is whole number (or close to it in precision) double resultDouble = (double)result.Value; if (Math.Round(resultDouble) == Math.Round(resultDouble, DecimalPrecision)) { result.Value = Convert.ToInt32(resultDouble); } return(true); }
// ............................ public override bool Compare(Operator op, bool a, object b, out bool result) { result = false; if (op == Operator.Equals) { // Equaliy, evaluate as bools bool bBool; if (!StoryVar.TryConvertTo <bool>(b, out bBool)) { return(false); } result = a == bBool; return(true); } else { // Evaluate as numbers for other operators double aDouble; return(ConvertTo <double>(a, out aDouble) && StoryVar.GetTypeService <double>(true).Compare(op, aDouble, b, out result)); } }
public static bool TryCombine(Operator op, object left, object right, out StoryVar result) { object a = GetInnerValue(left); object b = GetInnerValue(right); IVarTypeService service; if (a != null && _typeServices.TryGetValue(a.GetType(), out service) && service.Combine(op, a, b, out result)) { return(true); } if (a is IVarType) { if ((a as IVarType).Combine(op, b, out result)) { return(true); } } result = default(StoryVar); return(false); }
public void RemoveMember(StoryVar member) { if (Value == null) { throw new VarTypeMemberException("Cannot remove member of empty story variable."); } IVarTypeService service = GetTypeService(this.Value.GetType()); if (service != null) { service.RemoveMember(this.Value, member); return; } if (this.Value is IVarType) { ((IVarType)this.Value).RemoveMember(member); return; } throw new VarTypeMemberException(string.Format("Cannot remove member of a story variable of type {0}.", this.Value.GetType().Name)); }
public override StoryVar GetMember(string container, StoryVar member) { string containerString = (string)container; StoryVar value; int pos; if (StoryVar.TryConvertTo <int>(member, out pos)) { value = containerString[pos]; } else if (member.ToString() == "length") { value = containerString.Length; } else { value = default(StoryVar); } return(value); }
// Use this for initialization void Start() { twinePlayer = Object.FindObjectOfType <TextrisTwinePlayer>(); links = twinePlayer.Story.GetCurrentLinks(); items = twinePlayer.Story.Vars["inv"]; }
bool IVarTypeService.Unary(Operator op, object a, out StoryVar result) { return(Unary(op, (T)a, out result)); }
bool IVarTypeService.Combine(Operator op, object a, object b, out StoryVar result) { return(Combine(op, (T)a, b, out result)); }
public abstract bool Unary(Operator op, T a, out StoryVar result);
public abstract bool Combine(Operator op, T a, object b, out StoryVar result);
public abstract void RemoveMember(T container, StoryVar member);
public abstract void SetMember(T container, StoryVar member, StoryVar value);
public abstract StoryVar GetMember(T container, StoryVar member);