-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
202 lines (172 loc) · 5.5 KB
/
game.js
File metadata and controls
202 lines (172 loc) · 5.5 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
const canvas = document.getElementById('mainCanvas');
const ctx = canvas.getContext('2d');
const overlay = document.getElementById('game-overlay');
const instructions = document.getElementById('game-instructions');
let currentGame = 'defuse'; // 'defuse' or 'aim'
let gameState = 'waiting'; // 'waiting', 'playing', 'won', 'lost'
let score = 0;
let timer = 0;
// Game Logic Variables
let explosionTimer = 0;
let targetCode = "";
let currentCode = "";
let wires = [];
let bots = [];
let lastSpawn = 0;
// Aesthetic Config
const csGreen = "#00FF00";
const csDarkGreen = "#004400";
const csGold = "#FFD700";
// --- CORE UTILS ---
function switchGame(type) {
currentGame = type;
gameState = 'waiting';
overlay.style.display = 'flex';
overlay.innerText = `START ${type.toUpperCase()}`;
// Update instructions and button UI
document.querySelectorAll('.game-selector button').forEach(b => b.classList.remove('active'));
if (type === 'defuse') {
instructions.innerText = "DEFUSE: Input the correct 4-digit code before the timer hits zero!";
document.querySelector('button[onclick*="defuse"]').classList.add('active');
} else {
instructions.innerText = "AIM_BOTZ: Click the 'bots' as they appear. Don't miss!";
document.querySelector('button[onclick*="aim"]').classList.add('active');
}
resetGame();
}
function resetGame() {
score = 0;
if (currentGame === 'defuse') {
explosionTimer = 400; // frames
targetCode = Math.floor(1000 + Math.random() * 9000).toString();
currentCode = "";
} else {
bots = [];
timer = 600; // 10 seconds approx
}
}
overlay.addEventListener('click', () => {
gameState = 'playing';
overlay.style.display = 'none';
resetGame();
if (gameLoopId === null) loop();
});
// --- DEFUSE LOGIC ---
function updateDefuse() {
explosionTimer--;
if (explosionTimer <= 0) {
gameState = 'lost';
overlay.innerText = "TERRORISTS WIN (BOOM)";
overlay.style.display = 'flex';
}
// Win check
if (currentCode === targetCode) {
gameState = 'won';
overlay.innerText = "COUNTER-TERRORISTS WIN (DEFUSED)";
overlay.style.display = 'flex';
}
}
function drawDefuse() {
// C4 Appearance
ctx.fillStyle = "#333";
ctx.fillRect(150, 50, 300, 200);
ctx.strokeStyle = "#444";
ctx.strokeRect(150, 50, 300, 200);
// Timer Display
ctx.fillStyle = "red";
ctx.font = "30px 'Press Start 2P'";
ctx.textAlign = "center";
let displayTime = (explosionTimer / 60).toFixed(2);
ctx.fillText(displayTime, 300, 110);
// Code Display
ctx.fillStyle = csGreen;
ctx.font = "20px Courier Prime";
ctx.fillText("ENTER CODE:", 300, 160);
ctx.fillStyle = "white";
ctx.font = "40px Courier Prime";
ctx.fillText(currentCode.padEnd(4, "*"), 300, 210);
ctx.font = "12px Press Start 2P";
ctx.fillStyle = csGold;
ctx.fillText("[PRESS 0-9 KEYS TO DEFUSE]", 300, 240);
}
// --- AIM_BOTZ LOGIC ---
function updateAim() {
timer--;
if (Date.now() - lastSpawn > 800) {
bots.push({
x: Math.random() * (canvas.width - 40) + 20,
y: Math.random() * (canvas.height - 40) + 20,
size: 20,
life: 60
});
lastSpawn = Date.now();
}
for (let i = bots.length - 1; i >= 0; i--) {
bots[i].life--;
if (bots[i].life <= 0) bots.splice(i, 1);
}
if (timer <= 0) {
gameState = 'won';
overlay.innerText = `ROUND OVER. KILLS: ${score}`;
overlay.style.display = 'flex';
}
}
function drawAim() {
ctx.strokeStyle = csGreen;
ctx.lineWidth = 1;
for (let b of bots) {
ctx.beginPath();
ctx.arc(b.x, b.y, b.size, 0, Math.PI * 2);
ctx.stroke();
// Crosshair inside bot
ctx.moveTo(b.x - 5, b.y); ctx.lineTo(b.x + 5, b.y);
ctx.moveTo(b.x, b.y - 5); ctx.lineTo(b.x, b.y + 5);
ctx.stroke();
}
ctx.fillStyle = csWhite;
ctx.font = "16px Courier Prime";
ctx.textAlign = "left";
ctx.fillText(`KILLS: ${score}`, 10, 20);
ctx.fillText(`TIME: ${(timer / 60).toFixed(1)}s`, 10, 40);
}
// --- MAIN LOOP ---
window.addEventListener('keydown', (e) => {
if (gameState !== 'playing') return;
if (currentGame === 'defuse') {
if (e.key >= '0' && e.key <= '9' && currentCode.length < 4) {
currentCode += e.key;
}
}
});
canvas.addEventListener('mousedown', (e) => {
if (gameState !== 'playing' || currentGame !== 'aim') return;
let rect = canvas.getBoundingClientRect();
let mx = e.clientX - rect.left;
let my = e.clientY - rect.top;
for (let i = bots.length - 1; i >= 0; i--) {
let b = bots[i];
let dist = Math.sqrt((mx - b.x) ** 2 + (my - b.y) ** 2);
if (dist < b.size) {
bots.splice(i, 1);
score++;
break;
}
}
});
let gameLoopId = null;
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (gameState === 'playing') {
if (currentGame === 'defuse') {
updateDefuse();
drawDefuse();
} else {
updateAim();
drawAim();
}
}
gameLoopId = requestAnimationFrame(loop);
}
// Global aesthetic fallback
const csWhite = "#FFF";
switchGame('defuse');