babylon.condition.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var __extends = (this && this.__extends) || function (d, b) {
  2. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  3. function __() { this.constructor = d; }
  4. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var Condition = (function () {
  9. function Condition(actionManager) {
  10. this._actionManager = actionManager;
  11. }
  12. Condition.prototype.isValid = function () {
  13. return true;
  14. };
  15. Condition.prototype._getProperty = function (propertyPath) {
  16. return this._actionManager._getProperty(propertyPath);
  17. };
  18. Condition.prototype._getEffectiveTarget = function (target, propertyPath) {
  19. return this._actionManager._getEffectiveTarget(target, propertyPath);
  20. };
  21. Condition.prototype.serialize = function () {
  22. };
  23. Condition.prototype._serialize = function (serializedCondition) {
  24. return {
  25. type: 2,
  26. children: [],
  27. name: serializedCondition.name,
  28. properties: serializedCondition.properties
  29. };
  30. };
  31. return Condition;
  32. })();
  33. BABYLON.Condition = Condition;
  34. var ValueCondition = (function (_super) {
  35. __extends(ValueCondition, _super);
  36. function ValueCondition(actionManager, target, propertyPath, value, operator) {
  37. if (operator === void 0) { operator = ValueCondition.IsEqual; }
  38. _super.call(this, actionManager);
  39. this.propertyPath = propertyPath;
  40. this.value = value;
  41. this.operator = operator;
  42. this._target = target;
  43. this._effectiveTarget = this._getEffectiveTarget(target, this.propertyPath);
  44. this._property = this._getProperty(this.propertyPath);
  45. }
  46. Object.defineProperty(ValueCondition, "IsEqual", {
  47. get: function () {
  48. return ValueCondition._IsEqual;
  49. },
  50. enumerable: true,
  51. configurable: true
  52. });
  53. Object.defineProperty(ValueCondition, "IsDifferent", {
  54. get: function () {
  55. return ValueCondition._IsDifferent;
  56. },
  57. enumerable: true,
  58. configurable: true
  59. });
  60. Object.defineProperty(ValueCondition, "IsGreater", {
  61. get: function () {
  62. return ValueCondition._IsGreater;
  63. },
  64. enumerable: true,
  65. configurable: true
  66. });
  67. Object.defineProperty(ValueCondition, "IsLesser", {
  68. get: function () {
  69. return ValueCondition._IsLesser;
  70. },
  71. enumerable: true,
  72. configurable: true
  73. });
  74. // Methods
  75. ValueCondition.prototype.isValid = function () {
  76. switch (this.operator) {
  77. case ValueCondition.IsGreater:
  78. return this._effectiveTarget[this._property] > this.value;
  79. case ValueCondition.IsLesser:
  80. return this._effectiveTarget[this._property] < this.value;
  81. case ValueCondition.IsEqual:
  82. case ValueCondition.IsDifferent:
  83. var check;
  84. if (this.value.equals) {
  85. check = this.value.equals(this._effectiveTarget[this._property]);
  86. }
  87. else {
  88. check = this.value === this._effectiveTarget[this._property];
  89. }
  90. return this.operator === ValueCondition.IsEqual ? check : !check;
  91. }
  92. return false;
  93. };
  94. ValueCondition.prototype.serialize = function () {
  95. return this._serialize({
  96. name: "ValueCondition",
  97. properties: [
  98. BABYLON.Action._GetTargetProperty(this._target),
  99. { name: "propertyPath", value: this.propertyPath },
  100. { name: "value", value: BABYLON.Action._SerializeValueAsString(this.value) },
  101. { name: "operator", value: ValueCondition.GetOperatorName(this.operator) }
  102. ]
  103. });
  104. };
  105. ValueCondition.GetOperatorName = function (operator) {
  106. switch (operator) {
  107. case ValueCondition._IsEqual: return "IsEqual";
  108. case ValueCondition._IsDifferent: return "IsDifferent";
  109. case ValueCondition._IsGreater: return "IsGreater";
  110. case ValueCondition._IsLesser: return "IsLesser";
  111. default: return "";
  112. }
  113. };
  114. // Statics
  115. ValueCondition._IsEqual = 0;
  116. ValueCondition._IsDifferent = 1;
  117. ValueCondition._IsGreater = 2;
  118. ValueCondition._IsLesser = 3;
  119. return ValueCondition;
  120. })(Condition);
  121. BABYLON.ValueCondition = ValueCondition;
  122. var PredicateCondition = (function (_super) {
  123. __extends(PredicateCondition, _super);
  124. function PredicateCondition(actionManager, predicate) {
  125. _super.call(this, actionManager);
  126. this.predicate = predicate;
  127. }
  128. PredicateCondition.prototype.isValid = function () {
  129. return this.predicate();
  130. };
  131. return PredicateCondition;
  132. })(Condition);
  133. BABYLON.PredicateCondition = PredicateCondition;
  134. var StateCondition = (function (_super) {
  135. __extends(StateCondition, _super);
  136. function StateCondition(actionManager, target, value) {
  137. _super.call(this, actionManager);
  138. this.value = value;
  139. this._target = target;
  140. }
  141. // Methods
  142. StateCondition.prototype.isValid = function () {
  143. return this._target.state === this.value;
  144. };
  145. StateCondition.prototype.serialize = function () {
  146. return this._serialize({
  147. name: "StateCondition",
  148. properties: [
  149. BABYLON.Action._GetTargetProperty(this._target),
  150. { name: "value", value: this.value }
  151. ]
  152. });
  153. };
  154. return StateCondition;
  155. })(Condition);
  156. BABYLON.StateCondition = StateCondition;
  157. })(BABYLON || (BABYLON = {}));