forked from yining1023/brickBreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
156 lines (134 loc) · 5.59 KB
/
common.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
BWB_DEBUG = false; // Export mode
// CANVAS SETUP
var BWB_WIDTH = 884;
var BWB_HEIGHT = 497;
// DISPLAY-REFRESH-BASED GAME SPEED
// 30 frames per second at most
BWB_DEFAULT_FRAMERATE = 30;
function centerCanvas() {
let x = (windowWidth - width) / 2;
let y = (windowHeight - height) / 2;
cnv.parent().style.position = "absolute";
cnv.parent().style.left = x.toString() + "px";
cnv.parent().style.top = y.toString() + "px";
cnv.parent().style.border = "3px lightsteelblue solid";
}
function windowResized() {
centerCanvas();
}
function setupCanvas(withWebGL /*default: true*/) {
withWebGL = withWebGL == undefined ? true : withWebGL;
withWebGL = withWebGL ? WEBGL : P2D;
cnv = createCanvas(BWB_WIDTH, BWB_HEIGHT, withWebGL);
cnv.parent('gameScreen');
centerCanvas();
setFrameRate(BWB_DEFAULT_FRAMERATE);
}
// GAME LOCALIZATION
var BWB_LANG_FR = 'fr';
var BWB_LANG_EN = 'en';
var BWB_LANG_KEYWORD = 'langueter';
var BWB_DEFAULT_LANG = BWB_LANG_FR;
var BWB_LANG = BWB_DEFAULT_LANG;
// GAME LOGIC
var BWB_GAME_STATE_LOSE = 0;
var BWB_GAME_STATE_PLAYING = 1;
var BWB_GAME_STATE_WIN = 2;
var BWB_GAME_STATE_PAUSED = 3;
var BWB_GAME_STATE = BWB_GAME_STATE_PLAYING;
var BWB_DEFAULT_GAME_LIVES = 5;
var BWB_GAME_LIVES = BWB_DEFAULT_GAME_LIVES;
var BWB_DEFAULT_LEVEL_ID = 0;
// LEVEL ID, JUST LOST AND LIVES COUNT DETECTION AND REDIRECTION FROM URL
// anchor must be like: #levelNlivesM (if provided), defaults to #level0lives3 (ie. level 1, 3 lives)
var BWB_REDIRECTING = false;
var BWB_LEVEL_KEYWORD = 'livarot'; // levelN: current (or target level)
var BWB_LIVES_KEYWORD = 'calva'; // livesN: remaining lives
var BWB_LIVES_LOST_KEYWORD = 'yrpleut'; // lost: just lost one life from last level. If omitted, nothing just lost.
var BWB_LEVEL_ID = BWB_DEFAULT_LEVEL_ID; // Integer
var BWB_LEVEL_URL_DATA = location.hash; // Default BWB_LEVEL_ID is full hash. Will be just an integer after then.
// Detect language
if (BWB_LEVEL_URL_DATA == undefined || BWB_LEVEL_URL_DATA.indexOf(BWB_LANG_KEYWORD) == -1) {
console.log('Language is unset, defaulting to ' + BWB_DEFAULT_LANG);
BWB_LANG = BWB_DEFAULT_LANG;
} else {
BWB_LANG = BWB_LEVEL_URL_DATA.substr(BWB_LEVEL_URL_DATA.indexOf(BWB_LANG_KEYWORD)+BWB_LANG_KEYWORD.length, 2);
console.log('Language is set: ' + BWB_LANG);
}
// Detect level ID or set default level ID
if (BWB_LEVEL_URL_DATA == undefined || BWB_LEVEL_URL_DATA.indexOf('#' + BWB_LEVEL_KEYWORD) != 0 || BWB_LEVEL_URL_DATA.length < ('#' + BWB_LEVEL_KEYWORD).length+1) {
console.log('Level is unset, defaulting to 1');
BWB_LEVEL_ID = 0;
} else {
BWB_LEVEL_ID = parseInt(BWB_LEVEL_URL_DATA.substr(('#' + BWB_LEVEL_KEYWORD).length, 1));
console.log('Level is set: ' + BWB_LEVEL_ID.toString());
}
// Detect whether a life was just lost from last level
if (BWB_LEVEL_URL_DATA == undefined || BWB_LEVEL_URL_DATA.indexOf(BWB_LIVES_LOST_KEYWORD) == -1) {
console.log('No life lost just recently - starting level for the first time');
} else {
console.log('Just lost one life');
}
// Detect amount of lives left
if (BWB_LEVEL_URL_DATA == undefined || BWB_LEVEL_URL_DATA.indexOf(BWB_LIVES_KEYWORD) == -1) {
BWB_GAME_LIVES = BWB_DEFAULT_GAME_LIVES;
console.log('Lives is unset, defaulting to ' + BWB_GAME_LIVES.toString());
} else {
BWB_GAME_LIVES = parseInt(BWB_LEVEL_URL_DATA.substr(BWB_LEVEL_URL_DATA.indexOf(BWB_LIVES_KEYWORD)+BWB_LIVES_KEYWORD.length, 1));
console.log('Lives is set: ' + BWB_GAME_LIVES.toString());
}
// END OF LEVEL ID, JUST LOST AND LIVES COUNT DETECTION
var BWB_URL_GAME_OVER = 'level_intro.html#' + BWB_LEVEL_KEYWORD + BWB_LEVEL_ID.toString() + BWB_LIVES_LOST_KEYWORD + BWB_LIVES_KEYWORD; // Router should append lives ID before redirecting
var BWB_URL_WIN = 'youwin.html';
var BWB_URL_HOME = 'home.html';
var BWB_URL_ABOUT = 'about.html';
var BWB_URL_MAKING_OF = 'makingof.html';
var BWB_URL_HOW_TO = 'howto.html';
var BWB_URL_PICS = 'pics.html';
var BWB_URL_GALLERY = 'https://myselfhimself.github.io/portfolio/jeu-video-berlin-wall-breaker';
function urlFor(slug, lang) {
var nextUrl = '';
if (slug.indexOf('level') == 0) {
nextUrl = 'level.html#' + BWB_LEVEL_KEYWORD + slug.substr('level'.length, 1) + BWB_LIVES_KEYWORD + BWB_GAME_LIVES.toString();
} else if (slug.indexOf('intro') == 0) {
nextUrl = 'level_intro.html#' + BWB_LEVEL_KEYWORD + slug.substr('intro'.length, 1) + BWB_LIVES_KEYWORD + BWB_GAME_LIVES.toString();
} else if (slug == 'win') {
nextUrl = BWB_URL_WIN;
} else if (slug == 'home') {
nextUrl = BWB_URL_HOME;
} else if (slug == 'about') {
nextUrl = BWB_URL_ABOUT;
} else if (slug == 'gameover') {
nextUrl = BWB_URL_GAME_OVER + BWB_GAME_LIVES.toString();
} else if (slug == 'makingof') {
nextUrl = BWB_URL_MAKING_OF;
} else if (slug == 'howto') {
nextUrl = BWB_URL_HOW_TO;
} else if (slug == 'pics') {
nextUrl = BWB_URL_PICS;
} else if (slug == 'gallery') {
nextUrl = BWB_URL_GALLERY;
}
return nextUrl + (nextUrl.indexOf('#') != -1 ? '' : '#') + BWB_LANG_KEYWORD + lang;
}
function redirectToUrlFor(slug, lang) {
if(lang==undefined) {
lang = BWB_LANG == undefined ? BWB_DEFAULT_LANG : BWB_LANG;
}
redirectToUrl(urlFor(slug, lang));
}
function redirectToUrl(url) {
if (!BWB_REDIRECTING) {
BWB_REDIRECTING = true;
if(window.location.href != url) {
setTimeout(function(){
window.location.href = url;
}, 1500);
//window.location.href = url;
}
}
}
function redirectToGameOver() {
BWB_GAME_LIVES --;
redirectToUrlFor('gameover');
}