Can you detect mobile devices by resolution and add an in-screen analog, shoot buttons?…
- User
Can you detect mobile devices by resolution and add an in-screen analog, shoot buttons? Grappling should be by tapping on the squares.
// Get the canvas and context const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d');
// Set canvas dimensions canvas.width = window.innerWidth; canvas.height = window.innerHeight;
// Variables let keys = {}; let player = { x: canvas.width / 2, y: canvas.height / 2, size: 20, speed: 3, vx: 0, vy: 0, grappling: false, grapplePoint: null, maxHealth: 100, health: 100, maxStamina: 100, stamina: 100, staminaRegen: 0.5, staminaDrain: 1.0, ultimateReady: true, ultimateActive: false, ultimateDuration: 5000, ultimateTimer: 0, }; let gravity = 1.5; let friction = 0.9; let squares = []; let enemies = []; let particles = []; let projectiles = []; let enemySpawnInterval = 2000; let lastEnemySpawnTime = 0; let score = 0; let canShoot = true; const SHOOT_COOLDOWN = 100;
const musicBtn = document.getElementById('music'); const music = new Audio('./assets/music.mp3'); music.loop = true; music.volume = 0.1; let isMusic = false;
musicBtn.addEventListener('click', () => { if (isMusic) { music.pause(); isMusic = false; musicBtn.classList.remove('active'); } else { music.play(); isMusic = true; musicBtn.classList.add('active'); } });
window.addEventListener('keydown', e => { keys[e.key] = true; });
window.addEventListener('keyup', e => { keys[e.key] = false; });
canvas.addEventListener('mousedown', e => { if (e.button === 2) { // Right-click (Grapple) e.preventDefault(); const square = getSquareUnderMouse(e.clientX, e.clientY); if (square && player.stamina > 0) { player.grappling = true; player.grapplePoint = { x: square.x, y: square.y }; // Grapple point is the center of the square } } else if (e.button === 0) { // Left-click (Shoot) if (!canShoot) { return; }
code// Proceed with shooting shootProjectile(e.clientX, e.clientY); const fire = new Audio('./assets/fire.wav'); fire.volume = 0.05; fire.play(); // Start cooldown canShoot = false; setTimeout(() => { canShoot = true; }, SHOOT_COOLDOWN); } else if (e.button === 1) { // Middle-click (Ultimate) e.preventDefault(); activateUltimate(); }});
canvas.addEventListener('mouseup', e => { if (e.button === 2) { player.grappling = false; player.grapplePoint = null; } });
window.addEventListener('contextmenu', e => { e.preventDefault(); });
// Resize handler window.addEventListener('resize', () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; });
- Assistant