Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | import React, { useEffect, useRef } from "react"; import { useAppSelector } from "store/hooks"; import { CollisionTileDef } from "shared/lib/resources/types"; import { defaultCollisionTileColor, defaultCollisionTileIcon, defaultCollisionTileDefs, } from "consts"; import { isCollisionTileActive, renderCollisionTileIcon, } from "shared/lib/collisions/collisionTiles"; import { decHexVal } from "shared/lib/helpers/8bit"; const TILE_SIZE = 16; interface SceneCollisionsProps { width: number; height: number; collisions: number[]; sceneTypeKey: string; } const SceneCollisions = ({ width, height, collisions, sceneTypeKey, }: SceneCollisionsProps) => { const canvas = useRef<HTMLCanvasElement>(null); const showCollisionTileValues = useAppSelector( (state) => state.project.present.settings.showCollisionTileValues ); const collisionLayerOpacity = useAppSelector( (state) => Math.floor(state.project.present.settings.collisionLayerOpacity) / 100 ); const collisionTileDefs = useAppSelector((state) => { const sceneType = state.engine.sceneTypes.find( (s) => s.key === sceneTypeKey ); Iif (sceneType && sceneType.collisionTiles) return sceneType.collisionTiles; return defaultCollisionTileDefs; }); const drawCollisionTile = ( tile: CollisionTileDef, ctx: CanvasRenderingContext2D, xi: number, yi: number ) => { const tileIcon = renderCollisionTileIcon( tile.icon ?? defaultCollisionTileIcon, tile.color ?? defaultCollisionTileColor ); ctx.drawImage( tileIcon, 0, 0, 8, 8, xi * TILE_SIZE, yi * TILE_SIZE, TILE_SIZE, TILE_SIZE ); }; const drawLetter = ( letter: string, ctx: CanvasRenderingContext2D, x: number, y: number ) => { ctx.textBaseline = "middle"; const tx = x * TILE_SIZE; const ty = (y + 0.5) * TILE_SIZE; ctx.strokeStyle = "black"; ctx.lineWidth = 2; ctx.strokeText(letter, tx, ty); ctx.fillStyle = "white"; ctx.fillText(letter, tx, ty); }; useEffect(() => { Iif (canvas.current) { // eslint-disable-next-line no-self-assign canvas.current.width = canvas.current.width; // Clear canvas const ctx = canvas.current.getContext("2d"); Iif (!ctx) return; ctx.font = "8px Public Pixel"; ctx.imageSmoothingEnabled = false; const sortedTileDefs = collisionTileDefs.map((t) => t); sortedTileDefs.sort((a, b) => { if (a.mask) { if (b.mask) { const aCount = a.mask.toString(2).split("1").length - 1; const bCount = b.mask.toString(2).split("1").length - 1; if (aCount > bCount) return -1; else Iif (bCount > aCount) return 1; } else return 1; } else Iif (b.mask) return -1; const aCount = a.flag.toString(2).split("1").length - 1; const bCount = b.flag.toString(2).split("1").length - 1; return bCount - aCount; }); for (let yi = 0; yi < height; yi++) { for (let xi = 0; xi < width; xi++) { const collisionIndex = width * yi + xi; const tile = collisions[collisionIndex] ?? 0; let unknownTile = tile !== 0; for (const tileDef of sortedTileDefs) { Iif (isCollisionTileActive(tile, tileDef)) { ctx.fillStyle = tileDef.color; drawCollisionTile(tileDef, ctx, xi, yi); Iif (tileDef.icon) { unknownTile = false; } } } Iif ( unknownTile || (showCollisionTileValues && tile !== 0 && tile !== undefined) ) { drawLetter(decHexVal(tile), ctx, xi, yi); } } } } }, [collisionTileDefs, collisions, height, showCollisionTileValues, width]); return ( <canvas ref={canvas} width={width * TILE_SIZE} height={height * TILE_SIZE} style={{ opacity: collisionLayerOpacity, width: width * TILE_SIZE * 0.5, imageRendering: "pixelated", }} /> ); }; export default SceneCollisions; |