Skip to content

Commit 05017c2

Browse files
committed
es6 - Sketchbook
1 parent 04a0f85 commit 05017c2

23 files changed

+1021
-996
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*.js

.eslintrc.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
module.exports = {
22
"env": {
33
"browser": true,
4-
"es6": true
4+
"es6": true,
5+
"amd": true
56
},
67
"extends": "eslint:recommended",
78
"parserOptions": {
89
"ecmaVersion": 2018,
910
"sourceType": "module"
1011
},
12+
"globals": {
13+
"THREE": false,
14+
"CANNON": false
15+
},
1116
"rules": {
1217
"no-undef": 2,
1318
"no-unused-vars": 1,

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
.vscode
22
node_modules
3-
package-lock.json
4-
notes.txt
3+
package-lock.json

gulpfile.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

jsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"include": [
3+
"src/js/**/*"
4+
],
5+
6+
"exclude": [
7+
"src/js/lib/**/*"
8+
]
9+
}

package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
"@types/stats": "^0.16.30",
2222
"@types/three": "^0.91.3",
2323
"eslint": "^5.5.0",
24-
"gulp": "^4.0.0",
25-
"gulp-clean-css": "^3.9.4",
26-
"gulp-concat": "^2.6.1",
27-
"gulp-minify": "^3.1.0",
28-
"gulp-uglify": "^3.0.0",
29-
"gulp-uglifycss": "^1.0.9",
3024
"webpack": "^4.17.1",
3125
"webpack-cli": "^3.1.0"
3226
},

src/js/characters/Character.js

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
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+
87
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';
119
import * as CharacterAI from './CharacterAI';
1210
import * as CharacterStates from './CharacterStates';
1311

@@ -25,10 +23,10 @@ export class Character extends Object3D {
2523

2624
// Geometry
2725
this.height = 1;
28-
this.modelOffset = new Vector3();
26+
this.modelOffset = new THREE.Vector3();
2927

3028
// Default model
31-
const loader = new FBXLoader();
29+
const loader = new THREE.FBXLoader();
3230
loader.load('resources/models/game_man/game_man.fbx', function (object) {
3331

3432
object.traverse( function ( child ) {
@@ -37,8 +35,8 @@ export class Character extends Object3D {
3735
child.receiveShadow = true;
3836
}
3937
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' ),
4240
skinning: true
4341
});
4442
}
@@ -49,11 +47,11 @@ export class Character extends Object3D {
4947
// this->visuals->modelContainer->characterModel
5048

5149
// The visuals group is centered for easy character tilting
52-
scope.visuals = new Group();
50+
scope.visuals = new THREE.Group();
5351
scope.add(scope.visuals);
5452

5553
// 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();
5755
scope.modelContainer.position.y = -scope.height/2;
5856
scope.visuals.add(scope.modelContainer);
5957

@@ -63,56 +61,56 @@ export class Character extends Object3D {
6361
scope.modelContainer.add(object);
6462

6563
// Animation
66-
scope.mixer = new AnimationMixer(object);
64+
scope.mixer = new THREE.AnimationMixer(object);
6765

6866
// 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);
7169
} );
7270

7371
// 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();
7775
// Velocity spring simulator
7876
this.defaultVelocitySimulatorDamping = 0.8;
7977
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);
8179
this.moveSpeed = 4;
8280

8381
// Rotation
8482
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);
8785
// Rotation spring simulator
8886
this.defaultRotationSimulatorDamping = 0.5;
8987
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);
9189

9290
// States
93-
this.setState(CharacterStates.DefaultState);
94-
this.viewVector = new Vector3();
91+
this.setState(CharacterStates.CharacterState_DefaultState);
92+
this.viewVector = new THREE.Vector3();
9593

9694
// Controls
97-
this.behaviour = new CharacterAI.Default(this);
95+
this.behaviour = new CharacterAI.CharacterAI_Default(this);
9896
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()
110108
};
111109

112110
// Physics
113111
// Player Capsule
114112
const characterMass = 1;
115-
const initPosition = new Vec3(0, 0, 0);
113+
const initPosition = new CANNON.Vec3(0, 0, 0);
116114
const characterHeight = 0.5;
117115
const characterRadius = 0.25;
118116
const characterSegments = 12;
@@ -132,28 +130,28 @@ export class Character extends Object3D {
132130
this.characterCapsule.physical.updateMassProperties();
133131

134132
// Ray casting
135-
this.rayResult = new RaycastResult();
133+
this.rayResult = new CANNON.RaycastResult();
136134
this.rayHasHit = false;
137135
this.rayCastLength = 0.63;
138136
this.raySafeOffset = 0.03;
139137
this.wantsToJump = false;
140138
this.justJumped = false;
141139

142140
// 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({
145143
color: 0xff0000
146144
});
147-
this.raycastBox = new Mesh(boxGeo, boxMat);
145+
this.raycastBox = new THREE.Mesh(boxGeo, boxMat);
148146
this.raycastBox.visible = false;
149147

150148
// PreStep event
151149
this.characterCapsule.physical.preStep = function() {
152150

153151
// Player ray casting
154152
// 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);
157155
// Raycast options
158156
const rayCastOptions = {
159157
collisionFilterMask: ~2 /* cast against everything except second collision group (player) */,
@@ -176,7 +174,7 @@ export class Character extends Object3D {
176174

177175
// Player ray casting
178176
// Get velocities
179-
let simulatedVelocity = new Vec3().copy(this.velocity);
177+
let simulatedVelocity = new CANNON.Vec3().copy(this.velocity);
180178
let arcadeVelocity = this.character.velocity.clone().multiplyScalar(this.character.moveSpeed);
181179
arcadeVelocity = Utils.appplyVectorMatrixXZ(this.character.orientation, arcadeVelocity);
182180

@@ -204,7 +202,7 @@ export class Character extends Object3D {
204202
this.characterModel = model;
205203
this.modelContainer.add(this.characterModel);
206204

207-
this.mixer = new AnimationMixer(this.characterModel);
205+
this.mixer = new THREE.AnimationMixer(this.characterModel);
208206
}
209207

210208
setModelOffset(offset) {
@@ -224,7 +222,7 @@ export class Character extends Object3D {
224222
}
225223

226224
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);
228226
}
229227

230228
setVelocity(velZ, velX = 0) {
@@ -306,7 +304,7 @@ export class Character extends Object3D {
306304
setAnimation(clipName, fadeIn) {
307305

308306
let clips = this.characterModel.animations;
309-
let clip = AnimationClip.findByName( clips, clipName );
307+
let clip = THREE.AnimationClip.findByName( clips, clipName );
310308
let action = this.mixer.clipAction( clip );
311309
this.mixer.stopAllAction();
312310
action.fadeIn(fadeIn);
@@ -330,7 +328,7 @@ export class Character extends Object3D {
330328

331329
//Spring rotation
332330
//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);
334332
let dot = this.orientation.dot(this.orientationTarget);
335333

336334

@@ -349,7 +347,7 @@ export class Character extends Object3D {
349347
// Get angle difference in radians
350348
angle = Math.acos(dot);
351349
// 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);
353351
// Compare cross with normal to find out direction
354352
if (normal.dot(cross) < 0) {
355353
angle = -angle;
@@ -376,8 +374,8 @@ export class Character extends Object3D {
376374
const positiveZ = this.controls.up.value ? 1 : 0;
377375
const negativeZ = this.controls.down.value ? -1 : 0;
378376

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);
381379

382380
// If no direction is pressed, set target as current orientation
383381
// if(positiveX == 0 && negativeX == 0 && positiveZ == 0 && negativeZ == 0) {

0 commit comments

Comments
 (0)