Guest User

Untitled

a guest
Dec 15th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. class Main {
  2. constructor() {
  3. this.app = new PIXI.Application(800, 600);
  4. document.body.appendChild(this.app.view);
  5. this.currentSegment = 0;
  6. this.goWith = 0;
  7. this.segmentArray = [4,2,6,3,7,5,3,5,2,15,3,2,5,7,3,2];
  8. this.wheel = PIXI.Sprite.fromImage('assets/images/wheel.png');
  9. this.wheel.anchor.set(0.5);
  10. this.arrow = PIXI.Sprite.fromImage('assets/images/arrow.png');
  11. this.startSpin = false;
  12. this.countingText = new PIXI.Text('0', {
  13. fontWeight: 'bold',
  14. fontSize: 200,
  15. fontFamily: 'Arvo',
  16. fill: '#3e1707',
  17. align: 'center',
  18. stroke: '#a4410e',
  19. strokeThickness: 7
  20. });
  21. this.countingText.visible = false;
  22. this.countingText.anchor.set(0.5);
  23. this.countingText.x = window.innerWidth / 2;
  24. this.countingText.y = window.innerHeight / 2;
  25. this.spinSound = new Audio('assets/sounds/spin.mp3');
  26. this.winSound = new Audio('assets/sounds/win.mp3');
  27. this.app.ticker.add(
  28. (deltaTime) => this.enterFrame()
  29. );
  30. this.app.ticker.add(delta => this.spin());
  31. this.app.ticker.add(delta => this.showText());
  32. this.wheel.x = window.innerWidth / 2;
  33. this.wheel.y = window.innerHeight / 2;
  34. this.arrow.position.y = window.innerHeight / 2 - 79;
  35. this.arrow.position.x = window.innerWidth / 2 + 250;
  36. this.app.stage.addChild(this.wheel, this.arrow, this.countingText);
  37. this.app.stage.interactive = true;
  38. this.app.stage.on('pointerdown', () => {
  39. this.spinWheel();
  40. });
  41. }
  42. /**
  43. * Main enterFrame loop.
  44. */
  45. getRandomInt(min, max) {
  46. return Math.floor(Math.random() * (max - min + 1)) + min;
  47. }
  48.  
  49. enterFrame() {
  50. this.app.renderer.resize(
  51. Math.min(window.innerWidth, document.documentElement.clientWidth),
  52. Math.min(window.innerHeight, document.documentElement.clientHeight)
  53. );
  54.  
  55. }
  56.  
  57.  
  58. spinWheel() {
  59. this.startSpin = true;
  60. this.app.stage.off('pointerdown');
  61. this.ourResult = this.getRandomInt(0, 15);
  62. if (this.currentSegment <= this.ourResult) {
  63. this.goWith = this.ourResult - this.currentSegment;
  64. this.currentSegment = this.ourResult;
  65. } else {
  66. this.goWith = this.ourResult + 16 - this.currentSegment;
  67. this.currentSegment = this.ourResult;
  68. }
  69. this.spinResult = this.segmentArray[this.ourResult];
  70. this.spinTotal = 0;
  71. this.spinSound.play();
  72. console.log(this.ourResult);/*
  73. console.log(this.spinResult);*/
  74. }
  75.  
  76. spin() {
  77. if (this.startSpin == true) {
  78. if (this.spinTotal < Math.PI * 2 * 0.0625 * this.goWith + (Math.PI * 2 * 2) ) {
  79. this.spinTotal += Math.PI * 2 * 0.0125;
  80. this.wheel.rotation += Math.PI * 2 * 0.0125;
  81. } else {
  82. this.countNumber();
  83. }
  84. }
  85. }
  86.  
  87. countNumber() {
  88. this.winSound.play();
  89. this.countingText.visible = true;
  90. this.counter = 0;
  91. this.countingText.text = this.spinResult;
  92. this.startSpin = false;
  93. this.app.stage.on('pointerdown', () => {
  94. this.spinWheel();
  95. });
  96. }
  97.  
  98. showText() {
  99. if (this.startSpin == false) {
  100. if (Math.floor(this.counter) <= this.spinResult) {
  101. this.countingText.text = Math.floor(this.counter);
  102. this.counter += 0.2;
  103. } else {
  104. if (this.counter < this.spinResult+2) {
  105. this.counter += 0.01;
  106. } else {
  107. this.countingText.visible = false;
  108. }
  109. }
  110.  
  111. } else {
  112. this.countingText.visible = false;
  113. }
  114.  
  115. }
  116.  
  117. }
  118.  
  119. new Main();
Advertisement
Add Comment
Please, Sign In to add comment