-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
101 lines (83 loc) · 2.79 KB
/
game.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
//TODO: this should only really contain metadata for running the game.
//Should there be abstraction between this page and interacting with the window/mouse?
let FPS = 1;
var canvas = document.getElementById("myCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var context = canvas.getContext("2d");
let cursorList = [];
let gameWindow = null;
let gameInterval = null;
function init() {
console.log("Initializing Game:");
console.log(" Generating Game Window:");
gameWindow = generateGameWindow();
console.log(" Game Window Generated");
console.log("Game Initialized!")
gameInterval = setInterval(gameTick, 1 / FPS * 1000);
}
/**
* generates containers for starting layout. body with col for button & coins
*
* @return {Container} Container holding all other containers & buttons
*/
function generateGameWindow() {
console.log("generateGameWindow()");
//generate Containers
let gameWindowContainter = new Container('vertical');
let bodyContainer = new Container('horizontal');
let cursorButtonContainer = new Container('vertical');
let coinButtonContainer = new Container('vertical');
gameWindowContainter.add(bodyContainer);
bodyContainer.add(cursorButtonContainer, coinButtonContainer);
//generate starting buttons
let cursorButton = new Button();
let coinButton = new Button();
cursorButtonContainer.add(cursorButton);
coinButtonContainer.add(coinButton);
return gameWindowContainter;
}
function gameTick(){
try{
//clear canvas
context.clearRect(0, 0, canvas.width, canvas.height);
//all cursors take action, in order that they were made
for (var i = 0; i < cursorList.length; i++) {
cursorList[i].tick();
}
//buttons & layout update last
gameWindow.draw();
}catch(err){
console.error(err);
clearInterval(gameInterval);
}
}
function getMousePos(canvas, event) {
var rect = canvas.getBoundingClientRect();
return {
x: event.clientX - rect.left,
y: event.clientY - rect.top
};
}
function mouseMoveEvent(event) {
currentMousePos = getMousePos(canvas, event);
}
function mouseDownEvent(event) {
// body...
}
function mouseUpEvent(event) {
// body...
}
function clickEvent(event) {
let frame = currentMouse.getFrame(currentMouse.delay);
frame.click = true;
for (var i = 0; i < buttonList.length; i++) {
buttonList[i].checkFrame(frame);
}
}
window.addEventListener('mousemove', mouseMoveEvent, false);
window.addEventListener('mousedown', mouseDownEvent, false);
window.addEventListener('mouseup', mouseUpEvent, false);
window.addEventListener('click', clickEvent, false);
console.log("Javascript Loaded! FPS: " + FPS);
init();