diff --git a/dist/assets/index-CoFk0OHL.js b/dist/assets/index-CoFk0OHL.js new file mode 100644 index 0000000..ef9b7fb --- /dev/null +++ b/dist/assets/index-CoFk0OHL.js @@ -0,0 +1 @@ +(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const c of document.querySelectorAll('link[rel="modulepreload"]'))o(c);new MutationObserver(c=>{for(const r of c)if(r.type==="childList")for(const s of r.addedNodes)s.tagName==="LINK"&&s.rel==="modulepreload"&&o(s)}).observe(document,{childList:!0,subtree:!0});function n(c){const r={};return c.integrity&&(r.integrity=c.integrity),c.referrerPolicy&&(r.referrerPolicy=c.referrerPolicy),c.crossOrigin==="use-credentials"?r.credentials="include":c.crossOrigin==="anonymous"?r.credentials="omit":r.credentials="same-origin",r}function o(c){if(c.ep)return;c.ep=!0;const r=n(c);fetch(c.href,r)}})();let b=0,w=0;const L={I:{shape:[[1,1,1,1]],color:"#00f0f0"},L:{shape:[[1,0],[1,0],[1,1]],color:"#f0a000"},J:{shape:[[0,1],[0,1],[1,1]],color:"#0000f0"},T:{shape:[[1,1,1],[0,1,0]],color:"#a000f0"},O:{shape:[[1,1],[1,1]],color:"#f0f000"},S:{shape:[[0,1,1],[1,1,0]],color:"#00f000"},Z:{shape:[[1,1,0],[0,1,1]],color:"#f00000"},Single:{shape:[[1]],color:"#f0a0f0"},Plus:{shape:[[0,1,0],[1,1,1],[0,1,0]],color:"#a0a0a0"},U:{shape:[[1,0,1],[1,1,1]],color:"#f0f0a0"}};let C=0,l=null;function A(){const e=document.getElementById("game-grid"),t=5;for(let n=0;n{o.forEach(c=>{const r=document.createElement("div");if(r.className="piece-cell",c){r.style.backgroundImage='url("./bubble.png")',r.style.backgroundSize="cover";const s=S(n);r.style.filter=`hue-rotate(${s}deg)`}e.appendChild(r)})})}function S(e){const t=H(e),n=X(t.r,t.g,t.b);return Math.round(n[0]*360)}function H(e){e=e.replace(/^#/,"");const t=parseInt(e,16),n=t>>16&255,o=t>>8&255,c=t&255;return{r:n,g:o,b:c}}function X(e,t,n){e/=255,t/=255,n/=255;const o=Math.max(e,t,n),c=Math.min(e,t,n);let r,s,a=(o+c)/2;if(o===c)r=s=0;else{const i=o-c;switch(s=a>.5?i/(2-o-c):i/(o+c),o){case e:r=(t-n)/i+(te.map(c=>c[o]).reverse())}function I(){const e=document.getElementById("tetris-bank");e.innerHTML="";const t=5;for(let n=0;n{t.addEventListener("mousedown",P),t.addEventListener("touchstart",P,{passive:!1})}),document.addEventListener("mousemove",R),document.addEventListener("touchmove",R,{passive:!1}),document.addEventListener("mouseup",B),document.addEventListener("touchend",B)}function P(e){e.preventDefault();const t=e.target.closest(".tetris-piece");if(!t||t.classList.contains("unplaceable"))return;const n=JSON.parse(t.dataset.shape),o=t.getBoundingClientRect(),c=e.clientX||e.touches[0].clientX,r=e.clientY||e.touches[0].clientY,s=o.width/2,a=o.height/2,i=o.width/n[0].length,[f,d]=ne(n,s,a,i);l=document.createElement("div"),l.className="dragged-piece",l.style.position="fixed",l.style.pointerEvents="none",l.style.zIndex="1000",l.style.opacity="0.8";const m=document.querySelector(".grid-cell").offsetWidth;l.style.width=`${n[0].length*m}px`,l.style.height=`${n.length*m}px`;const u=document.createElement("div");u.style.display="grid",u.style.width="100%",u.style.height="100%",u.style.gridTemplateColumns=`repeat(${n[0].length}, 1fr)`,u.style.gridTemplateRows=`repeat(${n.length}, 1fr)`;const g=t.dataset.color;l.dataset.color=g,console.log("b",g),n.forEach((p,E)=>{p.forEach((T,oe)=>{const y=document.createElement("div");y.style.backgroundImage=T?'url("./bubble.png")':"transparent",y.style.backgroundSize="cover",console.log("a",g);const N=S(g);y.style.filter=`hue-rotate(${N}deg)`,u.appendChild(y)})}),l.appendChild(u),document.body.appendChild(l),O(c,r),l.dataset.shape=JSON.stringify(n),l.dataset.activeRow=f,l.dataset.activeCol=d,l.dataset.originalId=t.id,l.dataset.color=g,t.classList.add("dragging")}function R(e){if(e.preventDefault(),l){const t=e.clientX||e.touches[0].clientX,n=e.clientY||e.touches[0].clientY;b=t,w=n,document.querySelector(".grid-cell").offsetWidth,parseInt(l.dataset.activeRow||0),parseInt(l.dataset.activeCol||0),O(t,n),G(t,n)}}function B(e){if(e.preventDefault(),!l)return;const t=e.clientX||b,n=e.clientY||w,o=document.querySelector(".grid-cell").offsetWidth,c=l.dataset.originalId,r=JSON.parse(l.dataset.shape);b=0,w=0;const s=5,i=document.getElementById("game-grid-container").getBoundingClientRect();if(ti.right||ni.bottom){M(c),k(),v();return}const f=l.offsetWidth,d=l.offsetHeight,m=Math.floor((t-i.left-5-f/2)/o),g=Math.floor((n-i.top-5-d/2)/o)*s+m;if(x(r,g,s)){const p=l.dataset.color;F(r,g,s,p),J(document.getElementById(c))}else M(c);k(),v()}function O(e,t,n,o,c){if(l){const r=l.offsetWidth,s=l.offsetHeight,a=r/2,i=s/2;l.style.left=`${e-a}px`,l.style.top=`${t-i}px`}}function G(e,t){if(!l)return;const n=JSON.parse(l.dataset.shape),o=5,r=document.getElementById("game-grid-container").getBoundingClientRect(),s=(r.width-10)/o,a=l.offsetWidth,i=l.offsetHeight,f=Math.floor((e-r.left-5-a/2)/s),d=Math.floor((t-r.top-5-i/2)/s),m=d*o+f;if(v(),x(n,m,o)){for(let u=0;u{e.classList.remove("preview")})}function x(e,t,n){const o=Math.floor(t/n),c=t%n;for(let r=0;r=n||i>=n||a<0||i<0)return!1;const f=a*n+i,d=document.querySelector(`.grid-cell[data-index="${f}"]`);if(!d||d.style.backgroundImage!=="")return!1}return!0}function F(e,t,n,o){const c=Math.floor(t/n),r=t%n;for(let s=0;sz(o,e)),n.forEach(o=>Z(o,e)),Q(t.length,n.length),h()}function U(e,t){for(let n=0;n0&&t>0?n=10:n=e+t,C+=n;const o=document.getElementById("score");o?o.textContent=`Score: ${C}`:console.error("Score element not found")}function V(){document.querySelectorAll(".grid-cell").forEach(c=>{c.style.backgroundImage="",c.style.filter="",c.classList.remove("preview")}),C=0;const t=document.getElementById("score");t&&(t.textContent="Score: 0");const n=document.getElementById("tetris-bank");n.innerHTML="",I(),l&&(l.remove(),l=null),document.querySelectorAll(".tetris-piece").forEach(c=>{c.style.opacity="1"}),document.draggedPiece&&(document.draggedPiece=null),I(),h()}function _(){A(),I(),W();const e=document.createElement("div");e.id="score",e.textContent="Score: 0",document.getElementById("app").insertBefore(e,document.getElementById("game-container")),document.getElementById("reset-button").addEventListener("click",n=>{n.preventDefault(),V(),n.dataTransfer&&(n.dataTransfer.dropEffect="none",n.dataTransfer.effectAllowed="none")})}function ee(e){k(),v()}function k(){l&&(l.remove(),l=null),document.querySelectorAll(".tetris-piece").forEach(t=>{t.classList.remove("dragging")})}function te(e){for(let n=0;n<5;n++)for(let o=0;o<5;o++){const c=n*5+o;if(x(e,c,5))return!0}return!1}function h(){document.querySelectorAll(".tetris-piece").forEach(t=>{const n=JSON.parse(t.dataset.shape),o=te(n);t.classList.toggle("unplaceable",!o),t.draggable=o})}function M(e){const t=document.getElementById(e);t&&t.classList.remove("dragging")}function ne(e,t,n,o){const c=Math.floor(n/o),r=Math.floor(t/o);if(e[c]&&e[c][r])return[c,r];const s=e.length,a=e[0].length;let i=1/0,f=[0,0];for(let d=0;d + + + + + 5x5 Grid Game with Tetris Pieces + + + + +
+
+
+
+
+
+
+ +
+ + diff --git a/dist/vite.svg b/dist/vite.svg new file mode 100644 index 0000000..e7b8dfb --- /dev/null +++ b/dist/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/main.js b/main.js index 43da637..b3fad63 100644 --- a/main.js +++ b/main.js @@ -134,7 +134,7 @@ function updatePieceGrid(grid, shape, color) { cellElement.className = 'piece-cell'; //cellElement.style.backgroundColor = cell ? color : 'transparent'; if (cell) { - cellElement.style.backgroundImage = 'url("./public/bubble.png")'; + cellElement.style.backgroundImage = 'url("./bubble.png")'; cellElement.style.backgroundSize = 'cover'; const hueRotation = getHueRotationFromColor(color); cellElement.style.filter = `hue-rotate(${hueRotation}deg)`; @@ -300,7 +300,7 @@ function dragStart(e) { shape.forEach((row, rowIndex) => { row.forEach((cell, colIndex) => { const cellElement = document.createElement('div'); - cellElement.style.backgroundImage = cell ? 'url("./public/bubble.png")' : 'transparent'; + cellElement.style.backgroundImage = cell ? 'url("./bubble.png")' : 'transparent'; cellElement.style.backgroundSize = 'cover'; console.log('a', color); const hueRotation = getHueRotationFromColor(color); @@ -470,7 +470,7 @@ function placePiece(pieceShape, targetIndex, gridSize, color) { const cellIndex = (targetRow + row) * gridSize + (targetCol + col); const cell = document.querySelector(`.grid-cell[data-index="${cellIndex}"]`); if (cell) { - cell.style.backgroundImage = 'url("./public/bubble.png")'; + cell.style.backgroundImage = 'url("./bubble.png")'; cell.style.backgroundSize = 'cover'; const hueRotation = getHueRotationFromColor(color); cell.style.filter = `hue-rotate(${hueRotation}deg)`;