feat(hell-bullet): add touch controls and optimize game canvas styling
This commit is contained in:
3 files changed
+75
-16
No files matched your search
+6
-4
@@ -1,6 +1,8 @@
|
|||||||
#canvas {
|
#canvas {
|
||||||
display: flex;
|
display: block;
|
||||||
flex-direction: column;
|
margin: 0 auto;
|
||||||
justify-self: center;
|
width: 100%;
|
||||||
align-self: center;
|
max-width: 1000px;
|
||||||
|
height: auto;
|
||||||
|
touch-action: none;
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,7 @@ body #div-content {
|
|||||||
|
|
||||||
.grid-item p{
|
.grid-item p{
|
||||||
text-align: justify;
|
text-align: justify;
|
||||||
|
font-size: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.grid-item:hover {
|
.grid-item:hover {
|
||||||
@@ -54,8 +55,13 @@ body #div-content {
|
|||||||
border-radius: .40rem;
|
border-radius: .40rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.grid .two p {
|
||||||
|
max-width: 44.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
.grid, .two img {
|
.grid, .two img {
|
||||||
float: right;
|
float: right;
|
||||||
|
/* padding-left: 2rem; */
|
||||||
}
|
}
|
||||||
|
|
||||||
.right-text {
|
.right-text {
|
||||||
@@ -69,10 +75,6 @@ body #div-content {
|
|||||||
float: left;
|
float: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
.grid-item p {
|
|
||||||
font-size: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media screen and (max-width: 1280px) {
|
@media screen and (max-width: 1280px) {
|
||||||
.grid {
|
.grid {
|
||||||
grid-template-columns: 90%;
|
grid-template-columns: 90%;
|
||||||
|
|||||||
@@ -50,6 +50,44 @@ canvas.addEventListener("click", () => {
|
|||||||
else if (state === STATE.GAMEOVER || state === STATE.WIN) startGame();
|
else if (state === STATE.GAMEOVER || state === STATE.WIN) startGame();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const touch = { active: false, x: 0, y: 0 };
|
||||||
|
|
||||||
|
function touchToCanvas(t) {
|
||||||
|
const rect = canvas.getBoundingClientRect();
|
||||||
|
return {
|
||||||
|
x: (t.clientX - rect.left) * (W / rect.width),
|
||||||
|
y: (t.clientY - rect.top) * (H / rect.height)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.addEventListener("touchstart", e => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (state === STATE.MENU || state === STATE.GAMEOVER || state === STATE.WIN) {
|
||||||
|
startGame();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const p = touchToCanvas(e.touches[0]);
|
||||||
|
touch.active = true;
|
||||||
|
touch.x = p.x;
|
||||||
|
touch.y = p.y;
|
||||||
|
}, { passive: false });
|
||||||
|
|
||||||
|
canvas.addEventListener("touchmove", e => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (!touch.active || e.touches.length === 0) return;
|
||||||
|
const p = touchToCanvas(e.touches[0]);
|
||||||
|
touch.x = p.x;
|
||||||
|
touch.y = p.y;
|
||||||
|
}, { passive: false });
|
||||||
|
|
||||||
|
function endTouch(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (e.touches && e.touches.length > 0) return;
|
||||||
|
touch.active = false;
|
||||||
|
}
|
||||||
|
canvas.addEventListener("touchend", endTouch, { passive: false });
|
||||||
|
canvas.addEventListener("touchcancel", endTouch, { passive: false });
|
||||||
|
|
||||||
class Player {
|
class Player {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.w = 32;
|
this.w = 32;
|
||||||
@@ -67,13 +105,29 @@ class Player {
|
|||||||
if (keys.KeyD || keys.ArrowRight) this.x += this.speed;
|
if (keys.KeyD || keys.ArrowRight) this.x += this.speed;
|
||||||
if (keys.KeyW || keys.ArrowUp) this.y -= this.speed;
|
if (keys.KeyW || keys.ArrowUp) this.y -= this.speed;
|
||||||
if (keys.KeyS || keys.ArrowDown) this.y += this.speed;
|
if (keys.KeyS || keys.ArrowDown) this.y += this.speed;
|
||||||
|
|
||||||
|
if (touch.active) {
|
||||||
|
const tx = touch.x - this.w / 2;
|
||||||
|
const ty = touch.y - this.h / 2 - 50;
|
||||||
|
const dx = tx - this.x, dy = ty - this.y;
|
||||||
|
const d = Math.hypot(dx, dy);
|
||||||
|
const maxStep = 8;
|
||||||
|
if (d > maxStep) {
|
||||||
|
this.x += dx / d * maxStep;
|
||||||
|
this.y += dy / d * maxStep;
|
||||||
|
} else {
|
||||||
|
this.x = tx;
|
||||||
|
this.y = ty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.x = Math.max(0, Math.min(W - this.w, this.x));
|
this.x = Math.max(0, Math.min(W - this.w, this.x));
|
||||||
this.y = Math.max(0, Math.min(H - this.h, this.y));
|
this.y = Math.max(0, Math.min(H - this.h, this.y));
|
||||||
|
|
||||||
if (this.cooldown > 0) this.cooldown--;
|
if (this.cooldown > 0) this.cooldown--;
|
||||||
if (this.invul > 0) this.invul--;
|
if (this.invul > 0) this.invul--;
|
||||||
|
|
||||||
if ((keys.Space || keys.KeyJ) && this.cooldown <= 0) {
|
if ((keys.Space || keys.KeyJ || touch.active) && this.cooldown <= 0) {
|
||||||
playerBullets.push(new PlayerBullet(this.x + this.w / 2 - 3, this.y));
|
playerBullets.push(new PlayerBullet(this.x + this.w / 2 - 3, this.y));
|
||||||
this.cooldown = 9;
|
this.cooldown = 9;
|
||||||
}
|
}
|
||||||
@@ -121,9 +175,9 @@ class PlayerBullet {
|
|||||||
|
|
||||||
const ENEMY_CFG = {
|
const ENEMY_CFG = {
|
||||||
normal: { w: 36, h: 36, hp: 2, speed: 1.3, score: 50, fireRate: 30 },
|
normal: { w: 36, h: 36, hp: 2, speed: 1.3, score: 50, fireRate: 30 },
|
||||||
rare: { w: 44, h: 44, hp: 1, speed: 2.4, score: 200, fireRate: 999999 },
|
rare: { w: 44, h: 44, hp: 1, speed: 2.4, score: 200, fireRate: 0 },
|
||||||
mini: { w: 70, h: 70, hp: 18, speed: 1.0, score: 500, fireRate: 100 },
|
mini: { w: 70, h: 70, hp: 18, speed: 1.0, score: 500, fireRate: 20 },
|
||||||
boss: { w: 120, h: 120, hp: 70, speed: 0.7, score: 2000, fireRate: 600 }
|
boss: { w: 120, h: 120, hp: 70, speed: 0.7, score: 2000, fireRate: 10 }
|
||||||
};
|
};
|
||||||
|
|
||||||
class Enemy {
|
class Enemy {
|
||||||
@@ -451,13 +505,14 @@ function drawMenu() {
|
|||||||
ctx.font = "15px Arial"; ctx.fillStyle = "#fff";
|
ctx.font = "15px Arial"; ctx.fillStyle = "#fff";
|
||||||
ctx.fillText("WASD ou Flèches ─ déplacement", W / 2, 210);
|
ctx.fillText("WASD ou Flèches ─ déplacement", W / 2, 210);
|
||||||
ctx.fillText("Espace ou J ─ tirer", W / 2, 235);
|
ctx.fillText("Espace ou J ─ tirer", W / 2, 235);
|
||||||
ctx.fillText("5 zones, 4 types d'ennemis, 3 vies", W / 2, 275);
|
ctx.fillText("Mobile ─ glisser pour bouger, tir auto", W / 2, 260);
|
||||||
|
ctx.fillText("5 zones, 4 types d'ennemis, 3 vies", W / 2, 295);
|
||||||
ctx.fillStyle = "#0f0";
|
ctx.fillStyle = "#0f0";
|
||||||
ctx.fillText("Les Hull-C lâchent une vie.", W / 2, 300);
|
ctx.fillText("Les Hull-C lâchent une vie.", W / 2, 320);
|
||||||
|
|
||||||
ctx.font = "bold 22px Arial";
|
ctx.font = "bold 22px Arial";
|
||||||
ctx.fillStyle = "#0ff";
|
ctx.fillStyle = "#0ff";
|
||||||
ctx.fillText("ENTRÉE ou CLIC pour commencer", W / 2, 400);
|
ctx.fillText("ENTRÉE, CLIC ou TAP pour commencer", W / 2, 400);
|
||||||
ctx.textAlign = "start";
|
ctx.textAlign = "start";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -474,7 +529,7 @@ function drawEndScreen(title, color) {
|
|||||||
ctx.fillText("Zone atteinte : " + (zoneIndex + 1) + " / 5", W / 2, H / 2 + 40);
|
ctx.fillText("Zone atteinte : " + (zoneIndex + 1) + " / 5", W / 2, H / 2 + 40);
|
||||||
ctx.font = "bold 22px Arial";
|
ctx.font = "bold 22px Arial";
|
||||||
ctx.fillStyle = "#0ff";
|
ctx.fillStyle = "#0ff";
|
||||||
ctx.fillText("R ou CLIC pour rejouer", W / 2, H / 2 + 95);
|
ctx.fillText("R, CLIC ou TAP pour rejouer", W / 2, H / 2 + 95);
|
||||||
ctx.textAlign = "start";
|
ctx.textAlign = "start";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user