| import { BLOCK_SIZE } from '../constants.js'; |
|
|
| |
| |
| |
| export default class BlockRenderer { |
| |
| |
| |
| |
| |
| |
| |
| static createBlockTexture(scene, color, level, key) { |
| const graphics = scene.make.graphics({ x: 0, y: 0, add: false }); |
| |
| |
| const r = (color >> 16) & 0xFF; |
| const g = (color >> 8) & 0xFF; |
| const b = color & 0xFF; |
| |
| |
| const lightColor = Phaser.Display.Color.GetColor( |
| Math.min(255, r + 60), |
| Math.min(255, g + 60), |
| Math.min(255, b + 60) |
| ); |
| const darkColor = Phaser.Display.Color.GetColor( |
| Math.max(0, r - 60), |
| Math.max(0, g - 60), |
| Math.max(0, b - 60) |
| ); |
| |
| |
| const style = (level - 1) % 5; |
| |
| switch (style) { |
| case 0: |
| this.drawClassicBlock(graphics, color, lightColor, darkColor); |
| break; |
| case 1: |
| this.drawGradientBlock(graphics, color, lightColor, darkColor); |
| break; |
| case 2: |
| this.drawDottedBlock(graphics, color, lightColor); |
| break; |
| case 3: |
| this.drawCheckeredBlock(graphics, color, darkColor); |
| break; |
| case 4: |
| this.drawOutlinedBlock(graphics, color, lightColor, darkColor); |
| break; |
| } |
| |
| graphics.generateTexture(key, BLOCK_SIZE, BLOCK_SIZE); |
| graphics.destroy(); |
| } |
| |
| static drawClassicBlock(graphics, color, lightColor, darkColor) { |
| |
| graphics.fillStyle(color); |
| graphics.fillRect(0, 0, BLOCK_SIZE, BLOCK_SIZE); |
| |
| |
| graphics.fillStyle(lightColor); |
| graphics.fillRect(0, 0, BLOCK_SIZE, 1); |
| graphics.fillRect(0, 0, 1, BLOCK_SIZE); |
| |
| |
| graphics.fillStyle(darkColor); |
| graphics.fillRect(0, BLOCK_SIZE - 1, BLOCK_SIZE, 1); |
| graphics.fillRect(BLOCK_SIZE - 1, 0, 1, BLOCK_SIZE); |
| } |
| |
| static drawGradientBlock(graphics, color, lightColor, darkColor) { |
| |
| for (let y = 0; y < BLOCK_SIZE; y++) { |
| const ratio = y / BLOCK_SIZE; |
| const r = Phaser.Math.Linear((lightColor >> 16) & 0xFF, (darkColor >> 16) & 0xFF, ratio); |
| const g = Phaser.Math.Linear((lightColor >> 8) & 0xFF, (darkColor >> 8) & 0xFF, ratio); |
| const b = Phaser.Math.Linear(lightColor & 0xFF, darkColor & 0xFF, ratio); |
| graphics.fillStyle(Phaser.Display.Color.GetColor(r, g, b)); |
| graphics.fillRect(0, y, BLOCK_SIZE, 1); |
| } |
| } |
| |
| static drawDottedBlock(graphics, color, lightColor) { |
| graphics.fillStyle(color); |
| graphics.fillRect(0, 0, BLOCK_SIZE, BLOCK_SIZE); |
| |
| |
| graphics.fillStyle(lightColor); |
| for (let y = 1; y < BLOCK_SIZE; y += 3) { |
| for (let x = 1; x < BLOCK_SIZE; x += 3) { |
| graphics.fillRect(x, y, 1, 1); |
| } |
| } |
| } |
| |
| static drawCheckeredBlock(graphics, color, darkColor) { |
| for (let y = 0; y < BLOCK_SIZE; y += 2) { |
| for (let x = 0; x < BLOCK_SIZE; x += 2) { |
| const useMain = (x + y) % 4 === 0; |
| graphics.fillStyle(useMain ? color : darkColor); |
| graphics.fillRect(x, y, 2, 2); |
| } |
| } |
| } |
| |
| static drawOutlinedBlock(graphics, color, lightColor, darkColor) { |
| |
| graphics.fillStyle(color); |
| graphics.fillRect(1, 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2); |
| |
| |
| graphics.fillStyle(darkColor); |
| graphics.fillRect(0, 0, BLOCK_SIZE, 1); |
| graphics.fillRect(0, 0, 1, BLOCK_SIZE); |
| graphics.fillRect(0, BLOCK_SIZE - 1, BLOCK_SIZE, 1); |
| graphics.fillRect(BLOCK_SIZE - 1, 0, 1, BLOCK_SIZE); |
| |
| |
| graphics.fillStyle(lightColor); |
| graphics.fillRect(2, 2, BLOCK_SIZE - 4, 1); |
| graphics.fillRect(2, 2, 1, BLOCK_SIZE - 4); |
| } |
| } |
|
|
|
|