-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (137 loc) · 4.66 KB
/
script.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
// Store frame for motion functions
var previousFrame = null;
// Setup Leap loop with frame callback function
var controllerOptions = {enableGestures: true};
var position = window.innerWidth;
var acceleration = 5;
var points = 0;
var checkCounter = 0;
var lastTrueCheck = 0;
var randomX = getRandomArbitrary(300, 1000);
var randomY = getRandomArbitrary(-550, 30);
var started = false;
var finished = false;
// to use HMD mode:
// controllerOptions.optimizeHMD = true;
Leap.loop(controllerOptions, function(frame) {
if (checkCounter === 1000) {
checkCounter = 0;
lastTrueCheck = 0;
}
++checkCounter;
// Display Hand object data
if (frame.hands.length > 0) {
if (!started) {
started = true;
$("#presentation").hide();
$("#game").show();
}
if (!finished) {
++points;
$("#points").text(points);
}
var hand = frame.hands[0];
var rotation = Math.floor(hand.palmNormal[0]*50);
var x = hand.palmPosition[0];
var y = hand.palmPosition[1];
var rand = getRandomArbitrary(50,window.innerHeight - 150);
x += 350;
y -= 350;
x *= 2;
y *= 2;
if (position >= window.innerWidth) {
position = 0;
$("#wallBottom").css({height:rand});
$("#wallTop").css({height:window.innerHeight - rand - 150});
}
else position += acceleration;
$("#square").css({left:x,top:-y});
$("#wallTop").css({right:position});
$("#wallBottom").css({right:position});
rotation = -rotation;
$("#square").css({'-webkit-transform' : 'rotate('+ rotation +'deg)',
'-moz-transform' : 'rotate('+ rotation +'deg)',
'-ms-transform' : 'rotate('+ rotation +'deg)',
'transform' : 'rotate('+ rotation +'deg)'});
var leftWall = window.innerWidth - position-70;
var rightWall = window.innerWidth - position;
var topWall = parseInt($("#wallBottom").offset().top) - 150;
var botWall = parseInt($("#wallBottom").offset().top);
if (x+50 >= leftWall && x <= rightWall) {
var posPeix = parseInt($("#square").offset().top) + 25;
if (posPeix >= botWall-25 || posPeix <= topWall+25) {
$("#pointsDiv").hide();
$("#game").hide();
$("#endGame").show();
$("#finalPuntuation").text(points);
finished = true;
}
}
//Check for tortoise
var check1 = false;
var check2 = false;
if (x < randomX + 20 && x > randomX - 20) check1 = true;
if (y < randomY + 20 && y > randomY - 20) check2 = true;
if (check1 && check2) {
if (!(lastTrueCheck > checkCounter - 30)) {
randomX = 5000;
randomY = 5000;
if ($("#dogFood").is(":visible")) {
$("#dogFood").hide();
$("#seaStar").hide();
acceleration -= 4;
if (acceleration <= 0) acceleration = 1;
}
else {
$("#dogFood").hide();
$("#seaStar").hide();
points += 1000;
$("#points").text(points);
}
$("#square").animate({width:'75px', height: '75px', backgroundSize: '75px'}, 100, "linear", function() {
$("#square").animate({width:'50px', height: '50px', backgroundSize: '50px'}, 100, "linear");
});
lastTrueCheck = checkCounter;
}
}
}
// Store frame for motion functions
previousFrame = frame;
})
function vectorToString(vector, digits) {
if (typeof digits === "undefined") {
digits = 1;
}
return "(" + vector[0].toFixed(digits) + ", "
+ vector[1].toFixed(digits) + ", "
+ vector[2].toFixed(digits) + ")";
}
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
function renderFood(x, y) {
var rnd = Math.floor(getRandomArbitrary(1,3));
if (rnd === 1) {
$("#dogFood").show();
$("#seaStar").hide();
$("#dogFood").css({left:x,top:-y});
}
else {
$("#seaStar").show();
$("#dogFood").hide();
$("#seaStar").css({left:x,top:-y});
}
}
$(document).ready(function() {
window.setInterval(function(){
if (started && !finished)
acceleration += 1;
}, 4000);
window.setInterval(function(){
if (started && !finished) {
randomX = getRandomArbitrary(300, 1000);
randomY = getRandomArbitrary(-550, 30);
renderFood(randomX, randomY);
}
}, 15000);
});