All files / systems/animation FacialExpressions.ts

91.3% Statements 63/69
91.3% Branches 42/46
100% Functions 8/8
91.3% Lines 63/69

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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424                                                                                        6x                                                                                       6x                   78x 1x       77x 2x       75x 1x       74x 1x       73x 73x 70x       3x                           6x           3x 1x     2x                                     6x         4x 1x       3x 3x     3x 1x                   2x                                                           6x         19x 19x   19x     14x       14x       14x       1x       1x       1x         2x       2x       2x     1x       1x                                                   1x       1x       1x       19x         19x                         6x         3x 1x       2x 2x                                                     6x         6x     1x       1x       1x       1x       1x         1x                       6x 1x                     6x 72x    
/**
 * Facial expression system for realistic combat emotion
 * 
 * Manages facial expression state based on combat conditions:
 * - Health, stamina, pain, consciousness levels
 * - Recent combat events (hits taken/landed)
 * - Dynamic expression transitions
 * 
 * @module systems/animation/FacialExpressions
 * @category Animation System
 * @korean 얼굴표정시스템
 */
 
import {
  FacialExpression,
  type ExpressionState,
  type FacialDamageState,
  DEFAULT_FACIAL_DAMAGE,
  DEFAULT_EXPRESSION_STATE,
} from "../../types/facial";
 
/**
 * Expression transition configuration
 * 
 * @public
 * @korean 표정전환설정
 */
export interface ExpressionTransitionConfig {
  /** Default transition time in seconds */
  readonly defaultTransitionTime: number;
  
  /** Quick transition time for immediate reactions */
  readonly quickTransitionTime: number;
  
  /** Slow transition time for gradual changes */
  readonly slowTransitionTime: number;
}
 
/**
 * Default expression transition configuration
 * 
 * @public
 * @korean 기본표정전환설정
 */
export const DEFAULT_TRANSITION_CONFIG: ExpressionTransitionConfig = {
  defaultTransitionTime: 0.2,
  quickTransitionTime: 0.1,
  slowTransitionTime: 0.5,
};
 
/**
 * Get facial expression based on current combat state
 * 
 * Evaluates fighter's physical and mental state to determine appropriate
 * facial expression. Priority order:
 * 1. Defeated (unconscious)
 * 2. Pained (just hit)
 * 3. Exhausted (low stamina)
 * 4. Victorious (just landed hit)
 * 5. Focused (high resources)
 * 6. Neutral (default)
 * 
 * @param health - Current health (0-100)
 * @param maxHealth - Maximum health
 * @param stamina - Current stamina (0-100)
 * @param pain - Pain level (0-100)
 * @param consciousness - Consciousness level (0-100)
 * @param justHit - Whether fighter was just hit
 * @param justLanded - Whether fighter just landed a hit
 * @returns Appropriate facial expression for combat state
 * 
 * @example
 * ```typescript
 * const expression = getExpressionFromCombatState(
 *   85,  // health
 *   100, // maxHealth
 *   60,  // stamina
 *   20,  // pain
 *   100, // consciousness
 *   false, // justHit
 *   true   // justLanded
 * );
 * // Returns: FacialExpression.VICTORIOUS
 * ```
 * 
 * @public
 * @korean 전투상태로부터표정가져오기
 */
export const getExpressionFromCombatState = (
  health: number,
  maxHealth: number,
  stamina: number,
  _pain: number, // Reserved for future intensity calculation
  consciousness: number,
  justHit: boolean,
  justLanded: boolean
): FacialExpression => {
  // Knocked out
  if (consciousness < 20) {
    return FacialExpression.DEFEATED;
  }
 
  // Just got hit (always show immediate reaction; pain level can control intensity)
  if (justHit) {
    return FacialExpression.PAINED;
  }
 
  // Low stamina (exhausted)
  if (stamina < 30) {
    return FacialExpression.EXHAUSTED;
  }
 
  // Just landed a hit (brief satisfaction)
  if (justLanded) {
    return FacialExpression.VICTORIOUS;
  }
 
  // High focus (ready to fight)
  const healthPercentage = (health / maxHealth) * 100;
  if (stamina > 70 && healthPercentage > 60) {
    return FacialExpression.FOCUSED;
  }
 
  // Default calm state
  return FacialExpression.NEUTRAL;
};
 
/**
 * Create new expression state with transition
 * 
 * @param currentState - Current expression state
 * @param newExpression - New expression to transition to
 * @param transitionTime - Time to transition (seconds)
 * @returns New expression state with transition initialized
 * 
 * @public
 * @korean 새표정상태생성
 */
export const createExpressionTransition = (
  currentState: ExpressionState,
  newExpression: FacialExpression,
  transitionTime?: number
): ExpressionState => {
  // No transition needed if same expression
  if (currentState.expression === newExpression) {
    return currentState;
  }
 
  return {
    expression: newExpression,
    intensity: 1.0,
    transitionTime: transitionTime ?? DEFAULT_TRANSITION_CONFIG.defaultTransitionTime,
    previousExpression: currentState.expression,
    transitionProgress: 0,
  };
};
 
/**
 * Update expression state during transition
 * 
 * @param state - Current expression state
 * @param deltaTime - Time since last update (seconds)
 * @returns Updated expression state with transition progress
 * 
 * @public
 * @korean 표정상태업데이트
 */
export const updateExpressionState = (
  state: ExpressionState,
  deltaTime: number
): ExpressionState => {
  // No transition in progress
  if (state.transitionProgress === undefined || state.transitionProgress >= 1.0) {
    return state;
  }
 
  // Calculate new progress
  const progressIncrement = deltaTime / state.transitionTime;
  const newProgress = Math.min(state.transitionProgress + progressIncrement, 1.0);
 
  // Transition complete
  if (newProgress >= 1.0) {
    return {
      expression: state.expression,
      intensity: state.intensity,
      transitionTime: state.transitionTime,
      previousExpression: undefined,
      transitionProgress: undefined,
    };
  }
 
  // Transition in progress
  return {
    ...state,
    transitionProgress: newProgress,
  };
};
 
/**
 * Calculate facial damage from hit
 * 
 * Updates facial damage state based on hit location and damage amount.
 * Different facial regions accumulate damage independently.
 * 
 * @param currentDamage - Current facial damage state
 * @param hitLocation - Location of hit on face ("left_eye", "right_eye", "mouth", "nose", etc.)
 * @param damageAmount - Amount of damage dealt (0-100)
 * @returns Updated facial damage state
 * 
 * @example
 * ```typescript
 * const damage = calculateFacialDamage(
 *   DEFAULT_FACIAL_DAMAGE,
 *   "left_eye",
 *   25
 * );
 * // Returns damage state with left eye swelling increased
 * ```
 * 
 * @public
 * @korean 얼굴손상계산
 */
export const calculateFacialDamage = (
  currentDamage: FacialDamageState,
  hitLocation: string,
  damageAmount: number
): FacialDamageState => {
  const damageIntensity = Math.min(damageAmount / 100, 1.0);
  const newDamage = { ...currentDamage };
 
  switch (hitLocation.toLowerCase()) {
    case "left_eye":
    case "temple_left":
      newDamage.leftEyeSwelling = Math.min(
        currentDamage.leftEyeSwelling + damageIntensity * 0.3,
        1.0
      );
      newDamage.leftCheekBruise = Math.min(
        currentDamage.leftCheekBruise + damageIntensity * 0.2,
        1.0
      );
      break;
 
    case "right_eye":
    case "temple_right":
      newDamage.rightEyeSwelling = Math.min(
        currentDamage.rightEyeSwelling + damageIntensity * 0.3,
        1.0
      );
      newDamage.rightCheekBruise = Math.min(
        currentDamage.rightCheekBruise + damageIntensity * 0.2,
        1.0
      );
      break;
 
    case "mouth":
    case "jaw":
    case "chin":
      newDamage.mouthBleeding = Math.min(
        currentDamage.mouthBleeding + damageIntensity * 0.4,
        1.0
      );
      newDamage.jawBruise = Math.min(
        currentDamage.jawBruise + damageIntensity * 0.3,
        1.0
      );
      break;
 
    case "nose":
      newDamage.noseBleeding = Math.min(
        currentDamage.noseBleeding + damageIntensity * 0.5,
        1.0
      );
      break;
 
    case "forehead":
    case "crown":
      newDamage.foreheadBruise = Math.min(
        currentDamage.foreheadBruise + damageIntensity * 0.3,
        1.0
      );
      break;
 
    case "cheek_left":
      newDamage.leftCheekBruise = Math.min(
        currentDamage.leftCheekBruise + damageIntensity * 0.4,
        1.0
      );
      break;
 
    case "cheek_right":
      newDamage.rightCheekBruise = Math.min(
        currentDamage.rightCheekBruise + damageIntensity * 0.4,
        1.0
      );
      break;
 
    default:
      // General facial hit - add minor bruising
      newDamage.leftCheekBruise = Math.min(
        currentDamage.leftCheekBruise + damageIntensity * 0.1,
        1.0
      );
      newDamage.rightCheekBruise = Math.min(
        currentDamage.rightCheekBruise + damageIntensity * 0.1,
        1.0
      );
      break;
  }
 
  // Update total facial damage
  newDamage.totalFacialDamage = Math.min(
    currentDamage.totalFacialDamage + damageAmount,
    100
  );
 
  return newDamage;
};
 
/**
 * Reset facial damage (for new round or healing)
 * 
 * @param partialReset - If true, only reduces damage by percentage (healing)
 * @param resetPercentage - Percentage to reduce damage (0-1)
 * @returns Fresh facial damage state
 * 
 * @public
 * @korean 얼굴손상초기화
 */
export const resetFacialDamage = (
  currentDamage: FacialDamageState,
  partialReset = false,
  resetPercentage = 1.0
): FacialDamageState => {
  if (!partialReset) {
    return DEFAULT_FACIAL_DAMAGE;
  }
 
  // Partial healing - reduce all damage values
  const healingFactor = 1.0 - resetPercentage;
  return {
    leftEyeSwelling: currentDamage.leftEyeSwelling * healingFactor,
    rightEyeSwelling: currentDamage.rightEyeSwelling * healingFactor,
    mouthBleeding: currentDamage.mouthBleeding * healingFactor,
    noseBleeding: currentDamage.noseBleeding * healingFactor,
    leftCheekBruise: currentDamage.leftCheekBruise * healingFactor,
    rightCheekBruise: currentDamage.rightCheekBruise * healingFactor,
    foreheadBruise: currentDamage.foreheadBruise * healingFactor,
    jawBruise: currentDamage.jawBruise * healingFactor,
    totalFacialDamage: currentDamage.totalFacialDamage * healingFactor,
  };
};
 
/**
 * Get expression intensity based on combat state
 * 
 * Expression intensity affects degree of facial movement.
 * Higher pain/damage = more intense expressions.
 * 
 * @param expression - Current facial expression
 * @param pain - Pain level (0-100)
 * @param stamina - Stamina level (0-100)
 * @returns Expression intensity (0-1)
 * 
 * @public
 * @korean 표정강도가져오기
 */
export const getExpressionIntensity = (
  expression: FacialExpression,
  pain: number,
  stamina: number
): number => {
  switch (expression) {
    case FacialExpression.PAINED:
      // Higher pain = more intense pained expression
      return Math.min(pain / 100, 1.0);
 
    case FacialExpression.EXHAUSTED:
      // Lower stamina = more intense exhaustion
      return Math.min(1.0 - stamina / 100, 1.0);
 
    case FacialExpression.FOCUSED:
      // High stamina = more intense focus
      return Math.min(stamina / 100, 1.0);
 
    case FacialExpression.VICTORIOUS:
      // Brief, moderate intensity
      return 0.7;
 
    case FacialExpression.DEFEATED:
      // Full intensity (unconscious)
      return 1.0;
 
    case FacialExpression.NEUTRAL:
    default:
      // Default intensity
      return 1.0;
  }
};
 
/**
 * Create default expression state
 * 
 * @returns Default neutral expression state
 * 
 * @public
 * @korean 기본표정상태생성
 */
export const createDefaultExpressionState = (): ExpressionState => {
  return { ...DEFAULT_EXPRESSION_STATE };
};
 
/**
 * Create default facial damage state
 * 
 * @returns Default facial damage state with no damage
 * 
 * @public
 * @korean 기본얼굴손상생성
 */
export const createDefaultFacialDamage = (): FacialDamageState => {
  return { ...DEFAULT_FACIAL_DAMAGE };
};