directAudioActions.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Action } from "./action";
  2. import { Condition } from "./condition";
  3. import { _TypeStore } from '../Misc/typeStore';
  4. import { Sound } from "../Audio/sound";
  5. /**
  6. * This defines an action helpful to play a defined sound on a triggered action.
  7. */
  8. export class PlaySoundAction extends Action {
  9. private _sound: Sound;
  10. /**
  11. * Instantiate the action
  12. * @param triggerOptions defines the trigger options
  13. * @param sound defines the sound to play
  14. * @param condition defines the trigger related conditions
  15. */
  16. constructor(triggerOptions: any, sound: Sound, condition?: Condition) {
  17. super(triggerOptions, condition);
  18. this._sound = sound;
  19. }
  20. /** @hidden */
  21. public _prepare(): void {
  22. }
  23. /**
  24. * Execute the action and play the sound.
  25. */
  26. public execute(): void {
  27. if (this._sound !== undefined) {
  28. this._sound.play();
  29. }
  30. }
  31. /**
  32. * Serializes the actions and its related information.
  33. * @param parent defines the object to serialize in
  34. * @returns the serialized object
  35. */
  36. public serialize(parent: any): any {
  37. return super._serialize({
  38. name: "PlaySoundAction",
  39. properties: [{ name: "sound", value: this._sound.name }]
  40. }, parent);
  41. }
  42. }
  43. /**
  44. * This defines an action helpful to stop a defined sound on a triggered action.
  45. */
  46. export class StopSoundAction extends Action {
  47. private _sound: Sound;
  48. /**
  49. * Instantiate the action
  50. * @param triggerOptions defines the trigger options
  51. * @param sound defines the sound to stop
  52. * @param condition defines the trigger related conditions
  53. */
  54. constructor(triggerOptions: any, sound: Sound, condition?: Condition) {
  55. super(triggerOptions, condition);
  56. this._sound = sound;
  57. }
  58. /** @hidden */
  59. public _prepare(): void {
  60. }
  61. /**
  62. * Execute the action and stop the sound.
  63. */
  64. public execute(): void {
  65. if (this._sound !== undefined) {
  66. this._sound.stop();
  67. }
  68. }
  69. /**
  70. * Serializes the actions and its related information.
  71. * @param parent defines the object to serialize in
  72. * @returns the serialized object
  73. */
  74. public serialize(parent: any): any {
  75. return super._serialize({
  76. name: "StopSoundAction",
  77. properties: [{ name: "sound", value: this._sound.name }]
  78. }, parent);
  79. }
  80. }
  81. _TypeStore.RegisteredTypes["BABYLON.PlaySoundAction"] = StopSoundAction;
  82. _TypeStore.RegisteredTypes["BABYLON.StopSoundAction"] = StopSoundAction;