-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotGame.html
213 lines (185 loc) · 6.21 KB
/
dotGame.html
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 HTML>
<html>
<head>
<title>
Dot Game
</title>
<script src = "https://static.tumblr.com/ow7hyoy/Siwpa2pdc/jquery-3.3.1.min.js"></script>
<style>
#divSvg{
visibility:hidden;
}
#mainSvg{
height:100;
width:100;
position:fixed;
/*visibility:hidden;*/
}
#mainSvg #mainCircle{
cx:50;
cy:50;
r:40;
stroke:blue;
stroke-width:10;
fill:red;
}
#projSvg{
left:50%;
position:absolute;
height:60;
width:60;
}
#startButton{
position: fixed;
left:50%;
top:25%;
height: 3em;
width: 5em;
font-size:2em;
border:none;
background:#09f;
}
#startButton:hover{
background:#0cf;
box-shadow: 0px 0px 10px black;
}
#titleArea{
position:fixed;
left:50%;
top: 12%;
font-size: 2em;
}
</style>
<script>
//location of the cursor
//interval used in game
var t;
function setup(){
$("#startButton").on("click", function(){
var coords = getCoords();
var x = coords[0];
var y = coords[1];
t = setInterval(function(){getUv(x,y);}, 1);
start(x,y);
});
$(window).on("mousemove", function(){
clearInterval(t);
});
}
function stop(){
$(window).off("mousemove");
$("#title").text("Game Over");
clearInterval(t);
$("#divSvg").css("visibility", "hidden");
$("#startButton").css("visibility", "visible");
}
/**
* here is the code to be executed in the "main"
*/
function start(x, y){
$(window).on("mousemove", function(){
clearInterval(t);
t = setInterval(function(){
getUv(x,y);
}, 1);
var coords = getCoords();
x = coords[0];
y = coords[1];
});
$("#title").text("");
$("#divSvg").css("visibility", "visible");
$("#startButton").css("visibility", "hidden");
//sets the position of the coordinate output
$("#Pcoords").css("top", parseInt($(window).height()) - 60);
$("#mainSvg").css("top", $(window).height()*3/4);
$("#mainSvg").css("left", $(window).width()/2);
}
/**
* event handler for mouse move
* updates x and y coordinates of the cursor
*/
function getCoords(){
//-50 adjusts so that the centre of the circle move to the cursor
//not the top left corner
var x = event.pageX;
var y = event.pageY;
if(x>=$(window).width()-10 || y>=$(window).height()-10 || x<=10 || y<=10){
stop();
}
return [x,y];
//$("#Pcoords").html("Coords: "+x+"x "+y+"y");
//getUv(x,y);
}
/**
* calculates the unit vector from the circle to the
* cursor and translates the circle in that direction
* @param x the x ccordinate of the cursor
* @param y the y coordinate of the cursor
*/
function getUv(x, y){
var Ux;
var Uy;
//translation by 50 so that the vector is from the centre of the
//circle to the cursor
var origX = parseFloat($("#mainSvg").css("left"))+50;
var origY = parseFloat($("#mainSvg").css("top"))+50;
var Mv = Math.sqrt(Math.pow((x-origX),2) + Math.pow((y-origY),2));
if(Mv !== 0){
Ux = (x-origX)/Mv;
Uy = (y-origY)/Mv;
//$("#Pcoords").text("test");
}else{
Ux = 0;
Uy = 0;
}
translate(Ux, Uy, x, y, 3);
}
/**
* determines if the cursor is within the circle
* @param origX x coordinate of the circle
* @param origY y coordinate of the circle
* @return true if cursor is within the circle
*/
function isCaptured(origX, origY, x, y){
if((x>=origX && x<=origX+100)&&(y>=origY && y<=origY+100)){
return true;
}
return false;
}
/**
* moves the circle in the direction of the cursor
* @param Ux the x component of the unit vector from circle to cursor
* @param Uy the y component of the unit vector from circle to cursor
* @param vel the velocity of the circle
*/
function translate(Ux, Uy, x, y, vel){
var origX = parseFloat($("#mainSvg").css("left"));
var origY = parseFloat($("#mainSvg").css("top"));
origX += Ux*vel;
origY += Uy*vel;
$("#mainSvg").css("left", origX);
$("#mainSvg").css("top", origY);
if(isCaptured(origX, origY, x, y)){
stop();
}
}
/*
* Used as "main" for code execution
*/
$(function(){
setup();
});
</script>
</head>
<body>
<button id="startButton">Play</button>
<div id = "divSvg">
<svg id = "mainSvg">
<circle id = "mainCircle"/>
</svg>
</div>
<div id = "titleArea">
<p id = "title"></p>
</div>
</body>
</html>