All files / src/components/world/entities/scenes/cursor/modes paintCursorHelpers.ts

72.5% Statements 29/40
41.67% Branches 5/12
50% Functions 2/4
67.65% Lines 23/34

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 961x     1x                               1x       2x 1x     1x 1x 1x 1x 1x 1x 1x               1x                                                       1x                   3x 3x 3x   3x 5x 10x   10x 1x         2x    
import { BRUSH_16PX, TILE_SIZE } from "consts";
import type { Brush } from "store/features/editor/editorState";
 
export const paintCursorSize = (brush: Brush) =>
  brush === BRUSH_16PX ? TILE_SIZE * 2 : TILE_SIZE;
 
export interface AxisLockState {
  lockX?: boolean;
  lockY?: boolean;
}
 
export interface PaintInteractionSceneState extends AxisLockState {
  sceneId?: string;
  startX?: number;
  startY?: number;
  currentX?: number;
  currentY?: number;
}
 
export const resetPaintInteractionForScene = (
  state: PaintInteractionSceneState,
  sceneId: string,
) => {
  if (state.sceneId === sceneId) {
    return;
  }
 
  state.sceneId = sceneId;
  state.startX = undefined;
  state.startY = undefined;
  state.currentX = undefined;
  state.currentY = undefined;
  state.lockX = undefined;
  state.lockY = undefined;
};
 
export interface AxisLockedLine extends AxisLockState {
  endX: number;
  endY: number;
}
 
export const resolveAxisLockedLine = (
  state: AxisLockState,
  startX: number,
  startY: number,
  x: number,
  y: number,
): AxisLockedLine => {
  const { lockX, lockY } = state;
 
  Iif (lockX) {
    return { lockX, lockY, endX: startX, endY: y };
  }
 
  Iif (lockY) {
    return { lockX, lockY, endX: x, endY: startY };
  }
 
  Iif (x !== startX) {
    return { lockX, lockY: true, endX: x, endY: startY };
  }
 
  Iif (y !== startY) {
    return { lockX: true, lockY, endX: startX, endY: y };
  }
 
  return { lockX, lockY, endX: x, endY: y };
};
 
export const shouldPaintCollisionBrush = (
  collisions: readonly number[],
  sceneWidth: number,
  sceneHeight: number,
  x: number,
  y: number,
  brushSize: number,
  tile: number,
  mask: number,
): boolean => {
  const expectedTile = tile & mask;
  const endX = Math.min(x + brushSize, sceneWidth);
  const endY = Math.min(y + brushSize, sceneHeight);
 
  for (let xi = x; xi < endX; xi++) {
    for (let yi = y; yi < endY; yi++) {
      const currentTile = collisions[sceneWidth * yi + xi] ?? 0;
 
      if ((currentTile & mask) !== expectedTile) {
        return true;
      }
    }
  }
 
  return false;
};