-
Notifications
You must be signed in to change notification settings - Fork 2
/
HackReactor Game
293 lines (283 loc) · 8.12 KB
/
HackReactor Game
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<html>
<head>
</head>
<body id="body"> </body>
<!-- <link rel="stylesheet" type="text/css" media="screen" href="HRGame.css"> -->
<canvas id="canvas" height="1050" width="1050"> </canvas>
<style type="text/css" media="screen">
canvas, img {display: block; margin: 1em auto; border: 1px solid black;}
#canvas {
background-image: url(pink.png);
background-color: rgb(117, 67, 54);
/*background-image: url(wood1.jpg);*/
}
#body {
background-image: url(wood1.jpg);
}
</style>
<script>
var width = 1050;//width of bg
var height = 1050;//height of bg
var canvas = ctx = false;
var frameRate = 1/40; //
var frameDelay = frameRate * 400; // ms
var loopTimer = false;
var player1=true;
var shotInRange=false;
function createBall(x, y){
var ball = {
mass: 0.1, //kg
radius: 30, // 1px = 1cm // Size of ball
restitution: -0.7, // Effects speed
line: {},
velocity: {x: 0, y: 0},
Cd: 0.4,
rho: 1,
A: Math.PI * 30 /* ball radius*/* 30 /* ball radius*// (10000),
fx:{},
fy:{},
ax:{},
shootable: true
};
ball.position = {x: x, y: y}; // starting position // start speed
return ball;
}
var ball1 = createBall(200, 220);
var ball2 = createBall(800, 800);
var mouse = {x: 0, y: 0, isDown: false};
function getMousePosition(e) {
mouse.x = e.pageX - canvas.offsetLeft;
mouse.y = e.pageY - canvas.offsetTop;
//console.log(mouse)
}
var mouseDown = function(e,ball) {
if (e.which == 1)
{
getMousePosition(e);
mouse.isDown = true;
}
}
var launchBall = function(ball){
ball.velocity.y = (ball.position.y - mouse.y) /10;
ball.velocity.x = (ball.position.x - mouse.x) / 10;
}
var mousexInRange = function (ball)
{
if (mouse.x-ball.position.x<180 && mouse.x-ball.position.x>-180)
{
return true;
}
return false;
}
var mouseyInRange = function (ball)
{
if (mouse.y-ball.position.y<180 && mouse.y-ball.position.y>-180)
{
return true;
}
return false;
}
var mouseInRange = function (ball)
{
if (mouseyInRange(ball) && mousexInRange(ball))
{
return true;
}
return false;
}
var mouseUp = function(e,ball) {
if (e.which == 1)
{
mouse.isDown = false;
if (player1 && ball1.shootable && shotInRange)
{
launchBall(ball1);
ball1.shootable=false;
}
else if (!player1 && ball2.shootable && shotInRange)
{
launchBall(ball2);
ball2.shootable=false;
}
}
}
var setup = function() {
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
canvas.onmousemove = getMousePosition;
canvas.onmousedown = mouseDown;
canvas.onmouseup = mouseUp;
loopTimer = setInterval(loop, frameDelay);
}
var setBallSpeed=function(ball){
ball.fx = -0.5 * ball.Cd * ball.A * ball.rho * ball.velocity.x * ball.velocity.x * ball.velocity.x / Math.abs(ball.velocity.x);
ball.fy = -0.5 * ball.Cd * ball.A * ball.rho * ball.velocity.y * ball.velocity.y * ball.velocity.y / Math.abs(ball.velocity.y);
}
var ballShouldStop=function(ball){
if ((ball.velocity.x<0.5 && ball.velocity.x>0) && (ball.velocity.y<0.5 && ball.velocity.y>0)||(ball.velocity.y>-0.5 && ball.velocity.y<0))
{
return true;
}
else if ((ball.velocity.x>-0.5 && ball.velocity.x<0) && (ball.velocity.y<0.5 && ball.velocity.y>0)||(ball.velocity.y>-0.5 && ball.velocity.y<0))
{
return true;
}
return false;
}
var stopBall= function(ball){
ball.velocity.x=0;
ball.velocity.y=0;
ball.shootable=true;
}
var setBallLoopInfo=function(ball){
ball.fx = (isNaN(ball.fx) ? 0 : ball.fx);
ball.fy = (isNaN(ball.fy) ? 0 : ball.fy);
ball.ax = ball.fx / ball.mass;
ball.ay = ball.fy / ball.mass;
ball.velocity.x += ball.ax*frameRate;
ball.velocity.y += ball.ay*frameRate;
ball.position.x += ball.velocity.x*frameRate*100;
ball.position.y += ball.velocity.y*frameRate*100;
}
var wallCollision=function(ball){
if (ball.position.x<=30||ball.position.x>=1020)
{
ball.velocity.x = ball.velocity.x * (-1);
}
if (ball.position.y<=30||ball.position.y>=1020)
{
ball.velocity.y = ball.velocity.y * (-1);
}
}
var hole= function (ball){
if (ball.position.x>325+ball.radius && ball.position.x<725-ball.radius && ball.position.y>325+ball.radius && ball.position.y<725-ball.radius)
{
return true;
}
return false;
}
var outsideDonut= function (ball){
if (!ball.position.x>0+ball.radius && !ball.position.x<1050-ball.radius && !ball.position.y>0+ball.radius && !ball.position.y<1050-ball.radius)
{
return true;
}
return false;
}
var loop = function(){
if ( ! mouse.isDown && shotInRange)
{
if (player1)
{
setBallSpeed(ball1);
wallCollision(ball1);
if (hole(ball1))
{
// console.log ("sucess");
}
if (outsideDonut(ball1))
{
console.log ("sucess");
}
if (ballShouldStop(ball1))
{
stopBall(ball1);
shotInRange=false;
player1=false;
}
setBallLoopInfo(ball1);
}
else if (!player1 && shotInRange)
{
setBallSpeed(ball2);
wallCollision(ball2);
if (hole(ball2))
{
//console.log ("sucess2");
}
if (outsideDonut(ball2))
{
console.log ("sucess");
}
if (ballShouldStop(ball2))
{
stopBall(ball2);
shotInRange=false;
player1=true;
}
setBallLoopInfo(ball2);
}
//ball collision
// if(ball1.position.x-ball2.position.x< 20 && ball1.position.x-ball2.position.x >0 || ball1.position.x - ball2.position.x > -20 && ball1.position.x - ball2.position.x < 0 )
// {
// ball1.velocity.y = ball1.velocity.y * (-1);
// ball2.velocity.y = ball2.velocity.y * (-1);
// console.log("ball1:",ball1.position,"ball2:",ball2.position)
// }
// if (ball1.position.y - ball2.position.y > -20 && ball1.position.y - ball2.position.y < 0 || ball1.position.y-ball2.position.y<20 && ball1.position.y-ball2.position.y >0)
// {
// ball1.velocity.y = ball1.velocity.y * (-1);
// ball2.velocity.y = ball2.velocity.y * (-1);
// console.log("ball1:",ball1.position,"ball2:",ball2.position)
// }
}
var drawBall=function(ball){
ctx.beginPath();
ctx.arc(ball.position.x, ball.position.y, ball.radius, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
}
ctx.clearRect(0,0,width,height);
ctx.save();
//ctx.translate(ball1.position.x, ball1.position.y);
ctx.fillStyle = 'red';
drawBall(ball1);
ctx.fillStyle ='blue';
drawBall(ball2);
ctx.restore();
var lineToMouse= function (ball){
ctx.beginPath();
ctx.moveTo(ball.position.x, ball.position.y);
if (ball.shootable){//once ball shot no longer shootable
if (mousexInRange(ball) && mouseyInRange(ball))
{
ctx.lineTo(mouse.x, mouse.y);
ball.line.x=mouse.x;
ball.line.y=mouse.y;
shotInRange=true;
}
else if (mousexInRange(ball) && !mouseyInRange(ball))
{
ctx.lineTo(mouse.x, ball.line.y);
ball.line.x=mouse.x;
}
else if (!mousexInRange(ball) && mouseyInRange(ball))
{
ctx.lineTo(ball.line.x, mouse.y);
ball.line.y=mouse.y;
}
else
{
ctx.lineTo(ball.line.x, ball.line.y);
}
ctx.lineWidth = 10;
ctx.stroke();
ctx.closePath();
}
}
if (mouse.isDown)
{
if (player1)
{
ctx.strokeStyle = 'red';
lineToMouse(ball1);
}
else
{
ctx.strokeStyle = 'blue';
lineToMouse(ball2);
}
}
}
setup();
</script>
</html>