All files / blacktrigram/src/utils spriteUtils.ts

0% Statements 0/92
0% Branches 0/1
0% Functions 0/1
0% Lines 0/92

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                                                                                                                                                                                                                                                                                                                                                   
import { playerSpritesheet } from "@/assets/spritesheets/PlayerSpritesheet";
import { PlayerArchetype, TrigramStance } from "@/types/common";
 
/**
 * Utility functions for sprite management in combat and training
 */
 
export type MovementDirection =
  | "north"
  | "northeast"
  | "east"
  | "southeast"
  | "south"
  | "southwest"
  | "west"
  | "northwest";
 
export type AnimationState =
  | "idle"
  | "walk"
  | "run"
  | "attack"
  | "defend"
  | "hit"
  | "block"
  | "dodge"
  | "stance_change"
  | "technique_windup"
  | "technique_execute"
  | "technique_recover"
  | "knocked_down"
  | "getting_up"
  | "stunned"
  | "victory"
  | "defeat";
 
/**
 * Calculate movement direction based on velocity
 */
export function getMovementDirection(
  velocityX: number,
  velocityY: number
): MovementDirection {
  const angle = Math.atan2(velocityY, velocityX);
  const degrees = ((angle * 180) / Math.PI + 360) % 360;
 
  if (degrees >= 337.5 || degrees < 22.5) return "east";
  if (degrees >= 22.5 && degrees < 67.5) return "southeast";
  if (degrees >= 67.5 && degrees < 112.5) return "south";
  if (degrees >= 112.5 && degrees < 157.5) return "southwest";
  if (degrees >= 157.5 && degrees < 202.5) return "west";
  if (degrees >= 202.5 && degrees < 247.5) return "northwest";
  if (degrees >= 247.5 && degrees < 292.5) return "north";
  return "northeast";
}
 
/**
 * Get appropriate animation for player state
 */
export function getPlayerAnimation(
  archetype: PlayerArchetype,
  animationState: AnimationState,
  movementDirection?: MovementDirection,
  currentStance?: TrigramStance,
  isMoving: boolean = false
) {
  // Determine base animation type
  let baseAnimation: AnimationState = animationState;
 
  // Override with movement if player is moving
  if (isMoving && (animationState === "idle" || animationState === "walk")) {
    baseAnimation = "walk";
  }
 
  // Get direction for directional animations
  const direction = movementDirection || "south";
 
  // Get stance for stance-based animations
  const stance = currentStance || TrigramStance.GEON;
 
  // Fetch animation from spritesheet
  let animation = null;
 
  if (["idle", "walk", "run", "dodge"].includes(baseAnimation)) {
    animation = playerSpritesheet.getAnimation(
      archetype,
      baseAnimation,
      direction
    );
  } else if (
    [
      "attack",
      "stance_change",
      "technique_windup",
      "technique_execute",
      "technique_recover",
    ].includes(baseAnimation)
  ) {
    animation = playerSpritesheet.getAnimation(
      archetype,
      baseAnimation,
      undefined,
      stance
    );
  } else {
    animation = playerSpritesheet.getAnimation(archetype, baseAnimation);
  }
 
  return (
    animation || playerSpritesheet.getAnimation(archetype, "idle", "south")
  );
}
 
/**
 * Initialize all spritesheets for the game
 */
export async function initializeSpritesheets(): Promise<void> {
  console.log("Initializing player spritesheets...");
 
  // For now, using placeholders. In production, load actual files:
  const archetypes = [
    PlayerArchetype.MUSA,
    PlayerArchetype.AMSALJA,
    PlayerArchetype.HACKER,
    PlayerArchetype.JEONGBO_YOWON,
    PlayerArchetype.JOJIK_POKRYEOKBAE,
  ];
 
  for (const archetype of archetypes) {
    try {
      // await playerSpritesheet.loadArchetypeSpritesheetFromFile(
      //   archetype,
      //   `/assets/spritesheets/${archetype}_spritesheet.json`
      // );
      console.log(`✓ Loaded spritesheet for ${archetype}`);
    } catch (error) {
      console.warn(`⚠ Using placeholder for ${archetype}:`, error);
    }
  }
 
  console.log("Spritesheet initialization complete");
}
 
/**
 * Get facing direction based on player positions (for combat)
 */
export function getFacingDirection(
  player1X: number,
  player2X: number
): "left" | "right" {
  return player1X < player2X ? "right" : "left";
}
 
/**
 * Create PIXI.AnimatedSprite from animation data
 */
export function createAnimatedSpriteFromAnimation(animation: any): any {
  if (!animation || !animation.frames) return null;
 
  const textures = animation.frames.map((frame: any) => frame.texture);
 
  // This would create a PIXI.AnimatedSprite in actual implementation
  return {
    textures,
    animationSpeed: animation.speed,
    loop: animation.loop,
    anchor: animation.frames[0]?.anchor || { x: 0.5, y: 0.8 },
  };
}