slotreel.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. function SlotReel() {
  2. this.id = 0;
  3. this.position = {x: 0, y: 0};
  4. this.width = 256;
  5. this.height = 512;
  6. this.isSpinning = false;
  7. this.scrollAmount = 2048;
  8. this.scrollSpeed = 15;
  9. this.reelRotationsOffset = 0;
  10. this.remainingRevolutions = 0;
  11. this.spinTarget = 0;
  12. this.resizeModifier = 1;
  13. this.init = function(newPos, offset) {
  14. this.position.x = newPos.x;
  15. this.position.y = newPos.y;
  16. this.reelRotationsOffset = offset;
  17. this.spinTarget = Math.floor(7 * Math.random());
  18. this.scrollAmount = 256 * this.spinTarget - 128;
  19. this.scrollSpeed = Math.floor(10 * Math.random() - 5) + 25;
  20. }
  21. this.update = function(delta) {
  22. if(this.isSpinning) {
  23. this.scrollAmount -= this.scrollSpeed;
  24. if(this.remainingRevolutions <= 0) {
  25. if(this.scrollAmount <= 256 * this.spinTarget - 128) {
  26. this.scrollAmount = 256 * this.spinTarget - 128;
  27. this.isSpinning = false;
  28. }
  29. }
  30. if(this.scrollAmount <= -128) {
  31. this.scrollAmount += 2048;
  32. this.remainingRevolutions--;
  33. }
  34. }
  35. }
  36. this.draw = function(context) {
  37. context.save();
  38. context.translate(this.position.x * this.resizeModifier, this.position.y * this.resizeModifier);
  39. context.drawImage(system.assets.getSprite("slotreel"), 0, this.scrollAmount + 2048, 256, 512, 0, 0, 256 * this.resizeModifier, 512 * this.resizeModifier);
  40. context.drawImage(system.assets.getSprite("slotreel"), 0, this.scrollAmount, 256, 512, 0, 0, 256 * this.resizeModifier, 512 * this.resizeModifier);
  41. context.drawImage(system.assets.getSprite("slotreel"), 0, this.scrollAmount - 2048, 256, 512, 0, 0, 256 * this.resizeModifier, 512 * this.resizeModifier);
  42. context.restore();
  43. }
  44. this.handleResize = function(canvas) {
  45. this.resizeModifier = 1;
  46. if(canvas.width < 768) {
  47. this.resizeModifier = 0.5;
  48. }
  49. }
  50. this.startSpin = function() {
  51. this.isSpinning = true;
  52. this.remainingRevolutions = 1 + this.reelRotationsOffset;
  53. this.spinTarget = Math.floor(7 * Math.random());
  54. }
  55. };