const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); const W = canvas.width; const H = canvas.height; canvas.style.backgroundColor = "#000"; const STATE = { MENU: 0, PLAYING: 1, GAMEOVER: 2, WIN: 3 }; function loadImg(src) { const img = new Image(); img.src = src; return img; } const IMG = { player: loadImg("./public/images/game/player/Gladius.png"), normal: loadImg("./public/images/game/ennemies/SabreFirebird.png"), rare: loadImg("./public/images/ships/hull-c.jpg"), mini: loadImg("./public/images/ships/reclaimer.jpg"), boss: loadImg("./public/images/ships/idris-p.jpg"), zones: [ loadImg("./public/images/planets/arc-crop.jpg"), loadImg("./public/images/planets/crusader.png"), loadImg("./public/images/planets/hurston.jpg"), loadImg("./public/images/planets/microtech.jpg"), loadImg("./public/images/planets/pyro-iv.jpg") ] }; const ZONE_NAMES = ["ArcCorp", "Crusader", "Hurston", "MicroTech", "Pyro"]; let state = STATE.MENU; let zoneIndex = 0; let score = 0; let frame = 0; let player, enemies, playerBullets, enemyBullets, powerups; let zoneKills, zoneKillsRequired, bossSpawned, bossDefeated, transitionTimer; const keys = {}; window.addEventListener("keydown", e => { keys[e.code] = true; if (["Space", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(e.code)) e.preventDefault(); if (e.code === "Enter" && state === STATE.MENU) startGame(); if (e.code === "KeyR" && (state === STATE.GAMEOVER || state === STATE.WIN)) startGame(); }); window.addEventListener("keyup", e => { keys[e.code] = false; }); canvas.addEventListener("click", () => { if (state === STATE.MENU) 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 { constructor() { this.w = 32; this.h = 32; this.x = W / 2 - this.w / 2; this.y = H - 70; this.lives = 3; this.maxLives = 3; this.speed = 3.2; this.cooldown = 0; this.invul = 60; } update() { if (keys.KeyA || keys.ArrowLeft) this.x -= this.speed; if (keys.KeyD || keys.ArrowRight) this.x += this.speed; if (keys.KeyW || keys.ArrowUp) 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.y = Math.max(0, Math.min(H - this.h, this.y)); if (this.cooldown > 0) this.cooldown--; if (this.invul > 0) this.invul--; if ((keys.Space || keys.KeyJ || touch.active) && this.cooldown <= 0) { playerBullets.push(new PlayerBullet(this.x + this.w / 2 - 3, this.y)); this.cooldown = 9; } } draw() { if (this.invul > 0 && this.invul % 8 < 4) return; ctx.drawImage(IMG.player, this.x, this.y, this.w, this.h); const hb = this.getHitbox(); ctx.fillStyle = "rgb(255, 102, 0)"; ctx.beginPath(); ctx.arc(hb.x, hb.y, hb.r, 0, Math.PI * 2); ctx.fill(); } hit() { if (this.invul > 0) return; this.lives--; this.invul = 90; } getHitbox() { return { x: this.x + this.w / 2, y: this.y + this.h / 2, r: 2 }; } } class PlayerBullet { constructor(x, y) { this.x = x; this.y = y; this.w = 6; this.h = 12; this.dead = false; } update() { this.y -= 7; if (this.y < -this.h) this.dead = true; } draw() { ctx.fillStyle = "#0ff"; ctx.fillRect(this.x, this.y, this.w, this.h); ctx.fillStyle = "#fff"; ctx.fillRect(this.x + 2, this.y, 2, this.h); } } const ENEMY_CFG = { 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: 0 }, 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: 10 } }; class Enemy { constructor(type) { const cfg = ENEMY_CFG[type]; this.type = type; this.w = cfg.w; this.h = cfg.h; this.x = Math.random() * (W - this.w); this.y = -this.h; this.hp = cfg.hp; this.maxHp = cfg.hp; this.speed = cfg.speed; this.scoreVal = cfg.score; this.fireRate = cfg.fireRate; this.fireTimer = Math.floor(Math.random() * this.fireRate); this.dead = false; this.targetY = (type === "boss" || type === "mini") ? 60 : null; this.dropLife = (type === "rare"); this.img = IMG[type]; this.swingPhase = Math.random() * Math.PI * 2; } update() { if (this.targetY !== null && this.y < this.targetY) { this.y += this.speed; } else if (this.targetY !== null) { this.x += Math.sin((frame + this.swingPhase * 100) * 0.02) * 1.5; this.x = Math.max(0, Math.min(W - this.w, this.x)); } else { this.y += this.speed; if (this.y > H + this.h) this.dead = true; } this.fireTimer++; if (this.fireTimer >= this.fireRate) { this.fireTimer = 0; this.fire(); } } fire() { const cx = this.x + this.w / 2; const cy = this.y + this.h; if (this.type === "normal") { const dx = (player.x + player.w / 2) - cx; const dy = (player.y + player.h / 2) - cy; const d = Math.hypot(dx, dy) || 1; enemyBullets.push(new EnemyBullet(cx, cy, dx / d * 2.6, dy / d * 2.6, "#ff8040")); } else if (this.type === "mini") { for (let i = -2; i <= 2; i++) { const a = Math.PI / 2 + i * 0.22; enemyBullets.push(new EnemyBullet(cx, cy, Math.cos(a) * 2.5, Math.sin(a) * 2.5, "#ffa030")); } } else if (this.type === "boss") { const n = 14; const off = frame * 0.025; for (let i = 0; i < n; i++) { const a = (Math.PI * 2 * i / n) + off; enemyBullets.push(new EnemyBullet(cx, cy, Math.cos(a) * 2.3, Math.sin(a) * 2.3, "#ff3060")); } } } draw() { ctx.drawImage(this.img, this.x, this.y, this.w, this.h); if (this.maxHp > 1 && this.type !== "boss") { const ratio = this.hp / this.maxHp; ctx.fillStyle = "#400"; ctx.fillRect(this.x, this.y - 6, this.w, 4); ctx.fillStyle = "#f44"; ctx.fillRect(this.x, this.y - 6, this.w * ratio, 4); } } hit(dmg) { if (this.dead) return; this.hp -= dmg; if (this.hp <= 0) { this.dead = true; score += this.scoreVal; if (this.dropLife) { powerups.push(new PowerUp(this.x + this.w / 2, this.y + this.h / 2)); } if (this.type === "boss") bossDefeated = true; else zoneKills++; } } } class EnemyBullet { constructor(x, y, vx, vy, color) { this.x = x; this.y = y; this.vx = vx; this.vy = vy; this.r = 5; this.color = color || "#f80"; this.dead = false; } update() { this.x += this.vx; this.y += this.vy; if (this.x < -10 || this.x > W + 10 || this.y < -10 || this.y > H + 10) this.dead = true; } draw() { ctx.beginPath(); ctx.fillStyle = this.color; ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = "#fff"; ctx.lineWidth = 1; ctx.stroke(); } } class PowerUp { constructor(x, y) { this.x = x; this.y = y; this.r = 10; this.vy = 1; this.dead = false; } update() { this.y += this.vy; if (this.y > H + 10) this.dead = true; } draw() { ctx.beginPath(); ctx.fillStyle = "#0f0"; ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "#fff"; ctx.font = "bold 14px Arial"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText("+", this.x, this.y); ctx.textAlign = "start"; ctx.textBaseline = "alphabetic"; } } function startGame() { state = STATE.PLAYING; zoneIndex = 0; score = 0; frame = 0; player = new Player(); playerBullets = []; initZone(); } function initZone() { enemies = []; enemyBullets = []; powerups = []; zoneKills = 0; zoneKillsRequired = 30 + zoneIndex * 1.5; bossSpawned = false; bossDefeated = false; transitionTimer = 90; } function nextZone() { zoneIndex++; if (zoneIndex >= 5) { state = STATE.WIN; } else { initZone(); } } function spawnLogic() { if (transitionTimer > 0) { transitionTimer--; return; } if (bossSpawned) return; const baseRate = Math.max(35, 80 - zoneIndex * 8); if (frame % baseRate === 0) { const r = Math.random(); if (r < 0.08) enemies.push(new Enemy("rare")); else if (r < 0.18 && zoneIndex >= 1) enemies.push(new Enemy("mini")); else enemies.push(new Enemy("normal")); } if (zoneKills >= zoneKillsRequired) { bossSpawned = true; enemies.push(new Enemy("boss")); } } function rectCollide(a, b) { return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y; } function circleRect(c, r) { const nx = Math.max(r.x, Math.min(c.x, r.x + r.w)); const ny = Math.max(r.y, Math.min(c.y, r.y + r.h)); const dx = c.x - nx, dy = c.y - ny; return dx * dx + dy * dy < c.r * c.r; } function circleCircle(a, b) { const dx = a.x - b.x, dy = a.y - b.y; const rr = a.r + b.r; return dx * dx + dy * dy < rr * rr; } function update() { frame++; player.update(); spawnLogic(); for (const e of enemies) e.update(); for (const b of playerBullets) b.update(); for (const b of enemyBullets) b.update(); for (const p of powerups) p.update(); for (const b of playerBullets) { if (b.dead) continue; for (const e of enemies) { if (e.dead) continue; if (rectCollide(b, e)) { e.hit(1); b.dead = true; break; } } } for (const b of enemyBullets) { if (circleCircle(b, player.getHitbox())) { player.hit(); b.dead = true; } } for (const e of enemies) { if (!e.dead && circleRect(player.getHitbox(), e)) { player.hit(); e.hit(5); } } for (const p of powerups) { if (circleCircle(p, player.getHitbox())) { if (player.lives < player.maxLives) player.lives++; else score += 100; p.dead = true; } } enemies = enemies.filter(e => !e.dead); playerBullets = playerBullets.filter(b => !b.dead); enemyBullets = enemyBullets.filter(b => !b.dead); powerups = powerups.filter(p => !p.dead); if (player.lives <= 0) state = STATE.GAMEOVER; else if (bossDefeated) nextZone(); } function drawBackground() { const img = IMG.zones[zoneIndex]; if (img && img.complete && img.naturalWidth) { ctx.drawImage(img, 0, 0, W, H); ctx.fillStyle = "rgba(0,0,0,0.45)"; ctx.fillRect(0, 0, W, H); } else { ctx.fillStyle = "#000"; ctx.fillRect(0, 0, W, H); } } function drawHUD() { ctx.fillStyle = "#fff"; ctx.font = "16px Arial"; ctx.fillText("Score : " + score, 10, 22); ctx.fillText("Zone " + (zoneIndex + 1) + " / 5 ─ " + ZONE_NAMES[zoneIndex], 10, 44); ctx.fillStyle = "#f44"; for (let i = 0; i < player.lives; i++) { ctx.beginPath(); const cx = 14 + i * 18, cy = 62; ctx.moveTo(cx, cy + 4); ctx.bezierCurveTo(cx, cy, cx - 8, cy, cx - 8, cy + 5); ctx.bezierCurveTo(cx - 8, cy + 10, cx, cy + 12, cx, cy + 16); ctx.bezierCurveTo(cx, cy + 12, cx + 8, cy + 10, cx + 8, cy + 5); ctx.bezierCurveTo(cx + 8, cy, cx, cy, cx, cy + 4); ctx.fill(); } if (!bossSpawned) { ctx.fillStyle = "#bbb"; ctx.font = "14px Arial"; ctx.fillText("Progression : " + Math.min(zoneKills, zoneKillsRequired) + " / " + zoneKillsRequired, 10, H - 10); } else { const boss = enemies.find(e => e.type === "boss"); if (boss) { ctx.fillStyle = "#400"; ctx.fillRect(40, H - 22, W - 80, 12); ctx.fillStyle = "#f00"; ctx.fillRect(40, H - 22, (W - 80) * (boss.hp / boss.maxHp), 12); ctx.strokeStyle = "#fff"; ctx.lineWidth = 1; ctx.strokeRect(40, H - 22, W - 80, 12); ctx.fillStyle = "#fff"; ctx.font = "bold 12px Arial"; ctx.textAlign = "center"; ctx.fillText("BOSS", W / 2, H - 26); ctx.textAlign = "start"; } } if (transitionTimer > 0) { ctx.fillStyle = "rgba(0,0,0,0.55)"; ctx.fillRect(0, H / 2 - 40, W, 80); ctx.fillStyle = "#0ff"; ctx.textAlign = "center"; ctx.font = "bold 28px Arial"; ctx.fillText("ZONE " + (zoneIndex + 1) + " ─ " + ZONE_NAMES[zoneIndex], W / 2, H / 2 + 8); ctx.textAlign = "start"; } } function drawMenu() { ctx.fillStyle = "#000"; ctx.fillRect(0, 0, W, H); const bg = IMG.zones[0]; if (bg && bg.complete && bg.naturalWidth) { ctx.drawImage(bg, 0, 0, W, H); ctx.fillStyle = "rgba(0,0,0,0.6)"; ctx.fillRect(0, 0, W, H); } ctx.fillStyle = "#fff"; ctx.textAlign = "center"; ctx.font = "bold 40px Arial"; ctx.fillText("STAR BULLET", W / 2, 110); ctx.font = "16px Arial"; ctx.fillStyle = "#ccc"; ctx.fillText("Star Present ─ Danmaku", W / 2, 140); ctx.font = "15px Arial"; ctx.fillStyle = "#fff"; ctx.fillText("WASD ou Flèches ─ déplacement", W / 2, 210); ctx.fillText("Espace ou J ─ tirer", W / 2, 235); 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.fillText("Les Hull-C LOOT une vie.", W / 2, 320); ctx.font = "bold 22px Arial"; ctx.fillStyle = "#0ff"; ctx.fillText("ENTRÉE, CLIC ou TAP pour commencer", W / 2, 400); ctx.textAlign = "start"; } function drawEndScreen(title, color) { ctx.fillStyle = "rgba(0,0,0,0.75)"; ctx.fillRect(0, 0, W, H); ctx.fillStyle = color; ctx.textAlign = "center"; ctx.font = "bold 48px Arial"; ctx.fillText(title, W / 2, H / 2 - 30); ctx.fillStyle = "#fff"; ctx.font = "20px Arial"; ctx.fillText("Score final : " + score, W / 2, H / 2 + 10); if (state === STATE.WIN) ctx.fillText("Zone atteinte : " + (zoneIndex) + " / 5", W / 2, H / 2 + 40); else ctx.fillText("Zone atteinte : " + (zoneIndex + 1) + " / 5", W / 2, H / 2 + 40); ctx.font = "bold 22px Arial"; ctx.fillStyle = "#0ff"; ctx.fillText("R, CLIC ou TAP pour rejouer", W / 2, H / 2 + 95); ctx.textAlign = "start"; } function draw() { if (state === STATE.MENU) { drawMenu(); return; } drawBackground(); for (const e of enemies) e.draw(); for (const p of powerups) p.draw(); for (const b of playerBullets) b.draw(); for (const b of enemyBullets) b.draw(); player.draw(); drawHUD(); if (state === STATE.GAMEOVER) drawEndScreen("GAME OVER", "#f44"); if (state === STATE.WIN) drawEndScreen("VICTOIRE !", "#0f0"); } function loop() { if (state === STATE.PLAYING) update(); draw(); requestAnimationFrame(loop); } requestAnimationFrame(loop);