-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterlock.html
More file actions
235 lines (206 loc) · 11.6 KB
/
Copy pathinterlock.html
File metadata and controls
235 lines (206 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Interlock</title>
<script src="https://kit.fontawesome.com/180b94fa4b.js" crossorigin="anonymous"></script>
<style>
:root { --bg-color: #2c3e50; --board-bg: #34495e; --hero-color: #e74c3c; --accent: #ecf0f1; }
body { font-family: 'Segoe UI', sans-serif; background-color: var(--bg-color); color: var(--accent); margin: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; overflow: hidden; touch-action: none; }
.stats { margin-bottom: 20px; font-size: 1.2rem; display: flex; gap: 20px; text-transform: uppercase; letter-spacing: 1px; }
.game-container { position: relative; width: 90vw; max-width: 500px; aspect-ratio: 1; background-color: var(--board-bg); border-radius: 12px; padding: 10px; border: 4px solid #465f75; padding-right: 35px; box-shadow: 0 10px 30px rgba(0,0,0,0.5); }
.board { position: relative; width: 100%; height: 100%; background-image: linear-gradient(#2c3e50 2px, transparent 2px), linear-gradient(90deg, #2c3e50 2px, transparent 2px); background-size: 16.66% 16.66%; background-position: -1px -1px; border-radius: 8px; overflow: hidden; }
.exit-gate { position: absolute; right: -30px; top: 33.33%; height: 16.66%; width: 30px; background: repeating-linear-gradient(45deg, #27ae60, #27ae60 5px, #2ecc71 5px, #2ecc71 10px); border-radius: 0 6px 6px 0; box-shadow: 0 0 10px #2ecc71; }
.block { position: absolute; border-radius: 6px; box-sizing: border-box; transition: transform 0.1s linear; z-index: 10; cursor: grab; border: 1px solid rgba(255,255,255,0.2); box-shadow: 0 4px 6px rgba(0,0,0,0.3); display: flex; justify-content: center; align-items: center; touch-action: none; }
.block.hero { background: var(--hero-color); }
.block.hero::after { content: '➔'; font-size: 1.5rem; color: #fff; animation: pulse 2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 0.5; } 50% { opacity: 1; } }
.block.c1 { background: #3498db; } .block.c2 { background: #f1c40f; } .block.c3 { background: #9b59b6; } .block.c4 { background: #1abc9c; }
.block.selected { outline: 3px solid #fff; outline-offset: 2px; z-index: 11; }
.controls { margin-top: 25px; display: flex; gap: 10px; }
button { background: #34495e; color: #fff; border: 2px solid #576d7e; padding: 10px 20px; border-radius: 20px; cursor: pointer; font-weight: bold; transition: 0.2s; }
button:hover { background: #465f75; transform: translateY(-2px); }
.modal { position: absolute; top:0; left:0; width:100%; height:100%; background: rgba(0,0,0,0.85); display: none; flex-direction: column; align-items: center; justify-content: center; z-index: 100; border-radius: 12px; }
</style>
</head>
<body>
<h1>INTERL<i class="fa-solid fa-lock"></i>CK</h1>
<div class="stats"><span>Level: <span id="lvl">1</span></span><span>Moves: <span id="mvs">0</span></span></div>
<div class="game-container">
<div class="exit-gate"></div>
<div class="board" id="board"></div>
<div class="modal" id="win"><h2>ESCAPED!</h2><button onclick="nextLvl()">Next Level</button></div>
</div>
<div class="controls">
<button onclick="resetLvl()">Reset</button>
<button onclick="importLvl()" style="border-color: #2ecc71;">Play Custom Level</button>
</div>
<script>
// Levels are loaded from interlock-levels.txt (one level's JSON array
// per line) rather than embedded here, so this file stays small and the
// level list can be edited without touching the game code.
let levels = [];
async function loadLevelsFromFile() {
try {
const res = await fetch('interlock-levels.txt');
if (!res.ok) throw new Error('HTTP ' + res.status);
const text = await res.text();
levels = text.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#'))
.map(line => JSON.parse(line));
if (!levels.length) throw new Error('No levels found in file.');
} catch (err) {
console.error('Failed to load interlock-levels.txt:', err);
alert('Could not load interlock-levels.txt: ' + err.message +
'\n\nMake sure interlock-levels.txt is in the same folder and this page is loaded via http(s), not opened directly as a file.');
levels = [];
}
}
let curIdx = 0, blocks = [], moves = 0, cellSize = 0, locked = false;
// Mouse/pointer drag state
let drag = false, active = null, start = {x:0, y:0}, acc = 0, sessionMoved = false;
// Keyboard state
let selected = null; // currently selected block (object reference from `blocks`)
let kbActiveKey = null; // the arrow key currently "held" for the in-progress keyboard session
let kbSessionMoved = false;
const board = document.getElementById('board'), lvlDisp = document.getElementById('lvl'), mvsDisp = document.getElementById('mvs'), win = document.getElementById('win');
function loadLvl(idx) {
if (!levels[idx]) return;
curIdx = idx; blocks = JSON.parse(JSON.stringify(levels[idx])); moves = 0;
lvlDisp.innerText = idx + 1; mvsDisp.innerText = 0; win.style.display = 'none';
locked = false; drag = false; active = null; sessionMoved = false;
kbActiveKey = null; kbSessionMoved = false;
selected = blocks.find(b => b.id === 'hero') || blocks[0] || null;
render();
}
function render() {
board.innerHTML = '';
cellSize = board.clientWidth / 6;
blocks.forEach(b => {
const el = document.createElement('div'); el.className = `block ${b.color}` + (selected && b.id === selected.id ? ' selected' : ''); el.id = b.id;
el.style.width = (b.type==='h' ? b.len*16.66 : 16.66)+'%';
el.style.height = (b.type==='v' ? b.len*16.66 : 16.66)+'%';
el.style.left = (b.c*16.66)+'%'; el.style.top = (b.r*16.66)+'%';
el.onpointerdown = (e) => onBlockPointerDown(e, b, el);
board.appendChild(el);
});
}
function updateSelectedVisual() {
document.querySelectorAll('.block.selected').forEach(el => el.classList.remove('selected'));
if (selected) {
const el = document.getElementById(selected.id);
if (el) el.classList.add('selected');
}
}
// ---------- Mouse / pointer dragging ----------
function onBlockPointerDown(e, b, el) {
if (locked) return;
// Switching blocks mid-interaction should commit any pending keyboard session first.
commitKeyboardSession();
selected = b; updateSelectedVisual();
drag = true; active = b; start = {x:e.clientX, y:e.clientY}; acc = 0; sessionMoved = false;
el.setPointerCapture(e.pointerId);
}
window.onpointermove = (e) => {
if (locked || !drag || !active) return;
let delta = active.type === 'h' ? e.clientX - start.x : e.clientY - start.y;
acc += delta; start = {x:e.clientX, y:e.clientY};
if (Math.abs(acc) >= cellSize) {
let dir = Math.sign(acc);
if (move(active, dir)) { sessionMoved = true; acc -= dir * cellSize; check(); }
else { acc = dir * cellSize * 0.8; }
}
};
function endDrag() {
if (drag && sessionMoved) { moves++; mvsDisp.innerText = moves; }
drag = false; active = null; sessionMoved = false;
}
window.onpointerup = endDrag;
window.onpointercancel = endDrag;
// ---------- Shared move logic ----------
function move(b, d) {
let nr = b.r + (b.type==='v'?d:0), nc = b.c + (b.type==='h'?d:0);
if(nr<0 || nc<0 || (b.type==='h' && nc+b.len>6) || (b.type==='v' && nr+b.len>6)) return false;
const collision = blocks.some(o => {
if(o.id===b.id) return false;
for(let i=0;i<b.len;i++) for(let j=0;j<o.len;j++) {
let br = b.type==='v'?nr+i:nr, bc = b.type==='h'?nc+i:nc;
let or = o.type==='v'?o.r+j:o.r, oc = o.type==='h'?o.c+j:o.c;
if(br===or && bc===oc) return true;
} return false;
});
if(collision) return false;
b.r = nr; b.c = nc;
let el = document.getElementById(b.id); el.style.left = (b.c*16.66)+'%'; el.style.top = (b.r*16.66)+'%';
return true;
}
function check() {
if (blocks.find(b=>b.id==='hero').c === 4) {
win.style.display = 'flex';
locked = true;
drag = false; active = null; sessionMoved = false;
kbActiveKey = null; kbSessionMoved = false;
}
}
// ---------- Keyboard controls ----------
// Arrow keys matching the selected block's orientation move it, one cell
// per keydown (holding the key auto-repeats, same feel as dragging).
// The move is only committed to the counter when you:
// - press Enter, or
// - press the opposite direction (which commits, then starts a new session)
// Arrow keys on the other axis do nothing for the current block; switch
// blocks with a mouse click instead (which also commits any pending move).
function commitKeyboardSession() {
if (kbSessionMoved) { moves++; mvsDisp.innerText = moves; }
kbActiveKey = null; kbSessionMoved = false;
}
window.addEventListener('keydown', (e) => {
if (locked) return;
if (e.key === 'Enter') {
e.preventDefault();
commitKeyboardSession();
return;
}
const arrowKeys = ['ArrowLeft','ArrowRight','ArrowUp','ArrowDown'];
if (!arrowKeys.includes(e.key)) return;
e.preventDefault();
if (!selected) return;
const isHorizontalKey = (e.key === 'ArrowLeft' || e.key === 'ArrowRight');
const matchesOrientation = (selected.type === 'h' && isHorizontalKey) || (selected.type === 'v' && !isHorizontalKey);
if (!matchesOrientation) return; // wrong axis for this block, ignore
const dir = (e.key === 'ArrowRight' || e.key === 'ArrowDown') ? 1 : -1;
if (kbActiveKey && kbActiveKey !== e.key) {
// Direction reversed mid-session: commit the previous session as
// one move, then start a new one in the new direction.
commitKeyboardSession();
}
kbActiveKey = e.key;
if (move(selected, dir)) { kbSessionMoved = true; check(); }
});
function resetLvl() { loadLvl(curIdx); }
function nextLvl() { if(curIdx < levels.length-1) loadLvl(curIdx+1); else alert("Winner! More levels coming soon."); }
function importLvl() {
let snippet = prompt("Paste your level code:");
if(!snippet) return;
try {
let parsed = JSON.parse(snippet);
blocks = parsed;
moves = 0;
mvsDisp.innerText = 0;
win.style.display = 'none';
locked = false; drag = false; active = null; sessionMoved = false;
kbActiveKey = null; kbSessionMoved = false;
selected = blocks.find(b => b.id === 'hero') || blocks[0] || null;
render();
} catch(e) { alert("Invalid Code"); }
}
window.onresize = render;
(async () => {
await loadLevelsFromFile();
if (levels.length) loadLvl(0);
})();
</script>
</body>
</html>