-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
211 lines (185 loc) · 5.8 KB
/
Copy pathbundle.js
File metadata and controls
211 lines (185 loc) · 5.8 KB
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
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
const debug = false;
/* Classes */
const Game = require('./game');
/* Global variables */
var canvas = document.getElementById('screen');
var game = new Game(canvas, update, render);
var player = 0;
var scores = [0,0];
var image = new Image();
image.src = 'assets/animals.png';
var blip = new Audio();
blip.src = 'blip.wav';
var flip = new Audio();
flip.src = 'flip.wav';
var pair = new Audio();
pair.src = 'pair.wav';
var state = "waiting for first click";
var card1, card2;
var timer = 0;
// We have 9 pairs of possible cards that are about 212px square
var cards = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8];
var board = [];
while(cards.length > 0) {
var index = Math.floor(Math.random() * (cards.length - 1));
board.push({card: cards[index], flip: false});
cards.splice(index, 1);
}
// TODO: Place the cards on the board in random order
canvas.onclick = function(event) {
event.preventDefault();
switch(state) {
case "waiting for first click":
if(!board[currentIndex] || board[currentIndex].flip ) return blip.play();
card1 = board[currentIndex];
card1.flip = true;
flip.play();
state = "waiting for second click";
break;
case "waiting for second click":
if(!board[currentIndex] || board[currentIndex].flip ) return blip.play();
card2 = board[currentIndex];
card2.flip = true;
flip.play();
state = "waiting ...";
timer = 0;
break;
}
}
var currentIndex, currentX, currentY;
canvas.onmousemove = function(event) {
event.preventDefault();
currentX = event.offsetX;
currentY = event.offsetY;
var x = Math.floor((currentX + 3) / 165);
var y = Math.floor((currentY + 3) / 165);
currentIndex = y * 6 + x;
}
/**
* @function masterLoop
* Advances the game in sync with the refresh rate of the screen
* @param {DOMHighResTimeStamp} timestamp the current time
*/
var masterLoop = function(timestamp) {
game.loop(timestamp);
window.requestAnimationFrame(masterLoop);
}
masterLoop(performance.now());
/**
* @function update
* Updates the game state, moving
* game objects and handling interactions
* between them.
* @param {DOMHighResTimeStamp} elapsedTime indicates
* the number of milliseconds passed since the last frame.
*/
function update(elapsedTime) {
if(state == "waiting ...") {
timer += elapsedTime;
if(timer > 2000) {
if(card1.card == card2.card) {
pair.play();
} else {
blip.play();
card1.flip = false;
card2.flip = false;
player = (player == 0) ? 1 : 0;
}
state = "waiting for first click";
}
}
}
/**
* @function render
* Renders the current game state into a back buffer.
* @param {DOMHighResTimeStamp} elapsedTime indicates
* the number of milliseconds passed since the last frame.
* @param {CanvasRenderingContext2D} ctx the context to render to
*/
function render(elapsedTime, ctx) {
ctx.fillStyle = "#ff7777";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// TODO: Render the board
for(var y = 0; y < 3; y++) {
for(var x = 0; x < 6; x++) {
var card = board[y * 6 + x];
if(card.flip) {
// render cute animal
ctx.drawImage(image,
// Source rect
card.card % 3 * 212 + 2, Math.floor(card.card / 3) * 212 + 2, 212, 212,
// Dest rect
x * 165 + 3, y * 165 + 3, 160, 160
);
} else {
// draw the back of the card (212x212px)
ctx.fillStyle = "#3333ff";
ctx.fillRect(x * 165 + 3, y * 165 + 4, 160, 160);
}
}
}
if(debug) {
var x = currentIndex % 6;
var y = Math.floor(currentIndex / 6);
ctx.strokeStyle = "#ff0000";
ctx.beginPath();
ctx.arc(currentX, currentY, 3, 0, 2*Math.PI);
ctx.rect(x * 165 + 3, y * 165 + 3, 163, 163);
ctx.stroke();
}
ctx.strokeStyle = "black";
ctx.font = "48px serif";
ctx.strokeText("Player " + (player + 1) + "'s Turn", 350, 550);
}
},{"./game":2}],2:[function(require,module,exports){
"use strict";
/**
* @module exports the Game class
*/
module.exports = exports = Game;
/**
* @constructor Game
* Creates a new game object
* @param {canvasDOMElement} screen canvas object to draw into
* @param {function} updateFunction function to update the game
* @param {function} renderFunction function to render the game
*/
function Game(screen, updateFunction, renderFunction) {
this.update = updateFunction;
this.render = renderFunction;
// Set up buffers
this.frontBuffer = screen;
this.frontCtx = screen.getContext('2d');
this.backBuffer = document.createElement('canvas');
this.backBuffer.width = screen.width;
this.backBuffer.height = screen.height;
this.backCtx = this.backBuffer.getContext('2d');
// Start the game loop
this.oldTime = performance.now();
this.paused = false;
}
/**
* @function pause
* Pause or unpause the game
* @param {bool} pause true to pause, false to start
*/
Game.prototype.pause = function(flag) {
this.paused = (flag == true);
}
/**
* @function loop
* The main game loop.
* @param{time} the current time as a DOMHighResTimeStamp
*/
Game.prototype.loop = function(newTime) {
var game = this;
var elapsedTime = newTime - this.oldTime;
this.oldTime = newTime;
if(!this.paused) this.update(elapsedTime);
this.render(elapsedTime, this.frontCtx);
// Flip the back buffer
this.frontCtx.drawImage(this.backBuffer, 0, 0);
}
},{}]},{},[1]);