slotmachine.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. include("mouseinput.js");
  2. include("camera.js");
  3. include("background.js");
  4. include("headsupdisplay.js");
  5. include("curtain.js");
  6. include("slotreel.js");
  7. function SlotMachine() {
  8. this.game = null;
  9. this.system = null;
  10. this.canvas = null;
  11. this.mouse = null;
  12. this.camera = null;
  13. this.background = null;
  14. this.reels = [];
  15. this.spinButton = null;
  16. this.waitingToCalculate = false;
  17. this.prize = 20;
  18. this.prizeTextPosition = 1024;
  19. this.prizeTextFontSize = 72;
  20. this.curtain = null;
  21. this.isStarted = false;
  22. this.init = function(systemObject, canvas) {
  23. this.game = this;
  24. this.canvas = canvas;
  25. this.system = systemObject;
  26. system.assets.buildSprite("slotreel", "slotreel", {x:0, y:0}, {width: 256, height: 2048});
  27. this.mouse = new MouseInput();
  28. this.mouse.attach(canvas, this);
  29. canvas.style.cursor = "default";
  30. this.camera = new Camera();
  31. this.camera.init(canvas);
  32. this.background = new Background(this.camera);
  33. this.background.init();
  34. this.reels = [];
  35. this.reels[0] = new SlotReel();
  36. this.reels[1] = new SlotReel();
  37. this.reels[2] = new SlotReel();
  38. for(var reelIndex in this.reels) {
  39. var reel = this.reels[reelIndex];
  40. reel.init({x: (256 * reelIndex) + 32 + (16 * reelIndex), y: 32}, parseInt(reelIndex));
  41. }
  42. this.spinButton = new Button(canvas);
  43. this.spinButton.addClickEvent(this.spinReels);
  44. this.prize = 20;
  45. this.ui = new HeadsUpDisplay(this.game, this.camera);
  46. this.ui.init(canvas);
  47. var stage = this;
  48. this.curtain = new Curtain();
  49. this.curtain.init(systemObject, canvas);
  50. this.curtain.open(700, function() {
  51. stage.isStarted = true;
  52. });
  53. this.handleResize(canvas);
  54. };
  55. this.end = function(callback) {
  56. this.mouse.detatch();
  57. this.closeCurtain(callback);
  58. }
  59. this.closeCurtain = function(callback) {
  60. system.theater.stage.curtain.close(700, function() {
  61. callback();
  62. });
  63. }
  64. this.updateDelta = function(delta, canvas){
  65. this.camera.update(canvas, this.player, delta);
  66. this.ui.update(delta);
  67. this.update(canvas);
  68. this.curtain.updateDelta(delta);
  69. };
  70. this.update = function(canvas) {
  71. this.background.update(canvas);
  72. if(system.keyboard.isPressed(Keys.F1)) {
  73. system.theater.changeStage("mainmenu");
  74. }
  75. if(system.keyboard.isPressed(Keys.F2)) {
  76. system.theater.changeStage("end");
  77. }
  78. if(system.keyboard.isPressed(Keys.Space) || system.keyboard.isPressed(Keys.Enter)) {
  79. this.spinReels();
  80. }
  81. for(var reelIndex in this.reels) {
  82. var reel = this.reels[reelIndex];
  83. reel.update();
  84. }
  85. var spinResults = [];
  86. for(var reelIndex in this.reels) {
  87. var reel = this.reels[reelIndex];
  88. if(!reel.isSpinning) {
  89. spinResults.push(reel.spinTarget);
  90. }
  91. }
  92. if(this.waitingToCalculate && spinResults.length == 3) {
  93. if(spinResults[0] == spinResults[1]) {
  94. if(spinResults[0] == spinResults[2]) {
  95. this.prize += 50;
  96. } else {
  97. this.prize += 20;
  98. }
  99. } else if(spinResults[0] == spinResults[2]) {
  100. this.prize += 20;
  101. } else if(spinResults[1] == spinResults[2]) {
  102. this.prize += 20;
  103. }
  104. this.waitingToCalculate = false;
  105. if(this.prize > 100) {
  106. system.theater.changeStage("end");
  107. }
  108. }
  109. };
  110. this.draw = function(context) {
  111. context.save();
  112. context.translate(this.camera.position.x, this.camera.position.y);
  113. this.background.draw(context);
  114. for(var reelIndex in this.reels) {
  115. var reel = this.reels[reelIndex];
  116. reel.draw(context);
  117. }
  118. this.spinButton.draw(context);
  119. context.font = this.prizeTextFontSize + "px sans-serif";
  120. context.textAlign = "right";
  121. context.textBaseline = "middle";
  122. context.lineWidth = 2;
  123. context.fillStyle = "black";
  124. context.strokeStyle = "black";
  125. context.fillText("$" + this.prize, this.prizeTextPosition, 64);
  126. context.restore();
  127. this.ui.draw(context);
  128. this.curtain.draw(context);
  129. };
  130. this.handleResize = function(canvas) {
  131. this.prizeTextPosition = 1024;
  132. this.prizeTextFontSize = 72;
  133. this.camera.handleResize(canvas);
  134. var buttonOptions = {position: {x: 1024, y: 512}, font:{color:"#DDDDDD", stroke: "#333333", size: 72, family: "sans-serif"}};
  135. if(this.canvas.width < 768) {
  136. buttonOptions.font.size = 36;
  137. buttonOptions.position.x *= 0.5;
  138. buttonOptions.position.y *= 0.5;
  139. this.prizeTextPosition *= 0.5;
  140. this.prizeTextFontSize = 36;
  141. }
  142. this.spinButton.init("Spin", buttonOptions);
  143. for(var reelIndex in this.reels) {
  144. this.reels[reelIndex].handleResize(canvas);
  145. }
  146. this.ui.handleResize(canvas);
  147. }
  148. this.mouseMove = function(canvas, x, y) {
  149. this.ui.mouseMove(canvas, x, y);
  150. this.spinButton.mouseMove(x, y);
  151. };
  152. this.mouseDown = function(canvas, button, x, y) {
  153. this.ui.mouseDown(canvas, button, x, y);
  154. this.spinButton.mouseDown(button, x, y);
  155. };
  156. this.mouseUp = function(canvas, button, x, y) {
  157. this.ui.mouseUp(canvas, button, x, y);
  158. };
  159. this.touchStart = function(canvas, x, y) {
  160. this.ui.touchStart(canvas, x, y);
  161. this.spinButton.touchStart(x, y);
  162. };
  163. this.touchMove = function(canvas, x, y) {
  164. this.ui.touchMove(canvas, x, y);
  165. this.spinButton.touchMove(x, y);
  166. };
  167. this.touchEnd = function(canvas, x, y) {
  168. this.ui.touchEnd(canvas, x, y);
  169. };
  170. this.contextMenu = function(canvas, event) {
  171. };
  172. this.spinReels = function() {
  173. system.theater.stage.prize--;
  174. var reels = system.theater.stage.reels;
  175. for(var reelIndex in reels) {
  176. var reel = reels[reelIndex];
  177. if(reel.isSpinning) {
  178. return;
  179. }
  180. }
  181. for(var reelIndex in reels) {
  182. var reel = reels[reelIndex];
  183. reel.startSpin();
  184. }
  185. system.theater.stage.waitingToCalculate = true;
  186. }
  187. };