/
tiles.js
153 lines (123 loc) · 3.5 KB
/
tiles.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
var width;
var height;
var img;
var tiles;
var tileorder;
var c;
var tile_w;
var tile_h;
var tiles_y;
var tiles_x;
var clicked_tile_x;
var clicked_tile_y;
var EmptyTile;
function MakeTileGame(canvas, imgSrc, puzzleWidth, puzzleHeight)
{
canv = document.getElementById(canvas);
c = canv.getContext('2d');
tiles_x = puzzleWidth;
tiles_y = puzzleHeight;
img = new Image();
img.src = imgSrc;
img.onload = function()
{
width = img.width;
height = img.height;
$("#" + canvas).attr( { width: width, height: height } );
c.drawImage(img, 0, 0);
GetTiles();
tileorder = RandomizeTilePosition();
DrawBoard();
}
$("#" + canvas).click( MoveTile );
}
function MoveTile(event)
{
x = event.clientX;
y = event.clientY;
clicked_tile_x = ( x - x % tile_w ) / tile_w;
clicked_tile_y = ( y - y % tile_h ) / tile_h;
ClickedTile = tileorder[clicked_tile_x * tiles_y + clicked_tile_y];
console.log( "ClickX: " + clicked_tile_x + ", ClickY: " + clicked_tile_y + " Clicked ID: " + ClickedTile + " ClickLocation: " + ( clicked_tile_x * tiles_y + clicked_tile_y) );
// Check all around the clicked area for the blank tile
swap = false;
Right = ( clicked_tile_x + 1 ) * tiles_y + clicked_tile_y;
Above = clicked_tile_x * tiles_y + clicked_tile_y - 1;
Left = ( clicked_tile_x - 1 ) * tiles_y + clicked_tile_y;
Below = clicked_tile_x * tiles_y + clicked_tile_y + 1;
console.log( "Right: " + Right + " with ID " + tileorder[ Right ] );
console.log( "Above: " + Above + " with ID " + tileorder[ Above ] );
ClickedIndex = clicked_tile_x * tiles_y + clicked_tile_y;
// Above
if ( tileorder [ Above ] === ( tiles_x * tiles_y - 1) )
{
SwapTiles(Above, ClickedIndex);
}
// Right
if (tileorder [ Right ] === ( tiles_x * tiles_y - 1 ) )
{
SwapTiles(Right, ClickedIndex);
}
// Below
if (tileorder [ Below ] === ( tiles_x * tiles_y - 1) )
{
SwapTiles(Below, ClickedIndex);
}
// Left
if (tileorder [ Left ] === ( tiles_x * tiles_y - 1) )
{
SwapTiles(Left, ClickedIndex);
}
DrawBoard();
}
function SwapTiles(tile1, tile2)
{
console.log("Swapping " + tile1 + " and " + tile2);
temp = tileorder[tile1];
tileorder[tile1] = tileorder[tile2];
tileorder[tile2] = temp;
}
function RandomizeTilePosition()
{
tileorder = new Array(tiles_x * tiles_y);
// Populate the array with sequential order
for (i = 0; i < tiles_x * tiles_y; i++) { tileorder[i] = i; }
// FUCK IT UP
tileorder.sort( function(a, b) { return 0.5 - Math.random() } );
// YEAH FUCK IT
return tileorder;
}
function DrawBoard()
{
c.fillStyle = "#0000FF";
c.fillRect(0, 0, width, height);
for (i = 0; i < tiles_x; i++)
{
for (ii = 0; ii < tiles_y; ii++)
{
// Last tile, don't draw
if ( tileorder[ i * tiles_y + ii ] === tiles_x * tiles_y - 1)
continue;
c.putImageData(tiles[ tileorder[i * tiles_y + ii] ], i * tile_w, ii * tile_h);
//c.fillText( (i * tiles_y + ii), i * tile_w + tile_w / 2, ii * tile_h + tile_h / 2);
}
}
}
function GetTiles()
{
tiles = new Array();
tile_w = Math.ceil(width / tiles_x);
tile_h = Math.ceil(height / tiles_y);
for (i = 0; i < tiles_x; i++)
{
for (ii = 0; ii < tiles_y; ii++)
{
tiles[i * tiles_y + ii] = c.getImageData(i * tile_w, ii * tile_h, tile_w, tile_h);
}
}
}
function DrawRandomTile()
{
context = document.getElementById("testBuffer").getContext('2d');
context.putImageData(tiles[ Math.floor(Math.random() * 9) ], 0, 0);
}