aboutsummaryrefslogtreecommitdiff
path: root/src/pp/static/main.js
blob: 5930b323436b5d5dc49a83419ea353cd3f9d8083 (plain)
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// The callbacks to be called each frame on update, 
// and on draw
// The interval object
var gameInterval, selectedX, selectedY, selectedLayer, timeElapsed, timeStart, frame;
let width = 800;
let height = 600;
let fps = 30;
let gameOver = false

let mouseX, mouseY;

let IMG_WIDTH = 33
let IMG_HEIGHT = 39
let TILE_WIDTH = 2 + IMG_WIDTH
let TILE_HEIGHT = 2 + IMG_HEIGHT

var username = undefined

// leave first two blank
var board

window.onload = function () {
    canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");
    canvas.width = width;
    canvas.height = height;
    document.addEventListener("mousedown", mouseDownCallback);
    document.addEventListener("mouseup", mouseUpCallback);
    document.addEventListener("mousemove", function (e) {
        mouseX = e.x
        mouseY = e.y
    });
    init();
}

function game() {
    frame++
    updateCallback();
    drawCallback();
}

function init() {
    gameInterval = setInterval(game, 1000 / fps);
    timeStart = Math.floor(new Date().getTime() / 1000)
    timeElapsed = 0
    frame = 0
    loadTemplate()
}

function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

function swapTiles() {
    count = 0
    template = []
    for (var layer = 0; layer < board.length; layer++) {
        template.push([])
        for (var x = 0; x < board[layer].length; x++) {
            template[layer].push([])
            for (var y = 0; y < board[layer][x].length; y++) {
                template[layer][x].push(undefined)
                template[layer][x][y] = board[layer][x][y]
                if (board[layer][x][y] != undefined) {
                    count++
                }
            }
        }
    }
    for (var i = 0; i < count / 2; i++) {
        items.push(i)
        items.push(i)
    }
    shuffle(items)
    board = []
    for (var layer = 0; layer < template.length; layer++) {
        board.push([])
        for (var x = 0; x < template[layer].length; x++) {
            board[layer].push([])
            for (var y = 0; y < template[layer][x].length; y++) {
                board[layer][x].push(undefined)
                if (template[layer][x][y] != undefined) {
                    board[layer][x][y] = items.pop()
                }
            }
        }
    }
}

function countItems(template) {
    var count = 0;
    for (var layer = 0; layer < template.length; layer++) {
        for (var x = 0; x < template[layer].length; x++) {
            for (var y = 0; y < template[layer][x].length; y++) {
                if (template[layer][x][y]) {
                    count++
                }
            }
        }
    }
    return count
}

function loadTemplate() {
    template = dragon
    var itemCount = countItems(template) / 2
    items = []
    for (var i = 0; i < itemCount; i++) {
        items.push(i)
        items.push(i)
    }
    shuffle(items)
    board = []
    for (var layer = 0; layer < template.length; layer++) {
        board.push([])
        for (var x = 0; x < template[layer].length; x++) {
            board[layer].push([])
            for (var y = 0; y < template[layer][x].length; y++) {
                board[layer][x].push(undefined)
                if (template[layer][x][y]) {
                    board[layer][x][y] = items.pop() % 8
                }
            }
        }
    }
}

var updateCallback = function () {
    timeElapsed = Math.floor(new Date().getTime() / 1000) - timeStart

    var done = true
    for (var layer = 0; layer < board.length; layer++) {
        for (var x = 0; x < board[layer].length; x++) {
            for (var y = 0; y < board[layer][x].length; y++) {
                if (board[layer][x][y] != undefined) {
                    done = false
                }
            }
        }
    }
    if (done) {
        clearInterval(gameInterval)
        gameOver = true
    }
}

var drawCallback = function () {
    font(48);
    ctx.fillStyle = "#99b3ff";
    ctx.fillRect(0, 0, width, height);
    ctx.fillStyle = "#b30000"
    ctx.fillRect(0, 0, 800, 50)
    ctx.fillRect(600, 0, 200, 600)
    ctx.lineWidth = 2
    ctx.strokeStyle = "#ffcc00"
    ctx.fillStyle = "#ffcc00"
    ctx.strokeRect(1, 50, 600, 549)
    font(50)
    ctx.fillText("Picture Pieces", 100, 36)

    if (gameOver) {
        ctx.fillStyle = "#000080"
        font(50)
        ctx.fillText("Congratuations", 100, 200)
        font(24)
        var minutes = Math.floor(timeElapsed / 60)
        var seconds = timeElapsed % 60
        ctx.fillText("Your final time was " + minutes + ":" + seconds, 110, 240)
        username = submitScore("picture pieces", timeElapsed, username);
        return
    }


    let tileLeftOffset = 6
    let tileBottomOffset = 6
    for (var layer = 0; layer < board.length; layer++) {
        var xOffset = layer * tileLeftOffset
        var yOffset = -1 * layer * tileBottomOffset
        for (var x = board[layer].length - 1; x >= 0; x--) {
            for (var y = 0; y < board[layer][x].length; y++) {
                if (board[layer][x][y] == undefined) {
                    continue
                }
                ctx.fillStyle = "#ffe6e6"
                ctx.beginPath()
                ctx.moveTo(x * TILE_WIDTH + xOffset, y * TILE_HEIGHT + yOffset)
                ctx.lineTo(x * TILE_WIDTH - tileLeftOffset + xOffset, y * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.lineTo(x * TILE_WIDTH - tileLeftOffset + xOffset, (y + 1) * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.lineTo(x * TILE_WIDTH + xOffset, (y + 1) * TILE_HEIGHT + yOffset)
                ctx.closePath()
                ctx.fill();

                ctx.fillStyle = '#ff9999'
                ctx.beginPath()
                ctx.moveTo(x * TILE_WIDTH + xOffset, (y + 1) * TILE_HEIGHT + yOffset)
                ctx.lineTo((x + 1) * TILE_WIDTH + xOffset, (y + 1) * TILE_HEIGHT + yOffset)
                ctx.lineTo((x + 1) * TILE_WIDTH - tileLeftOffset + xOffset, (y + 1) * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.lineTo(x * TILE_WIDTH - tileLeftOffset + xOffset, (y + 1) * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.closePath()
                ctx.fill();

                ctx.lineWidth
                ctx.strokeStyle = "#ffcc00"
                ctx.beginPath()
                ctx.moveTo(x * TILE_WIDTH + xOffset, y * TILE_HEIGHT + yOffset)
                ctx.lineTo(x * TILE_WIDTH - tileLeftOffset + xOffset, y * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.lineTo(x * TILE_WIDTH - tileLeftOffset + xOffset, (y + 1) * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.lineTo(x * TILE_WIDTH + xOffset, (y + 1) * TILE_HEIGHT + yOffset)
                ctx.closePath()
                ctx.stroke();
                ctx.beginPath()
                ctx.moveTo(x * TILE_WIDTH + xOffset, (y + 1) * TILE_HEIGHT + yOffset)
                ctx.lineTo((x + 1) * TILE_WIDTH + xOffset, (y + 1) * TILE_HEIGHT + yOffset)
                ctx.lineTo((x + 1) * TILE_WIDTH - tileLeftOffset + xOffset, (y + 1) * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.lineTo(x * TILE_WIDTH - tileLeftOffset + xOffset, (y + 1) * TILE_HEIGHT + tileBottomOffset + yOffset)
                ctx.closePath()
                ctx.stroke();
            }
        }

        ctx.strokeStyle = "#ffcc00"
        var xOffset = layer * tileLeftOffset
        var yOffset = -1 * layer * tileBottomOffset
        for (var x = 0; x < board[layer].length; x++) {
            for (var y = 0; y < board[layer][x].length; y++) {
                if (board[layer][x][y] == undefined) {
                    continue
                }
                ctx.strokeRect(x * TILE_WIDTH + xOffset, y * TILE_HEIGHT + yOffset, TILE_WIDTH, TILE_HEIGHT)
                ctx.drawImage(document.getElementById("img" + board[layer][x][y]), 1 + x * TILE_WIDTH + xOffset, 1 + y * TILE_HEIGHT + yOffset)
            }
        }
    }

    ctx.strokeStyle = "#ffcc00"
    ctx.fillStyle = "#ffcc00"
    font(24)
    ctx.fillText("Time: " + timeElapsed, 600, 36)
    ctx.fillText("Shuffle", 650, 130)

    if (650 - 10 < mouseX && mouseX < 700 + 10 && 150 - 10 < mouseY && mouseY < 150 + 10) {
        ctx.fillStyle = "#ffee22"
    } else {
        ctx.fillStyle = "#ffcc00"
    }
    ctx.beginPath();
    ctx.moveTo(700, 150 + 10)
    ctx.arc(650, 150, 10, Math.PI / 2, 3 * Math.PI / 2)
    ctx.lineTo(700, 150 - 10)
    ctx.arc(700, 150, 10, 3 * Math.PI / 2, Math.PI / 2)
    ctx.fill();
    // ctx.fillRect(625, 125, 150, 20)

    ctx.strokeStyle = "red"
    xOffset = selectedLayer * tileLeftOffset
    yOffset = -1 * selectedLayer * tileBottomOffset
    if (selectedX != undefined && selectedY != undefined && selectedLayer != undefined && board[selectedLayer][selectedX][selectedY] != undefined) {
        ctx.beginPath();
        let rad = TILE_WIDTH / 2 + Math.cos(frame / 7) * 2
        ctx.arc(selectedX * TILE_WIDTH + TILE_WIDTH / 2 + xOffset, selectedY * TILE_HEIGHT + TILE_HEIGHT / 2 + yOffset, rad, 0, 2 * Math.PI)
        ctx.stroke()
    }
}

var mouseDownCallback = function (e) {
    if (650 - 10 < mouseX && mouseX < 700 + 10 && 150 - 10 < mouseY && mouseY < 150 + 10) {
        swapTiles(board)
        return
    }

    var tileX = Math.floor(e.x / TILE_WIDTH)
    var tileY = Math.floor(e.y / TILE_HEIGHT)
    for (var layer = board.length - 1; layer >= 0; layer--) {
        if (board[layer][tileX] != undefined && board[layer][tileX][tileY] != undefined) {
            tileLayer = layer
            break
        }
    }

    if (tileLayer != undefined && board[tileLayer][tileX] != undefined && board[tileLayer][tileX][tileY] != undefined) {
        if (selectedX != undefined &&
            selectedY != undefined &&
            selectedLayer != undefined) {
            if (board[selectedLayer][selectedX][selectedY] != undefined &&
                (tileX != selectedX || tileY != selectedY) &&
                (board[tileLayer][tileX + 1][tileY] == undefined || board[tileLayer][tileX - 1][tileY] == undefined)) {
                if (board[selectedLayer][selectedX][selectedY] == board[tileLayer][tileX][tileY]) {
                    board[selectedLayer][selectedX][selectedY] = undefined
                    board[tileLayer][tileX][tileY] = undefined
                }
            }
        }
        if (board[tileLayer][tileX + 1][tileY] == undefined || board[tileLayer][tileX - 1][tileY] == undefined) {
            selectedLayer = tileLayer
            selectedX = tileX
            selectedY = tileY
        }
    }
}

var mouseUpCallback = function (e) {

}

function font(size) {
    ctx.font = size + "px Courier";
}