-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
executable file
·175 lines (130 loc) · 4.68 KB
/
game.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
175
//Fix animation for new animal, now it's too fast
var animal = 0;
var delay = 500;
console.log("Browser plugin installed: " + zig.pluginInstalled);
console.log("Zig.js version: " + zig.version);
zig.addEventListener('statuschange', function() {
console.log("Sensor connected: " + zig.sensorConnected);
});
var ce = document.createElement('div');
ce.id = 'mycursor';
var img = document.createElement('img');
img.id = 'animal';
var succes = new Audio("sound/PUNCH.wav");
succes.preload = 'auto';
succes.load();
var wrong = new Audio("sound/wrong.mp3");
wrong.preload = 'auto';
wrong.load();
$(function (){
document.body.appendChild(ce);
document.body.appendChild(img);
setTimeout(newAnimal(),1000);
// Code to delete logo from the interface, breaks the game after one minute
// setTimeout(function(){$("img[alt='Powered by Zigfu']").first().hide()},500);
});
function newAnimal(){
//Check if we are done
if(level.length == animal){
window.location.href = "level" + world + "finished.html";
}
$("#animal").removeClass();
img.style.visibility = "hidden";
img.style.left = level[animal][0]+ "px";
img.style.top = level[animal][1] + "px";
img.style.width = imageSizex + "px";
img.style.height = imageSizey + "px";
$("#animal").attr("src", "img/" + world + "-" + animal + ".png");
$("#animal").addClass(level[animal][2]);
img.style.visibility = "visible";
}
function fireMotion(direction){
//Get position of the cursor and correct for its size
cursorleft = parseFloat(ce.style.left) - 25;
cursortop = parseFloat(ce.style.top) - 25;
//Get position of the image and correct for its size
imageleft = parseFloat(img.style.left) - (imageSizex/2);
imagetop = parseFloat(img.style.top) - (imageSizey/2);
distance = Math.sqrt(Math.pow(Math.abs(imageleft-cursorleft),2) + Math.pow(Math.abs(imagetop-cursortop),2));
console.log("distance: " + distance);
//Hit
if(distance < difficulty && $("#animal").hasClass(direction)){
console.log("succes")
var click=succes.cloneNode();
click.play();
animal += 1;
if($("#animal").hasClass("push")){
$("#animal").addClass("fade");
} else {
$("#animal").addClass("grow");
}
setTimeout(function(){newAnimal()},1500);
}
else {
var click=wrong.cloneNode();
click.play();
}
}
function clamp(x, min, max) {
if (x < min) return min;
if (x > max) return max;
return x;
}
var hand = zig.EngageFirstUserInSession();
hand.addEventListener('sessionstart', function(focusPosition) {
console.log("started");
addPull();
addPush();
ce.style.display = 'block';
});
hand.addEventListener('sessionend', function() {
console.log("ended");
});
hand.addEventListener('sessionupdate', function(position) {
var d = $V(position).subtract($V([0,0,0])).elements;
var val = [clamp((d[0]/300) + 0.5, 0, 1),
clamp((d[1]/250), 0, 1),
clamp(d[2]/ + 0.5, 0, 1)];
ce.style.left = (val[0] * window.innerWidth - (ce.offsetWidth / 2)) + "px";
ce.style.top = ((1- val[1]) * window.innerHeight - (ce.offsetHeight / 2)) + "px";
});
// PushDetector and Pull detector
var pushDetector = zig.controls.PushDetector();
var pullDetector = zig.controls.PullDetector();
function addPush(){
console.log("add push");
pushDetector.addEventListener('push',
function(pd) {
setTimeout(addPull, delay);
removePull();
fireMotion("push");
console.log('PushDetector: Push');
});
}
//Remove push eventlisteners
function removePush(){
console.log("remove push");
pushDetector.removeEventListener('push');
}
//Add the pull event listeners
function addPull(){
console.log("add pull");
pullDetector.addEventListener('pull',
function(pd) {
setTimeout(addPush, delay);;
removePush();
fireMotion("pull");
console.log('PullDetector: Pull');
ce.classList.add('pulled');
});
}
//Remove the pull event listeners
function removePull(){
console.log("remove pull");
pullDetector.removeEventListener('pull');
}
//Init push/pull event listeners
zig.singleUserSession.addListener(pushDetector);
zig.singleUserSession.addListener(pullDetector);
//Init the hand
zig.addListener(hand);