levelPackSelectState.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. function LevelPackSelectState() {
  2. LevelPackSelectState.superclass.constructor.apply(this);
  3. this.back = null;
  4. this.icons = [];
  5. this.labels = [];
  6. this.iconsLayer = null;
  7. this.state = this.STATE_WAIT;
  8. this.prevMousePoint = {x: _TrinGame.mouse.x, y: _TrinGame.mouse.y};
  9. this.title = null;
  10. this.bBack = null;
  11. this.a10Logo = null;
  12. }
  13. extend(LevelPackSelectState, TrinState);
  14. LevelPackSelectState.prototype.create = function() {
  15. LevelPackSelectState.superclass.create.apply(this);
  16. this.back = new TrinSprite();
  17. this.back.addAnimationFromCache("CompleteBack");
  18. var levelPacks = Global.prototype.levelpacks;
  19. for (var i = 0; i < levelPacks.length; i++) {
  20. var levelPack = levelPacks[i];
  21. var stars = 0;
  22. var levels = Global.prototype.levels[levelPack.name];
  23. for (var j = 0; j < levels.length; j++) {
  24. if (levels[j] > 0) {
  25. stars += levels[j];
  26. }
  27. }
  28. var icon = new TrinButton(function() {
  29. _TrinGame.switchState(new LevelSelectState(Global.prototype.levelpacks[0]));
  30. }, levelPack.name + "Icon", false);
  31. this.icons[this.icons.length] = new LevelPackIcon(icon, stars + "/" + (levels.length * 3));
  32. }
  33. var comingSoon = new TrinSprite();
  34. comingSoon.addAnimationFromCache("ComingSoonIcon");
  35. this.icons[this.icons.length] = new LevelPackIcon(comingSoon, "");
  36. this.iconsLayer = new TrinLayer();
  37. for (i = 0; i < this.icons.length; i++) {
  38. icon = this.icons[i];
  39. icon.reset(320 + i * _TrinGame.width / 2, _TrinGame.height / 2);
  40. if (i > 0) {
  41. icon.scale.x = icon.scale.y = this.minIconScale;
  42. }
  43. this.iconsLayer.add(this.icons[i]);
  44. }
  45. this.title = new TrinSprite();
  46. this.title.addAnimationFromCache("LevelPackSelectTitle");
  47. this.title.orign.x = this.title.width / 2;
  48. this.title.reset(320, 180);
  49. this.bBack = new TrinButton(function() {
  50. _TrinGame.switchState(new MainMenuState());
  51. }, "bBack", false);
  52. this.bBack.orign.x = this.bBack.width;
  53. this.bBack.orign.y = this.bBack.height;
  54. this.bBack.reset(632, _TrinGame.visibleArea.bottom - 8);
  55. var a10Logo = new TrinButton(function() {
  56. //_TrinGame.SPIL_LOGO.action();
  57. }, "A10Logo", true);
  58. a10Logo.orign.y = a10Logo.height;
  59. a10Logo.reset(8, _TrinGame.visibleArea.bottom - 8);
  60. this.a10Logo = a10Logo;
  61. this.add(this.back);
  62. this.add(this.iconsLayer);
  63. this.add(this.title);
  64. this.add(this.bBack);
  65. this.add(a10Logo);
  66. };
  67. LevelPackSelectState.prototype.update = function() {
  68. LevelPackSelectState.superclass.update.apply(this);
  69. var mouse = _TrinGame.mouse;
  70. var d = 0;
  71. switch (this.state) {
  72. case this.STATE_WAIT:
  73. if (mouse.isPressed()) {
  74. this.state = this.STATE_SCROLLING;
  75. }
  76. break;
  77. case this.STATE_SCROLLING:
  78. var d = Math.min(320 - this.icons[0].x,
  79. Math.max(mouse.x - this.prevMousePoint.x, 320 - this.icons[this.icons.length - 1].x));
  80. if (mouse.isReleased()) {
  81. this.state = this.STATE_FOCUSING;
  82. }
  83. if (Math.abs(d) > 4) {
  84. this.iconsLayer.active = false;
  85. }
  86. break;
  87. case this.STATE_FOCUSING:
  88. this.iconsLayer.active = true;
  89. d = 640;
  90. for (var i = 0; i < this.icons.length; i++) {
  91. if (Math.abs(320 - this.icons[i].x) < d) {
  92. d = (320 - this.icons[i].x);
  93. }
  94. }
  95. if (Math.abs(d) > 0.2) {
  96. d /= 8;
  97. }
  98. if (d === 0) {
  99. this.state = this.STATE_WAIT;
  100. }
  101. if (mouse.isPressed()) {
  102. this.state = this.STATE_SCROLLING;
  103. }
  104. break;
  105. }
  106. if (d !== 0) {
  107. var icon;
  108. for (var i = 0; i < this.icons.length; i++) {
  109. icon = this.icons[i];
  110. icon.reset(icon.x + d, icon.y);
  111. icon.scale.x = icon.scale.y = Math.max(LevelPackIcon.prototype.minIconScale, 1 - (Math.abs(320 - icon.x) / 640));
  112. }
  113. }
  114. this.prevMousePoint.x = mouse.x;
  115. };
  116. LevelPackSelectState.prototype.resized = function() {
  117. this.bBack.y = this.a10Logo.y = _TrinGame.visibleArea.bottom - 8;
  118. };
  119. LevelPackSelectState.prototype.STATE_WAIT = 0;
  120. LevelPackSelectState.prototype.STATE_SCROLLING = 1;
  121. LevelPackSelectState.prototype.STATE_FOCUSING = 2;
  122. LevelPackSelectState.prototype.minIconScale = 0.6;