-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshoot.js
279 lines (236 loc) · 7.42 KB
/
shoot.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
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
// bowling (physics test)
// Made by clucle 2017
/* Canvas Setting */
var c = document.getElementById("board");
var ctx = c.getContext("2d");
var width = 600;
var height = 600;
/* Board Setting */
// Egg's radius
var radius = 14;
// Board Line distance (horizontal, vertical)
var blank = 12;
// Egg's Array
var egg_array = new Array();
function init(){
// Init Eggs (Spawn)
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
egg_array.push(new Egg(76 + (2 * 32 * i), 76 + (2 * 32 * j), 0));
}
}
// Mouse Event Init
c.addEventListener("mousedown", mouseDownListener, false);
// push using addForce
//egg_array[0].addForce(Math.cos(1), Math.sin(1), 40);
// when push call runPhysics
//runPhysics();
}
function runPhysics(){
// check_meet use for checking kiss Eggs
var check_meet;
for (i = 0; i < egg_array.length; i++) {
if (egg_array[i].speed > 0) {
// Egg Move
egg_array[i].x_pos += egg_array[i].x_dir * egg_array[i].speed;
egg_array[i].y_pos += egg_array[i].y_dir * egg_array[i].speed;
egg_array[i].speed -= 0.1; // accelate = ㎍ (friction) -> 50 Frame /50
for (j = 0; j < egg_array.length; j++) {
check_meet = false;
if (j != i) {
while (isMeet(egg_array[i].x_pos, egg_array[i].y_pos,
egg_array[j].x_pos, egg_array[j].y_pos)) {
if (!check_meet) check_meet = true;
if (egg_array[i].x_pos > egg_array[j].x_pos) {
egg_array[i].x_pos = egg_array[i].x_pos + Math.abs(egg_array[i].x_dir);
} else {
egg_array[i].x_pos = egg_array[i].x_pos - Math.abs(egg_array[i].x_dir);
}
if (egg_array[i].y_pos > egg_array[j].y_pos) {
egg_array[i].y_pos = egg_array[i].y_pos + Math.abs(egg_array[i].y_dir);
} else {
egg_array[i].y_pos = egg_array[i].y_pos - Math.abs(egg_array[i].y_dir);
}
}
if (check_meet) {
// When Kiss Break direction Degree = A
// When Kiss Other Egg's direction between origin Degree = B
// Calculate Two Egg's direction and speed
var kiss_dir_x = (egg_array[j].x_pos - egg_array[i].x_pos);
var kiss_dir_y = (egg_array[j].y_pos - egg_array[i].y_pos);
var distance = Math.sqrt(kiss_dir_x * kiss_dir_x + kiss_dir_y * kiss_dir_y);
egg_array[j].x_dir = kiss_dir_x / distance;
egg_array[j].y_dir = kiss_dir_y / distance;
var cosB = (egg_array[i].x_dir * egg_array[j].x_dir +
egg_array[i].y_dir * egg_array[j].y_dir);
var cosA = Math.sqrt(1 - Math.abs(cosB));
if (cosA < 0.0001 && cosA > 0) cosA = 0.0001;
if (cosA > -0.0001 && cosA < 0) cosA = -0.0001;
if (cosB < 0.0001 && cosB > 0) cosB = 0.0001;
if (cosB > -0.0001 && cosB < 0) cosB = -0.0001;
egg_array[i].x_dir = egg_array[i].x_dir - (egg_array[j].x_dir) * cosB;
egg_array[i].y_dir = egg_array[i].y_dir - (egg_array[j].y_dir) * cosB;
egg_array[j].speed = egg_array[i].speed * (1 / (cosA * cosA / cosB + cosB));
egg_array[i].speed = egg_array[i].speed * (1 / (cosB * cosB / cosA + cosA));
}
}
}
}
}
// If Egg have Energy Run Again
var check_remain_energy = false;
for (i = 0; i < egg_array.length; i++) {
if (egg_array[i].speed > 0) {
check_remain_energy = true;
break;
}
}
if (check_remain_energy) {
setTimeout(runPhysics, 20);
}
}
// Check is Meet
function isMeet(x1, y1, x2, y2) {
var distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
if (distance <= radius * 2) {
return true;
}
return false;
}
// Canvas Loop
function updateBoard(){
// board fill color
ctx.fillStyle="#ffcc66";
ctx.fillRect(0, 0, width, height);
// board draw line
ctx.strokeStyle="#333300";
ctx.fillStyle="#333300";
for (i = 0; i < 19; i++) {
// horizontal line draw
ctx.beginPath();
ctx.moveTo(blank + i * 32, blank);
ctx.lineTo(blank + i * 32, height - blank);
ctx.stroke();
// vertical line draw
ctx.beginPath();
ctx.moveTo(blank, blank + i * 32);
ctx.lineTo(height - blank, blank + i * 32);
ctx.stroke();
}
// board draw point
var circle_radius = 3;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
// board circle draw
ctx.beginPath();
ctx.arc(blank + 3 * 32 + i * 6 * 32, blank + 3 * 32 + j * 6 * 32, circle_radius, 0, 2*Math.PI);
ctx.fill();
ctx.stroke();
}
}
// Draw Shooting Range
if (dragging == true)
{
ctx.beginPath();
ctx.strokeStyle="rgba(255, 255, 255, 0.9)"
ctx.fillStyle="rgba(255, 255, 255, 0.6)"
ctx.arc(egg_array[drag_index].x_pos, egg_array[drag_index].y_pos, radius * 3, 0, 2*Math.PI);
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(egg_array[drag_index].x_pos - drag_x, egg_array[drag_index].y_pos - drag_y);
ctx.lineTo(egg_array[drag_index].x_pos, egg_array[drag_index].y_pos);
ctx.stroke();
}
// Draw Egg
for (i = 0; i < egg_array.length; i++) {
ctx.beginPath();
if (egg_array[i].color == 0) {
ctx.strokeStyle="#000000";
ctx.fillStyle="#000000";
} else {
ctx.strokeStyle="#FFFFFF";
ctx.fillStyle="#FFFFFF";
}
ctx.arc(egg_array[i].x_pos, egg_array[i].y_pos, radius, 0, 2*Math.PI);
ctx.fill();
ctx.stroke();
}
setTimeout(updateBoard, 20);
}
/* Mouse Event */
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
/* Drag and Drop */
var dragging = false;
var drag_index;
var drag_x;
var drag_y;
function mouseDownListener(evt) {
var canvas_blank = c.getBoundingClientRect();
var canvas_x = (evt.clientX - canvas_blank.left) * (c.width / canvas_blank.width);
var canvas_y = (evt.clientY - canvas_blank.top) * (c.height / canvas_blank.height);
var i;
for (i = 0; i < egg_array.length; i++) {
if (egg_array[i].HitTest(canvas_x, canvas_y)) {
dragging = true;
drag_x = egg_array[i].x_pos - canvas_x;
drag_y = egg_array[i].y_pos - canvas_y;
drag_index = i;
}
}
if (dragging) {
window.addEventListener("mousemove", mouseMoveListener, false);
window.addEventListener("mouseup", mouseUpListener, false);
}
}
function mouseMoveListener(evt) {
var canvas_blank = c.getBoundingClientRect();
var canvas_x = (evt.clientX - canvas_blank.left) * (c.width / canvas_blank.width);
var canvas_y = (evt.clientY - canvas_blank.top) * (c.height / canvas_blank.height);
drag_x = egg_array[drag_index].x_pos - canvas_x;
drag_y = egg_array[drag_index].y_pos - canvas_y;
}
function mouseUpListener(evt) {
window.removeEventListener("mousemove", mouseMoveListener, false);
window.removeEventListener("mouseup", mouseUpListener, false);
dragging = false;
var distance = Math.sqrt(drag_x * drag_x + drag_y * drag_y);
var x_dir = drag_x / distance;
var y_dir = drag_y / distance;
if (distance > 3 * radius) {
// push using addForce
egg_array[drag_index].addForce(x_dir, y_dir, distance / 5);
distance = 0;
// when push call runPhysics
runPhysics();
}
}
/* Drag and Drop End */
// Egg Class
function Egg(x_pos, y_pos, color) {
this.x_pos = x_pos;
this.y_pos = y_pos;
this.color = color;
this.x_dir = 0;
this.y_dir = 0;
this.speed = 0;
this.initspeed = 0;
}
Egg.prototype.addForce = function(x_dir, y_dir, force) {
this.x_dir = x_dir;
this.y_dir = y_dir;
this.speed = Math.sqrt(2 * force);
return true;
};
Egg.prototype.HitTest = function(cx, cy) {
return ((cx > this.x_pos - radius) && (cx < this.x_pos + radius)
&& (cy > this.y_pos - radius) && (cy < this.y_pos + radius));
}
init();
updateBoard();