All files / systems/combat SlowMotionController.ts

90% Statements 63/70
79.16% Branches 19/24
100% Functions 10/10
90% Lines 63/70

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                                                                                                                39x               6x                 33x 33x 33x 33x 33x 33x 33x 33x               25x   1x     24x 24x 24x 24x 24x 24x 24x                               97x 4x     93x     93x     93x 93x   93x   18x 18x 18x   75x   21x 21x 21x       54x       93x 5x 5x 5x 5x       93x                       7x   1x               1x       6x     6x     6x             6x         6x     6x 6x             6x       6x 6x                   7x                                 10x             2x 2x 2x 2x                 17x                   33x           1x                   1x            
/**
 * SlowMotionController.ts
 *
 * Slow-motion time dilation system for Li (Fire) trigram precision strikes.
 * Triggers dramatic slow-motion effects on vital point hits with camera zoom.
 *
 * Features:
 * - Time dilation (0.3x speed for 1-2 seconds)
 * - Smooth camera zoom to focus on vital point impact
 * - Easing functions for smooth transitions
 * - Integration with Three.js animation loop
 * - State management for slow-motion phases
 *
 * Performance: <0.1ms overhead per frame, no GC pressure
 *
 * @module systems/combat/SlowMotionController
 * @korean 슬로우모션컨트롤러
 */
 
import * as THREE from "three";
 
/**
 * Slow-motion effect configuration
 */
export interface SlowMotionConfig {
  /** Time dilation factor (0.0 = stopped, 1.0 = normal speed) */
  readonly timeDilation: number;
  /** Duration of slow-motion effect in seconds */
  readonly duration: number;
  /** Camera zoom multiplier (>1.0 = zoom in) */
  readonly cameraZoom: number;
  /** Focal point for camera zoom [x, y, z] */
  readonly focusPoint: readonly [number, number, number];
}
 
/**
 * Slow-motion effect state
 */
export interface SlowMotionState {
  /** Whether slow-motion is currently active */
  readonly active: boolean;
  /** Current time dilation factor */
  readonly currentTimeDilation: number;
  /** Elapsed time since effect started (seconds) */
  readonly elapsed: number;
  /** Total duration of effect (seconds) */
  readonly duration: number;
  /** Camera zoom progress (0.0 to 1.0) */
  readonly zoomProgress: number;
}
 
/**
 * Easing function for smooth transitions
 * Uses cubic ease-in-out for natural motion
 */
function easeInOutCubic(t: number): number {
  return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
}
 
/**
 * Easing function for camera zoom
 * Uses quadratic ease-in for dramatic effect
 */
function easeInQuad(t: number): number {
  return t * t;
}
 
/**
 * SlowMotionController Class
 *
 * Manages time dilation and camera effects for precision strike slow-motion
 */
export class SlowMotionController {
  private active: boolean = false;
  private elapsed: number = 0;
  private duration: number = 0;
  private timeDilation: number = 1.0;
  private currentTimeDilation: number = 1.0;
  private cameraZoom: number = 1.0;
  private focusPoint: THREE.Vector3 = new THREE.Vector3();
  private originalCameraPosition: THREE.Vector3 | null = null;
 
  /**
   * Trigger slow-motion effect
   *
   * @param config - Slow-motion configuration
   */
  public trigger(config: SlowMotionConfig): void {
    if (this.active) {
      // If already active, don't interrupt
      return;
    }
 
    this.active = true;
    this.elapsed = 0;
    this.duration = config.duration;
    this.timeDilation = Math.max(0.1, Math.min(1.0, config.timeDilation));
    this.currentTimeDilation = 1.0; // Will ease into slow-motion
    this.cameraZoom = Math.max(1.0, config.cameraZoom); // Store zoom multiplier
    this.focusPoint.set(
      config.focusPoint[0],
      config.focusPoint[1],
      config.focusPoint[2]
    );
  }
 
  /**
   * Update slow-motion effect
   *
   * Call this in your game loop / useFrame hook
   *
   * @param delta - Frame time delta (seconds)
   * @returns Modified delta accounting for time dilation
   */
  public update(delta: number): number {
    if (!this.active) {
      return delta; // No modification
    }
 
    this.elapsed += delta;
 
    // Calculate progress (0.0 to 1.0)
    const progress = Math.min(this.elapsed / this.duration, 1.0);
 
    // Split into ramp-in, hold, ramp-out phases
    const rampInDuration = 0.2; // 20% of duration for ramp-in
    const rampOutStart = 0.8; // Start ramp-out at 80%
 
    if (progress < rampInDuration) {
      // Ramp in: ease from 1.0 to target time dilation
      const rampProgress = progress / rampInDuration;
      const easedProgress = easeInOutCubic(rampProgress);
      this.currentTimeDilation =
        1.0 - easedProgress * (1.0 - this.timeDilation);
    } else if (progress > rampOutStart) {
      // Ramp out: ease from target time dilation back to 1.0
      const rampProgress = (progress - rampOutStart) / (1.0 - rampOutStart);
      const easedProgress = easeInOutCubic(rampProgress);
      this.currentTimeDilation =
        this.timeDilation + easedProgress * (1.0 - this.timeDilation);
    } else {
      // Hold: maintain target time dilation
      this.currentTimeDilation = this.timeDilation;
    }
 
    // Check if effect is complete
    if (progress >= 1.0) {
      this.active = false;
      this.currentTimeDilation = 1.0;
      this.cameraZoom = 1.0;
      this.originalCameraPosition = null;
    }
 
    // Return modified delta
    return delta * this.currentTimeDilation;
  }
 
  /**
   * Update camera position for zoom effect
   *
   * Call this in your game loop after update()
   *
   * @param camera - Three.js camera to modify
   * @param delta - Frame time delta (seconds)
   */
  public updateCamera(camera: THREE.Camera, delta: number): void {
    if (!this.active) {
      // Restore original camera position if we have it
      Iif (this.originalCameraPosition) {
        camera.position.lerp(this.originalCameraPosition, delta * 5);
        // Check if we're close enough to the original position
        if (camera.position.distanceTo(this.originalCameraPosition) < 0.01) {
          camera.position.copy(this.originalCameraPosition);
          this.originalCameraPosition = null;
        }
      }
      return;
    }
 
    // Store original camera position on first frame
    this.originalCameraPosition ??= camera.position.clone();
 
    // Calculate zoom progress
    const progress = Math.min(this.elapsed / this.duration, 1.0);
    
    // During ramp-out phase, start returning to original position
    Iif (progress > 0.8) {
      // Last 20% of duration - return to original
      const returnProgress = (progress - 0.8) / 0.2;
      const easedReturn = easeInOutCubic(returnProgress);
      camera.position.lerp(this.originalCameraPosition, easedReturn * delta * 5);
    } else {
      // Normal zoom behavior
      const zoomProgress = easeInQuad(Math.min(progress * 2, 1.0)); // Zoom faster than time dilation
      
      // Calculate zoom amount based on cameraZoom multiplier
      // cameraZoom = 1.5 means move 33% closer (1 - 1/1.5)
      // cameraZoom = 2.0 means move 50% closer (1 - 1/2.0)
      const zoomAmount = 1 - 1 / this.cameraZoom;
      
      // Calculate target camera position (closer to focus point)
      const targetPosition = new THREE.Vector3();
      targetPosition.lerpVectors(
        this.originalCameraPosition,
        this.focusPoint,
        zoomProgress * zoomAmount
      );
 
      // Smoothly interpolate camera position
      camera.position.lerp(targetPosition, delta * 5);
    }
 
    // Update camera to look at focus point
    Eif (camera instanceof THREE.PerspectiveCamera) {
      camera.lookAt(this.focusPoint);
    }
  }
 
  /**
   * Get current slow-motion state
   *
   * @returns Current state
   */
  public getState(): SlowMotionState {
    return {
      active: this.active,
      currentTimeDilation: this.currentTimeDilation,
      elapsed: this.elapsed,
      duration: this.duration,
      zoomProgress: this.active
        ? Math.min(this.elapsed / this.duration, 1.0)
        : 0,
    };
  }
 
  /**
   * Check if slow-motion is currently active
   *
   * @returns True if active
   */
  public isActive(): boolean {
    return this.active;
  }
 
  /**
   * Force stop slow-motion effect
   */
  public stop(): void {
    this.active = false;
    this.currentTimeDilation = 1.0;
    this.elapsed = 0;
    this.originalCameraPosition = null;
  }
 
  /**
   * Get current time dilation factor
   *
   * @returns Current time dilation (0.0 to 1.0)
   */
  public getTimeDilation(): number {
    return this.currentTimeDilation;
  }
}
 
/**
 * Create a new SlowMotionController instance
 *
 * @returns New controller instance
 */
export function createSlowMotionController(): SlowMotionController {
  return new SlowMotionController();
}
 
/**
 * Default slow-motion configuration for Li vital point hits
 */
export const LI_VITAL_POINT_SLOW_MOTION: SlowMotionConfig = {
  timeDilation: 0.3, // 30% speed (dramatic)
  duration: 1.5, // 1.5 seconds
  cameraZoom: 1.5, // 50% closer
  focusPoint: [0, 1.5, 0], // Mid-height focus (will be updated dynamically)
};
 
/**
 * Moderate slow-motion configuration for regular precision strikes
 */
export const LI_PRECISION_SLOW_MOTION: SlowMotionConfig = {
  timeDilation: 0.5, // 50% speed (moderate)
  duration: 1.0, // 1 second
  cameraZoom: 1.3, // 30% closer
  focusPoint: [0, 1.5, 0], // Mid-height focus
};