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.

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