|
| 1 | +import { PonyDisplay, PonyRenderProp, Pair, WHPair } from '../type/ponyEscape' |
| 2 | +import { Asset } from './asset' |
| 3 | +import { getContext2d } from '../util/getContext2d' |
| 4 | +import { white, darkCoal, black, coal } from './color' |
| 5 | +import { scaleToFitIn } from '../util/scaleToFitIn' |
| 6 | + |
| 7 | +export interface PonyDisplayProp { |
| 8 | + asset: Asset |
| 9 | + canvas: HTMLCanvasElement |
| 10 | +} |
| 11 | + |
| 12 | +export interface ScorePosition { |
| 13 | + x: number |
| 14 | + y: number |
| 15 | + scale: number |
| 16 | +} |
| 17 | + |
| 18 | +export let createDisplay = (prop: PonyDisplayProp): PonyDisplay => { |
| 19 | + let { asset, canvas } = prop |
| 20 | + let ctx = getContext2d(canvas) |
| 21 | + |
| 22 | + let getGridSize = (grid: PonyRenderProp['grid']): Pair => { |
| 23 | + if (!(grid.length > 0)) throw new Error() |
| 24 | + |
| 25 | + return { |
| 26 | + x: grid[0].length, |
| 27 | + y: grid.length, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + let getSquareSize = (gridSize: Pair): Pair => { |
| 32 | + return { |
| 33 | + x: 800 / gridSize.x, |
| 34 | + y: 600 / gridSize.y, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + let renderGrid = async (grid: PonyRenderProp['grid'], gridSize: Pair) => { |
| 39 | + let image = new ImageData(gridSize.x, gridSize.y) |
| 40 | + let { data } = image |
| 41 | + |
| 42 | + grid.forEach((line, ky) => { |
| 43 | + let kky = 4 * line.length * ky |
| 44 | + line.forEach((square, kx) => { |
| 45 | + let kk = kky + 4 * kx |
| 46 | + let color: number[] |
| 47 | + if (square.type === 'exterior') { |
| 48 | + color = black |
| 49 | + } else if (square.type === 'ground') { |
| 50 | + color = coal |
| 51 | + } else if (square.visibility === 'invisible') { |
| 52 | + color = darkCoal |
| 53 | + } else if (square.filled === 'filled') { |
| 54 | + color = white |
| 55 | + } else { |
| 56 | + color = coal |
| 57 | + } |
| 58 | + ;[data[kk + 0], data[kk + 1], data[kk + 2]] = color |
| 59 | + data[kk + 3] = 255 |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + let bitmap = await createImageBitmap(image) |
| 64 | + ctx.imageSmoothingEnabled = false |
| 65 | + ctx.drawImage(bitmap, 0, 0, 800, 600) |
| 66 | + } |
| 67 | + |
| 68 | + let renderCharacter = ( |
| 69 | + character: Pair, |
| 70 | + squareSize: Pair, |
| 71 | + which: 'player' | 'monster', |
| 72 | + ) => { |
| 73 | + let image = asset[which] |
| 74 | + |
| 75 | + let fit = scaleToFitIn(image, squareSize) |
| 76 | + |
| 77 | + let dx = character.x * squareSize.x |
| 78 | + let dy = character.y * squareSize.y |
| 79 | + ctx.strokeStyle = 'red' |
| 80 | + ctx.strokeRect(dx, dy, squareSize.x, squareSize.y) |
| 81 | + ctx.drawImage(image, dx + fit.x, dy + fit.y, fit.w, fit.h) |
| 82 | + } |
| 83 | + |
| 84 | + let renderScore = (score: number, scorePos: ScorePosition) => { |
| 85 | + let { x, y, scale } = scorePos |
| 86 | + |
| 87 | + ctx.font = `${32 * scale}px Impact` |
| 88 | + ctx.fillStyle = 'white' |
| 89 | + ctx.strokeStyle = 'black' |
| 90 | + |
| 91 | + let text = `${score}` |
| 92 | + ctx.fillText(text, x, y) |
| 93 | + ctx.strokeText(text, x, y) |
| 94 | + } |
| 95 | + |
| 96 | + let render = async (prop: PonyRenderProp): Promise<undefined> => { |
| 97 | + if (prop.screen === 'score') { |
| 98 | + renderScore(prop.score, { x: 350, y: 250, scale: 2 }) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + let gridSize = getGridSize(prop.grid) |
| 103 | + let squareSize = getSquareSize(gridSize) |
| 104 | + |
| 105 | + await renderGrid(prop.grid, gridSize) |
| 106 | + renderCharacter(prop.player, squareSize, 'player') |
| 107 | + if (prop.monster !== undefined) { |
| 108 | + renderCharacter(prop.monster, squareSize, 'monster') |
| 109 | + } |
| 110 | + renderScore(prop.score, { x: 50, y: 540, scale: 1 }) |
| 111 | + } |
| 112 | + |
| 113 | + let me: PonyDisplay = { |
| 114 | + render, |
| 115 | + } |
| 116 | + |
| 117 | + return me |
| 118 | +} |
0 commit comments