forked from Hextris/hextris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.js
219 lines (198 loc) · 7.25 KB
/
view.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
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
// t: current time, b: begInnIng value, c: change In value, d: duration
function easeOutCubic(t, b, c, d) {
return c * ((t = t / d - 1) * t * t + 1) + b;
}
function renderText(x, y, fontSize, color, text, font) {
ctx.save();
if (!font) {
var font = 'px Exo';
}
fontSize *= settings.scale;
ctx.font = fontSize + font;
ctx.textAlign = 'center';
ctx.fillStyle = color;
ctx.fillText(text, x, y + (fontSize / 2) - 9 * settings.scale);
ctx.restore();
}
function drawScoreboard() {
if (scoreOpacity < 1) {
scoreOpacity += 0.01;
textOpacity += 0.01;
}
ctx.globalAlpha = textOpacity;
var scoreSize = 50;
var scoreString = String(score);
if (scoreString.length == 6) {
scoreSize = 43;
} else if (scoreString.length == 7) {
scoreSize = 35;
} else if (scoreString.length == 8) {
scoreSize = 31;
} else if (scoreString.length == 9) {
scoreSize = 27;
}
//if (rush ==1){
var color = "rgb(236, 240, 241)";
//}
var fontSize = settings.platform == 'mobile' ? 35 : 30;
var h = trueCanvas.height / 2 + gdy + 100 * settings.scale;
if (gameState === 0) {
renderText(trueCanvas.width / 2 + gdx + 6 * settings.scale, trueCanvas.height / 2 + gdy, 60, "rgb(236, 240, 241)", String.fromCharCode("0xf04b"), 'px FontAwesome');
renderText(trueCanvas.width / 2 + gdx + 6 * settings.scale, trueCanvas.height / 2.1 + gdy - 155 * settings.scale, 150, "#2c3e50", "Hextris");
renderText(trueCanvas.width / 2 + gdx + 5 * settings.scale, h + 10, fontSize, "rgb(44,62,80)", 'Play!');
} else if (gameState != 0 && textOpacity > 0) {
textOpacity -= 0.05;
renderText(trueCanvas.width / 2 + gdx + 6 * settings.scale, trueCanvas.height / 2 + gdy, 60, "rgb(236, 240, 241)", String.fromCharCode("0xf04b"), 'px FontAwesome');
renderText(trueCanvas.width / 2 + gdx + 6 * settings.scale, trueCanvas.height / 2 + gdy - 155 * settings.scale, 150, "#2c3e50", "Hextris");
renderText(trueCanvas.width / 2 + gdx + 5 * settings.scale, h, fontSize, "rgb(44,62,80)", 'Play!');
ctx.globalAlpha = scoreOpacity;
renderText(trueCanvas.width / 2 + gdx, trueCanvas.height / 2 + gdy, scoreSize, color, score);
} else {
ctx.globalAlpha = scoreOpacity;
renderText(trueCanvas.width / 2 + gdx, trueCanvas.height / 2 + gdy, scoreSize, color, score);
}
ctx.globalAlpha = 1;
}
function clearGameBoard() {
drawPolygon(trueCanvas.width / 2, trueCanvas.height / 2, 6, trueCanvas.width / 2, 30, hexagonBackgroundColor, 0, 'rgba(0,0,0,0)');
}
function drawPolygon(x, y, sides, radius, theta, fillColor, lineWidth, lineColor) {
ctx.fillStyle = fillColor;
ctx.lineWidth = lineWidth;
ctx.strokeStyle = lineColor;
ctx.beginPath();
var coords = rotatePoint(0, radius, theta);
ctx.moveTo(coords.x + x, coords.y + y);
var oldX = coords.x;
var oldY = coords.y;
for (var i = 0; i < sides; i++) {
coords = rotatePoint(oldX, oldY, 360 / sides);
ctx.lineTo(coords.x + x, coords.y + y);
oldX = coords.x;
oldY = coords.y;
}
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.strokeStyle = 'rgba(0,0,0,0)';
}
function toggleClass(element, active) {
if ($(element).hasClass(active)) {
$(element).removeClass(active);
} else {
$(element).addClass(active);
}
}
function showText(text) {
var messages = {
'paused': "<div class='centeredHeader unselectable'>Game Paused</div>",
'pausedAndroid': "<div class='centeredHeader unselectable'>Game Paused</div><div class='unselectable centeredSubHeader' style='position:absolute;margin-left:-150px;left:50%;margin-top:20px;width:300px;font-size:16px;'><a href = 'https://play.google.com/store/apps/details?id=com.hextris.hextrisadfree' target='_blank'Want to support the developers? Don't like ads? Tap for Hextris ad-free!</a></div>",
'pausediOS': "<div class='centeredHeader unselectable'>Game Paused</div><div class='unselectable centeredSubHeader' style='position:absolute;margin-left:-150px;left:50%;margin-top:20px;width:300px;font-size:16px;'><a href = 'https://itunes.apple.com/us/app/hextris-ad-free/id912895524?mt=8' target='_blank'>Want to support the developers? Don't like ads? Tap for Hextris ad-free!</a></div>",
'pausedOther': "<div class='centeredHeader unselectable'>Game Paused</div><div class='unselectable centeredSubHeader' style='margin-top:10px;position:absolute;left:50%;margin-left:-190px;max-width:380px;font-size:18px;'><a href = 'http://hextris.github.io/' target='_blank'>Want to support the developers? Click here to buy one of the ad-free mobile versions!</a></div>",
'start': "<div class='centeredHeader unselectable' style='line-height:80px;'>Press enter to start</div>"
};
if (text == 'paused') {
if (settings.os == 'android') {
text = 'pausedAndroid'
} else if (settings.os == 'ios') {
text = 'pausediOS'
} else if (settings.platform == 'nonmobile') {
text = 'pausedOther'
}
}
if (text == 'gameover') {
//Clay('client.share.any', {text: 'Think you can beat my score of '+ score + ' in Super Cool Game?'})
$("#gameoverscreen").fadeIn();
}
$(".overlay").html(messages[text]);
$(".overlay").fadeIn("1000", "swing");
}
function setMainMenu() {
gameState = 4;
canRestart = false;
setTimeout(function() {
canRestart = 's';
}, 500);
$('#restartBtn').hide();
if ($("#pauseBtn").replace(/^.*[\\\/]/, '') == "btn_pause.svg") {
$("#pauseBtn").attr("src","./images/btn_resume.svg");
} else {
$("#pauseBtn").attr("src","./images/btn_pause.svg");
}
}
function hideText() {
$(".overlay").fadeOut(150, function() {
$(".overlay").html("");
})
}
function gameOverDisplay() {
settings.ending_block=false;
Cookies.set("visited",true);
var c = document.getElementById("canvas");
c.className = "blur";
updateHighScores();
if (highscores.length === 0 ){
$("#currentHighScore").text(0);
}
else {
$("#currentHighScore").text(highscores[0])
}
$("#gameoverscreen").fadeIn();
$("#buttonCont").fadeIn();
$("#container").fadeIn();
$("#socialShare").fadeIn();
$("#restart").fadeIn();
set_score_pos();
}
function updateHighScores (){
$("#cScore").text(score);
$("#1place").text(highscores[0]);
$("#2place").text(highscores[1]);
$("#3place").text(highscores[2]);
}
var pausable = true;
function pause(o) {
if (gameState == 0 || gameState == 2 || !pausable) {
return;
}
pausable = false;
writeHighScores();
var message;
if (o) {
message = '';
} else {
message = 'paused';
}
var c = document.getElementById("canvas");
if (gameState == -1) {
$('#fork-ribbon').fadeOut(300, 'linear');
$('#restartBtn').fadeOut(300, "linear");
$('#buttonCont').fadeOut(300, "linear");
if ($('#helpScreen').is(':visible')) {
$('#helpScreen').fadeOut(300, "linear");
}
$("#pauseBtn").attr("src", "./images/btn_pause.svg");
$('.helpText').fadeOut(300, 'linear');
$('#overlay').fadeOut(300, 'linear');
hideText();
setTimeout(function() {
gameState = prevGameState;
pausable =true;
}, 400);
} else if (gameState != -2 && gameState !== 0 && gameState !== 2) {
$('#restartBtn').fadeIn(300, "linear");
$('#buttonCont').fadeIn(300, "linear");
$('.helpText').fadeIn(300, 'linear');
if (message == 'paused') {
showText(message);
}
$('#fork-ribbon').fadeIn(300, 'linear');
$("#pauseBtn").attr("src","./images/btn_resume.svg");
$('#overlay').fadeIn(300, 'linear');
prevGameState = gameState;
setTimeout(function() {
pausable = true;
}, 400);
gameState = -1;
}
}