You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

133 lines
2.4 KiB

  1. define([
  2. 'js/system/client',
  3. 'ui/factory',
  4. 'js/rendering/renderer',
  5. 'js/objects/objects',
  6. 'js/rendering/effects',
  7. 'js/rendering/numbers',
  8. 'js/input',
  9. 'js/system/events',
  10. 'js/resources',
  11. 'js/sound/sound',
  12. 'js/system/globals',
  13. 'ui/templates/online/online',
  14. 'ui/templates/tooltips/tooltips'
  15. ], function (
  16. client,
  17. uiFactory,
  18. renderer,
  19. objects,
  20. effects,
  21. numbers,
  22. input,
  23. events,
  24. resources,
  25. sound,
  26. globals
  27. ) {
  28. let fnQueueTick = null;
  29. const getQueueTick = updateMethod => {
  30. return () => requestAnimationFrame(updateMethod);
  31. };
  32. let restoreSoundOnFocus = false;
  33. return {
  34. hasFocus: true,
  35. lastRender: 0,
  36. msPerFrame: ~~(1000 / 60),
  37. init: function () {
  38. if (isMobile)
  39. $('.ui-container').addClass('mobile');
  40. client.init(this.onClientReady.bind(this));
  41. },
  42. onClientReady: function () {
  43. client.request({
  44. module: 'clientConfig',
  45. method: 'getClientConfig',
  46. callback: this.onGetClientConfig.bind(this)
  47. });
  48. },
  49. onGetClientConfig: async function (config) {
  50. globals.clientConfig = config;
  51. await resources.init();
  52. events.emit('onResourcesLoaded');
  53. this.start();
  54. },
  55. start: function () {
  56. window.addEventListener('focus', this.onFocus.bind(this, true));
  57. window.addEventListener('blur', this.onFocus.bind(this, false));
  58. $(window).on('contextmenu', this.onContextMenu.bind(this));
  59. sound.init();
  60. objects.init();
  61. renderer.init();
  62. input.init();
  63. numbers.init();
  64. uiFactory.init(null);
  65. fnQueueTick = getQueueTick(this.update.bind(this));
  66. fnQueueTick();
  67. $('.loader-container').remove();
  68. },
  69. onFocus: function (isFocussed) {
  70. this.hasFocus = isFocussed;
  71. if (window.isMobile) {
  72. if (!this.hasFocus) {
  73. restoreSoundOnFocus = !sound.muted;
  74. sound.onToggleAudio(false);
  75. } else if (restoreSoundOnFocus) {
  76. sound.onToggleAudio(true);
  77. restoreSoundOnFocus = false;
  78. }
  79. }
  80. if (!isFocussed)
  81. input.resetKeys();
  82. },
  83. onContextMenu: function (e) {
  84. const allowed = ['txtUsername', 'txtPassword'].some(s => $(e.target).hasClass(s));
  85. if (!allowed) {
  86. e.preventDefault();
  87. return false;
  88. }
  89. },
  90. update: function () {
  91. const time = +new Date();
  92. if (time - this.lastRender < this.msPerFrame - 1) {
  93. fnQueueTick();
  94. return;
  95. }
  96. objects.update();
  97. renderer.update();
  98. uiFactory.update();
  99. numbers.update();
  100. renderer.render();
  101. this.lastRender = time;
  102. fnQueueTick();
  103. }
  104. };
  105. });