dummyruntimeobject.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. namespace gdjs {
  2. const logger = new gdjs.Logger('Dummy object');
  3. /**
  4. * A dummy object doing showing a text on screen.
  5. * @ignore
  6. */
  7. export class DummyRuntimeObject extends gdjs.RuntimeObject {
  8. // Load any required data from the object properties.
  9. _property1: string;
  10. // Create the renderer (see dummyruntimeobject-pixi-renderer.js)
  11. _renderer: any;
  12. // @ts-expect-error ts-migrate(2564) FIXME: Property 'opacity' has no initializer and is not d... Remove this comment to see the full error message
  13. opacity: float;
  14. constructor(instanceContainer: gdjs.RuntimeInstanceContainer, objectData) {
  15. // *ALWAYS* call the base gdjs.RuntimeObject constructor.
  16. super(instanceContainer, objectData);
  17. this._property1 = objectData.content.property1;
  18. this._renderer = new gdjs.DummyRuntimeObjectRenderer(
  19. this,
  20. instanceContainer
  21. );
  22. // *ALWAYS* call `this.onCreated()` at the very end of your object constructor.
  23. this.onCreated();
  24. }
  25. getRendererObject() {
  26. return this._renderer.getRendererObject();
  27. }
  28. updateFromObjectData(oldObjectData, newObjectData): boolean {
  29. // Compare previous and new data for the object and update it accordingly.
  30. // This is useful for "hot-reloading".
  31. if (oldObjectData.content.property1 !== newObjectData.content.property1) {
  32. this._property1 = newObjectData.content.property1;
  33. this._renderer.updateText();
  34. }
  35. return true;
  36. }
  37. /**
  38. * Called once during the game loop, before events and rendering.
  39. * @param instanceContainer The gdjs.RuntimeScene the object belongs to.
  40. */
  41. update(instanceContainer: gdjs.RuntimeInstanceContainer): void {
  42. // This is an example: typically you want to make sure the renderer
  43. // is up to date with the object.
  44. this._renderer.ensureUpToDate();
  45. }
  46. /**
  47. * Initialize the extra parameters that could be set for an instance.
  48. */
  49. extraInitializationFromInitialInstance(initialInstanceData: InstanceData) {}
  50. // For example:
  51. // this.setSomething(initialInstanceData.something);
  52. /**
  53. * Update the object position.
  54. */
  55. private _updatePosition() {
  56. // This is an example: typically you want to tell the renderer to update
  57. // the position of the object.
  58. this._renderer.updatePosition();
  59. }
  60. /**
  61. * Set object position on X axis.
  62. */
  63. setX(x: float): void {
  64. // Always call the parent method first.
  65. super.setX(x);
  66. this._updatePosition();
  67. }
  68. /**
  69. * Set object position on Y axis.
  70. */
  71. setY(y: float): void {
  72. // Always call the parent method first.
  73. super.setY(y);
  74. this._updatePosition();
  75. }
  76. /**
  77. * Set the angle of the object.
  78. * @param angle The new angle of the object
  79. */
  80. setAngle(angle: float): void {
  81. // Always call the parent method first.
  82. super.setAngle(angle);
  83. // Tell the renderer to update the rendered object
  84. this._renderer.updateAngle();
  85. }
  86. /**
  87. * Set object opacity.
  88. */
  89. setOpacity(opacity): void {
  90. if (opacity < 0) {
  91. opacity = 0;
  92. }
  93. if (opacity > 255) {
  94. opacity = 255;
  95. }
  96. this.opacity = opacity;
  97. // Tell the renderer to update the rendered object
  98. this._renderer.updateOpacity();
  99. }
  100. /**
  101. * Get object opacity.
  102. */
  103. getOpacity() {
  104. return this.opacity;
  105. }
  106. /**
  107. * Get the text that must be displayed by the dummy object.
  108. */
  109. getText() {
  110. return this._property1;
  111. }
  112. /**
  113. * A dummy method that can be called from events
  114. */
  115. myMethod(number1: float, text1: string) {
  116. logger.log('Congrats, this method was called on a DummyRuntimeObject');
  117. logger.log('Here is the object:', this);
  118. logger.log('Here are the arguments passed from events:', number1, text1);
  119. }
  120. }
  121. gdjs.registerObject(
  122. //Replace by your extension + object name.
  123. 'MyDummyExtension::DummyObject',
  124. gdjs.DummyRuntimeObject
  125. );
  126. }