This repository has been archived by the owner on Nov 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
179 lines (154 loc) · 4.82 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
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
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Globais
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Possíveis estados do game
*/
const STATES = {
INIT: 0,
ASSETS_LOADED: 1,
PLAYING: 2,
PAUSE: 3,
OVER: 4,
CONTINUE: 5
}
const BALL_STATE = {
INIT: 0,
MOVING: 1,
DEAD: 2,
}
const INIT_VAL = {
BALL_SPEED: 5,
PADDLE_SPEED: 7,
PADDLE_SCORE: 100,
LIVES: 3,
SCORE: 0,
LEVEL: 1
}
const BOUND = {
MAX_X: 464,
MAX_Y: 304,
MIN_Y: 46
}
// comprimento da letra true type -> pixels na tela
const LETTER_W = {
"_15" : 8,
"_20" : 10
}
function Game(imageRepo) {
var assets = imageRepo;
var self = this;
var tsLastFrame;
self.scenes = {
titleScene: null,
mainScene: null
}
self.state = STATES.INIT;
self.init = function() {
var paddleCanvas = document.getElementById("paddle");
var mainCanvas = document.getElementById("main");
var bgCanvas = document.getElementById("background");
var scoreCanvas = document.getElementById("score");
if (!bgCanvas.getContext) {
console.warn("O navegador não suporta canvas, desculpe");
return false;
}
var mainContext = mainCanvas.getContext('2d');
var scoreContext = scoreCanvas.getContext('2d');
var bgContext = bgCanvas.getContext('2d');
var paddle = new Paddle(
assets.paddle.width * 3,
(mainCanvas.height / 2) - (assets.paddle.height / 2),
paddleCanvas.getContext('2d'),
assets.paddle);
paddle.width = assets.paddle.width;
paddle.height = assets.paddle.height;
paddle.canvasWidth = paddleCanvas.width;
paddle.canvasHeight = paddleCanvas.height;
var ball = new Ball(
paddle.pos.x + assets.ball.height,
(mainCanvas.height / 2) - (assets.ball.height / 2),
mainContext,
assets.ball,
paddle);
ball.width = assets.ball.width;
ball.height = assets.ball.height;
ball.canvasWidth = mainCanvas.width;
ball.canvasHeight = mainCanvas.height;
var scoreBoard = new ScoreBoard(0, 0, scoreContext);
scoreBoard.width = scoreCanvas.width;
scoreBoard.height = scoreCanvas.height;
scoreBoard.canvasWidth = scoreCanvas.width;
scoreBoard.canvasHeight = scoreCanvas.height;
var background = new Background(bgContext, assets.bg);
background.width = assets.bg.width;
background.height = assets.bg.height;
background.canvasWidth = bgCanvas.width;
background.canvasHeight = bgCanvas.height;
var mainScene = new MainScene({
ball: ball,
paddle: paddle,
scoreBoard: scoreBoard,
bg: background
}, mainContext, mainCanvas);
self.scenes = {
mainScene: mainScene,
//pauseScene: new PauseScene(mainContext, mainCanvas),
//gameOverScene: new GameOverScene(mainContext, mainCanvas),
titleScene: new TitleScene(mainContext, mainCanvas)
}
updateGameState(STATES.INIT);
return true;
}
self.theLoop = function() {
var msLastFrame = +new Date - tsLastFrame;
update(msLastFrame);
draw();
tsLastFrame = +new Date;
requestAnimFrame(self.theLoop);
}
function update(msLastFrame) {
var lastState = self.state;
for (var scene in self.scenes) {
self.scenes[scene].update(msLastFrame);
if(self.scenes[scene].state !== lastState) {
updateGameState(self.scenes[scene].state);
}
}
}
function draw() {
for (var scene in self.scenes) {
self.scenes[scene].draw();
}
}
// uma forma de "avisar" as cenas de que o estado mudou
// a forma "correta" seria a implementação dos listeners de eventos dentro das cenas.
function updateGameState(state) {
self.state = state;
console.log("Update game state to %d", state);
for (var scene in self.scenes) {
console.log("Update scene %s state to %d", scene, state);
self.scenes[scene].updateState(state);
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Inicia aqui
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function loadGame() {
//http://stackoverflow.com/questions/221539/what-does-the-plus-sign-do-in-new-date
// converte para um numero, em millis.
var now = +new Date;
console.log("Inicializando o game");
var imageRepo = new ImageRepository();
imageRepo.init(function() {
var game = new Game(imageRepo);
console.log("Game inicializado: %d ms", +new Date - now);
if (game.init()) {
game.theLoop();
} else {
game = null;
}
});
}
window.onload = loadGame;