babylon.directActions.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. module BABYLON {
  2. export class SwitchBooleanAction extends Action {
  3. private _target: any;
  4. private _property: string;
  5. constructor(trigger: number, target: any, public propertyPath: string, condition?: Condition) {
  6. super(trigger, condition);
  7. this._target = target;
  8. }
  9. public _prepare(): void {
  10. this._target = this._getEffectiveTarget(this._target, this.propertyPath);
  11. this._property = this._getProperty(this.propertyPath);
  12. }
  13. public execute(): void {
  14. this._target[this._property] = !this._target[this._property];
  15. }
  16. }
  17. export class SetValueAction extends Action {
  18. private _target: any;
  19. private _property: string;
  20. constructor(trigger: number, target: any, public propertyPath: string, public value: any, condition?: Condition) {
  21. super(trigger, condition);
  22. this._target = target;
  23. }
  24. public _prepare(): void {
  25. this._target = this._getEffectiveTarget(this._target, this.propertyPath);
  26. this._property = this._getProperty(this.propertyPath);
  27. }
  28. public execute(): void {
  29. this._target[this._property] = this.value;
  30. }
  31. }
  32. export class IncrementValueAction extends Action {
  33. private _target: any;
  34. private _property: string;
  35. constructor(trigger: number, target: any, public propertyPath: string, public value: any, condition?: Condition) {
  36. super(trigger, condition);
  37. this._target = target;
  38. }
  39. public _prepare(): void {
  40. this._target = this._getEffectiveTarget(this._target, this.propertyPath);
  41. this._property = this._getProperty(this.propertyPath);
  42. if (typeof this._target[this._property] !== "number") {
  43. Tools.Warn("Warning: IncrementValueAction can only be used with number values");
  44. }
  45. }
  46. public execute(): void {
  47. this._target[this._property] += this.value;
  48. }
  49. }
  50. export class PlayAnimationAction extends Action {
  51. private _target: any;
  52. constructor(trigger: number, target: any, public from: number, public to: number, public loop?: boolean, condition?: Condition) {
  53. super(trigger, condition);
  54. this._target = target;
  55. }
  56. public _prepare(): void {
  57. }
  58. public execute(): void {
  59. var scene = this._actionManager.getScene();
  60. scene.beginAnimation(this._target, this.from, this.to, this.loop);
  61. }
  62. }
  63. export class StopAnimationAction extends Action {
  64. private _target: any;
  65. constructor(trigger: number, target: any, condition?: Condition) {
  66. super(trigger, condition);
  67. this._target = target;
  68. }
  69. public _prepare(): void {
  70. }
  71. public execute(): void {
  72. var scene = this._actionManager.getScene();
  73. scene.stopAnimation(this._target);
  74. }
  75. }
  76. export class DoNothingAction extends Action {
  77. constructor(trigger: number = ActionManager.NoneTrigger, condition?: Condition) {
  78. super(trigger, condition);
  79. }
  80. public execute(): void {
  81. }
  82. }
  83. export class CombineAction extends Action {
  84. constructor(trigger: number, public children: Action[], condition?: Condition) {
  85. super(trigger, condition);
  86. }
  87. public _prepare(): void {
  88. for (var index = 0; index < this.children.length; index++) {
  89. this.children[index]._actionManager = this._actionManager;
  90. this.children[index]._prepare();
  91. }
  92. }
  93. public execute(): void {
  94. for (var index = 0; index < this.children.length; index++) {
  95. this.children[index].execute();
  96. }
  97. }
  98. }
  99. export class ExecuteCodeAction extends Action {
  100. constructor(trigger: number, public func: () => void, condition?: Condition) {
  101. super(trigger, condition);
  102. }
  103. public execute(): void {
  104. this.func();
  105. }
  106. }
  107. export class SetParentAction extends Action {
  108. private _parent: any;
  109. private _target: any;
  110. constructor(trigger: number, target: any, parent: any, condition?: Condition) {
  111. super(trigger, condition);
  112. this._target = target;
  113. this._parent = parent;
  114. }
  115. public _prepare(): void {
  116. }
  117. public execute(): void {
  118. if (this._target.parent === this._parent) {
  119. return;
  120. }
  121. var invertParentWorldMatrix = this._parent.getWorldMatrix().clone();
  122. invertParentWorldMatrix.invert();
  123. this._target.position = BABYLON.Vector3.TransformCoordinates(this._target.position, invertParentWorldMatrix);
  124. this._target.parent = this._parent;
  125. }
  126. }
  127. }