hongcha-6 5 vuotta sitten
vanhempi
commit
5d7aeab755
2 muutettua tiedostoa jossa 63 lisäystä ja 8 poistoa
  1. 5
    1
      src/controller.ts
  2. 58
    7
      src/index.ts

+ 5
- 1
src/controller.ts Näytä tiedosto

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

+ 58
- 7
src/index.ts Näytä tiedosto

1
 import * as THREE from "three"
1
 import * as THREE from "three"
2
 import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
2
 import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
3
-import { Object3D } from "three";
3
+import { Object3D, Camera } from "three";
4
 
4
 
5
 import { GameController } from "./controller"
5
 import { GameController } from "./controller"
6
 
6
 
7
+//let step=0;
8
+
7
 
9
 
8
 export class FooGame {
10
 export class FooGame {
9
 
11
 
14
     cam_fov = 70; //视角大小
16
     cam_fov = 70; //视角大小
15
     cam_aspect = window.innerWidth / window.innerHeight;
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
     loader: GLTFLoader;
31
     loader: GLTFLoader;
18
 
32
 
19
     updateMap: Object;
33
     updateMap: Object;
20
 
34
 
21
     controller: GameController;
35
     controller: GameController;
22
 
36
 
37
+
23
     constructor() {
38
     constructor() {
24
         this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
39
         this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
25
         this.camera.position.x = 0;
40
         this.camera.position.x = 0;
33
 
48
 
34
         document.body.appendChild(this.renderer.domElement);
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
         this.loader = new GLTFLoader();
54
         this.loader = new GLTFLoader();
37
 
55
 
38
         this.updateMap = {};
56
         this.updateMap = {};
43
         //this.loadScene();
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
         this.controller.onKey_A(() => {
86
         this.controller.onKey_A(() => {
48
-            if (box) box.position.x -= 0.1;
87
+            if (Camera) camera.position.x -= 0.1;
49
         });
88
         });
50
         this.controller.onKey_D(() => {
89
         this.controller.onKey_D(() => {
51
-            if (box) box.position.x += 0.1;
90
+            if (Camera) camera.position.x += 0.1;
52
         });
91
         });
53
         this.controller.onKey_S(() => {
92
         this.controller.onKey_S(() => {
54
-            if (box) box.position.z += 0.1;
93
+            if (Camera) camera.position.z += 0.1;
55
         });
94
         });
56
         this.controller.onKey_W(() => {
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
     animate() {
106
     animate() {
63
         requestAnimationFrame(() => {
107
         requestAnimationFrame(() => {
64
             this.animate();
108
             this.animate();
109
+
65
         });
110
         });
111
+
66
         this.updateObjects(this.scene);
112
         this.updateObjects(this.scene);
67
         this.renderer.render(this.scene, this.camera);
113
         this.renderer.render(this.scene, this.camera);
68
     }
114
     }
116
 
162
 
117
 game.loadScene(() => {
163
 game.loadScene(() => {
118
     let box = game.scene.getObjectByName("Box");
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
     if (box) {
170
     if (box) {
120
-        game.bindKeyAction(box);
171
+        game.bindKeyAction(game.camera);
121
         game.setUpdate(box, (obj: Object3D) => {
172
         game.setUpdate(box, (obj: Object3D) => {
122
             //game.camera.rotateOnAxis(obj.position, 0.01)
173
             //game.camera.rotateOnAxis(obj.position, 0.01)
123
             //obj.position.x += performance.now() * 0.000001;
174
             //obj.position.x += performance.now() * 0.000001;

Loading…
Peruuta
Tallenna