hongcha-6 5 years ago
parent
commit
5d7aeab755
2 changed files with 63 additions and 8 deletions
  1. 5
    1
      src/controller.ts
  2. 58
    7
      src/index.ts

+ 5
- 1
src/controller.ts View File

@@ -1,7 +1,6 @@
1 1
 
2 2
 export class GameController {
3 3
     root: HTMLElement;
4
-
5 4
     callbackMap: any;
6 5
 
7 6
     constructor(root: HTMLElement) {
@@ -25,6 +24,11 @@ export class GameController {
25 24
         this.callbackMap["D"] = callback;
26 25
     }
27 26
 
27
+    onKey_Space(callback:Function){
28
+        this.callbackMap[" "]=callback;
29
+    }
30
+    
31
+
28 32
     private bindKey() {
29 33
         this.root.onkeypress = (event) => {
30 34
             let c = event.key.toUpperCase();

+ 58
- 7
src/index.ts View File

@@ -1,9 +1,11 @@
1 1
 import * as THREE from "three"
2 2
 import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
3
-import { Object3D } from "three";
3
+import { Object3D, Camera } from "three";
4 4
 
5 5
 import { GameController } from "./controller"
6 6
 
7
+//let step=0;
8
+
7 9
 
8 10
 export class FooGame {
9 11
 
@@ -14,12 +16,25 @@ export class FooGame {
14 16
     cam_fov = 70; //视角大小
15 17
     cam_aspect = window.innerWidth / window.innerHeight;
16 18
 
19
+    mouseX = 0;
20
+    mouseY = 0;
21
+    mouseXOnMouseDown = 0;
22
+    currentRotationX = 0;
23
+    currentRotationXOnMouseDown = 0;
24
+
25
+    step = 0;
26
+
27
+
28
+    windowHalfX = window.innerWidth / 2;
29
+    windowHalfY = window.innerHeight / 2;
30
+
17 31
     loader: GLTFLoader;
18 32
 
19 33
     updateMap: Object;
20 34
 
21 35
     controller: GameController;
22 36
 
37
+
23 38
     constructor() {
24 39
         this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
25 40
         this.camera.position.x = 0;
@@ -33,6 +48,9 @@ export class FooGame {
33 48
 
34 49
         document.body.appendChild(this.renderer.domElement);
35 50
 
51
+        document.addEventListener('mousedown', this.onDocumentMouseDown, false);
52
+        //document.body.addEventListener('mousemove', this.onDocumentMouseMove);
53
+
36 54
         this.loader = new GLTFLoader();
37 55
 
38 56
         this.updateMap = {};
@@ -43,18 +61,44 @@ export class FooGame {
43 61
         //this.loadScene();
44 62
     }
45 63
 
46
-    bindKeyAction(box: Object3D) {
64
+    onDocumentMouseDown = (event: MouseEvent) => {
65
+        event.preventDefault();
66
+        //保证监听拖拽事件
67
+        document.addEventListener('mousemove', this.onDocumentMouseMove, false);
68
+        document.addEventListener('mouseup', this.onDocumentMouseUp, false);
69
+        this.mouseXOnMouseDown = event.clientX - this.windowHalfX;
70
+        this.currentRotationXOnMouseDown = this.currentRotationX;
71
+    }
72
+
73
+    onDocumentMouseMove = (event: MouseEvent) => {
74
+        this.mouseX = event.clientX - this.windowHalfX;
75
+        this.mouseY = event.clientY - this.windowHalfY;
76
+        this.currentRotationX = this.currentRotationXOnMouseDown + (this.mouseX - this.mouseXOnMouseDown) * 0.01;
77
+        //game.camera.rotation.x += (this.currentRotationX - game.camera.rotation.x);
78
+        game.camera.rotation.y += (this.currentRotationX - game.camera.rotation.y);
79
+    }
80
+
81
+    onDocumentMouseUp = (event: MouseEvent) => {
82
+        document.removeEventListener('mousemove', this.onDocumentMouseMove);
83
+    }
84
+
85
+    bindKeyAction(camera: Camera) {
47 86
         this.controller.onKey_A(() => {
48
-            if (box) box.position.x -= 0.1;
87
+            if (Camera) camera.position.x -= 0.1;
49 88
         });
50 89
         this.controller.onKey_D(() => {
51
-            if (box) box.position.x += 0.1;
90
+            if (Camera) camera.position.x += 0.1;
52 91
         });
53 92
         this.controller.onKey_S(() => {
54
-            if (box) box.position.z += 0.1;
93
+            if (Camera) camera.position.z += 0.1;
55 94
         });
56 95
         this.controller.onKey_W(() => {
57
-            if (box) box.position.z -= 0.1;
96
+            if (Camera) camera.position.z -= 0.1;
97
+        });
98
+        this.controller.onKey_Space(() => {
99
+            if (Camera)
100
+                this.step += 0.07 
101
+            camera.position.y = 3 + (0.5 * Math.abs(Math.sin(this.step)))
58 102
         });
59 103
 
60 104
     }
@@ -62,7 +106,9 @@ export class FooGame {
62 106
     animate() {
63 107
         requestAnimationFrame(() => {
64 108
             this.animate();
109
+
65 110
         });
111
+
66 112
         this.updateObjects(this.scene);
67 113
         this.renderer.render(this.scene, this.camera);
68 114
     }
@@ -116,8 +162,13 @@ let game = new FooGame();
116 162
 
117 163
 game.loadScene(() => {
118 164
     let box = game.scene.getObjectByName("Box");
165
+    //let cam=game.scene.getObjectByName("Camera");
166
+
167
+    // if(game.camera){
168
+    //     game.bindKeyAction(game.camera);
169
+    // }
119 170
     if (box) {
120
-        game.bindKeyAction(box);
171
+        game.bindKeyAction(game.camera);
121 172
         game.setUpdate(box, (obj: Object3D) => {
122 173
             //game.camera.rotateOnAxis(obj.position, 0.01)
123 174
             //obj.position.x += performance.now() * 0.000001;

Loading…
Cancel
Save