-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharacterControlsGame.ts
51 lines (42 loc) · 1.51 KB
/
characterControlsGame.ts
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
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
export class CharacterControls {
model: THREE.Group
mixer: THREE.AnimationMixer
animationsMap: Map<string, THREE.AnimationAction> = new Map() // Walk, Run, Idle
orbitControl: OrbitControls
camera: THREE.Camera
// state
currentAction: string = 'Idle'
// constants
fadeDuration: number = 0.2
constructor(model: THREE.Group,
mixer: THREE.AnimationMixer, animationsMap: Map<string, THREE.AnimationAction>,
orbitControl: OrbitControls, camera: THREE.Camera) {
this.model = model
this.mixer = mixer
this.animationsMap = animationsMap
this.orbitControl = orbitControl
this.camera = camera
// Start with idle animation
const idleAction = this.animationsMap.get('Idle')
if (idleAction) {
idleAction.play()
}
}
public update(delta: number) {
// Ensure only idle animation is played
const idleAction = this.animationsMap.get('Idle')
if (idleAction && this.currentAction !== 'Idle') {
const current = this.animationsMap.get(this.currentAction)
if (current) {
current.fadeOut(this.fadeDuration)
}
idleAction.reset().fadeIn(this.fadeDuration).play()
this.currentAction = 'Idle'
}
// Update mixer
this.mixer.update(delta)
// No movement logic for idle mode
}
}