/
xem
/
mini-m
Обзор
Документация
Войти
/
xem
/
mini-m
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
js/inputHandler.js
39 строк
1 KB
romican
first_commit
15 май 2026, 20:10
15 май 2026, 20:10
65014ec
Код
Авторство
О чём код?
// js/inputHandler.js import { Utils } from './utils.js'; export class InputHandler { constructor(canvas, world) { this.canvas = canvas; this.world = world; this._onClick = this._onClick.bind(this); canvas.addEventListener('click', this._onClick); } _onClick(event) { if (this.world.win || this.world.lose) return; const rect = this.canvas.getBoundingClientRect(); const mx = (event.clientX - rect.left) * (this.canvas.width / rect.width); const my = (event.clientY - rect.top) * (this.canvas.height / rect.height); let target = null; for (const m of this.world.monsters) { if (Utils.distance(mx, my, m.x, m.y) <= m.radius + 4) { target = m; break; } } if (target) { for (const h of this.world.heroes) { h.attackTarget = target; h.moveTarget = null; } } else { const heroes = this.world.heroes.filter(h => h.alive); const count = heroes.length; if (count === 0) return; const spreadRadius = 18; for (let i = 0; i < count; i++) { const angle = (i / count) * Math.PI * 2; heroes[i].moveTarget = { x: mx + Math.cos(angle) * spreadRadius, y: my + Math.sin(angle) * spreadRadius }; heroes[i].attackTarget = null; } } } destroy() { this.canvas.removeEventListener('click', this._onClick); } }