This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouseKeyEvents.js
174 lines (137 loc) · 3.08 KB
/
MouseKeyEvents.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
//
// Mouse and Key event functions
//
// Diogo Ferreira
// Luís Leira
// 2017
//
var mouseDown = false;
var currentlyPressedKeys = {};
// From https://stackoverflow.com/questions/42309715/how-to-correctly-pass-mouse-coordinates-to-webgl
function getRelativeMousePosition(event, target) {
target = target || event.target;
var rect = target.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top,
}
}
// assumes target or event.target is canvas
function getNoPaddingNoBorderCanvasRelativeMousePosition(event, target) {
target = target || event.target;
var pos = getRelativeMousePosition(event, target);
pos.x = pos.x * target.width / target.clientWidth;
pos.y = pos.y * target.height / target.clientHeight;
return pos;
}
function handleMouseDown(event) {
mouseDown = true;
}
function handleMouseUp(event) {
mouseDown = false;
}
function handleKeyDown(event) {
currentlyPressedKeys[event.keyCode] = true;
}
function handleKeyUp(event) {
currentlyPressedKeys[event.keyCode] = false;
}
function handleMouseWheel(event){
// Move map backwards
if(event.deltaY>0){
tz -= 0.01;
globalTZZ -= 0.05;
}
// Move maze forward
else if(event.deltaY<0){
tz += 0.01;
globalTZZ += 0.05;
}
}
function handleMouseMove(event) {
if (!mouseDown) {
return;
}
// Rotation angles proportional to cursor displacement
var pos = getNoPaddingNoBorderCanvasRelativeMousePosition(event, gl.canvas);
var x = pos.x / gl.canvas.width;
var y = pos.y / gl.canvas.height;
// Limit x and y to viewport
if(x<0)
x=0;
if(x>1)
x=1;
if(y<0)
y=0;
if(y>1)
y=1;
// On Left/Right mouse movement, change YY angle and XX translation
globalYYAngle = x*180 - 90;
globalTXX = (x-0.5)*10;
// On Up/Down mouse movement, change XX angle and YY translation
globalXXAngle = -y*90 + 90;
globalTYY = -Math.abs((y-1))*5;
// Must be 2.5 because the cube has a global translation of -2.5
variationGlobalTZZ = 2.5*Math.abs(y-1)+5*Math.abs((x-0.5));
}
function handleKeys() {
if (currentlyPressedKeys[38]) {
// Jump (up arrow or space)
if(onGround || onTopOfCube){
vY = 0.15;
onGround = false;
lastXXAngle = angleXX;
}
}
if(currentlyPressedKeys[39]){
// Move right (right arrow)
if(tx+0.05>1){
tx = 1.0;
}
else{
tx +=0.05;
}
}
if(currentlyPressedKeys[37]){
// Move left (left arrow)
if(tx-0.05<-1)
tx = -1.0;
else
tx -= 0.05;
}
if (currentlyPressedKeys[83]) {
// Start game
if(maze.length>0){
PLAY_GAME = true;
game_velocity = INITIAL_GAME_VELOCITY;
}
}
if (currentlyPressedKeys[82]) {
// Restart game
reset();
}
if (currentlyPressedKeys[76]) {
// 'l' key
if(!PLAY_GAME){
// Set game velocity as low
INITIAL_GAME_VELOCITY = 1/10000;
game_velocity_type = 0;
}
}
if (currentlyPressedKeys[77]) {
// 'm' key
if(!PLAY_GAME){
// Set game velocity as medium
INITIAL_GAME_VELOCITY = 1/1000;
game_velocity_type = 1;
}
}
if (currentlyPressedKeys[72]) {
// 'h' key
if(!PLAY_GAME){
// Set game velocity as high
INITIAL_GAME_VELOCITY = 1/500;
game_velocity_type = 2;
}
}
}