Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Main {
- constructor() {
- this.app = new PIXI.Application(800, 600);
- document.body.appendChild(this.app.view);
- this.currentSegment = 0;
- this.goWith = 0;
- this.segmentArray = [4,2,6,3,7,5,3,5,2,15,3,2,5,7,3,2];
- this.wheel = PIXI.Sprite.fromImage('assets/images/wheel.png');
- this.wheel.anchor.set(0.5);
- this.arrow = PIXI.Sprite.fromImage('assets/images/arrow.png');
- this.startSpin = false;
- this.countingText = new PIXI.Text('0', {
- fontWeight: 'bold',
- fontSize: 200,
- fontFamily: 'Arvo',
- fill: '#3e1707',
- align: 'center',
- stroke: '#a4410e',
- strokeThickness: 7
- });
- this.countingText.visible = false;
- this.countingText.anchor.set(0.5);
- this.countingText.x = window.innerWidth / 2;
- this.countingText.y = window.innerHeight / 2;
- this.spinSound = new Audio('assets/sounds/spin.mp3');
- this.winSound = new Audio('assets/sounds/win.mp3');
- this.app.ticker.add(
- (deltaTime) => this.enterFrame()
- );
- this.app.ticker.add(delta => this.spin());
- this.app.ticker.add(delta => this.showText());
- this.wheel.x = window.innerWidth / 2;
- this.wheel.y = window.innerHeight / 2;
- this.arrow.position.y = window.innerHeight / 2 - 79;
- this.arrow.position.x = window.innerWidth / 2 + 250;
- this.app.stage.addChild(this.wheel, this.arrow, this.countingText);
- this.app.stage.interactive = true;
- this.app.stage.on('pointerdown', () => {
- this.spinWheel();
- });
- }
- /**
- * Main enterFrame loop.
- */
- getRandomInt(min, max) {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- enterFrame() {
- this.app.renderer.resize(
- Math.min(window.innerWidth, document.documentElement.clientWidth),
- Math.min(window.innerHeight, document.documentElement.clientHeight)
- );
- }
- spinWheel() {
- this.startSpin = true;
- this.app.stage.off('pointerdown');
- this.ourResult = this.getRandomInt(0, 15);
- if (this.currentSegment <= this.ourResult) {
- this.goWith = this.ourResult - this.currentSegment;
- this.currentSegment = this.ourResult;
- } else {
- this.goWith = this.ourResult + 16 - this.currentSegment;
- this.currentSegment = this.ourResult;
- }
- this.spinResult = this.segmentArray[this.ourResult];
- this.spinTotal = 0;
- this.spinSound.play();
- console.log(this.ourResult);/*
- console.log(this.spinResult);*/
- }
- spin() {
- if (this.startSpin == true) {
- if (this.spinTotal < Math.PI * 2 * 0.0625 * this.goWith + (Math.PI * 2 * 2) ) {
- this.spinTotal += Math.PI * 2 * 0.0125;
- this.wheel.rotation += Math.PI * 2 * 0.0125;
- } else {
- this.countNumber();
- }
- }
- }
- countNumber() {
- this.winSound.play();
- this.countingText.visible = true;
- this.counter = 0;
- this.countingText.text = this.spinResult;
- this.startSpin = false;
- this.app.stage.on('pointerdown', () => {
- this.spinWheel();
- });
- }
- showText() {
- if (this.startSpin == false) {
- if (Math.floor(this.counter) <= this.spinResult) {
- this.countingText.text = Math.floor(this.counter);
- this.counter += 0.2;
- } else {
- if (this.counter < this.spinResult+2) {
- this.counter += 0.01;
- } else {
- this.countingText.visible = false;
- }
- }
- } else {
- this.countingText.visible = false;
- }
- }
- }
- new Main();
Advertisement
Add Comment
Please, Sign In to add comment