forked from y0ast/KinectGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
164 lines (127 loc) · 3.88 KB
/
game.js
File metadata and controls
164 lines (127 loc) · 3.88 KB
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
var animal = 0;
var timestart = 0;
var timeend = 0;
$(function (){
for(var i = 0; i<gridSizey; i++){
$("#grid").append('<tr id="' + i + '">');
for(var j=0; j<gridSizex; j++){
$("#" + i).append('<td id="' + i + '-' + j + '"></td>');
}
$(".grid").append('</tr>');
}
$("td").css("width",1200/gridSizex);
$("td").css("height",700/gridSizey);
var sound = new Audio("PUNCH.wav");
sound.preload = 'auto';
sound.load();
var ce = document.createElement('div');
ce.id = 'mycursor';
document.body.appendChild(ce);
});
function createID(x,y){
id = "#" + x + "-" + y;
return id;
}
function setAnimal(){
if(animal == level.length){
alert("done!");
return false;
}
//append an image to the right place in the grid
x = level[animal][0];
y = level[animal][1];
id = createID(x,y);
console.log(id);
$(id).append('<img src="furry' + animal + '.png"></img>');
$("img").css("margin-left",(700/gridSizey - 50)/2);
timestart = new Date().getTime() / 1000;
$(id).click(function (){
$(this).off('click');
$(this).html("");
timeend = new Date().getTime() / 1000;
//score();
animal += 1;
setAnimal();
});
}
function fireClick(){
x = ce.css("left");
y = ce.css("top");
console.log([x,y]);
$(document.elementFromPoint(x, y)).click();
}
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
var pushDetector = zig.controls.PushDetector();
function addPush(){
pushDetector.addEventListener('push', function(pd) {
console.log('PushDetector: Push');
ce.classList.add('pushed');
var click=sound.cloneNode();
click.play();
fireClick();
});
pushDetector.addEventListener('release', function(pd) {
console.log('PushDetector: Release');
ce.classList.remove('pushed');
});
pushDetector.addEventListener('click', function(pd) {
console.log('PushDetector: Click');
});
}
function removePush(){
console.log("remove push");
pushDetector.removeEventListener('push');
pushDetector.removeEventListener('release');
pushDetector.removeEventListener('click');
}
zig.singleUserSession.addListener(pushDetector);
//PullDetector
var pullDetector = zig.controls.PullDetector();
function addPull(){
console.log("add pull");
pullDetector.addEventListener('pull', function(pd) {
console.log('PullDetector: Pull');
ce.classList.add('pulled');
var click=sound.cloneNode();
click.play();
fireClick();
});
pullDetector.addEventListener('release', function(pd) {
console.log('PullDetector: Release');
ce.classList.remove('pulled');
});
pullDetector.addEventListener('click', function(pd) {
console.log('PullDetector: Click');
});
}
function removePull(){
console.log("remove pull");
pullDetector.removeEventListener('pull');
pullDetector.removeEventListener('release');
pullDetector.removeEventListener('click');
}
zig.singleUserSession.addListener(pullDetector);
zig.addListener(hand);