Advertisement
Qpel

4prakt

Nov 15th, 2019
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {Component} from 'react';
  2. import ReactDOM from 'react-dom';
  3. import App from './App';
  4.  
  5. class Dice extends React.Component {
  6.   constructor(props) {
  7.     super(props);
  8.     this.state = {
  9.       faceValue: 0,
  10.       face: "⚀",
  11.       rollCount: 1,
  12.       isRolling: false,      
  13.     };
  14.     this.DiceRoll = this.DiceRoll.bind(this);
  15.     this.HandleDiceThrow = this.HandleDiceThrow.bind(this);
  16.     this.rez = 1;
  17.    
  18.   }
  19.  
  20.   GenerateRandomInt(min, max) {
  21.     return Math.floor(Math.random() * (max - min + 1)) + min;
  22.   }
  23.  
  24.   DiceRoll() {
  25.     let faceValue = this.GenerateRandomInt(0, 5);          
  26.     this.setState({      
  27.       rollCount: this.state.rollCount - 1,
  28.       isRolling: (this.state.rollCount > 0),
  29.       faceValue: faceValue,
  30.       face: "&#x" + String(2680 + faceValue) + ";"
  31.      
  32.     });  
  33.     console.log(this.state);
  34.   }
  35.  
  36.   HandleDiceThrow(){    
  37.     if (this.state.isRolling)return;
  38.     let val = this.GenerateRandomInt(5,15);
  39.     this.setState({rollCount: val});
  40.     for (let i = 0; i <= val; i++){
  41.       setTimeout(this.DiceRoll, 50 * i);
  42.      
  43.     }
  44.     this.rez = this.state.faceValue;
  45.    
  46.   }
  47.  
  48.   render() {
  49.     return (
  50.       <div>
  51.            
  52.         <div onClick={this.HandleDiceThrow} id="diceFace" dangerouslySetInnerHTML={{ __html: `${this.state.face}` }}></div>        
  53.         <span>Dice Value: {this.state.faceValue + 1}</span>  <br/>
  54.         <span>Roll: {this.state.rollCount}</span> <br/>        
  55.         {this.rez+1} abc
  56.       </div>
  57.     );
  58.   }
  59. }
  60.  
  61. ReactDOM.render(
  62.   <Dice />,
  63.   document.getElementById('app')
  64. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement