You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
10 KiB

  1. import {CARD_COLORS} from "./uno.js";
  2. export default class Style {
  3. constructor(gameInstanz) {
  4. this._game = gameInstanz;
  5. this._cardOnDeck = null;
  6. this._drawCardRunnig = false;
  7. this._firstDraw = $('#first-draw');
  8. this._firstPut = $('#first-put');
  9. this._firstPutAnim = $('#first-put-anim');
  10. this._unohover = false;
  11. this._unoClicked = false;
  12. $('#sayUno').on('mouseenter', () => {
  13. if (this._unoClicked) return;
  14. this.unoHover = true;
  15. $('.uno').css('width', '28vw');
  16. $('#sayUno-glow').css('opacity', 1);
  17. })
  18. $('#sayUno').on('mouseleave', () => {
  19. if (this._unoClicked) return;
  20. this.unoHover = false;
  21. $('.uno').css('width', '25vw');
  22. $('#sayUno-glow').css('opacity', 0.3);
  23. this.startUnoLoop();
  24. })
  25. $('#sayUno').on('click', () => {
  26. this.unoHover = true;
  27. this._unoClicked = true;
  28. $('.uno').css('transition', 'width ease-out 0.2s, filter 0.1s');
  29. $('.uno').css('width', '22vw');
  30. $('.uno').css('filter', 'grayscale(80%)');
  31. $('#sayUno-glow').css('opacity', 1);
  32. this.game.currentPlayerInstanz.sayUno();
  33. this.game.currentPlayerInstanz.mustSayUno = false;
  34. })
  35. $('#drawCard').on('click', () => {
  36. if(this.game.currentPlayerInstanz.mustSayUno){
  37. this.game.currentPlayerInstanz.mustSayUno = false;
  38. }
  39. this.game.currentPlayerInstanz.drawCard(1, true, true);
  40. this.refreshDebug();
  41. });
  42. this.firstDraw.on('mouseenter', () => {
  43. this.drawCardAnim(true, false);
  44. });
  45. this.firstDraw.on('mouseleave', () => {
  46. this.drawCardAnim(false, false);
  47. });
  48. this.firstDraw.on('click', () => {
  49. if(this.game.currentPlayerInstanz.mustSayUno){
  50. this.game.currentPlayerInstanz.mustSayUno = false;
  51. this.game.style.hideUnoButton();
  52. }
  53. let playerInstanz = this.game.currentPlayerInstanz;
  54. this.drawCardAnim(false, true);
  55. playerInstanz.drawCard(1, true, true);
  56. });
  57. }
  58. refreshHtml() {
  59. this.refreshCardOnDeck();
  60. this.showPlayerDeck(this.game.currentPlayerInstanz, true, false, true)
  61. this.refreshDebug();
  62. }
  63. refreshCardOnDeck() {
  64. if (this._cardOnDeck !== this.game.cardOnDeck) {
  65. this.putCardAnim();
  66. this._cardOnDeck = this.game.cardOnDeck;
  67. } else {
  68. this.firstPut.attr('src', './img/stackCards/' + this.game.cardOnDeck.color + '/' + this.game.cardOnDeck.name + '.png');
  69. this.firstPutAnim.attr('src', './img/stackCards/' + this.game.cardOnDeck.color + '/' + this.game.cardOnDeck.name + '.png');
  70. }
  71. }
  72. refreshDebug() {
  73. $('#drawCard').css('background-color', 'white');
  74. $("#player").html("Aktueller Spieler: " + this.game.currentPlayerInstanz.name + " | " + this.game.currentPlayerInstanz.hand.length);
  75. $("#playerCards").html("Weitere Spieler: <br>");
  76. for (let i = 0; i < this.game.players.length; i++) {
  77. if(this.game.players[i] === this.game.currentPlayerInstanz) continue;
  78. $('#playerCards').append("" + this.game.players[i].name + " | " + this.game.players[i].hand.length + "<br>");
  79. }
  80. $("#playerInGame").html("Spieler im Spiel: " + this.game.players.length);
  81. }
  82. showUnoButton(){
  83. $('.uno').css('transition', 'width ease-in-out 0.5s');
  84. $('#sayUno-glow').css('transition', 'width ease-in-out 0.5s, opacity 0.5s');
  85. $('.uno').css('filter', 'grayscale(0%)');
  86. $('#sayUno').css('opacity', 1);
  87. this.unoHover = false;
  88. this._unoClicked = false;
  89. this.startUnoLoop();
  90. }
  91. hideUnoButton(){
  92. $('#sayUno').css('opacity', 0);
  93. }
  94. startUnoLoop() {
  95. if (!this.unoHover && !this._unoClicked) setTimeout(() => {
  96. $('.uno').css('width', '28vw');
  97. $('#sayUno-glow').css('opacity', 0.8);
  98. setTimeout(() => {
  99. if (!this.unoHover && !this._unoClicked) {
  100. $('.uno').css('width', '25vw');
  101. $('#sayUno-glow').css('opacity', 0.3);
  102. this.startUnoLoop();
  103. }
  104. }, 500);
  105. }, 500);
  106. }
  107. stopUnoLoop(){
  108. this._unoClicked = true;
  109. this.unoHover = true;
  110. }
  111. putCardAnim() {
  112. this.firstPutAnim.attr('src', './img/stackCards/' + this.game.cardOnDeck.color + '/' + this.game.cardOnDeck.name + '.png');
  113. this.firstPutAnim.css('transition', 'top 0.5s, opacity 0.3s');
  114. this.firstPutAnim.css('top', '47vh');
  115. this.firstPutAnim.css('opacity', '1');
  116. setTimeout(() => {
  117. this.firstPutAnim.css('transition', '');
  118. this.firstPutAnim.css('top', '30vh');
  119. this.firstPutAnim.css('opacity', '0');
  120. this.firstPut.attr('src', './img/stackCards/' + this.game.cardOnDeck.color + '/' + this.game.cardOnDeck.name + '.png');
  121. this.firstPut.css('opacity', '1');
  122. }, 500);
  123. }
  124. showSelectColor(card){
  125. $('.selectColor').css('opacity', 1);
  126. $('#cc-green').on('click', ()=>{
  127. card.color = CARD_COLORS[2];
  128. card.putSelf();
  129. this.hideSelectColor();
  130. })
  131. $('#cc-blue').on('click', ()=>{
  132. card.color = CARD_COLORS[1];
  133. card.putSelf();
  134. this.hideSelectColor();
  135. })
  136. $('#cc-red').on('click', ()=>{
  137. card.color = CARD_COLORS[3];
  138. card.putSelf();
  139. this.hideSelectColor();
  140. })
  141. $('#cc-yellow').on('click', ()=>{
  142. card.color = CARD_COLORS[4];
  143. card.putSelf();
  144. this.hideSelectColor();
  145. })
  146. }
  147. hideSelectColor(){
  148. $('.selectColor').css('opacity', 0);
  149. }
  150. showPlayerDeck(player, click, lastDraw, anim) {
  151. $('#playerDeck').html("");
  152. let currentPlayerHand = player.hand;
  153. let width = window.innerWidth;
  154. let height = window.innerHeight;
  155. let percent = 0;
  156. let top = window.innerHeight * 0.68;
  157. let cardAmount = currentPlayerHand.length;
  158. for (let i = 0; i < cardAmount; i++) {
  159. currentPlayerHand.xPos = 0;
  160. currentPlayerHand.yPos = 0;
  161. $('#playerDeck').append('<div id="card' + i + '" class="pictureCard">' + '<img src="./img/cards/' + currentPlayerHand[i].color + '/' + currentPlayerHand[i].name + '.png' + '"></div>');
  162. currentPlayerHand[i].xPos = (width * (((43 + cardAmount / 2 * 5) - percent)) / 100);
  163. currentPlayerHand[i].yPos = top;
  164. let card = $('#card' + i);
  165. let put = false;
  166. if (click) {
  167. let yPos = currentPlayerHand[i].yPos;
  168. card.on('mouseenter', () => {
  169. if (!put && !anim) card.css('top', yPos - 60 + 'px');
  170. })
  171. card.on('mouseleave', () => {
  172. if (!put && !anim) card.css('top', yPos);
  173. })
  174. card.on('click', () => {
  175. if (!this.game.players[this.game.currentPlayer].hand[i].canPut || anim) return;
  176. put = true
  177. let lastPlayer = player;
  178. player.putCard(i);
  179. card.css('top', yPos - height * 0.2);
  180. card.css('opacity', 0);
  181. setTimeout(() => {
  182. this.refreshCardOnDeck();
  183. }, 400);
  184. })
  185. }
  186. if (anim) {
  187. card.css('top', height);
  188. setTimeout(() => {
  189. card.css('top', top + 'px')
  190. card.css('left', (width * 45 / 100));
  191. setTimeout(() => {
  192. card.css('left', currentPlayerHand[i].xPos + 'px');
  193. card.css('top', top + 'px')
  194. anim = false;
  195. }, 250);
  196. }, 100);
  197. } else {
  198. card.css('left', currentPlayerHand[i].xPos + 'px');
  199. card.css('top', top + 'px')
  200. }
  201. if (lastDraw && i === cardAmount - 1) {
  202. card.css('opacity', 0);
  203. card.css('top', currentPlayerHand[i].yPos - 100);
  204. setTimeout(() => {
  205. card.css('left', currentPlayerHand[i].xPos + 'px');
  206. card.css('top', top + 'px')
  207. card.css('opacity', 1);
  208. }, 50);
  209. }
  210. percent += 5;
  211. }
  212. }
  213. drawCardAnim(enable, playerDraw) {
  214. if (this.drawCardRunning) return;
  215. this.drawCardRunning = true;
  216. if (playerDraw) {
  217. this.firstDraw.css('transition', 'min-width 0.5s, top 0.5s, opacity 0.5s');
  218. this.firstDraw.css('min-width', '35vh');
  219. this.firstDraw.css('top', '60vh');
  220. this.firstDraw.css('opacity', 0);
  221. setTimeout(() => {
  222. this.firstDraw.css('transition', '');
  223. this.firstDraw.css('top', '47vh');
  224. this.firstDraw.css('min-width', '20vh');
  225. this.firstDraw.css('opacity', '1');
  226. this.drawCardRunning = false;
  227. }, 500);
  228. } else {
  229. if (enable) {
  230. this.firstDraw.css('transition', 'min-width 0.35s, top 0.5s');
  231. this.firstDraw.css('min-width', '27vh');
  232. this.firstDraw.css('top', '50vh');
  233. } else {
  234. this.firstDraw.css('transition', 'min-width 1s, top 0.5s');
  235. this.firstDraw.css('top', '47vh');
  236. this.firstDraw.css('min-width', '20vh');
  237. }
  238. this.drawCardRunning = false;
  239. }
  240. }
  241. showDebug() {
  242. $('#debug').show();
  243. this.refreshDebug();
  244. }
  245. hideDebug() {
  246. $('#debug').hide();
  247. }
  248. get game() {
  249. return this._game;
  250. }
  251. get firstDraw() {
  252. return this._firstDraw;
  253. }
  254. get firstPut() {
  255. return this._firstPut;
  256. }
  257. get firstPutAnim() {
  258. return this._firstPutAnim;
  259. }
  260. get drawCardRunning() {
  261. return this._drawCardRunnig;
  262. }
  263. set drawCardRunning(bool) {
  264. this._drawCardRunnig = bool;
  265. }
  266. get unoHover() {
  267. return this._unohover;
  268. }
  269. set unoHover(bool) {
  270. this._unohover = bool;
  271. }
  272. }