Board.jsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import React from 'react';
  2. import FreeParking from "./FreeParking";
  3. import Street from "./Street";
  4. import Chance from "./Chance";
  5. import TrainStation from "./TrainStation";
  6. import Utility from "./Utility";
  7. import GoToJail from "./GoToJail";
  8. import Community from "./Community";
  9. import SuperTax from "./SuperTax";
  10. import Jail from "./Jail";
  11. import Go from "./Go";
  12. import IncomeTax from "./IncomeTax";
  13. import Dice from "./Dice";
  14. import {gameService} from "./services/GameService";
  15. import Token from "./Token";
  16. import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
  17. import {faQuestion, faInbox} from "@fortawesome/free-solid-svg-icons";
  18. let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0, dragging = null;
  19. let lastUpdate = 0;
  20. export default class Board extends React.Component {
  21. constructor(props) {
  22. super(props);
  23. this.state = {};
  24. }
  25. rollDice = () => {
  26. if(gameService.currentPlayer != 1){
  27. gameService.rollDie();
  28. }
  29. }
  30. dragStart = (e, player, playerWhoMoves) => {
  31. if(player.id == playerWhoMoves){
  32. e = e || window.event;
  33. e.preventDefault();
  34. dragging = e.currentTarget;
  35. // get the mouse cursor position at startup:
  36. pos3 = e.clientX;
  37. pos4 = e.clientY;
  38. document.onmouseup = (e) => this.dragStopped(e, player);
  39. // call a function whenever the cursor moves:
  40. document.onmousemove = (e) => this.dragging(e, player);
  41. }
  42. }
  43. dragging = (e, player) => {
  44. e = e || window.event;
  45. e.preventDefault();
  46. // calculate the new cursor position:
  47. pos1 = pos3 - e.clientX;
  48. pos2 = pos4 - e.clientY;
  49. pos3 = e.clientX;
  50. pos4 = e.clientY;
  51. // set the element's new position:
  52. dragging.style.top = (dragging.offsetTop - pos2) + "px";
  53. dragging.style.left = (dragging.offsetLeft - pos1) + "px";
  54. const currentTime = Date.now();
  55. if (currentTime - lastUpdate > 250) {
  56. lastUpdate = currentTime;
  57. const y = ("" + dragging.style.left).replace("px", "");
  58. const x = (dragging.style.top).replace("px", "");
  59. gameService.setPlayerPosition(player.id, x, y);
  60. }
  61. }
  62. dragStopped = (e, player) => {
  63. const y = ("" + dragging.style.left).replace("px", "");
  64. const x = ("" + dragging.style.top).replace("px", "");
  65. gameService.setPlayerPosition(player.id, x, y);
  66. // stop moving when mouse button is released:
  67. document.onmouseup = null;
  68. document.onmousemove = null;
  69. lastUpdate = 0;
  70. gameService.sendToWs('moveFinished', {id: player.id, x: x, y: y,});
  71. }
  72. soundBeep= (g) => {
  73. setTimeout(() => { gameService.startTurn(); }, 1000);
  74. return (<div>
  75. <audio src="https://freesound.org/data/previews/528/528863_7614679-lq.mp3" styles="display: none;" autoPlay/>
  76. </div>)
  77. }
  78. render() {
  79. if (this.props.game) {
  80. const game = this.props.game;
  81. const players = this.props.game.players.filter(p => p.id !== 1);
  82. return (<div className="board-container">
  83. {players.map(p => <div key={p.id} className="board-token"
  84. onMouseDown={(e) => this.dragStart(e, p, gameService.currentPlayer)}
  85. style={{top: p.x + 'px', left: p.y + 'px'}}>
  86. <Token selected={p.id === gameService.currentPlayer} token={p.token}/>
  87. </div>)}
  88. {
  89. (game.players.length > 1 && gameService.currentPlayer == game.players[game.turn].id && this.props.game.changedPlayer) ? (
  90. this.soundBeep(game)
  91. ) : ("")
  92. }
  93. <div className="dice-set" onClick={this.rollDice}>
  94. <Dice diceValue={this.props.game.dice[0]} rolling={this.props.game.rollingDice}/>
  95. <Dice diceValue={this.props.game.dice[1]} rolling={this.props.game.rollingDice}/>
  96. </div>
  97. <div className="community-stack card-stack" onClick={() => gameService.drawCard('community')}>
  98. <span>Community Chest</span>
  99. <div className="icon">
  100. <FontAwesomeIcon icon={faInbox}/>
  101. </div>
  102. </div>
  103. <div className="chance-stack card-stack" onClick={() => gameService.drawCard('chance')}>
  104. <span>Chance</span>
  105. <div className="icon">
  106. <FontAwesomeIcon icon={faQuestion}/>
  107. </div>
  108. </div>
  109. <div className="board">
  110. <Go/>
  111. <Street position="1" boardPos="bottom" game={game}/>
  112. <Community position="2" boardPos="bottom"/>
  113. <Street position="3" boardPos="bottom" game={game}/>
  114. <IncomeTax position="4" boardPos="bottom"/>
  115. <TrainStation position="5" boardPos="bottom" game={game}/>
  116. <Street position="6" boardPos="bottom" game={game}/>
  117. <Chance position="7" boardPos="bottom"/>
  118. <Street position="8" boardPos="bottom" game={game}/>
  119. <Street position="9" boardPos="bottom" game={game}/>
  120. <Jail boardPos="bottom"/>
  121. <Street position="11" boardPos="left" game={game}/>
  122. <Utility position="12" boardPos="left" game={game}/>
  123. <Street position="13" boardPos="left" game={game}/>
  124. <Street position="14" boardPos="left" game={game}/>
  125. <TrainStation position="15" boardPos="left" game={game}/>
  126. <Street position="16" boardPos="left" game={game}/>
  127. <Community position="17" boardPos="left" game={game}/>
  128. <Street position="18" boardPos="left" game={game}/>
  129. <Street position="19" boardPos="left" game={game}/>
  130. <FreeParking boardPos="top"/>
  131. <Street position="21" boardPos="top" game={game}/>
  132. <Chance position="22" boardPos="top"/>
  133. <Street position="23" boardPos="top" game={game}/>
  134. <Street position="24" boardPos="top" game={game}/>
  135. <TrainStation position="25" boardPos="top" game={game}/>
  136. <Street position="26" boardPos="top" game={game}/>
  137. <Street position="27" boardPos="top" game={game}/>
  138. <Utility position="28" boardPos="top" game={game}/>
  139. <Street position="29" boardPos="top" game={game}/>
  140. <GoToJail/>
  141. <Street position="31" boardPos="right" game={game}/>
  142. <Street position="32" boardPos="right" game={game}/>
  143. <Community position="33" boardPos="right" game={game}/>
  144. <Street position="34" boardPos="right" game={game}/>
  145. <TrainStation position="35" boardPos="right" game={game}/>
  146. <Chance position="36" boardPos="right"/>
  147. <Street position="37" boardPos="right" game={game}/>
  148. <SuperTax position="38" boardPos="right"/>
  149. <Street position="39" boardPos="right" game={game}/>
  150. <div style={{gridArea: 'e'}}></div>
  151. </div>
  152. </div>)
  153. } else {
  154. return (<div>Waiting for game...</div>);
  155. }
  156. }
  157. }