Added pause button

This commit is contained in:
Thomas Forgione 2018-08-22 11:20:45 +02:00
parent 7c2ce752ee
commit c64d9965e3
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
7 changed files with 115 additions and 20 deletions

View File

@ -4,6 +4,7 @@ www/js/escalator.js: $(shell find src -name "*.js")
mkdir -p www/js mkdir -p www/js
cat \ cat \
src/open.js \ src/open.js \
src/utils.js \
src/screen.js \ src/screen.js \
src/scene.js \ src/scene.js \
src/box.js \ src/box.js \

View File

@ -1,6 +1,6 @@
const MAX_SPEED = 20; const MAX_SPEED = 3;
const MAX_JUMP = 2; const MAX_JUMP = 2;
const G = 500; const G = 10;
const MAX_FRAME = 9; const MAX_FRAME = 9;
@ -25,7 +25,7 @@ class Box {
this.jumpCounter = MAX_JUMP; this.jumpCounter = MAX_JUMP;
} }
update(time = 0.002) { update(time = 0.02) {
if (this.animate !== 0) { if (this.animate !== 0) {
this.frameNumber += this.animate; this.frameNumber += this.animate;

View File

@ -21,7 +21,7 @@ class Game {
screen.addListeners(); screen.addListeners();
} }
update(time = 0.002) { update(time = 0.02) {
this.mainScreen.update(time); this.mainScreen.update(time);
if (this.mainScreen.status === Status.Finished) { if (this.mainScreen.status === Status.Finished) {

View File

@ -95,21 +95,17 @@ class Menu extends Screen {
let e = event.changedTouches[0]; let e = event.changedTouches[0];
let box = this.enlargeBox(this.newGameBox()); let box = this.enlargeBox(this.newGameBox());
if (e.clientX > box.x && e.clientX < box.x + box.width) { if (isInBox(position(e), box)) {
if (e.clientY > box.y && e.clientY < box.y + box.height) {
this.status = Status.Finished; this.status = Status.Finished;
this.after = AfterMenu.Start; this.after = AfterMenu.Start;
} }
}
box = this.enlargeBox(this.exitBox()); box = this.enlargeBox(this.exitBox());
if (e.clientX > box.x && e.clientX < box.x + box.width) { if (isInBox(position(e), box)) {
if (e.clientY > box.y && e.clientY < box.y + box.height) {
this.status = Status.Finished; this.status = Status.Finished;
this.after = AfterMenu.Exit; this.after = AfterMenu.Exit;
} }
} }
}
enlargeBox(box) { enlargeBox(box) {
return { return {

View File

@ -43,7 +43,7 @@ class Scene extends Screen {
start() { start() {
if (!this.started) { if (!this.started) {
this.started = true; this.started = true;
this.cameraSpeed = 0.005; this.cameraSpeed = 0.0005;
} }
} }
@ -55,7 +55,20 @@ class Scene extends Screen {
this.context.fill(); this.context.fill();
} }
update(time = 0.002) { update(time = 0.02) {
if (this.wakingTimer !== undefined) {
this.wakingTimer -= time;
if (this.wakingTimer < 0) {
this.wakingTimer = undefined;
this.status = Status.Running;
}
}
if (this.status !== Status.Running) {
return;
}
// Check if the game is lost // Check if the game is lost
if (this.player.y + this.player.size < this.cameraHeight) { if (this.player.y + this.player.size < this.cameraHeight) {
@ -171,15 +184,86 @@ class Scene extends Screen {
} }
drawHud() { drawHud() {
this.context.font = "20px Arial"; // Draw scores
let fontSize = 20;
this.context.font = fontSize + "px Arial";
this.context.fillStyle = 'rgb(255, 255, 255)'; this.context.fillStyle = 'rgb(255, 255, 255)';
this.context.fillText("Current score: " + Math.floor(100 * this.currentHeight), 0, 20); this.context.fillText("Current score: " + Math.floor(100 * this.currentHeight), 0, 20);
this.context.fillText("High score: " + Math.floor(100 * this.maxHeight), 0, 50); this.context.fillText("High score: " + Math.floor(100 * this.maxHeight), 0, 50);
// Draw pause buttons
let box = this.makePauseBox();
this.context.fillStyle = 'rgba(255, 255, 255, 0.5)';
this.context.beginPath();
this.context.rect(box.x, box.y, box.width, box.height);
this.context.fill();
this.context.fillStyle = 'rgba(0, 0, 0, 0.5)';
this.context.beginPath();
this.context.rect(
box.x + box.width * 0.3,
box.y + box.height * 0.1,
box.width * 0.1,
box.height * 0.8,
);
this.context.fill();
this.context.beginPath();
this.context.rect(
box.x + box.width * 0.6,
box.y + box.height * 0.1,
box.width * 0.1,
box.height * 0.8,
);
this.context.fill();
if (this.status === Status.Paused || this.status === Status.Waking) {
let fontSize = 50;
this.context.font = fontSize + "px Arial";
this.context.beginPath();
this.context.rect(0, 0, this.width(), this.height());
this.context.fill();
let text = this.status === Status.Paused ? "Paused" : Math.floor(this.wakingTimer + 1);
text = text + "";
this.context.fillStyle = 'rgb(255, 255, 255)';
let size = this.context.measureText(text).width;
this.context.fillText(text, (this.width() - size) / 2, this.height() / 2 + fontSize);
}
}
makePauseBox() {
let startX = 0.85;
let startY = 0.05;
let size = 0.1;
return {
x: startX * this.width(),
y: startY * this.width(),
width: size * this.width(),
height: size * this.width(),
};
} }
onTouchStart(event) { onTouchStart(event) {
let e = event.changedTouches[0]; let e = event.changedTouches[0];
if (this.started) {
if (this.status === Status.Running && isInBox(position(e), this.makePauseBox())) {
this.status = Status.Paused;
} else if (this.status === Status.Paused) {
this.status = Status.Waking;
this.wakingTimer = 3;
}
}
this.start(); this.start();
if (e !== undefined) { if (e !== undefined) {
this.player.jump(e.clientX / window.innerWidth, e.clientY / window.innerHeight); this.player.jump(e.clientX / window.innerWidth, e.clientY / window.innerHeight);

View File

@ -1,7 +1,9 @@
const Status = { const Status = {
Idle: 0, Idle: 0,
Running: 1, Running: 1,
Finished: 2, Paused: 2,
Waking: 3,
Finished: 4,
}; };
class Screen { class Screen {
@ -55,7 +57,7 @@ class Screen {
} }
update(time = 0.002) { update(time = 0.02) {
} }

12
src/utils.js Normal file
View File

@ -0,0 +1,12 @@
function isInBox(pos, box) {
return (
(pos.x > box.x && pos.x < box.x + box.width) &&
(pos.y > box.y && pos.y < box.y + box.height));
}
function position(event) {
return {
x: event.clientX,
y: event.clientY,
};
}