| 1 | import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 2 | |
| 3 | export const PLAY_IMAGES = [ |
| 4 | { name: "ground-grass", url: "/assets/grounds/ground-grass.png" }, |
| 5 | { name: "ground-wood", url: "/assets/grounds/ground-wood.png" }, |
| 6 | { name: "block-stone-wall", url: "/assets/blocks/block-stone-wall.png" }, |
| 7 | { name: "block-torch", url: "/assets/blocks/block-torch.png" }, |
| 8 | { name: "player-body-male", url: "/assets/characters/player-body-male.png" }, |
| 9 | { name: "player-body-female", url: "/assets/characters/player-body-female.png" }, |
| 10 | { name: "night-shade", url: shadeDataUrl() } |
| 11 | ]; |
| 12 | |
| 13 | export function shadeDataUrl () |
| 14 | { |
| 15 | const canvas = document.createElement("canvas"); |
| 16 | canvas.width = SPRITE_SIZE_PX; |
| 17 | canvas.height = SPRITE_SIZE_PX; |
| 18 | const context = canvas.getContext("2d"); |
| 19 | context.fillStyle = "#000000"; |
| 20 | context.fillRect(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 21 | return canvas.toDataURL("image/png"); |
| 22 | } |
| 23 | |
| 24 | export function assertSpriteSizes (images) |
| 25 | { |
| 26 | const offenders = []; |
| 27 | for (const [name, sprite] of Object.entries(images)) |
| 28 | { |
| 29 | if (sprite.width !== SPRITE_SIZE_PX || sprite.height !== SPRITE_SIZE_PX) |
| 30 | { |
| 31 | offenders.push(`${name} is ${sprite.width}×${sprite.height}`); |
| 32 | } |
| 33 | } |
| 34 | if (offenders.length) |
| 35 | { throw new Error(`World sprites must be ${SPRITE_SIZE_PX}×${SPRITE_SIZE_PX}. Offenders: ${offenders.join("; ")}`); } |
| 36 | } |
| 37 | |
| 38 | export function useNearestNeighbour (scene, images) |
| 39 | { |
| 40 | const gl = scene.glContext.gl; |
| 41 | for (const sprite of Object.values(images)) |
| 42 | { |
| 43 | const texture = scene.glContext.getTexture(sprite.g_image); |
| 44 | gl.bindTexture(gl.TEXTURE_2D, texture); |
| 45 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 46 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 47 | } |
| 48 | } |