import { GRID_GAP } from '../constants.js'; import { getColor } from '../utils/color.js'; /** * Block - 方块实体 * 带有数字标记,被小球碰撞时数字减少,归零时消除 */ export class Block { /** * @param {number} gridX - 网格列位置 (0-6) * @param {number} gridY - 网格行位置 (0-N) * @param {number} count - 方块数字(剩余生命值) * @param {number} size - 方块边长(像素) */ constructor(gridX, gridY, count, size) { this.gridX = gridX; this.gridY = gridY; this.count = count; this.size = size; this.destroyed = false; } /** * 被小球击中,count 减 1 * count <= 0 时标记为已消除 * @returns {boolean} 是否被消除 */ hit() { this.count -= 1; this._hitTime = performance.now(); if (this.count <= 0) { this.destroyed = true; } return this.destroyed; } /** * 将网格坐标转为像素坐标的碰撞矩形 * @returns {{ x: number, y: number, width: number, height: number }} */ getRect() { return { x: this.gridX * (this.size + GRID_GAP) + GRID_GAP, y: this.gridY * (this.size + GRID_GAP) + GRID_GAP, width: this.size, height: this.size }; } /** * 根据 count 计算当前颜色 * @returns {string} 十六进制颜色字符串 */ getColor() { return getColor(this.count); } /** * 判断方块是否已被消除 * @returns {boolean} */ isDestroyed() { return this.count <= 0; } /** * 回合结束时方块向下移动一行 */ moveDown() { this.gridY += 1; } }