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.
 
 
 

140 lines
2.6 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. 'js/components/components',
  14. 'ui/templates/online/online',
  15. 'ui/templates/tooltips/tooltips'
  16. ], function (
  17. client,
  18. uiFactory,
  19. renderer,
  20. objects,
  21. effects,
  22. numbers,
  23. input,
  24. events,
  25. resources,
  26. sound,
  27. globals,
  28. components
  29. ) {
  30. let fnQueueTick = null;
  31. const getQueueTick = updateMethod => {
  32. return () => requestAnimationFrame(updateMethod);
  33. };
  34. const loadLongPress = async () => {
  35. return new Promise(res => {
  36. require(['longPress'], res);
  37. });
  38. };
  39. return {
  40. hasFocus: true,
  41. lastRender: 0,
  42. msPerFrame: ~~(1000 / 60),
  43. init: async function () {
  44. if (isMobile) {
  45. $('.ui-container').addClass('mobile');
  46. //If we're on an ios device, we need to load longPress since that polyfills contextmenu for us
  47. if (_.isIos())
  48. await loadLongPress();
  49. }
  50. if (window.location.search.includes('hideMonetization'))
  51. $('.ui-container').addClass('hideMonetization');
  52. client.init(this.onClientReady.bind(this));
  53. },
  54. onClientReady: function () {
  55. client.request({
  56. module: 'clientConfig',
  57. method: 'getClientConfig',
  58. callback: this.onGetClientConfig.bind(this)
  59. });
  60. },
  61. onGetClientConfig: async function (config) {
  62. globals.clientConfig = config;
  63. await resources.init();
  64. await components.init();
  65. events.emit('onResourcesLoaded');
  66. this.start();
  67. },
  68. start: function () {
  69. window.onfocus = this.onFocus.bind(this, true);
  70. window.onblur = this.onFocus.bind(this, false);
  71. $(window).on('contextmenu', this.onContextMenu.bind(this));
  72. sound.init();
  73. objects.init();
  74. renderer.init();
  75. input.init();
  76. numbers.init();
  77. uiFactory.init(null);
  78. fnQueueTick = getQueueTick(this.update.bind(this));
  79. fnQueueTick();
  80. $('.loader-container').remove();
  81. },
  82. onFocus: function (hasFocus) {
  83. //Hack: Later we might want to make it not render when out of focus
  84. this.hasFocus = true;
  85. if (!hasFocus)
  86. input.resetKeys();
  87. },
  88. onContextMenu: function (e) {
  89. const allowed = ['txtUsername', 'txtPassword'].some(s => $(e.target).hasClass(s));
  90. if (!allowed) {
  91. e.preventDefault();
  92. return false;
  93. }
  94. },
  95. update: function () {
  96. const time = +new Date();
  97. if (time - this.lastRender < this.msPerFrame - 1) {
  98. fnQueueTick();
  99. return;
  100. }
  101. objects.update();
  102. renderer.update();
  103. uiFactory.update();
  104. numbers.update();
  105. renderer.render();
  106. this.lastRender = time;
  107. fnQueueTick();
  108. }
  109. };
  110. });