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.

181 lines
3.7 KiB

  1. define([
  2. 'js/system/events',
  3. 'js/system/client',
  4. 'html!ui/templates/createCharacter/template',
  5. 'css!ui/templates/createCharacter/styles',
  6. 'ui/factory'
  7. ], function(
  8. events,
  9. client,
  10. template,
  11. styles,
  12. uiFactory
  13. ) {
  14. return {
  15. tpl: template,
  16. centered: true,
  17. classSprites: null,
  18. class: 'wizard',
  19. costume: 0,
  20. prophecies: [],
  21. postRender: function() {
  22. this.getSkins();
  23. uiFactory.build('tooltips');
  24. this.find('.txtClass').on('click', this.changeClass.bind(this));
  25. this.find('.txtCostume').on('click', this.changeCostume.bind(this));
  26. this.find('.btnBack').on('click', this.back.bind(this));
  27. this.find('.btnCreate').on('click', this.create.bind(this));
  28. this.find('.prophecy')
  29. .on('click', this.onProphecyClick.bind(this))
  30. .on('mousemove', this.onProphecyHover.bind(this))
  31. .on('mouseleave', this.onProphecyUnhover.bind(this));
  32. },
  33. getSkins: function() {
  34. this.el.addClass('disabled');
  35. client.request({
  36. cpn: 'auth',
  37. method: 'getSkins',
  38. data: {
  39. },
  40. callback: this.onGetSkins.bind(this)
  41. });
  42. },
  43. onGetSkins: function(result) {
  44. this.el.removeClass('disabled');
  45. this.classSprites = result;
  46. this.costume = -1;
  47. this.changeCostume({
  48. target: this.find('.txtCostume')
  49. });
  50. },
  51. onProphecyHover: function(e) {
  52. var el = $(e.currentTarget);
  53. var pos = {
  54. x: e.clientX + 25,
  55. y: e.clientY
  56. };
  57. var text = el.attr('tooltip');
  58. events.emit('onShowTooltip', text, el[0], pos);
  59. $('.uiTooltips .tooltip').addClass('bright');
  60. },
  61. onProphecyUnhover: function(e) {
  62. var el = $(e.currentTarget);
  63. events.emit('onHideTooltip', el[0]);
  64. },
  65. onProphecyClick: function(e) {
  66. var el = $(e.currentTarget);
  67. var pName = el.attr('prophecy');
  68. if (el.hasClass('active')) {
  69. this.prophecies.spliceWhere(function(p) {
  70. return (p == pName);
  71. });
  72. el.removeClass('active');
  73. }
  74. else {
  75. this.prophecies.push(pName);
  76. el.addClass('active');
  77. }
  78. },
  79. clear: function() {
  80. this.prophecies = [];
  81. this.find('.prophecy').removeClass('active');
  82. },
  83. back: function() {
  84. this.clear();
  85. this.el.remove();
  86. uiFactory.build('characters', {});
  87. },
  88. create: function() {
  89. this.el.addClass('disabled');
  90. client.request({
  91. cpn: 'auth',
  92. method: 'createCharacter',
  93. data: {
  94. name: this.find('.txtName').val(),
  95. class: this.class,
  96. costume: this.costume,
  97. prophecies: this.prophecies
  98. },
  99. callback: this.onCreate.bind(this)
  100. });
  101. },
  102. onCreate: function(result) {
  103. this.el.removeClass('disabled');
  104. this.clear();
  105. if (!result) {
  106. this.el.remove();
  107. events.emit('onEnterGame');
  108. } else
  109. this.el.find('.message').html(result);
  110. },
  111. changeClass: function(e) {
  112. var el = $(e.target);
  113. var classes = Object.keys(this.classSprites);
  114. var nextIndex = (classes.indexOf(el.html()) + 1) % classes.length;
  115. this.costume = -1;
  116. var newClass = classes[nextIndex];
  117. el.html(newClass);
  118. this.class = newClass;
  119. this.changeCostume({
  120. target: this.find('.txtCostume')
  121. });
  122. },
  123. changeCostume: function(e) {
  124. var el = $(e.target);
  125. var spriteList = this.classSprites[this.class];
  126. this.costume = (this.costume + 1) % spriteList.length;
  127. el.html((this.costume + 1) + '/' + spriteList.length);
  128. this.setSprite();
  129. },
  130. setSprite: function() {
  131. var classSprite = this.classSprites[this.class][this.costume];
  132. var costume = classSprite.sprite.split(',');
  133. var spirteX = -costume[0] * 32;
  134. var spriteY = -costume[1] * 32;
  135. var spritesheet = classSprite.spritesheet || '../../../images/charas.png';
  136. this.find('.sprite')
  137. .css('background', 'url("' + spritesheet + '") ' + spirteX + 'px ' + spriteY + 'px');
  138. }
  139. };
  140. });