Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

gameover-overlay.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * -------------------------------------------
  3. * Magic AR Basketball
  4. * A demo game of the encantar.js WebAR engine
  5. * -------------------------------------------
  6. * @fileoverview An overlay displayed at the end of a match - 2D Graphical User Interface (GUI)
  7. * @author Alexandre Martins <alemartf(at)gmail.com> (https://github.com/alemart/encantar-js)
  8. */
  9. import { GUIControl } from './gui-control.js';
  10. import { GameEvent } from '../../core/events.js';
  11. /**
  12. * An overlay displayed at the end of a match
  13. */
  14. export class GameOverOverlay extends GUIControl
  15. {
  16. /**
  17. * Constructor
  18. * @param {BasketballDemo} demo
  19. */
  20. constructor(demo)
  21. {
  22. super(demo);
  23. this._messages = {
  24. 'S' : 'YOU ARE A\nLEGEND!!!!!',
  25. 'A+': 'Well done!\nYou\'re a Pro!',
  26. 'A' : 'Well done!\nYou\'re a Pro!',
  27. 'B+': 'Nice, but you\'re\nnot yet a Pro!',
  28. 'B' : 'You can do better!',
  29. 'C' : 'Try again!',
  30. 'F' : 'Try again!'
  31. };
  32. }
  33. /**
  34. * Create the control
  35. * @returns {BABYLON.GUI.Control}
  36. */
  37. _createControl()
  38. {
  39. const container = new BABYLON.GUI.Container();
  40. const title = new BABYLON.GUI.TextBlock();
  41. const rank = new BABYLON.GUI.TextBlock('rank');
  42. const message = new BABYLON.GUI.TextBlock('message');
  43. const circle = new BABYLON.GUI.Ellipse();
  44. container.background = 'rgba(51, 51, 76, 0.75)';
  45. container.zIndex = 1;
  46. title.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  47. title.textVerticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER;
  48. title.text = 'Rank';
  49. title.color = 'white';
  50. title.fontFamily = 'sans-serif';
  51. title.fontStyle = 'bold';
  52. title.fontSize = 80;
  53. title.top = '-192px';
  54. title.left = '0px';
  55. container.addControl(title);
  56. rank.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  57. rank.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER;
  58. rank.text = '';
  59. rank.color = 'white';
  60. rank.fontFamily = 'sans-serif';
  61. rank.fontStyle = 'bold';
  62. rank.fontSize = 112;
  63. container.addControl(rank);
  64. circle.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  65. circle.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER;
  66. circle.width = '192px';
  67. circle.height = circle.width;
  68. circle.color = 'white';
  69. circle.thickness = 16;
  70. container.addControl(circle);
  71. message.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_CENTER;
  72. message.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_CENTER;
  73. message.text = '';
  74. message.textWrapping = true;
  75. message.color = 'white';
  76. message.fontFamily = 'sans-serif';
  77. message.fontSize = 56;
  78. message.top = '192px';
  79. message.left = '0px';
  80. message.paddingLeft = '10%';
  81. message.paddingRight = '10%';
  82. container.addControl(message);
  83. return container;
  84. }
  85. /**
  86. * Update the entity
  87. * @returns {void}
  88. */
  89. update()
  90. {
  91. const container = this.control;
  92. if(!container.isVisible)
  93. return;
  94. const ar = this.ar;
  95. if(ar.pointers.length == 0)
  96. return;
  97. const pointer = ar.pointers[0];
  98. if(pointer.phase != 'began')
  99. return;
  100. // hide the overlay when touching the screen
  101. container.isVisible = false;
  102. this._broadcast(new GameEvent('restarted'));
  103. }
  104. /**
  105. * Handle an event
  106. * @param {GameEvent} event
  107. * @returns {void}
  108. */
  109. handleEvent(event)
  110. {
  111. if(event.type == 'gameover') {
  112. const container = this.control;
  113. const rank = container.getChildByName('rank');
  114. const message = container.getChildByName('message');
  115. rank.text = event.detail.rank;
  116. message.text = this._messages[event.detail.rank] || '';
  117. container.isVisible = true;
  118. }
  119. else if(event.type == 'targetlost')
  120. this.control.isVisible = false;
  121. }
  122. }