import type Armature from '../armature/Armature' ; import type Pose from '../armature/Pose' ; import type SpringItem from './SpringItem' ; import SpringChain from './SpringChain' ; interface ISpringType { setRestPose ( chain , pose ) : void ; updatePose ( chain , pose , dt ) : void ; } class BoneSpring { arm : Armature ; items : Map < string , SpringChain > = new Map ( ) ; constructor ( arm ) { this . arm = arm ; } addRotChain ( chName , bNames , osc = 5.0 , damp = 0.5 ) { const chain = new SpringChain ( chName , 0 ) ; chain . setBones ( bNames , this . arm , osc , damp ) ; this . items . set ( chName , chain ) ; return this ; } addPosChain ( chName , bNames , osc = 5.0 , damp = 0.5 ) { const chain = new SpringChain ( chName , 1 ) ; chain . setBones ( bNames , this . arm , osc , damp ) ; this . items . set ( chName , chain ) ; return this ; } setRestPose ( pose ) { let ch ; for ( ch of this . items . values ( ) ) { ch . setRestPose ( pose ) ; } return this ; } updatePose ( dt , pose , doWorldUpdate ) { let ch ; for ( ch of this . items . values ( ) ) { ch . updatePose ( dt , pose ) ; } if ( doWorldUpdate ) pose . updateWorld ( true ) ; return this ; } /** Set Oscillation Per Section for all Chain Items */ setOsc ( chName , osc ) { const ch = this . items . get ( chName ) ; if ( ! ch ) { console . error ( 'Spring Chain name not found' , chName ) ; return this ; } let si ; for ( si of ch . items ) si . spring . setOscPerSec ( osc ) ; return this ; } /** Spread a Oscillation range on the chain */ setOscRange ( chName , a , b ) { const ch = this . items . get ( chName ) ; if ( ! ch ) { console . error ( 'Spring Chain name not found' , chName ) ; return this ; } const len = ch . items . length - 1 ; let t ; for ( let i = 0 ; i <= len ; i ++ ) { t = i / len ; ch . items [ i ] . spring . setOscPerSec ( a * ( 1 - t ) + b * t ) ; } return this ; } setDamp ( chName , damp ) { const ch = this . items . get ( chName ) ; if ( ! ch ) { console . error ( 'Spring Chain name not found' , chName ) ; return this ; } let si ; for ( si of ch . items ) si . spring . setDamp ( damp ) ; return this ; } setDampRange ( chName , a , b ) { const ch = this . items . get ( chName ) ; if ( ! ch ) { console . error ( 'Spring Chain name not found' , chName ) ; return this ; } const len = ch . items . length - 1 ; let t ; for ( let i = 0 ; i <= len ; i ++ ) { t = i / len ; ch . items [ i ] . spring . setDamp ( a * ( 1 - t ) + b * t ) ; } return this ; } } export default BoneSpring ; export type { ISpringType } ;