All files / src/shared/lib/sprites spriteData.ts

98.11% Statements 104/106
88.57% Branches 31/35
100% Functions 12/12
97.65% Lines 83/85

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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 1833x                         3x 3x 3x 3x 3x 3x 3x     3x 3584x   3584x 1969x 1615x 336x   1279x       3x           30x 30x 30x   30x 384x 3072x 3072x 3072x 3072x 3072x             192x 2880x   368x         30x     3x           30x 30x 30x 30x 384x 3072x 3072x 3072x 3072x         30x     3x       37x             37x 1190x         33x     4x     3x 30x 288x       28x     2x     3x       4x 4x 448x 96x     4x     3x     37x 37x 2368x 208x     37x           3x     13x 13x 13x 104x 104x 104x 832x 832x 832x 832x   104x 104x 104x 104x     13x         13x   832x 208x  
import {
  cloneIndexedImage,
  ImageIndexFunction,
  IndexedImage,
  toIndex,
} from "shared/lib/tiles/indexedImage";
 
export type OptimisedTile = {
  tile: number;
  flipX: boolean;
  flipY: boolean;
};
 
enum Color {
  Transparent = 0,
  Light = 1,
  Mid = 2,
  Dark = 3,
  Divider = 254,
  Unknown = 255,
}
 
export const spriteDataIndexFn: ImageIndexFunction = (r, g, b, _a) => {
  Iif ((g > 249 && r < 180 && b < 20) || (b >= 200 && g < 20) || _a < 200) {
    return Color.Transparent;
  } else if (g >= 205) {
    return Color.Light;
  } else if (g >= 130) {
    return Color.Mid;
  } else {
    return Color.Dark;
  }
};
 
export const removeIndexedImageMask = (
  inData: IndexedImage,
  maskData: IndexedImage,
  offsetX: number,
  offsetY: number,
): IndexedImage => {
  const output = cloneIndexedImage(inData);
  const inWidth = inData.width;
  const inHeight = inData.height;
 
  for (let y = 0; y < inHeight; y++) {
    for (let x = 0; x < inWidth; x++) {
      const drawIndex = toIndex(x, y, inData);
      const maskIndex = toIndex(x + offsetX, y + offsetY, maskData);
      const maskX = x + offsetX;
      const maskY = y + offsetY;
      if (
        maskX < 0 ||
        maskY < 0 ||
        maskX >= maskData.width ||
        maskY >= maskData.height
      ) {
        // Mark tile data outside of canvas bounds as unknown
        output.data[drawIndex] = Color.Unknown;
      } else if (maskData.data[maskIndex] !== Color.Transparent) {
        // Mark occluded tile data as unknown
        output.data[drawIndex] = Color.Unknown;
      }
    }
  }
 
  return output;
};
 
export const blitIndexedImageData = (
  canvasData: IndexedImage,
  inData: IndexedImage,
  offsetX: number,
  offsetY: number,
): IndexedImage => {
  const output = cloneIndexedImage(canvasData);
  const drawWidth = inData.width;
  const drawHeight = inData.height;
  for (let y = 0; y < drawHeight; y++) {
    for (let x = 0; x < drawWidth; x++) {
      const drawIndex = toIndex(x, y, inData);
      if (inData.data[drawIndex] !== Color.Transparent) {
        const canvasIndex = toIndex(x + offsetX, y + offsetY, canvasData);
        output.data[canvasIndex] = inData.data[drawIndex];
      }
    }
  }
 
  return output;
};
 
export const isIndexedImageEqual = (
  dataA: IndexedImage,
  dataB: IndexedImage,
): boolean => {
  Iif (
    dataA.width !== dataB.width ||
    dataA.height !== dataB.height ||
    dataA.data.length !== dataB.data.length
  ) {
    return false;
  }
  for (let i = 0; i < dataA.data.length; i++) {
    if (
      dataA.data[i] !== dataB.data[i] &&
      dataA.data[i] !== Color.Unknown &&
      dataB.data[i] !== Color.Unknown
    ) {
      return false;
    }
  }
  return true;
};
 
export const isBlankIndexedImage = (image: IndexedImage): boolean => {
  for (let i = 0; i < image.data.length; i++) {
    if (
      image.data[i] !== Color.Transparent &&
      image.data[i] !== Color.Unknown
    ) {
      return false;
    }
  }
  return true;
};
 
export const mergeIndexedImages = (
  dataA: IndexedImage,
  dataB: IndexedImage,
): IndexedImage => {
  const output = cloneIndexedImage(dataA);
  for (let i = 0; i < output.data.length; i++) {
    if (output.data[i] === Color.Unknown) {
      output.data[i] = dataB.data[i];
    }
  }
  return output;
};
 
export const indexedUnknownToTransparent = (
  inData: IndexedImage,
): IndexedImage => {
  const output = cloneIndexedImage(inData);
  for (let i = 0; i < output.data.length; i++) {
    if (output.data[i] === Color.Unknown) {
      output.data[i] = Color.Transparent;
    }
  }
  return output;
};
 
// ------------
// To 2bpp
 
export const indexedImageTo2bppSpriteData = (
  image: IndexedImage,
): Uint8Array => {
  const output = new Uint8Array(roundUp8((image.width * image.height) / 4));
  let i = 0;
  for (let y = 0; y < 8; y++) {
    let row1 = "";
    let row2 = "";
    for (let x = 0; x < 8; x++) {
      const index = toIndex(x, y, image);
      const binary = bin2(image.data[index]);
      row1 += binary[1];
      row2 += binary[0];
    }
    output[i] = binDec(row1);
    i++;
    output[i] = binDec(row2);
    i++;
  }
 
  return output;
};
 
// ------------
 
const roundUp8 = (x: number): number => Math.ceil(x / 8) * 8;
 
const bin2 = (value: Color) => value.toString(2).padStart(2, "0");
const binDec = (binary: string) => parseInt(binary, 2);