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

29.41% Statements 40/136
0% Branches 0/90
10% Functions 2/20
29.91% Lines 35/117

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291                                    4x                     4x             4x   4x                   4x       4x             4x                                                                     4x 4x 4x 4x 4x 4x 4x 4x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x         4x 4x   4x                                                                                                   4x                                                                                                                                                                             4x 1x                                 4x 1x                      
import type { SpriteAnimationType } from "shared/lib/entities/entitiesTypes";
 
export type AnimationType =
  | "idle"
  | "moving"
  | "idleLeft"
  | "idleRight"
  | "idleUp"
  | "idleDown"
  | "movingLeft"
  | "movingRight"
  | "movingUp"
  | "movingDown"
  | "jumpingLeft"
  | "jumpingRight"
  | "climbing"
  | "hover";
 
const animationTypes: AnimationType[] = [
  "idleRight",
  "idleLeft",
  "idleUp",
  "idleDown",
  "movingRight",
  "movingLeft",
  "movingUp",
  "movingDown",
];
 
const multiAnimationTypes: AnimationType[] = [
  "idleRight",
  "idleLeft",
  "idleUp",
  "idleDown",
];
 
const fixedAnimationTypes: AnimationType[] = ["idle", "moving"];
 
const platformAnimationTypes: AnimationType[] = [
  "idleRight",
  "idleLeft",
  "jumpingRight",
  "jumpingLeft",
  "movingRight",
  "movingLeft",
  "climbing",
];
 
const cursorAnimationTypes: AnimationType[] = ["idle", "hover"];
 
// Iso diagonal directions reuse existing AnimationType slots for simplicity:
// NE → idleRight, SE → idleDown, SW → idleLeft, NW → idleUp
const isoMovementAnimationTypes: AnimationType[] = [
  "idleRight",  // NE
  "idleDown",   // SE
  "idleLeft",   // SW
  "idleUp",     // NW
];
 
export const getAnimationTypeByIndex = (
  type: SpriteAnimationType,
  flipLeft: boolean,
  animationIndex: number,
): AnimationType => {
  Iif (type === "iso_fixed") {
    return "idle";
  }
  Iif (type === "iso_movement") {
    return isoMovementAnimationTypes[animationIndex] ?? "idle";
  }
  Iif (type === "fixed" || type === "fixed_movement") {
    return fixedAnimationTypes[animationIndex];
  }
  Iif (type === "platform_player") {
    return filterAnimationsBySpriteType(platformAnimationTypes, type, flipLeft)[
      animationIndex
    ];
  }
  Iif (type === "cursor") {
    return filterAnimationsBySpriteType(cursorAnimationTypes, type, flipLeft)[
      animationIndex
    ];
  }
  Iif (type === "multi") {
    return filterAnimationsBySpriteType(multiAnimationTypes, type, flipLeft)[
      animationIndex
    ];
  }
  return filterAnimationsBySpriteType(animationTypes, type, flipLeft)[
    animationIndex
  ];
};
 
/* eslint-disable @typescript-eslint/no-unused-vars */
const ANIM_IDLE_RIGHT = 0;
const ANIM_IDLE_LEFT = 1;
const ANIM_IDLE_UP = 2;
const ANIM_IDLE_DOWN = 3;
const ANIM_MOVE_RIGHT = 4;
const ANIM_MOVE_LEFT = 5;
const ANIM_MOVE_UP = 6;
const ANIM_MOVE_DOWN = 7;
/* eslint-enable @typescript-eslint/no-unused-vars */
 
const fixedIndexes = [0];
const fixedMovementIndexes = [0, 4];
const multiIndexes = [0, 1, 2, 3];
const multiFlipIndexes = [0, 2, 3];
const horizontalIndexes = [0, 1];
const horizontalFlipIndexes = [0];
const horizontalMovementIndexes = [0, 1, 4, 5];
const horizontalMovementFlipIndexes = [0, 4];
const platformIndexes = [0, 1, 4, 5, 2, 3, 6];
const platformFlipIndexes = [0, 4, 2, 6];
const flipIndexes = [0, 2, 3, 4, 6, 7];
const cursorIndexes = [0, 1];
 
// Isometric: slot 0 = NE, 1 = SE, 2 = SW, 3 = NW
// iso_fixed    → single animation (all directions share slot 0)
// iso_movement → 4 animations, one per diagonal direction
const isoFixedIndexes = [0];
const isoMovementIndexes = [0, 1, 2, 3];
 
export const filterAnimationsBySpriteType = <T>(
  animationIds: T[],
  type: SpriteAnimationType,
  flipLeft: boolean,
): T[] => {
  Iif (type === "iso_fixed") {
    return isoFixedIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "iso_movement") {
    return isoMovementIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "fixed") {
    return fixedIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "fixed_movement") {
    return fixedMovementIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "multi" && !flipLeft) {
    return multiIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "multi" && flipLeft) {
    return multiFlipIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "horizontal" && !flipLeft) {
    return horizontalIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "horizontal" && flipLeft) {
    return horizontalFlipIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "horizontal_movement" && !flipLeft) {
    return horizontalMovementIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "horizontal_movement" && flipLeft) {
    return horizontalMovementFlipIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "platform_player" && !flipLeft) {
    return platformIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "platform_player" && flipLeft) {
    return platformFlipIndexes.map((i) => animationIds[i]);
  }
  Iif (type === "cursor") {
    return cursorIndexes.map((i) => animationIds[i]);
  }
  Iif (flipLeft) {
    return flipIndexes.map((i) => animationIds[i]);
  }
  return animationIds;
};
 
export const animationMapBySpriteType = <T, U>(
  items: T[],
  type: SpriteAnimationType,
  flipLeft: boolean,
  fn: (item: T | undefined, flip: boolean) => U,
): U[] => {
  return Array.from(Array(8)).map((_item, index) => {
    Iif (type === "iso_fixed") {
      return fn(items[0], false);
    }
    Iif (type === "iso_movement") {
      // 4 directions; clamp index to 0-3
      return fn(items[index % 4], false);
    }
    Iif (type === "fixed") {
      // All animations map to 0
      return fn(items[0], false);
    }
    Iif (type === "fixed_movement") {
      // Idle maps to 0, Moving maps to 4
      return fn(index < 4 ? items[0] : items[4], false);
    }
    Iif (type === "multi" && !flipLeft) {
      // Idle and moving map to first 4
      return fn(items[index % 4], false);
    }
    Iif (type === "multi" && flipLeft) {
      // Left facing maps to flipped 0
      // All other idle and moving map to first 4
      Iif (index === 1 || index === 5) {
        return fn(items[0], true);
      }
      return fn(items[index % 4], false);
    }
    Iif (type === "horizontal" && !flipLeft) {
      // All frames map to first 2 (right + left idle)
      return fn(items[index % 2], false);
    }
    Iif (type === "horizontal" && flipLeft) {
      // Left facing maps to flipped right
      return fn(items[ANIM_IDLE_RIGHT], index % 2 !== 0);
    }
    Iif (type === "horizontal_movement" && !flipLeft) {
      // All frames map to first 2 (right + left idle)
      // Moving maps to 4 and 5
      const isMoving = index >= ANIM_MOVE_RIGHT;
      return fn(items[(index % 2) + (isMoving ? ANIM_MOVE_RIGHT : 0)], false);
    }
    Iif (type === "horizontal_movement" && flipLeft) {
      // Left facing maps to flipped right
      // Moving maps to 4 and 5
      const isMoving = index >= ANIM_MOVE_RIGHT;
      return fn(items[isMoving ? ANIM_MOVE_RIGHT : 0], index % 2 !== 0);
    }
    Iif (type === "platform_player" && !flipLeft) {
      // Map all in order
      return fn(items[index], false);
    }
    Iif (type === "platform_player" && flipLeft) {
      // Left facing maps to previous animation (right) flipped
      // others map in order
      Iif (index === 1 || index === 5 || index === 3) {
        return fn(items[index - 1], true);
      }
      return fn(items[index], false);
    }
    Iif (type === "cursor") {
      // Flip order of hover and idle state
      if (index === 0) {
        return fn(items[1], false);
      } else {
        return fn(items[0], false);
      }
    }
    Iif (flipLeft) {
      // Left facing maps to previous animation (right) flipped
      // others map in order
      Iif (index === 1 || index === 5) {
        return fn(items[index - 1], true);
      }
      return fn(items[index], false);
    }
    // Map all in order
    return fn(items[index], false);
  });
};
 
export const toEngineOrder = <T>(arr: T[]): T[] => {
  return [
    arr[3], // Down
    arr[0], // Right
    arr[2], // Up
    arr[1], // Left
    arr[7], // Down Moving
    arr[4], // Right Moving
    arr[6], // Up Moving
    arr[5], // Left Moving
  ];
};
 
/**
 * Reorders the isometric editor slots (NE, SE, SW, NW) into the runtime's
 * animation table order (down/SW, right/SE, up/NE, left/NW), followed by the
 * corresponding moving slots.
 */
export const toIsometricEngineOrder = <T>(arr: T[]): T[] => {
  return [
    arr[2], // Down / SW
    arr[1], // Right / SE
    arr[0], // Up / NE
    arr[3], // Left / NW
    arr[6], // Down / SW moving
    arr[5], // Right / SE moving
    arr[4], // Up / NE moving
    arr[7], // Left / NW moving
  ];
};