babylon.condition.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. module BABYLON {
  2. export class Condition {
  3. public _actionManager: ActionManager;
  4. public _evaluationId: number;
  5. public _currentResult: boolean;
  6. constructor(actionManager: ActionManager) {
  7. this._actionManager = actionManager;
  8. }
  9. public isValid(): boolean {
  10. return true;
  11. }
  12. public _getProperty(propertyPath: string): string {
  13. return this._actionManager._getProperty(propertyPath);
  14. }
  15. public _getEffectiveTarget(target: any, propertyPath: string): any {
  16. return this._actionManager._getEffectiveTarget(target, propertyPath);
  17. }
  18. }
  19. export class StateCondition extends Condition {
  20. // Statics
  21. public static IsEqual = 0;
  22. public static IsDifferent = 1;
  23. public static IsGreater= 2;
  24. public static IsLesser = 3;
  25. // Members
  26. public _actionManager: ActionManager;
  27. private _target: any;
  28. private _property: string;
  29. constructor(actionManager: ActionManager, target: any, public propertyPath: string, public value: any, public operator: number = StateCondition.IsEqual) {
  30. super(actionManager);
  31. this._target = this._getEffectiveTarget(target, this.propertyPath);
  32. this._property = this._getProperty(this.propertyPath);
  33. }
  34. // Methods
  35. public isValid(): boolean {
  36. switch (this.operator) {
  37. case StateCondition.IsGreater:
  38. return this._target[this._property] > this.value;
  39. case StateCondition.IsLesser:
  40. return this._target[this._property] < this.value;
  41. case StateCondition.IsEqual:
  42. case StateCondition.IsDifferent:
  43. var check: boolean;
  44. if (this.value.equals) {
  45. check = this.value.equals(this._target[this._property]);
  46. } else {
  47. check = this.value === this._target[this._property];
  48. }
  49. return this.operator === StateCondition.IsEqual ? check: !check;
  50. }
  51. return false;
  52. }
  53. }
  54. export class PredicateCondition extends Condition {
  55. // Members
  56. public _actionManager: ActionManager;
  57. constructor(actionManager: ActionManager, public predicate: () => boolean) {
  58. super(actionManager);
  59. }
  60. public isValid(): boolean {
  61. return this.predicate();
  62. }
  63. }
  64. }