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.

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