babylon.condition.ts 2.7 KB

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