1
- import {
2
- Object3D , Group , Vector3 ,
3
- FBXLoader , TextureLoader ,
4
- AnimationMixer , AnimationClip ,
5
- Mesh , BoxGeometry ,
6
- MeshLambertMaterial } from '../lib/core/three' ;
7
- import { Vec3 , RaycastResult } from '../lib/core/cannon' ;
1
+ // import THREE from 'three';
2
+ // import CANNON from 'cannon';
3
+
4
+ import { Object3D } from 'three' ;
5
+ import { Utilities as Utils } from '../sketchbook/Utilities' ;
6
+
8
7
import * as Springs from '../simulation/SpringSimulation' ;
9
- import * as Controls from '../sketchbook/Input' ;
10
- import * as Utils from '../sketchbook/Utilities' ;
8
+ import * as Controls from '../sketchbook/Controls' ;
11
9
import * as CharacterAI from './CharacterAI' ;
12
10
import * as CharacterStates from './CharacterStates' ;
13
11
@@ -25,10 +23,10 @@ export class Character extends Object3D {
25
23
26
24
// Geometry
27
25
this . height = 1 ;
28
- this . modelOffset = new Vector3 ( ) ;
26
+ this . modelOffset = new THREE . Vector3 ( ) ;
29
27
30
28
// Default model
31
- const loader = new FBXLoader ( ) ;
29
+ const loader = new THREE . FBXLoader ( ) ;
32
30
loader . load ( 'resources/models/game_man/game_man.fbx' , function ( object ) {
33
31
34
32
object . traverse ( function ( child ) {
@@ -37,8 +35,8 @@ export class Character extends Object3D {
37
35
child . receiveShadow = true ;
38
36
}
39
37
if ( child . name == 'game_man' ) {
40
- child . material = new MeshLambertMaterial ( {
41
- map : new TextureLoader ( ) . load ( 'resources/models/game_man/game_man.png' ) ,
38
+ child . material = new THREE . MeshLambertMaterial ( {
39
+ map : new THREE . TextureLoader ( ) . load ( 'resources/models/game_man/game_man.png' ) ,
42
40
skinning : true
43
41
} ) ;
44
42
}
@@ -49,11 +47,11 @@ export class Character extends Object3D {
49
47
// this->visuals->modelContainer->characterModel
50
48
51
49
// The visuals group is centered for easy character tilting
52
- scope . visuals = new Group ( ) ;
50
+ scope . visuals = new THREE . Group ( ) ;
53
51
scope . add ( scope . visuals ) ;
54
52
55
53
// Model container is used to reliably ground the character, as animation can alter the position of the model itself
56
- scope . modelContainer = new Group ( ) ;
54
+ scope . modelContainer = new THREE . Group ( ) ;
57
55
scope . modelContainer . position . y = - scope . height / 2 ;
58
56
scope . visuals . add ( scope . modelContainer ) ;
59
57
@@ -63,56 +61,56 @@ export class Character extends Object3D {
63
61
scope . modelContainer . add ( object ) ;
64
62
65
63
// Animation
66
- scope . mixer = new AnimationMixer ( object ) ;
64
+ scope . mixer = new THREE . AnimationMixer ( object ) ;
67
65
68
66
// scope.player.setModel(object);
69
- scope . setModelOffset ( new Vector3 ( 0 , - 0.1 , 0 ) ) ;
70
- scope . setState ( CharacterStates . Idle ) ;
67
+ scope . setModelOffset ( new THREE . Vector3 ( 0 , - 0.1 , 0 ) ) ;
68
+ scope . setState ( CharacterStates . CharacterState_Idle ) ;
71
69
} ) ;
72
70
73
71
// Movement
74
- this . acceleration = new Vector3 ( ) ;
75
- this . velocity = new Vector3 ( ) ;
76
- this . velocityTarget = new Vector3 ( ) ;
72
+ this . acceleration = new THREE . Vector3 ( ) ;
73
+ this . velocity = new THREE . Vector3 ( ) ;
74
+ this . velocityTarget = new THREE . Vector3 ( ) ;
77
75
// Velocity spring simulator
78
76
this . defaultVelocitySimulatorDamping = 0.8 ;
79
77
this . defaultVelocitySimulatorMass = 50 ;
80
- this . velocitySimulator = new Springs . SpringVSimulator ( 60 , this . defaultVelocitySimulatorMass , this . defaultVelocitySimulatorDamping ) ;
78
+ this . velocitySimulator = new Springs . VectorSpringSimulator ( 60 , this . defaultVelocitySimulatorMass , this . defaultVelocitySimulatorDamping ) ;
81
79
this . moveSpeed = 4 ;
82
80
83
81
// Rotation
84
82
this . angularVelocity = 0 ;
85
- this . orientation = new Vector3 ( 0 , 0 , 1 ) ;
86
- this . orientationTarget = new Vector3 ( 0 , 0 , 1 ) ;
83
+ this . orientation = new THREE . Vector3 ( 0 , 0 , 1 ) ;
84
+ this . orientationTarget = new THREE . Vector3 ( 0 , 0 , 1 ) ;
87
85
// Rotation spring simulator
88
86
this . defaultRotationSimulatorDamping = 0.5 ;
89
87
this . defaultRotationSimulatorMass = 10 ;
90
- this . rotationSimulator = new Springs . SpringRSimulator ( 60 , this . defaultRotationSimulatorMass , this . defaultRotationSimulatorDamping ) ;
88
+ this . rotationSimulator = new Springs . RelativeSpringSimulator ( 60 , this . defaultRotationSimulatorMass , this . defaultRotationSimulatorDamping ) ;
91
89
92
90
// States
93
- this . setState ( CharacterStates . DefaultState ) ;
94
- this . viewVector = new Vector3 ( ) ;
91
+ this . setState ( CharacterStates . CharacterState_DefaultState ) ;
92
+ this . viewVector = new THREE . Vector3 ( ) ;
95
93
96
94
// Controls
97
- this . behaviour = new CharacterAI . Default ( this ) ;
95
+ this . behaviour = new CharacterAI . CharacterAI_Default ( this ) ;
98
96
this . controls = {
99
- up : new Controls . EventControl ( ) ,
100
- down : new Controls . EventControl ( ) ,
101
- left : new Controls . EventControl ( ) ,
102
- right : new Controls . EventControl ( ) ,
103
- run : new Controls . EventControl ( ) ,
104
- jump : new Controls . EventControl ( ) ,
105
- use : new Controls . EventControl ( ) ,
106
- primary : new Controls . EventControl ( ) ,
107
- secondary : new Controls . EventControl ( ) ,
108
- tertiary : new Controls . EventControl ( ) ,
109
- lastControl : new Controls . EventControl ( )
97
+ up : new Controls . Control_EventControl ( ) ,
98
+ down : new Controls . Control_EventControl ( ) ,
99
+ left : new Controls . Control_EventControl ( ) ,
100
+ right : new Controls . Control_EventControl ( ) ,
101
+ run : new Controls . Control_EventControl ( ) ,
102
+ jump : new Controls . Control_EventControl ( ) ,
103
+ use : new Controls . Control_EventControl ( ) ,
104
+ primary : new Controls . Control_EventControl ( ) ,
105
+ secondary : new Controls . Control_EventControl ( ) ,
106
+ tertiary : new Controls . Control_EventControl ( ) ,
107
+ lastControl : new Controls . Control_EventControl ( )
110
108
} ;
111
109
112
110
// Physics
113
111
// Player Capsule
114
112
const characterMass = 1 ;
115
- const initPosition = new Vec3 ( 0 , 0 , 0 ) ;
113
+ const initPosition = new CANNON . Vec3 ( 0 , 0 , 0 ) ;
116
114
const characterHeight = 0.5 ;
117
115
const characterRadius = 0.25 ;
118
116
const characterSegments = 12 ;
@@ -132,28 +130,28 @@ export class Character extends Object3D {
132
130
this . characterCapsule . physical . updateMassProperties ( ) ;
133
131
134
132
// Ray casting
135
- this . rayResult = new RaycastResult ( ) ;
133
+ this . rayResult = new CANNON . RaycastResult ( ) ;
136
134
this . rayHasHit = false ;
137
135
this . rayCastLength = 0.63 ;
138
136
this . raySafeOffset = 0.03 ;
139
137
this . wantsToJump = false ;
140
138
this . justJumped = false ;
141
139
142
140
// Ray cast debug
143
- const boxGeo = new BoxGeometry ( 0.1 , 0.1 , 0.1 ) ;
144
- const boxMat = new MeshLambertMaterial ( {
141
+ const boxGeo = new THREE . BoxGeometry ( 0.1 , 0.1 , 0.1 ) ;
142
+ const boxMat = new THREE . MeshLambertMaterial ( {
145
143
color : 0xff0000
146
144
} ) ;
147
- this . raycastBox = new Mesh ( boxGeo , boxMat ) ;
145
+ this . raycastBox = new THREE . Mesh ( boxGeo , boxMat ) ;
148
146
this . raycastBox . visible = false ;
149
147
150
148
// PreStep event
151
149
this . characterCapsule . physical . preStep = function ( ) {
152
150
153
151
// Player ray casting
154
152
// Create ray
155
- const start = new Vec3 ( this . position . x , this . position . y , this . position . z ) ;
156
- const end = new Vec3 ( this . position . x , this . position . y - this . character . rayCastLength , this . position . z ) ;
153
+ const start = new CANNON . Vec3 ( this . position . x , this . position . y , this . position . z ) ;
154
+ const end = new CANNON . Vec3 ( this . position . x , this . position . y - this . character . rayCastLength , this . position . z ) ;
157
155
// Raycast options
158
156
const rayCastOptions = {
159
157
collisionFilterMask : ~ 2 /* cast against everything except second collision group (player) */ ,
@@ -176,7 +174,7 @@ export class Character extends Object3D {
176
174
177
175
// Player ray casting
178
176
// Get velocities
179
- let simulatedVelocity = new Vec3 ( ) . copy ( this . velocity ) ;
177
+ let simulatedVelocity = new CANNON . Vec3 ( ) . copy ( this . velocity ) ;
180
178
let arcadeVelocity = this . character . velocity . clone ( ) . multiplyScalar ( this . character . moveSpeed ) ;
181
179
arcadeVelocity = Utils . appplyVectorMatrixXZ ( this . character . orientation , arcadeVelocity ) ;
182
180
@@ -204,7 +202,7 @@ export class Character extends Object3D {
204
202
this . characterModel = model ;
205
203
this . modelContainer . add ( this . characterModel ) ;
206
204
207
- this . mixer = new AnimationMixer ( this . characterModel ) ;
205
+ this . mixer = new THREE . AnimationMixer ( this . characterModel ) ;
208
206
}
209
207
210
208
setModelOffset ( offset ) {
@@ -224,7 +222,7 @@ export class Character extends Object3D {
224
222
}
225
223
226
224
setPosition ( x , y , z ) {
227
- this . characterCapsule . physical . position = new Vec3 ( x , y , z ) ;
225
+ this . characterCapsule . physical . position = new CANNON . Vec3 ( x , y , z ) ;
228
226
}
229
227
230
228
setVelocity ( velZ , velX = 0 ) {
@@ -306,7 +304,7 @@ export class Character extends Object3D {
306
304
setAnimation ( clipName , fadeIn ) {
307
305
308
306
let clips = this . characterModel . animations ;
309
- let clip = AnimationClip . findByName ( clips , clipName ) ;
307
+ let clip = THREE . AnimationClip . findByName ( clips , clipName ) ;
310
308
let action = this . mixer . clipAction ( clip ) ;
311
309
this . mixer . stopAllAction ( ) ;
312
310
action . fadeIn ( fadeIn ) ;
@@ -330,7 +328,7 @@ export class Character extends Object3D {
330
328
331
329
//Spring rotation
332
330
//Figure out angle between current and target orientation
333
- let normal = new Vector3 ( 0 , 1 , 0 ) ;
331
+ let normal = new THREE . Vector3 ( 0 , 1 , 0 ) ;
334
332
let dot = this . orientation . dot ( this . orientationTarget ) ;
335
333
336
334
@@ -349,7 +347,7 @@ export class Character extends Object3D {
349
347
// Get angle difference in radians
350
348
angle = Math . acos ( dot ) ;
351
349
// Get vector pointing up or down
352
- let cross = new Vector3 ( ) . crossVectors ( this . orientation , this . orientationTarget ) ;
350
+ let cross = new THREE . Vector3 ( ) . crossVectors ( this . orientation , this . orientationTarget ) ;
353
351
// Compare cross with normal to find out direction
354
352
if ( normal . dot ( cross ) < 0 ) {
355
353
angle = - angle ;
@@ -376,8 +374,8 @@ export class Character extends Object3D {
376
374
const positiveZ = this . controls . up . value ? 1 : 0 ;
377
375
const negativeZ = this . controls . down . value ? - 1 : 0 ;
378
376
379
- const localDirection = new Vector3 ( positiveX + negativeX , 0 , positiveZ + negativeZ ) ;
380
- const flatViewVector = new Vector3 ( this . viewVector . x , 0 , this . viewVector . z ) ;
377
+ const localDirection = new THREE . Vector3 ( positiveX + negativeX , 0 , positiveZ + negativeZ ) ;
378
+ const flatViewVector = new THREE . Vector3 ( this . viewVector . x , 0 , this . viewVector . z ) ;
381
379
382
380
// If no direction is pressed, set target as current orientation
383
381
// if(positiveX == 0 && negativeX == 0 && positiveZ == 0 && negativeZ == 0) {
0 commit comments