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 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | 3844x 3844x 3844x 3844x 3844x 3844x 3844x 31298x 31298x 3392x 3392x 3844x 3844x 3844x 3844x 876x 876x 876x 876x 876x 876x 876x 872x 872x 875x 875x 62x 62x 874x 874x 3844x 3844x 3844x 3847x 3847x 875x 3x 1x 2x 5x 5x 4x 4x 2x 3x 3x 31x 31x 31x 31x 31x 31x | /**
* Animation Builder - Fluent API for creating skeletal animations
*
* Provides a cleaner, more maintainable way to define animations with
* reduced boilerplate and better readability.
*
* @module systems/animation/AnimationBuilder
* @category Animation System
* @korean 애니메이션빌더
*/
import * as THREE from "three";
import type {
AnimationKeyframe,
SkeletalAnimation,
} from "../../types/skeletal";
import { BoneName } from "../../types/skeletal";
/**
* Keyframe builder for fluent keyframe construction
* @korean 키프레임빌더
*/
class KeyframeBuilder {
private time: number;
private easing: string;
private boneRotations: Map<BoneName, THREE.Euler>;
private bonePositions: Map<BoneName, THREE.Vector3>;
private parentBuilder: AnimationBuilder | null = null;
constructor(time: number, easing: string = "linear") {
this.time = time;
this.easing = easing;
this.boneRotations = new Map();
this.bonePositions = new Map();
}
/**
* Set parent animation builder (for chaining)
* @internal
*/
setParent(parent: AnimationBuilder): this {
this.parentBuilder = parent;
return this;
}
/**
* Add bone rotation to keyframe
* @param bone - Bone to rotate
* @param x - X rotation in radians
* @param y - Y rotation in radians
* @param z - Z rotation in radians
* @param order - Rotation order (default: XYZ)
* @returns This builder for chaining
*/
rotate(
bone: BoneName,
x: number,
y: number,
z: number,
order: THREE.EulerOrder = "XYZ"
): this {
this.boneRotations.set(bone, new THREE.Euler(x, y, z, order));
return this;
}
/**
* Add bone position to keyframe
* @param bone - Bone to position
* @param x - X position
* @param y - Y position
* @param z - Z position
* @returns This builder for chaining
*/
position(bone: BoneName, x: number, y: number, z: number): this {
this.bonePositions.set(bone, new THREE.Vector3(x, y, z));
return this;
}
/**
* Build the keyframe and return to animation builder
* @returns Animation builder for chaining
*/
build(): AnimationBuilder {
const keyframe: AnimationKeyframe = {
time: this.time,
easing: this.easing as "linear" | "ease-in" | "ease-out" | "ease-in-out",
boneRotations: this.boneRotations,
bonePositions: this.bonePositions,
};
Eif (this.parentBuilder) {
this.parentBuilder.addKeyframe(keyframe);
return this.parentBuilder;
}
// This indicates incorrect usage of the builder API; parent must be set via setParent(...)
throw new Error(
"KeyframeBuilder.build() called without a parent AnimationBuilder. Ensure setParent(...) is called before build()."
);
}
}
/**
* Animation builder for fluent animation construction
* @korean 애니메이션빌더
*/
export class AnimationBuilder {
private animationName: string;
private koreanName: string;
private duration: number;
private loop: boolean;
private type: "attack" | "defense" | "movement" | "idle";
private keyframes: AnimationKeyframe[];
private constructor(name: string) {
this.animationName = name;
this.koreanName = name;
this.duration = 1.0;
this.loop = false;
this.type = "idle";
this.keyframes = [];
}
/**
* Create a new animation builder
* @param name - Animation name
* @returns New animation builder
*/
static create(name: string): AnimationBuilder {
return new AnimationBuilder(name);
}
/**
* Set Korean name for animation
* @param name - Korean name
* @returns This builder for chaining
*/
withKoreanName(name: string): this {
this.koreanName = name;
return this;
}
/**
* Set animation duration
* @param seconds - Duration in seconds
* @returns This builder for chaining
*/
withDuration(seconds: number): this {
this.duration = seconds;
return this;
}
/**
* Set animation loop behavior
* @param shouldLoop - Whether animation should loop
* @returns This builder for chaining
*/
withLoop(shouldLoop: boolean): this {
this.loop = shouldLoop;
return this;
}
/**
* Set animation type
* @param animType - Animation type
* @returns This builder for chaining
*/
withType(animType: "attack" | "defense" | "movement" | "idle"): this {
this.type = animType;
return this;
}
/**
* Add a keyframe to the animation
* @param time - Time of keyframe in seconds
* @param easing - Easing function name
* @returns Keyframe builder for defining keyframe contents
*/
keyframe(time: number, easing: string = "linear"): KeyframeBuilder {
const kfBuilder = new KeyframeBuilder(time, easing);
kfBuilder.setParent(this);
return kfBuilder;
}
/**
* Add a pre-built keyframe directly
* @param keyframe - Complete keyframe
* @returns This builder for chaining
*/
addKeyframe(keyframe: AnimationKeyframe): this {
this.keyframes.push(keyframe);
return this;
}
/**
* Build the complete animation
* @returns Complete skeletal animation
*/
build(): SkeletalAnimation {
return {
name: this.animationName,
koreanName: this.koreanName,
duration: this.duration,
loop: this.loop,
type: this.type,
keyframes: this.keyframes,
};
}
}
/**
* Common keyframe factories for reusable animation patterns
* @korean 키프레임팩토리
*/
export class KeyframeFactories {
/**
* Create a guard return keyframe (return to defensive position)
* @param time - Time of keyframe
* @returns Guard position keyframe
*/
static guardReturn(time: number): AnimationKeyframe {
return {
time,
easing: "ease-in",
boneRotations: new Map([
[BoneName.SHOULDER_R, new THREE.Euler(-0.2, -0.3, -0.3, "XYZ")],
[BoneName.SHOULDER_L, new THREE.Euler(-0.2, 0.3, 0.3, "XYZ")],
[BoneName.ELBOW_R, new THREE.Euler(0, 0, 1.2, "XYZ")],
[BoneName.ELBOW_L, new THREE.Euler(0, 0, -1.2, "XYZ")],
[BoneName.SPINE_UPPER, new THREE.Euler(0, 0, 0, "XYZ")],
[BoneName.SPINE_MIDDLE, new THREE.Euler(0, 0, 0, "XYZ")],
[BoneName.PELVIS, new THREE.Euler(0, 0, 0, "XYZ")],
]),
bonePositions: new Map([
[BoneName.HAND_R, new THREE.Vector3(0, 0, 0)],
[BoneName.HAND_L, new THREE.Vector3(0, 0, 0)],
]),
};
}
/**
* Create a neutral stance keyframe
* @param time - Time of keyframe
* @returns Neutral stance keyframe
*/
static neutralStance(time: number): AnimationKeyframe {
return {
time,
easing: "linear",
boneRotations: new Map([
[BoneName.SPINE_UPPER, new THREE.Euler(0, 0, 0, "XYZ")],
[BoneName.SPINE_MIDDLE, new THREE.Euler(0, 0, 0, "XYZ")],
[BoneName.PELVIS, new THREE.Euler(0, 0, 0, "XYZ")],
[BoneName.HIP_L, new THREE.Euler(0, 0, 0, "XYZ")],
[BoneName.HIP_R, new THREE.Euler(0, 0, 0, "XYZ")],
]),
bonePositions: new Map(),
};
}
/**
* Create a torso rotation keyframe
* @param time - Time of keyframe
* @param angle - Rotation angle in radians (positive = clockwise)
* @param easing - Easing function
* @returns Torso rotation keyframe
*/
static rotateTorso(
time: number,
angle: number,
easing: "linear" | "ease-in" | "ease-out" | "ease-in-out" = "linear"
): AnimationKeyframe {
return {
time,
easing,
boneRotations: new Map([
[BoneName.SPINE_UPPER, new THREE.Euler(0, angle, 0, "XYZ")],
[BoneName.SPINE_MIDDLE, new THREE.Euler(0, angle * 0.75, 0, "XYZ")],
[BoneName.PELVIS, new THREE.Euler(0, angle * 0.5, 0, "XYZ")],
]),
bonePositions: new Map(),
};
}
}
/**
* Bone rotation helper utilities
* @korean 뼈회전헬퍼
*/
export class BoneRotationHelpers {
/**
* Create shoulder rotation for arm extension
* @param side - "L" or "R"
* @param forward - Forward rotation amount
* @param up - Upward rotation amount
* @returns Euler rotation
*/
static shoulderExtension(
side: "L" | "R",
forward: number,
up: number = 0
): THREE.Euler {
const sign = side === "L" ? -1 : 1;
return new THREE.Euler(up, 0, forward * sign, "XYZ");
}
/**
* Create elbow rotation for arm bend
* @param side - "L" or "R"
* @param bend - Bend amount (0 = straight, PI/2 = 90 degrees)
* @returns Euler rotation
*/
static elbowBend(side: "L" | "R", bend: number): THREE.Euler {
const sign = side === "L" ? -1 : 1;
return new THREE.Euler(0, 0, bend * sign, "XYZ");
}
/**
* Create hip rotation for leg movement
* @param _side - "L" or "R" (reserved for future asymmetric animations)
* @param forward - Forward rotation (positive = leg forward)
* @param outward - Outward rotation (positive = leg out)
* @returns Euler rotation
*/
static hipRotation(
_side: "L" | "R",
forward: number,
outward: number = 0
): THREE.Euler {
return new THREE.Euler(forward, outward, 0, "XYZ");
}
/**
* Create knee rotation for leg bend
* @param _side - "L" or "R" (reserved for future asymmetric animations)
* @param bend - Bend amount (positive = knee bends)
* @returns Euler rotation
*/
static kneeBend(_side: "L" | "R", bend: number): THREE.Euler {
// NOTE: `_side` is intentionally unused: knee bends are currently symmetric for both legs.
// The parameter is kept to preserve API compatibility for future asymmetric leg animations.
void _side;
return new THREE.Euler(0, 0, bend, "XYZ");
}
}
/**
* Reusable animation presets for common combat patterns
* @korean 재사용애니메이션프리셋
*/
export class AnimationPresets {
/**
* Standard fighting guard position - protects face and body
* Both elbows tight, hands at chin/temple level
* @korean 기본방어자세
*/
static readonly FIGHTING_GUARD = {
leftArm: {
shoulder: new THREE.Euler(-0.6, 0.4, 0.3), // Elbow forward, hand near face
elbow: new THREE.Euler(0, 0, -1.8), // Tight 105° bend
wrist: new THREE.Euler(0.2, 0.1, 0), // Fist near chin
},
rightArm: {
shoulder: new THREE.Euler(-0.6, -0.4, -0.3), // Mirror
elbow: new THREE.Euler(0, 0, 1.8), // Tight 105° bend
wrist: new THREE.Euler(0.2, -0.1, 0), // Fist near chin
},
} as const;
/**
* High guard protecting head - both hands at temple level
* Used during kicks or when expecting high attacks
* @korean 상단방어자세
*/
static readonly HIGH_GUARD = {
leftArm: {
shoulder: new THREE.Euler(-0.9, 0.3, 0.4), // Raised high
elbow: new THREE.Euler(0, 0, -1.6), // Tight bend
wrist: new THREE.Euler(0.3, 0.1, 0), // Hand near temple
},
rightArm: {
shoulder: new THREE.Euler(-0.9, -0.3, -0.4), // Mirror
elbow: new THREE.Euler(0, 0, 1.6), // Tight bend
wrist: new THREE.Euler(0.3, -0.1, 0), // Hand near temple
},
} as const;
/**
* Kick chamber position - leg lifted, hip rotated
* Shared starting position for most kicks
* @korean 킥체임버자세
*/
static readonly KICK_CHAMBER_RIGHT = {
hip: new THREE.Euler(1.57, 0, 0), // 90° hip flexion
knee: new THREE.Euler(-2.0, 0, 0), // Tight chamber
ankle: new THREE.Euler(0, 0, 0), // Relaxed
supportKnee: new THREE.Euler(-0.25, 0, 0), // Slight bend for balance
pelvis: new THREE.Euler(-0.1, 0, 0), // Slight backward tilt
} as const;
/**
* Kick extension position - leg fully extended
* @korean 킥확장자세
*/
static readonly KICK_EXTENSION_RIGHT = {
hip: new THREE.Euler(1.7, 0, 0), // Hip drives forward
knee: new THREE.Euler(0.1, 0, 0), // Full extension
ankle: new THREE.Euler(0.5, 0, 0), // Dorsiflexion for ball strike
supportKnee: new THREE.Euler(-0.35, 0, 0), // Deeper bend for balance
pelvis: new THREE.Euler(0.15, 0, 0), // Forward drive
} as const;
/**
* Punch wind-up - arm coiled, torso rotated back
* @korean 펀치준비자세
*/
static readonly PUNCH_WINDUP_RIGHT = {
shoulder: new THREE.Euler(0.3, 0, -0.3),
elbow: new THREE.Euler(0, 0, 1.8),
spine: new THREE.Euler(0, -0.15, 0),
pelvis: new THREE.Euler(0, -0.1, 0),
} as const;
/**
* Punch extension - arm extended with torso rotation
* @korean 펀치확장자세
*/
static readonly PUNCH_EXTENSION_RIGHT = {
shoulder: new THREE.Euler(-0.7, 0, 0.5),
elbow: new THREE.Euler(0, 0, 0.05),
spine: new THREE.Euler(0, 0.35, 0),
pelvis: new THREE.Euler(0, 0.2, 0),
} as const;
}
/**
* Applies common animation patterns to KeyframeBuilder
* Allows reusing shared motion patterns across animations
* @korean 애니메이션패턴헬퍼
*/
export class AnimationPatternHelpers {
/**
* Apply fighting guard to a keyframe
* Keeps hands protecting face during kicks
* @param kf - KeyframeBuilder to modify
* @returns Modified KeyframeBuilder
*/
static applyFightingGuard(kf: KeyframeBuilder): KeyframeBuilder {
const guard = AnimationPresets.FIGHTING_GUARD;
return kf
.rotate(
BoneName.SHOULDER_L,
guard.leftArm.shoulder.x,
guard.leftArm.shoulder.y,
guard.leftArm.shoulder.z
)
.rotate(
BoneName.ELBOW_L,
guard.leftArm.elbow.x,
guard.leftArm.elbow.y,
guard.leftArm.elbow.z
)
.rotate(
BoneName.SHOULDER_R,
guard.rightArm.shoulder.x,
guard.rightArm.shoulder.y,
guard.rightArm.shoulder.z
)
.rotate(
BoneName.ELBOW_R,
guard.rightArm.elbow.x,
guard.rightArm.elbow.y,
guard.rightArm.elbow.z
);
}
/**
* Apply high guard during kicks
* @param kf - KeyframeBuilder to modify
* @returns Modified KeyframeBuilder
*/
static applyHighGuard(kf: KeyframeBuilder): KeyframeBuilder {
const guard = AnimationPresets.HIGH_GUARD;
return kf
.rotate(
BoneName.SHOULDER_L,
guard.leftArm.shoulder.x,
guard.leftArm.shoulder.y,
guard.leftArm.shoulder.z
)
.rotate(
BoneName.ELBOW_L,
guard.leftArm.elbow.x,
guard.leftArm.elbow.y,
guard.leftArm.elbow.z
)
.rotate(
BoneName.SHOULDER_R,
guard.rightArm.shoulder.x,
guard.rightArm.shoulder.y,
guard.rightArm.shoulder.z
)
.rotate(
BoneName.ELBOW_R,
guard.rightArm.elbow.x,
guard.rightArm.elbow.y,
guard.rightArm.elbow.z
);
}
/**
* Apply kick chamber for right leg
* @param kf - KeyframeBuilder to modify
* @returns Modified KeyframeBuilder
*/
static applyKickChamber(kf: KeyframeBuilder): KeyframeBuilder {
const chamber = AnimationPresets.KICK_CHAMBER_RIGHT;
return kf
.rotate(BoneName.HIP_R, chamber.hip.x, chamber.hip.y, chamber.hip.z)
.rotate(BoneName.KNEE_R, chamber.knee.x, chamber.knee.y, chamber.knee.z)
.rotate(
BoneName.KNEE_L,
chamber.supportKnee.x,
chamber.supportKnee.y,
chamber.supportKnee.z
)
.rotate(
BoneName.PELVIS,
chamber.pelvis.x,
chamber.pelvis.y,
chamber.pelvis.z
);
}
}
|