forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
character-hitter.js
214 lines (194 loc) · 6.58 KB
/
character-hitter.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
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
import * as THREE from 'three';
import {scene, camera} from './renderer.js';
import physics from './physics-manager.js';
// import physx from './physx.js';
import Avatar from './avatars/avatars.js';
import metaversefile from 'metaversefile';
import * as metaverseModules from './metaverse-modules.js';
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localEuler = new THREE.Euler();
const localMatrix = new THREE.Matrix4();
//
const physicsScene = physics.getScene();
// const y180Quaternion = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), Math.PI);
//
const hitAttemptEventData = {
type: '',
args: null,
};
const hitAttemptEvent = new MessageEvent('hitattempt', {
data: hitAttemptEventData,
});
export class CharacterHitter {
constructor(player) {
this.player = player;
this.lastHitTime = -Infinity;
}
attemptHit({
type,
args,
timestamp = performance.now(),
}) {
hitAttemptEventData.type = type;
hitAttemptEventData.args = args;
hitManager.dispatchEvent(hitAttemptEvent);
switch (type) {
case 'sword': {
const {
hitRadius,
hitHalfHeight,
position,
quaternion,
} = args;
const collision = physicsScene.getCollisionObject(
hitRadius,
hitHalfHeight,
position,
quaternion,
);
if (collision) {
const collisionId = collision.objectId;
const result = metaversefile.getPairByPhysicsId(collisionId);
if (result) {
const [app, physicsObject] = result;
const timeDiff = timestamp - this.lastHitTime;
if (timeDiff > 1000) {
const useAction = this.player.getAction('use');
const damage = typeof useAction.damage === 'number' ? useAction.damage : 10;
const hitDirection = app.position.clone()
.sub(this.player.position);
hitDirection.y = 0;
hitDirection.normalize();
const damageMeshOffsetDistance = 1.5;
const hitPosition = localVector.copy(this.player.position)
.add(localVector2.set(0, 0, -damageMeshOffsetDistance).applyQuaternion(this.player.quaternion))
.clone();
localEuler.setFromQuaternion(camera.quaternion, 'YXZ');
localEuler.x = 0;
localEuler.z = 0;
const hitQuaternion = localQuaternion.setFromEuler(localEuler);
// const willDie = app.willDieFrom(damage);
app.hit(damage, {
type: 'sword',
collisionId,
physicsObject,
hitPosition,
hitQuaternion,
hitDirection,
// willDie,
});
this.lastHitTime = timestamp;
return collision;
}
}
}
return null;
}
case 'bullet': {
const result = physics.raycast(args.position, args.quaternion);
if (result) {
const _performHit = () => {
const targetApp = metaversefile.getAppByPhysicsId(result.objectId);
if (targetApp) {
const damage = 2;
const hitPosition = new THREE.Vector3().fromArray(result.point);
const hitQuaternion = new THREE.Quaternion().setFromRotationMatrix(
localMatrix.lookAt(
this.player.position,
hitPosition,
localVector.set(0, 1, 0)
)
);
const hitDirection = targetApp.position.clone()
.sub(this.player.position);
// hitDirection.y = 0;
hitDirection.normalize();
// const willDie = targetApp.willDieFrom(damage);
targetApp.hit(damage, {
type: 'bullet',
collisionId: result.objectId,
hitPosition,
hitDirection,
hitQuaternion,
// willDie,
});
} else {
console.warn('no app with physics id', result.objectId);
}
};
_performHit();
return result
}
return null;
}
default: {
throw new Error('unknown hit type :' + type);
}
}
}
getHit(damage) {
const newAction = {
type: 'hurt',
animation: Math.random() < 0.5 ? 'pain_arch' : 'pain_back',
};
const hurtAction = this.player.addAction(newAction);
const emotions = [
// 'joy',
// 'fun',
'sorrow',
'angry',
// 'neutral',
'surprise',
];
const emotion = emotions[Math.floor(Math.random() * emotions.length)];
const faceposeAction = this.player.addAction({
type: 'facepose',
emotion,
value: 1,
});
const gruntTypes = [
'hurt',
'scream',
'attack',
'angry',
'gasp',
];
const gruntType = gruntTypes[Math.floor(Math.random() * gruntTypes.length)];
// console.log('play grunt', emotion, gruntType);
this.player.characterSfx.playGrunt(gruntType);
{
const damageMeshApp = metaversefile.createApp();
(async () => {
await metaverseModules.waitForLoad();
const {modules} = metaversefile.useDefaultModules();
const m = modules['damageMesh'];
await damageMeshApp.addModule(m);
})();
damageMeshApp.position.copy(this.player.position);
localEuler.setFromQuaternion(camera.quaternion, 'YXZ');
localEuler.x = 0;
localEuler.z = 0;
damageMeshApp.quaternion.setFromEuler(localEuler);
damageMeshApp.updateMatrixWorld();
scene.add(damageMeshApp);
}
const animations = Avatar.getAnimations();
const hurtAnimation = animations.find(a => a.isHurt);
const hurtAnimationDuration = hurtAnimation.duration;
setTimeout(() => {
const hurtActionIndex = this.player.indexOfAction(hurtAction);
this.player.removeActionIndex(hurtActionIndex);
}, hurtAnimationDuration * 1000);
setTimeout(() => {
const faceposeActionIndex = this.player.indexOfAction(faceposeAction);
this.player.removeActionIndex(faceposeActionIndex);
}, 1000);
}
update() {
// nothing
}
}
const hitManager = new EventTarget();
export default hitManager;