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 | 32x 32x 32x 32x 32x 32x 103x 103x 103x 103x 103x 103x 103x 293x 293x 293x 293x 293x 293x 293x 293x 293x 293x 293x 2x 291x 293x 293x 208x 81x 1x 1x 293x 293x 97x 196x 196x 196x 293x 293x 1x 292x 292x 283x 9x 1x 8x 3x 5x 293x 1x 292x 292x 1x 1x | /**
* Speed Modifier System
*
* **Korean**: 속도 변경 시스템 (Speed Modifier System)
*
* Implements comprehensive movement speed modifier system that dynamically adjusts
* player movement speed based on multiple factors: combat stance, leg injuries,
* stamina depletion, and combat state. Integrates with physics-based movement
* system for authentic Korean martial arts combat feel.
*
* ## System Features
*
* - Base movement speeds for walking, running, backward, lateral, and crouching
* - Eight Trigram stance-based speed modifiers (80% to 125%)
* - Injury-based speed reduction from leg damage (0% to 100% penalty)
* - Stamina-based acceleration penalty (0% to 75% penalty)
* - Combat state speed adjustments (0% to 100% penalty)
* - Multiplicative and additive modifier stacking
*
* ## Speed Calculation Formula
*
* ```
* Final Speed = Base Speed
* × Stance Modifier
* × (1 - Injury Penalty from Legs, Torso & Pain)
* × (1 - Combat State Penalty)
*
* Final Acceleration = Base Acceleration
* × (1 - Stamina Penalty)
* ```
*
* @module systems/physics/SpeedModifierSystem
* @category Physics System
* @korean 속도변경시스템
*/
import type {
BodyPartHealth,
BodyPartMaxHealth,
} from "@/systems/bodypart/types";
import type { PlayerState } from "@/systems/player";
import { CombatState, TrigramStance } from "@/types/common";
import { BASE_MOVEMENT_ACCELERATION } from "@/types/physicsConstants";
import { STANCE_SPEED_MODIFIERS } from "./MovementPhysics";
import { injuryMovementModifier } from "@/systems/movement/InjuryMovementModifier";
/**
* Complete speed modifier state for physics calculations.
*
* **Korean**: 속도 변경 상태 (Speed Modifier State)
*
* Contains all calculated speed modifiers and final values for application
* to the physics-based movement system.
*
* @public
* @category Physics System
* @korean 속도변경상태
*/
export interface SpeedModifierState {
/** Base movement speed before modifiers (m/s) */
readonly baseSpeed: number;
/** Stance-based speed multiplier (0.80 to 1.25) */
readonly stanceModifier: number;
/** Injury-based speed reduction (0.0 to 0.75, percentage reduction) - Korean: 부상감소 */
readonly injuryPenalty: number;
/** Combat state speed reduction (0.0 to 1.0, percentage reduction) */
readonly combatStatePenalty: number;
/** Stamina-based acceleration reduction (0.0 to 0.75, percentage reduction) - Korean: 스태미나효과 */
readonly staminaPenalty: number;
/** Final calculated movement speed (m/s) */
readonly finalSpeed: number;
/** Final calculated acceleration (m/s²) */
readonly finalAcceleration: number;
/** Whether running is allowed (false when stamina < 10%) */
readonly canRun: boolean;
}
/**
* Movement type enumeration.
*
* **Korean**: 이동 유형 (Movement Type)
*
* @public
* @category Physics System
*/
export enum MovementType {
/** Walking forward - Korean: 걷기 */
WALKING = "walking",
/** Running/sprinting forward - Korean: 달리기 */
RUNNING = "running",
/** Moving backward - Korean: 후퇴 */
BACKWARD = "backward",
/** Side-stepping left/right - Korean: 측면이동 */
LATERAL = "lateral",
/** Crouching movement - Korean: 웅크리기 */
CROUCHING = "crouching",
}
/**
* Speed Modifier System class.
*
* **Korean**: 속도 변경 시스템 클래스
*
* Calculates comprehensive speed modifiers from multiple factors including
* stance, injuries, stamina, and combat state. Integrates with existing
* body part and movement systems.
*
* @example
* ```typescript
* const system = new SpeedModifierSystem();
*
* const modifiers = system.calculateSpeedModifiers(
* playerState,
* MovementType.RUNNING,
* true // isCrouching
* );
*
* // Apply to movement physics
* movementPhysics.setMaxSpeed(modifiers.finalSpeed);
* movementPhysics.setAcceleration(modifiers.finalAcceleration);
* ```
*
* @public
* @category Physics System
*/
export class SpeedModifierSystem {
/**
* Base walking speed (m/s)
* Standard walking pace for combat movement - crosses 14m arena in ~2.3s
*
* **Korean**: 기본 걷기 속도 (Base Walking Speed)
*/
private readonly BASE_WALKING_SPEED = 6.0;
/**
* Base running speed (m/s)
* Sprint speed for rapid repositioning - crosses 14m arena in ~1.4s
*
* **Korean**: 기본 달리기 속도 (Base Running Speed)
*/
private readonly BASE_RUNNING_SPEED = 10.0;
/**
* Lateral movement speed (m/s)
* Fast side-stepping for combat positioning
*
* **Korean**: 측면 이동 속도 (Lateral Movement Speed)
*/
private readonly LATERAL_SPEED = 5.0;
/**
* Crouching movement speed (m/s)
* Defensive low movement for cautious approach
*
* **Korean**: 웅크림 이동 속도 (Crouching Movement Speed)
*/
private readonly CROUCHING_SPEED = 3.0;
/**
* Backward speed multiplier (75% of forward speed)
*
* **Korean**: 후퇴 속도 배수 (Backward Speed Multiplier)
*/
private readonly BACKWARD_SPEED_MULTIPLIER = 0.75;
/**
* Base acceleration rate (m/s²)
* Instant-response acceleration for combat movement
* (e.g., reaches 6 m/s walking speed in 0.2s; 10 m/s sprint speed in 0.33s)
* Increased from 12.0 to 30.0 for responsive, arcade-style combat feel
*
* Imported from physicsConstants.ts to maintain consistency across systems.
*
* **Korean**: 기본 가속도 (Base Acceleration)
*/
private readonly BASE_ACCELERATION = BASE_MOVEMENT_ACCELERATION;
/**
* Combat state speed penalty multipliers
*
* **Korean**: 전투 상태 속도 패널티 (Combat State Speed Penalties)
*/
private readonly COMBAT_STATE_PENALTIES: Record<CombatState, number> = {
[CombatState.IDLE]: 0.0, // No penalty when idle
[CombatState.ATTACKING]: 0.3, // -30% during attack commitment
[CombatState.DEFENDING]: 0.2, // -20% while guarding
[CombatState.STUNNED]: 1.0, // Cannot move when stunned
[CombatState.RECOVERING]: 0.4, // -40% during recovery frames
[CombatState.COUNTERING]: 0.25, // -25% during counter execution
[CombatState.TRANSITIONING]: 0.15, // -15% during stance transitions
[CombatState.GRAPPLING]: 0.8, // -80% while controlling opponent
[CombatState.GRAPPLED]: 1.0, // Cannot move while being controlled
};
// Constructor removed - no longer need MovementPenaltySystem instance
// Using injuryMovementModifier singleton instead
/**
* Calculate comprehensive speed modifiers for player movement.
*
* **Korean**: 속도 변경 계산 (Calculate Speed Modifiers)
*
* Analyzes player state and returns complete speed modifier information
* including all individual factors and final calculated values.
*
* @param playerState - Current player state with stance, health, stamina
* @param movementType - Type of movement being performed
* @param isCrouching - Whether player is in crouching stance
* @returns Complete speed modifier state with all factors
*
* @public
*/
public calculateSpeedModifiers(
playerState: PlayerState,
movementType: MovementType = MovementType.WALKING,
isCrouching: boolean = false,
): SpeedModifierState {
// Get base speed based on movement type and archetype-specific speeds
const baseSpeed = this.getBaseSpeed(
movementType,
isCrouching,
playerState.physicalAttributes,
);
// Calculate injury penalty from leg damage using NEW InjuryMovementModifier system
// This replaces the old MovementPenaltySystem with comprehensive injury calculations
const injuryPenalty = this.calculateInjuryPenalty(
playerState.bodyPartHealth,
playerState.bodyPartMaxHealth,
playerState.pain,
);
// Calculate stance modifier separately.
// NOTE: InjuryMovementModifier is called with neutral GEON stance inside
// calculateInjuryPenalty() so that injury effects are isolated from stance.
// The actual current stance effect must be applied here via stanceModifier.
const stanceModifier = this.getStanceSpeedModifier(
playerState.currentStance,
);
// Calculate combat state penalty
const combatStatePenalty = this.getCombatStatePenalty(
playerState.combatState,
);
// Calculate stamina penalty (affects acceleration only)
const staminaPenalty = this.calculateStaminaPenalty(
playerState.stamina,
playerState.maxStamina,
);
// Check if running is allowed
const canRun = this.canPlayerRun(
playerState.stamina,
playerState.maxStamina,
);
// Calculate final speed with all modifiers applied multiplicatively
// Formula: Base × Stance × (1 - Injury) × (1 - CombatState)
// Note: Injury penalty now excludes stance (calculated with GEON), so we apply stance here
const finalSpeed =
baseSpeed *
stanceModifier *
(1.0 - injuryPenalty) *
(1.0 - combatStatePenalty);
// Calculate final acceleration using archetype-specific base and stamina penalty
// Formula: ArchetypeAcceleration × (1 - StaminaPenalty)
const archetypeAcceleration =
playerState.physicalAttributes?.acceleration ?? this.BASE_ACCELERATION;
const finalAcceleration = archetypeAcceleration * (1.0 - staminaPenalty);
return {
baseSpeed,
stanceModifier,
injuryPenalty,
combatStatePenalty,
staminaPenalty,
finalSpeed,
finalAcceleration,
canRun,
};
}
/**
* Get base speed for movement type.
*
* **Korean**: 기본 속도 계산 (Get Base Speed)
*
* Uses archetype-specific walk/run speeds when available,
* falls back to default values for backwards compatibility.
*
* @param movementType - Type of movement
* @param isCrouching - Whether player is crouching
* @param physicalAttributes - Player's physical attributes (optional)
* @returns Base speed in m/s
*
* @private
*/
private getBaseSpeed(
movementType: MovementType,
isCrouching: boolean,
physicalAttributes?: import("@/types").PhysicalAttributes,
): number {
if (isCrouching) {
return this.CROUCHING_SPEED;
}
// Use archetype-specific speeds if available
const archetypeWalkSpeed =
physicalAttributes?.walkSpeed ?? this.BASE_WALKING_SPEED;
const archetypeRunSpeed =
physicalAttributes?.runSpeed ?? this.BASE_RUNNING_SPEED;
switch (movementType) {
case MovementType.WALKING:
return archetypeWalkSpeed;
case MovementType.RUNNING:
return archetypeRunSpeed;
case MovementType.BACKWARD:
return archetypeWalkSpeed * this.BACKWARD_SPEED_MULTIPLIER;
case MovementType.LATERAL:
// Lateral speed scales proportionally to archetype walk speed
return (
archetypeWalkSpeed * (this.LATERAL_SPEED / this.BASE_WALKING_SPEED)
);
case MovementType.CROUCHING:
return this.CROUCHING_SPEED;
default:
return archetypeWalkSpeed;
}
}
/**
* Get stance-based speed modifier.
*
* **Korean**: 자세 속도 배수 계산 (Get Stance Speed Modifier)
*
* @param stance - Current Eight Trigram stance
* @returns Speed multiplier (0.80 to 1.25)
*
* @private
*/
private getStanceSpeedModifier(stance: TrigramStance): number {
return STANCE_SPEED_MODIFIERS[stance] ?? 1.0;
}
/**
* Calculate injury-based speed penalty from leg damage.
*
* **Korean**: 부상 속도 패널티 계산 (Calculate Injury Penalty)
*
* Uses NEW InjuryMovementModifier to determine speed reduction based on
* leg damage severity, torso damage, and pain levels.
* NOTE: Stance modifier is calculated separately to maintain compatibility
* with existing SpeedModifierState interface.
*
* Penalties are progressive and realistic:
* - Leg injuries: 0-100% penalty based on health
* - Torso injuries: 0-30% minor penalty
* - Both legs injured: Additional 20% cumulative penalty
* - Pain overload: -15% when pain ≥ 80
*
* @param bodyPartHealth - Current body part health
* @param bodyPartMaxHealth - Maximum body part health
* @param painLevel - Current pain level (0-100)
* @returns Speed penalty as percentage (0.0 to 0.9 max)
*
* @private
*/
private calculateInjuryPenalty(
bodyPartHealth: BodyPartHealth | undefined,
bodyPartMaxHealth: BodyPartMaxHealth | undefined,
painLevel: number,
): number {
if (!bodyPartHealth || !bodyPartMaxHealth) {
return 0.0; // No penalty if body part health not tracked
}
// Use NEW InjuryMovementModifier system for comprehensive injury calculation
// Use NEUTRAL stance (GEON = 1.0x) to get pure injury penalty without stance influence
// Stance is applied separately in calculateSpeedModifiers for compatibility
const result = injuryMovementModifier.calculateMovementSpeed(
1.0, // Base speed of 1.0 to get pure multiplier
bodyPartHealth,
TrigramStance.GEON, // Neutral stance to isolate injury penalty
painLevel,
);
// Convert speedMultiplier to penalty percentage
// speedMultiplier of 1.0 = no penalty (0.0)
// speedMultiplier of 0.5 = 50% penalty (0.5)
// speedMultiplier of 0.1 = 90% penalty (0.9)
const penalty = 1.0 - result.speedMultiplier;
// Return penalty (already clamped by InjuryMovementModifier to min 0.1 speed)
return Math.min(penalty, 0.9);
}
/**
* Calculate combat state speed penalty.
*
* **Korean**: 전투 상태 속도 패널티 계산 (Get Combat State Penalty)
*
* Different combat states restrict movement:
* - IDLE: 0% penalty (full movement)
* - ATTACKING: -30% penalty (committed to technique)
* - DEFENDING: -20% penalty (guard up, limited mobility)
* - STUNNED: -100% penalty (cannot move)
* - RECOVERING: -40% penalty (vulnerable, slow movement)
* - COUNTERING: -25% penalty (counter execution)
* - TRANSITIONING: -15% penalty (stance change)
*
* @param combatState - Current combat state
* @returns Speed penalty as percentage (0.0 to 1.0)
*
* @private
*/
private getCombatStatePenalty(combatState: CombatState): number {
return this.COMBAT_STATE_PENALTIES[combatState] ?? 0.0;
}
/**
* Calculate stamina-based acceleration penalty.
*
* **Korean**: 스태미나 가속 패널티 계산 (Calculate Stamina Penalty)
*
* Stamina affects acceleration rate, making it harder to change direction
* or speed up when fatigued:
* - High stamina (70-100%): 0% penalty (normal acceleration)
* - Medium stamina (40-70%): -20% penalty
* - Low stamina (10-40%): -50% penalty
* - Depleted stamina (<10%): -75% penalty, cannot run
*
* @param currentStamina - Current stamina points
* @param maxStamina - Maximum stamina capacity
* @returns Acceleration penalty as percentage (0.0 to 0.75)
*
* @private
*/
private calculateStaminaPenalty(
currentStamina: number,
maxStamina: number,
): number {
if (maxStamina <= 0) {
return 0.0; // No penalty if stamina not tracked
}
const staminaPercent = (currentStamina / maxStamina) * 100;
if (staminaPercent >= 70) {
return 0.0; // High stamina: no penalty
} else if (staminaPercent >= 40) {
return 0.2; // Medium stamina: -20% acceleration
} else if (staminaPercent >= 10) {
return 0.5; // Low stamina: -50% acceleration
} else {
return 0.75; // Depleted stamina: -75% acceleration
}
}
/**
* Check if player can run based on stamina level.
*
* **Korean**: 달리기 가능 확인 (Can Player Run)
*
* @param currentStamina - Current stamina points
* @param maxStamina - Maximum stamina capacity
* @returns True if player has enough stamina to run
*
* @private
*/
private canPlayerRun(currentStamina: number, maxStamina: number): boolean {
if (maxStamina <= 0) {
return true; // Allow running if stamina not tracked
}
const staminaPercent = (currentStamina / maxStamina) * 100;
return staminaPercent >= 10; // Cannot run when stamina < 10%
}
/**
* Apply speed modifiers to movement physics system.
*
* **Korean**: 속도 변경 적용 (Apply Speed Modifiers)
*
* Convenience method to directly apply calculated modifiers to a
* movement physics instance.
*
* @param movementPhysics - Movement physics system to update
* @param modifiers - Calculated speed modifier state
*
* @public
*/
public applySpeedModifiers(
movementPhysics: {
setMaxSpeed: (speed: number) => void;
setAcceleration: (accel: number) => void;
},
modifiers: SpeedModifierState,
): void {
movementPhysics.setMaxSpeed(modifiers.finalSpeed);
movementPhysics.setAcceleration(modifiers.finalAcceleration);
}
}
|