Files
arkanoid/src/lib/spawnRandomPerk/spawnRandomPerk.js
T

29 lines
889 B
JavaScript
Raw Normal View History

import { PERK_FALL_SPEED, PERK_HEIGHT, PERK_WIDTH } from '../../config';
import { Perk } from '../../entities/perk/perk';
/**
* Типы бонусов
*/
const DROPPABLE_PERKS = ['slow'];
/**
* Создает случайный бонус в заданной точке
* @param {number} x координата бонуса по оси X
* @param {number} y координата бонуса по оси Y
* @returns {Perk | null}
*/
export function spawnRandomPerk(x, y) {
try {
if (typeof x !== 'number' || typeof y !== 'number') {
throw new Error('Координаты бонуса должны являться числами');
}
const type = DROPPABLE_PERKS[Math.floor(Math.random() * DROPPABLE_PERKS.length)];
return new Perk(x, y, PERK_WIDTH, PERK_HEIGHT, type, PERK_FALL_SPEED);
} catch (err) {
console.error(err);
return null;
}
}