-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
284 lines (268 loc) · 12.3 KB
/
index.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
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="draw.js"></script>
<script src="game.js"></script>
<script src="simplecanvas.js"></script>
<script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<meta name="viewport" content="width=device-width, user-scalable=no">
<style>
body {
user-select:none;
-moz-user-select:none;
-webkit-user-select:none;
-ms-user-select:none;
}
#main {
left:0;
top:0;
float:left;
margin-right:20px;
}
canvas {
width: 96vmin;
height: 96vmin;
image-rendering: optimizeSpeed; /* Older versions of FF */
image-rendering: -moz-crisp-edges; /* FF 6.0+ */
image-rendering: -webkit-optimize-contrast; /* Safari */
image-rendering: -o-crisp-edges; /* OS X & Windows Opera (12.02+) */
image-rendering: pixelated; /* Awesome future-browsers */
-ms-interpolation-mode: nearest-neighbor; /* IE */
}
</style>
</head>
<body>
<div id="main">
</div>
2048을 만드세요!<BR>
<button id="restart">재시작</button>
<script>
(function () {
// initializes touch and scroll events
var supportTouch = $.support.touch,
scrollEvent = "touchmove scroll",
touchStartEvent = supportTouch ? "touchstart" : "mousedown",
touchStopEvent = supportTouch ? "touchend" : "mouseup",
touchMoveEvent = supportTouch ? "touchmove" : "mousemove";
// handles swipe up and swipe down
$.event.special.swipeupdown = {
setup: function () {
var thisObject = this;
var $this = $(thisObject);
$this.bind(touchStartEvent, function (event) {
var data = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] :
event,
start = {
time: (new Date).getTime(),
coords: [ data.pageX, data.pageY ],
origin: $(event.target)
},
stop;
function moveHandler(event) {
if (!start) {
return;
}
var data = event.originalEvent.touches ?
event.originalEvent.touches[ 0 ] :
event;
stop = {
time: (new Date).getTime(),
coords: [ data.pageX, data.pageY ]
};
// prevent scrolling
if (Math.abs(start.coords[1] - stop.coords[1]) > 10) {
event.preventDefault();
}
}
$this
.bind(touchMoveEvent, moveHandler)
.one(touchStopEvent, function (event) {
$this.unbind(touchMoveEvent, moveHandler);
if (start && stop) {
if (stop.time - start.time < 1000 &&
Math.abs(start.coords[1] - stop.coords[1]) > 30 &&
Math.abs(start.coords[0] - stop.coords[0]) < 75) {
start.origin
.trigger("swipeupdown")
.trigger(start.coords[1] > stop.coords[1] ? "swipeup" : "swipedown");
}
}
start = stop = undefined;
});
});
}
};
//Adds the events to the jQuery events special collection
$.each({
swipedown: "swipeupdown",
swipeup: "swipeupdown"
}, function (event, sourceEvent) {
$.event.special[event] = {
setup: function () {
$(this).bind(sourceEvent, $.noop);
}
};
});
})();
is_moving = false;
moving_start_time = new Date().getTime();
function draw_board(board) {
var last_step = false;
var TIME_STEP1 = 0.2
var TIME_STEP2 = 0.1
if (is_moving) {
var dt = (new Date().getTime()-moving_start_time)/1000;
var rate = dt / TIME_STEP1;
if (dt > TIME_STEP1+TIME_STEP2) {
is_moving = false;
} else if (dt > TIME_STEP1) {
last_step = true;
}
}
for(var x=0;x<4;x++)
for(var y=0;y<4;y++)
if (board[y*4+x] != 0) {
if (last_step) {
if (board[y*4+x][1].length >= 2) {
____find_and_call_function([
['EXPR', x],
['EXPR', y],
['NAME', '에'],
['EXPR', board[y*4+x][0]],
['NAME', '타일'],
['NAME', '그리기'],
['EXPR', true],
['EXPR', false],
], {}, ____functions);
} else if (board[y*4+x][1].length == 1 && board[y*4+x][1][0] == -1) {
____find_and_call_function([
['EXPR', x],
['EXPR', y],
['NAME', '에'],
['EXPR', board[y*4+x][0]],
['NAME', '타일'],
['NAME', '그리기'],
['EXPR', false],
['EXPR', true],
], {}, ____functions);
} else {
____find_and_call_function([
['EXPR', x],
['EXPR', y],
['NAME', '에'],
['EXPR', board[y*4+x][0]],
['NAME', '타일'],
['NAME', '그리기'],
['EXPR', false],
['EXPR', false],
], {}, ____functions);
}
} else if (is_moving) {
if (board[y*4+x][1].length >= 2) {
for(var pi = 0; pi < board[y*4+x][1].length; pi++)
{
var p = board[y*4+x][1][pi];
fx = (p-1)%4;
fy = ((p-1)/4)>>0;
dx = (x-fx)*rate;
dy = (y-fy)*rate;
____find_and_call_function([
['EXPR', fx+dx],
['EXPR', fy+dy],
['NAME', '에'],
['EXPR', (board[y*4+x][0]/2)>>0],
['NAME', '타일'],
['NAME', '그리기'],
['EXPR', false],
['EXPR', false],
], {}, ____functions);
}
} else if (board[y*4+x][1].length == 1 && board[y*4+x][1][0] == -1) {
;
} else {
fx = (board[y*4+x][1][0]-1)%4
fy = ((board[y*4+x][1][0]-1)/4) >>0
dx = (x-fx)*rate
dy = (y-fy)*rate
____find_and_call_function([
['EXPR', fx+dx],
['EXPR', fy+dy],
['NAME', '에'],
['EXPR', board[y*4+x][0]],
['NAME', '타일'],
['NAME', '그리기'],
['EXPR', false],
['EXPR', false],
], {}, ____functions);
}
} else {
____find_and_call_function([
['EXPR', x],
['EXPR', y],
['NAME', '에'],
['EXPR', board[y*4+x][0]],
['NAME', '타일'],
['NAME', '그리기'],
['EXPR', false],
['EXPR', false],
], {}, ____functions)
}
}
}
$(document).ready(
function() {
sc=simplecanvas;
CELL_SPACING = 타일간격;
sc.init(2+CELL_SPACING*4-1,2+CELL_SPACING*4-1,"main");
putpixel = sc.putpixel;
putrectangle = sc.putrectangle;
var board = ____find_and_call_function([['NAME', '새'], ['NAME', '게임'], ['NAME', '준비']], {}, ____functions)
$("#restart").click(function(){
board = ____find_and_call_function([['NAME', '새'], ['NAME', '게임'], ['NAME', '준비']], {}, ____functions);
is_moving = true;
});
function handle_key(key) {
// 38
// 37 40 39
if (is_moving) return;
ret = false;
if (key == 38) //up
ret = ____find_and_call_function([['EXPR', board], ['NAME', '위로']], {}, ____functions)
else if (key == 40) //down
ret = ____find_and_call_function([['EXPR', board], ['NAME', '아래로']], {}, ____functions)
else if (key == 39) //right
ret = ____find_and_call_function([['EXPR', board], ['NAME', '오른쪽으로']], {}, ____functions)
else if (key == 37) //left
ret = ____find_and_call_function([['EXPR', board], ['NAME', '왼쪽으로']], {}, ____functions)
if (ret) {
____find_and_call_function([['EXPR', board], ['NAME', '랜덤'], ['NAME', '생성']], {}, ____functions);
moving_start_time = new Date().getTime();
is_moving = true;
}
}
$('canvas').on('swipeup', function(){handle_key(38);});
$('canvas').on('swipedown', function(){handle_key(40);});
$('canvas').on('swipeleft', function(){handle_key(37);});
$('canvas').on('swiperight', function(){handle_key(39);});
sc.onkeydown(handle_key);
sc.run(function() {
sc.clear([0,0,0])
draw_board(board);
});
sc.mainloop(
function(force_redraw) {
if (is_moving || force_redraw) {
console.log(new Date().getTime());
sc.clear([0,0,0])
draw_board(board);
}
}
);
});
//var ctx = document.getElementById('main').getContext('2d');
</script>
</body>
</html>