Skip to content

Commit bccf526

Browse files
authored
Merge pull request #1 from Kotmin/reScreen
feat: updated responsiveness border limit
2 parents ddfc0dd + fa0306a commit bccf526

7 files changed

Lines changed: 197 additions & 42 deletions

File tree

ViInvaders.ino

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@ void notifyClients() {
3434
ws.textAll(msg);
3535
}
3636

37+
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
38+
AwsEventType type, void *arg, uint8_t *data, size_t len) {
39+
switch (type) {
40+
case WS_EVT_CONNECT:
41+
Serial.printf("[WS] Client %u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
42+
break;
43+
case WS_EVT_DISCONNECT:
44+
Serial.printf("[WS] Client %u disconnected\n", client->id());
45+
break;
46+
case WS_EVT_ERROR:
47+
Serial.printf("[WS] Error on client %u\n", client->id());
48+
break;
49+
case WS_EVT_DATA:
50+
// Optional: handle incoming client messages here
51+
break;
52+
default:
53+
break;
54+
}
55+
}
56+
3757

3858
void setup() {
3959
Serial.begin(115200);
@@ -57,10 +77,12 @@ void setup() {
5777
request->send(SPIFFS, "/index.html", "text/html");
5878
});
5979

60-
ws.onEvent([](AsyncWebSocket *server, AsyncWebSocketClient *client,
61-
AwsEventType type, void *arg, uint8_t *data, size_t len) {
62-
// optional: handle messages from clients
63-
});
80+
// ws.onEvent([](AsyncWebSocket *server, AsyncWebSocketClient *client,
81+
// AwsEventType type, void *arg, uint8_t *data, size_t len) {
82+
// // optional: handle messages from clients
83+
// });
84+
85+
ws.onEvent(onWsEvent);
6486
server.addHandler(&ws);
6587

6688
server.begin();
@@ -72,4 +94,5 @@ void loop() {
7294
notifyClients();
7395
lastSend = millis();
7496
}
97+
ws.cleanupClients(); // rm unactive clients
7598
}

data/game.js

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,67 @@
1-
// game.js z aktywacją gracza ruchem i wstrzymaniem wrogów przed aktywacją
2-
31
const canvas = document.getElementById("gameCanvas");
42
const ctx = canvas.getContext("2d");
5-
canvas.width = 800;
6-
canvas.height = 600;
73

4+
let player1 = null;
5+
let player2 = null;
6+
7+
function resizeCanvas() {
8+
const ratio = 4 / 3;
9+
const w = window.innerWidth;
10+
const h = window.innerHeight;
11+
if (w / h > ratio) {
12+
canvas.height = h;
13+
canvas.width = h * ratio;
14+
} else {
15+
canvas.width = w;
16+
canvas.height = w / ratio;
17+
}
18+
if (player1) {
19+
player1.y = canvas.height - 50;
20+
player1.x = canvas.width / 2 - 60;
21+
}
22+
if (player2) {
23+
player2.y = canvas.height - 50;
24+
player2.x = canvas.width / 2 + 60;
25+
}
26+
}
27+
window.addEventListener('resize', resizeCanvas);
28+
29+
let wakeLock = null;
30+
async function requestWakeLock() {
31+
try {
32+
if ('wakeLock' in navigator) {
33+
wakeLock = await navigator.wakeLock.request('screen');
34+
}
35+
} catch (err) {
36+
console.warn("WakeLock error:", err);
37+
}
38+
}
39+
document.addEventListener("visibilitychange", () => {
40+
if (wakeLock !== null && document.visibilityState === "visible") {
41+
requestWakeLock();
42+
}
43+
});
44+
requestWakeLock();
845

946
const pauseBtn = document.createElement("button");
1047
pauseBtn.innerText = "⏸️ Pauza";
11-
pauseBtn.style.position = "absolute";
48+
pauseBtn.className = "game-button";
49+
pauseBtn.style.right = "120px";
1250
pauseBtn.style.top = "10px";
13-
pauseBtn.style.left = "10px";
14-
pauseBtn.style.zIndex = 10;
1551
pauseBtn.onclick = () => paused = !paused;
1652
document.body.appendChild(pauseBtn);
1753

1854
const restartBtn = document.createElement("button");
1955
restartBtn.innerText = "🔄 Restart";
20-
restartBtn.style.position = "absolute";
56+
restartBtn.className = "game-button";
57+
restartBtn.style.right = "10px";
2158
restartBtn.style.top = "10px";
22-
restartBtn.style.left = "100px";
23-
restartBtn.style.zIndex = 10;
2459
restartBtn.onclick = () => newGame();
2560
document.body.appendChild(restartBtn);
2661

62+
// resizeCanvas();
63+
64+
2765

2866
const playerColors = ["white", "lime", "cyan", "orange", "violet"];
2967
const enemySprites = {
@@ -67,6 +105,14 @@ class Player {
67105
this.respawnTimer = 0;
68106
}
69107

108+
moveLeft() {
109+
this.x = Math.max(this.x - this.speed, this.width / 2);
110+
}
111+
112+
moveRight() {
113+
this.x = Math.min(this.x + this.speed, canvas.width - this.width / 2);
114+
}
115+
70116
draw() {
71117
if (this.active && this.alive) {
72118
ctx.beginPath();
@@ -118,6 +164,7 @@ class Player {
118164
}
119165
}
120166

167+
121168
class Enemy {
122169
constructor(x, y, type) {
123170
this.x = x;
@@ -146,12 +193,13 @@ class Enemy {
146193
}
147194
}
148195

149-
let player1, player2;
196+
150197
let enemies = [];
151198

152199
function newGame() {
153200
player1 = new Player(canvas.width / 2 - 60, randomColor());
154201
player2 = new Player(canvas.width / 2 + 60, randomColor());
202+
resizeCanvas();
155203
enemyBullets = [];
156204
level = 1;
157205
gameOver = false;
@@ -321,12 +369,13 @@ function activatePlayer(player) {
321369
player.active = true;
322370
}
323371

372+
324373
window.addEventListener("keydown", (e) => {
325-
if (e.key === "a") { player1.x -= player1.speed; activatePlayer(player1); }
326-
if (e.key === "d") { player1.x += player1.speed; activatePlayer(player1); }
374+
if (e.key === "a") { player1.moveLeft(); activatePlayer(player1); }
375+
if (e.key === "d") { player1.moveRight(); activatePlayer(player1); }
327376
if (e.key === " ") { player1.shoot(); activatePlayer(player1); }
328-
if (e.key === "ArrowLeft") { player2.x -= player2.speed; activatePlayer(player2); }
329-
if (e.key === "ArrowRight") { player2.x += player2.speed; activatePlayer(player2); }
377+
if (e.key === "ArrowLeft") { player2.moveLeft(); activatePlayer(player2); }
378+
if (e.key === "ArrowRight") { player2.moveRight(); activatePlayer(player2); }
330379
if (e.key === "ArrowUp") { player2.shoot(); activatePlayer(player2); }
331380
if (e.key === "r") newGame();
332381
});
@@ -341,6 +390,7 @@ canvas.addEventListener("touchstart", (e) => {
341390
else player1.shoot();
342391
}, { passive: false });
343392

393+
344394
const ws = new WebSocket("ws://" + location.hostname + "/ws");
345395
ws.onmessage = (event) => {
346396
const data = JSON.parse(event.data);
@@ -353,6 +403,7 @@ ws.onmessage = (event) => {
353403
if (data.j2f) { player2.shoot(); activatePlayer(player2); }
354404
};
355405

406+
356407
waitForSprites(() => {
357408
newGame();
358409
draw();

data/game.js.bak

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,67 @@
1-
// game.js z aktywacją gracza ruchem i wstrzymaniem wrogów przed aktywacją
2-
31
const canvas = document.getElementById("gameCanvas");
42
const ctx = canvas.getContext("2d");
5-
canvas.width = 800;
6-
canvas.height = 600;
73

4+
let player1 = null;
5+
let player2 = null;
6+
7+
function resizeCanvas() {
8+
const ratio = 4 / 3;
9+
const w = window.innerWidth;
10+
const h = window.innerHeight;
11+
if (w / h > ratio) {
12+
canvas.height = h;
13+
canvas.width = h * ratio;
14+
} else {
15+
canvas.width = w;
16+
canvas.height = w / ratio;
17+
}
18+
if (player1) {
19+
player1.y = canvas.height - 50;
20+
player1.x = canvas.width / 2 - 60;
21+
}
22+
if (player2) {
23+
player2.y = canvas.height - 50;
24+
player2.x = canvas.width / 2 + 60;
25+
}
26+
}
27+
window.addEventListener('resize', resizeCanvas);
28+
29+
let wakeLock = null;
30+
async function requestWakeLock() {
31+
try {
32+
if ('wakeLock' in navigator) {
33+
wakeLock = await navigator.wakeLock.request('screen');
34+
}
35+
} catch (err) {
36+
console.warn("WakeLock error:", err);
37+
}
38+
}
39+
document.addEventListener("visibilitychange", () => {
40+
if (wakeLock !== null && document.visibilityState === "visible") {
41+
requestWakeLock();
42+
}
43+
});
44+
requestWakeLock();
845

946
const pauseBtn = document.createElement("button");
1047
pauseBtn.innerText = "⏸️ Pauza";
11-
pauseBtn.style.position = "absolute";
48+
pauseBtn.className = "game-button";
49+
pauseBtn.style.right = "120px";
1250
pauseBtn.style.top = "10px";
13-
pauseBtn.style.left = "10px";
14-
pauseBtn.style.zIndex = 10;
1551
pauseBtn.onclick = () => paused = !paused;
1652
document.body.appendChild(pauseBtn);
1753

1854
const restartBtn = document.createElement("button");
1955
restartBtn.innerText = "🔄 Restart";
20-
restartBtn.style.position = "absolute";
56+
restartBtn.className = "game-button";
57+
restartBtn.style.right = "10px";
2158
restartBtn.style.top = "10px";
22-
restartBtn.style.left = "100px";
23-
restartBtn.style.zIndex = 10;
2459
restartBtn.onclick = () => newGame();
2560
document.body.appendChild(restartBtn);
2661

62+
resizeCanvas();
63+
64+
2765

2866
const playerColors = ["white", "lime", "cyan", "orange", "violet"];
2967
const enemySprites = {
@@ -67,6 +105,14 @@ class Player {
67105
this.respawnTimer = 0;
68106
}
69107

108+
moveLeft() {
109+
this.x = Math.max(this.x - this.speed, this.width / 2);
110+
}
111+
112+
moveRight() {
113+
this.x = Math.min(this.x + this.speed, canvas.width - this.width / 2);
114+
}
115+
70116
draw() {
71117
if (this.active && this.alive) {
72118
ctx.beginPath();
@@ -118,6 +164,7 @@ class Player {
118164
}
119165
}
120166

167+
121168
class Enemy {
122169
constructor(x, y, type) {
123170
this.x = x;
@@ -146,12 +193,13 @@ class Enemy {
146193
}
147194
}
148195

149-
let player1, player2;
196+
150197
let enemies = [];
151198

152199
function newGame() {
153200
player1 = new Player(canvas.width / 2 - 60, randomColor());
154201
player2 = new Player(canvas.width / 2 + 60, randomColor());
202+
resizeCanvas();
155203
enemyBullets = [];
156204
level = 1;
157205
gameOver = false;
@@ -321,12 +369,13 @@ function activatePlayer(player) {
321369
player.active = true;
322370
}
323371

372+
324373
window.addEventListener("keydown", (e) => {
325-
if (e.key === "a") { player1.x -= player1.speed; activatePlayer(player1); }
326-
if (e.key === "d") { player1.x += player1.speed; activatePlayer(player1); }
374+
if (e.key === "a") { player1.moveLeft(); activatePlayer(player1); }
375+
if (e.key === "d") { player1.moveRight(); activatePlayer(player1); }
327376
if (e.key === " ") { player1.shoot(); activatePlayer(player1); }
328-
if (e.key === "ArrowLeft") { player2.x -= player2.speed; activatePlayer(player2); }
329-
if (e.key === "ArrowRight") { player2.x += player2.speed; activatePlayer(player2); }
377+
if (e.key === "ArrowLeft") { player2.moveLeft(); activatePlayer(player2); }
378+
if (e.key === "ArrowRight") { player2.moveRight(); activatePlayer(player2); }
330379
if (e.key === "ArrowUp") { player2.shoot(); activatePlayer(player2); }
331380
if (e.key === "r") newGame();
332381
});
@@ -341,6 +390,7 @@ canvas.addEventListener("touchstart", (e) => {
341390
else player1.shoot();
342391
}, { passive: false });
343392

393+
344394
const ws = new WebSocket("ws://" + location.hostname + "/ws");
345395
ws.onmessage = (event) => {
346396
const data = JSON.parse(event.data);
@@ -353,6 +403,7 @@ ws.onmessage = (event) => {
353403
if (data.j2f) { player2.shoot(); activatePlayer(player2); }
354404
};
355405

406+
356407
waitForSprites(() => {
357408
newGame();
358409
draw();

data/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<meta charset="UTF-8">
55
<title>Vi Invaders</title>
6+
<link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet">
67
<link rel="stylesheet" href="style.css">
78
</head>
89
<body>

data/index.html.bak

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Vi Invaders</title>
6+
<link rel="stylesheet" href="style.css">
7+
</head>
8+
<body>
9+
<canvas id="gameCanvas" width="800" height="600"></canvas>
10+
<script src="game.js"></script>
11+
</body>
12+
</html>

data/style.css

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ canvas {
1616

1717
.game-button {
1818
position: absolute;
19-
top: 10px;
20-
left: 10px;
2119
padding: 10px 15px;
22-
background: black;
20+
background: transparent;
2321
color: white;
22+
font-family: 'Press Start 2P', monospace;
23+
font-size: 12px;
2424
border: 2px solid white;
25-
border-radius: 6px;
26-
font-size: 16px;
25+
border-radius: 8px;
2726
cursor: pointer;
2827
z-index: 1000;
28+
text-shadow: 1px 1px black;
2929
}
3030

31+

0 commit comments

Comments
 (0)