-
Notifications
You must be signed in to change notification settings - Fork 207
/
character-behavior.js
87 lines (85 loc) · 3.22 KB
/
character-behavior.js
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
class CharacterBehavior {
constructor(player) {
this.player = player;
this.mouthMovementState=null;
this.mouthMovementStartTime=0;
this.mouthMovementAttackTime=0;
this.mouthMovementDecayTime=0;
this.mouthMovementSustainTime=0;
this.mouthMovementReleaseTime=0;
}
update(timestamp, timeDiffS) {
if (!this.player.avatar) {
return;
}
//#################################### manually set mouth movement ##########################################
const _handleMouthMovementAttack=()=>{
this.player.avatar.volume = ((timestamp/1000 - this.mouthMovementStartTime) / this.mouthMovementAttackTime)/12;
if(timestamp/1000 - this.mouthMovementStartTime >= this.mouthMovementAttackTime){
this.mouthMovementState = 'decay';
this.mouthMovementStartTime = timestamp/1000;
}
}
const _handleMouthMovementDecay=()=>{
this.player.avatar.volume = (1 - ((timestamp/1000 - this.mouthMovementStartTime) / this.mouthMovementDecayTime) * 0.8)/12;
if(timestamp/1000 - this.mouthMovementStartTime >= this.mouthMovementDecayTime){
this.mouthMovementState='sustain';
this.mouthMovementStartTime = timestamp/1000;
}
}
const _handleMouthMovementSustain=()=>{
if(timestamp/1000 - this.mouthMovementStartTime >= this.mouthMovementSustainTime){
this.mouthMovementState='release';
this.mouthMovementStartTime = timestamp/1000;
}
}
const _handleMouthMovementRelease=()=>{
this.player.avatar.volume = (0.2 - ((timestamp/1000 - this.mouthMovementStartTime) / this.mouthMovementReleaseTime) * 0.2)/12;
if(timestamp/1000 - this.mouthMovementStartTime >= this.mouthMovementReleaseTime){
this.mouthMovementState=null;
this.manuallySetMouth=false;
this.mouthMovementStartTime = -1;
}
}
const _handleMouthMovementNull=()=>{
this.mouthMovementState = this.manuallySetMouth ? 'attack' : null;
this.mouthMovementStartTime = timestamp/1000;
}
switch (this.mouthMovementState) {
case 'attack': {
_handleMouthMovementAttack();
break;
}
case 'decay': {
_handleMouthMovementDecay();
break;
}
case 'sustain': {
_handleMouthMovementSustain();
break;
}
case 'release': {
_handleMouthMovementRelease();
break;
}
case null: {
_handleMouthMovementNull();
break;
}
}
}
setMouthMoving(attack, decay, sustain, release){
this.mouthMovementState=null;
this.manuallySetMouth=true;
this.mouthMovementAttackTime=attack;
this.mouthMovementDecayTime=decay;
this.mouthMovementSustainTime=sustain;
this.mouthMovementReleaseTime=release;
}
destroy() {
// nothing
}
}
export {
CharacterBehavior,
};