-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
213 lines (171 loc) · 6.3 KB
/
game.html
File metadata and controls
213 lines (171 loc) · 6.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!DOCTYPE html5>
<html>
<head>
<title></title>
</head>
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
<style id="ballStyle">
.circle {
width: 100px;
height: 100px;
background: #fc2e5a;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
position: absolute;
left: 50px;
top: 79px;
}
</style>
<script src="http://remote-tilt.com/js/device-motion-polyfill.js"></script>
<script>
function initBall(item){
var ball = item;
ball.obstacles = new Array();
ball.setTop = function(top){
if(top>=ball.maxTop && top<=ball.maxBottom){
ball.style.top = top + "px" ;
}
}
ball.getTop = function(){
var results = parseFloat(ball.style.top)
if(isNaN(results)){
results =0;
}
return results;
}
ball.setLeft= function(left){
if(left>=ball.maxLeft && left<=ball.maxRight){
ball.style.left = left +"px";
}
}
ball.getLeft = function(){
var results = parseInt(ball.style.left);
if(isNaN(results)){
results=0;
}
return results;
}
ball.moveLeft=function(rate){
ball.setLeft(ball.getLeft()+rate);
}
ball.moveTop = function(rate){
ball.setTop(ball.getTop()+rate);
}
ball.setMaxTop = function(maxTop){
ball.maxTop=maxTop;
}
ball.setMaxLeft = function(maxLeft){
ball.maxLeft=maxLeft;
}
ball.setMaxRight = function(maxRight){
ball.maxRight=maxRight;
}
ball.setMaxBottom = function(maxBottom){
ball.maxBottom = maxBottom;
}
ball.getDiameter= function (){
return parseInt(ball.getCircleStyleSheet().rules[0].style.width);
}
ball.getCircleStyleSheet = function (){
var totalSheets = document.styleSheets.length;
for(var i=0;i<=totalSheets;i++){
if(document.styleSheets[i].rules[0].selectorText == ".circle"){
return document.styleSheets[i];
}
}
}
ball.setBoundaries= function(maxTop,maxLeft,maxBottom,maxRight){
ball.setMaxTop(maxTop);
ball.setMaxLeft(maxLeft);
ball.setMaxRight(maxRight);
ball.setMaxBottom(maxBottom);
}
ball.savePos = function (){
ball.savedTop = ball.getTop();
ball.savedLeft = ball.getLeft();
}
ball.rollBackPos = function (){
ball.setTop(ball.savedTop);
ball.setLeft(ball.savedLeft);
}
ball.isNextPosOK = function (gamma,beta){
var results = true;
window.ball.savePos();
if(gamma>0){
ball.moveLeft(10);
}else if(gamma<0){
ball.moveLeft(-10);
}
if(beta>0){
ball.moveTop(10);
}else if(beta<0){
ball.moveTop(-10);
}
// TODO: problem with scope here...will fix later if there is time
for(var obstacle in window.ball.obstacles){
if(offLimits(window.ball,obstacle)){
window.ball.rollBackPos();
results = false;
}
}
return results;
}
ball.addObstacle = function(item){
ball.obstacles.push(item);
}
ball.getObstacles= function (){
ball.obstacles;
}
return ball;
}
function Wood(item){
return item;
}
function offLimits(ball,wood){
ball.topLeft = [ball.getLeft(),ball.getTop()];
ball.topRight = [ball.getLeft()+ball.getDiameter(),ball.getTop()];
ball.bottomLeft = [ball.getLeft(),ball.getTop()+ball.getDiameter()];
ball.bottomRight = [ball.getLeft()+ball.getDiameter(),ball.getTop()+ball.getDiameter()];
wood.topLeft = [wood.offsetLeft,wood.offsetTop];
wood.topRight = [wood.offsetLeft+wood.clientWidth,wood.offsetTop];
wood.bottomLeft = [wood.offsetLeft,wood.offsetTop+wood.clientHeight];
wood.bottomRight = [wood.offsetLeft+wood.clientWidth,wood.offsetTop+wood.clientHeight];
return isInBox(ball.topLeft,wood) || isInBox(ball.topRight,wood) || isInBox(ball.bottomLeft,wood) || isInBox(ball.bottomRight,wood);
}
function isInBox(point,obj){
var testX = (obj.bottomLeft[0] <= point[0]) && (point[0] <= obj.bottomRight[0]);
var testY = (obj.topLeft[1] <= point[1]) && (point[1] <= obj.bottomLeft[1]);
return (testX && testY)
}
window.onload = function(){
// init the game
window.ball = initBall(document.getElementById('ball'));
window.wood1 = Wood(document.getElementById('wood1'));
window.wood2 = Wood(document.getElementById('wood2'));
ball.setBoundaries(0,0,window.innerHeight-ball.getDiameter(),window.innerWidth-ball.getDiameter());
window.ball.addObstacle(window.wood1);
window.ball.addObstacle(window.wood2);
// console.log(isInBox([2,2],[1,2],[3,3],[1,1],[3,1]));
// console.log(isInBox([4,4],[1,2],[3,3],[1,1],[3,1]));
// console.log(isInBox([0,0],[1,2],[3,3],[1,1],[3,1]));
window.addEventListener("deviceorientation", function(event) {
document.getElementById('alpha').innerText = ball.style.left;
document.getElementById('beta').innerText = ball.style.top;
document.getElementById('gamma').innerText = window.innerHeight + " " + window.innerWidth;
if(ball.isNextPosOK(event.gamma,event.beta)){
ball.moveLeft(event.gamma);
ball.moveTop(event.beta);
};
});
};
</script>
<body>
<div id="alpha">0</div>
<div id="beta">0</div>
<div id="gamma">0</div>
<div class="wood" id="wood1" style="width: 5%;height: 30%;background-color:darkred;position: absolute;left: 50%;top: 0px"></div>
<div class="wood" id="wood2" style="width: 5%;height: 30%;background-color:darkred;position: absolute;left: 50%;bottom: 0px"></div>
<div class="circle" id="ball"></div>
</body>
</html>