You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

318 lines
7.9 KiB

"use strict"
class Game{
constructor(){}
load(){
let minSize = int(values.minWorldSize),
maxSize = int(values.maxWorldSize);
let world = {pos: createVector(0, 0), dim: createVector(random(minSize, maxSize), random(minSize, maxSize))};
let bricks = [];
loader = new Loader(Loader.Bar(), "Loading The Game...");
this.createBrick(world.pos.x + int(values.maxBrickWidth), bricks, world, 4);
}
next(num, bricks, world, loadingParts, percentage, partIndex, info, func){
setTimeout(function(){
let progress = percentage / loadingParts + partIndex / loadingParts;
loader.updateProgress(progress, info);
game[func](num, bricks, world, loadingParts);
});
}
createBrick(x, bricks, world, loadingParts){
let minBrickWidth = int(values.minBrickWidth),
maxBrickWidth = int(values.maxBrickWidth),
maxWorldSize = int(values.maxWorldSize);
if (x < world.pos.x + world.dim.x - maxBrickWidth){
let y = random(world.pos.y, world.pos.y + world.dim.y),
w = random(minBrickWidth, maxBrickWidth),
h = 30,
v = {
x: ranBool() ? random(-4, 4) : 0,
y: ranBool() ? random(-4, 4) : 0
};
bricks.push(new Brick(x, y, w, h, v));
x += 400000 / world.dim.y;
let percentage = (x - maxBrickWidth) / (world.pos.x + world.dim.x - maxBrickWidth * 2);
this.next(x, bricks, world, loadingParts, percentage, 0, "Building Platforms...", "createBrick");
} else {
this.createTrees(0, bricks, world, loadingParts);
}
}
createTrees(index, bricks, world, loadingParts){
bricks[index].createTrees();
if (bricks[index + 1]){
index++;
let percentage = index / bricks.length;
this.next(index, bricks, world, loadingParts, percentage, 1, "Planting Trees...", "createTrees");
} else {
this.createPlants(0, bricks, world, loadingParts);
}
}
createPlants(index, bricks, world, loadingParts){
bricks[index].createPlants();
if (bricks[index + 1]){
index++;
let percentage = index / bricks.length;
this.next(index, bricks, world, loadingParts, percentage, 2, "Planting Flowers...", "createPlants");
} else {
this.createCoins(0, bricks, world, loadingParts);
}
}
createCoins(index, bricks, world, loadingParts){
bricks[index].createCoins();
if (bricks[index + 1]){
index++;
let percentage = index / bricks.length;
this.next(index, bricks, world, loadingParts, percentage, 3, "Hiding Treasures...", "createCoins");
} else {
this.tidyUp(bricks, world);
}
}
tidyUp(bricks, world){
let variables = this.createVariables(bricks, world);
this.applyVariables(variables);
this.safeStartingConditions(variables);
resetMusics();
loader.updateProgress(1, "Cleaning up...");
//setTimeout(function(){loadAudioFiles(audioFilesLoaded)}, 1000); crashes PC :D
setTimeout(function(){game.finishLoading();}, int(values.loadedWaitTime));
}
createVariables(bricks, world){
let maxWorldSize = int(values.maxWorldSize),
maxMapSize = int(values.maxMapSize);
let iconMap = new Map(map(world.dim.x, 0, maxWorldSize, 0, maxMapSize), map(world.dim.y, 0, maxWorldSize, 0, maxMapSize));
let player = new Player(random(world.pos.x + width, world.pos.x + world.dim.x - width), random(world.pos.y + height, world.pos.y - height + world.dim.y));
let goal = new Goal();
let background = new Background(world);
viewPort.x = player.pos.x - width / 2;
viewPort.y = player.pos.y - height / 2;
let showHint = true;
let showMap = true;
let isWon = false;
let level = 0;
let maxLevel = int(values.maxLevelCount);
let variablesObject = {
world: world,
bricks: bricks,
player: player,
goal: goal,
iconMap: iconMap,
background: background,
showHint: showHint,
showMap: showMap,
isWon: isWon,
level: level,
maxLevel: maxLevel
};
return variablesObject;
}
applyVariables(variables){
for (let property in variables){
this[property] = variables[property];
}
}
safeStartingConditions(v){
let sc = {};
for (let key in v){
if (v[key] instanceof Array){
sc[key] = v[key].clone();
} else if (v[key] instanceof Player){
sc[key] = new Player(v[key].pos.x, v[key].pos.y);
} else if (v[key] instanceof Goal){
sc[key] = new Goal();
}
}
sc.isWon = false;
sc.level = 0;
this.startingConditions = sc;
}
finishLoading(){
loader.destroy();
$("#loaderWrapper").hide();
if (!menuOpen) toggleMenu($("#menuOpener"));
this.nextLevel();
this.ready = true;
this.start();
updateMenu();
}
start(){
this.playMusic();
this.resume();
}
restart(){
this.applyVariables(this.startingConditions);
this.safeStartingConditions(this.startingConditions);
this.nextLevel();
this.resume();
}
update(){
if (!this.ready || this.paused){
return;
}
this.background.update();
for (let b of this.bricks){
b.update(this.world);
}
this.goal.update();
this.player.update();
if (!this.paused){
this.player.collision(this.bricks, this.world);
}
if (this.goal.contains(this.player)
&& this.goal.brick == this.player.brick
&& !this.paused
&& !this.isWon){
this.goalReached();
}
if (this.levelText.isActive()){
this.levelText.update();
}
}
display(){
if (!this.ready){
return;
}
checkViewPort();
this.background.display();
translate(-viewPort.x, -viewPort.y);
for (let b of this.bricks){
b.display(this.world);
}
if (this.showHint){
this.goal.hint(this.player, this.world);
}
this.goal.display();
this.player.display();
translate(viewPort.x, viewPort.y);
if (this.showMap){
this.iconMap.display(this.player, this.goal, this.world);
}
if (this.levelText.isActive()){
this.levelText.display();
}
}
nextLevel(){
this.level++;
this.goal.setBrick(random(this.bricks));
this.player.maxVelocityX = int(values.maxVelocityX[this.level - 1]);
this.player.jumpVelocity = int(values.jumpVelocity[this.level - 1]);
this.levelText = new LevelText(this.level);
}
goalReached(){
if (this.level < this.maxLevel) this.nextLevel();
else this.won();
}
won(){
this.isWon = true;
this.pause();
new Sound(Sound.Won()).play();
new Message(Message.Won()).display();
}
lost(){
this.pause();
new Sound(Sound.Lost()).play();
new Message(Message.Lost()).display();
}
pause(){
this.paused = true;
this.pauseMusic();
}
resume(){
this.paused = false;
this.unpauseMusic();
}
toggleRun(){
if (this.paused) this.resume();
else this.pause();
}
toggleMap(){
this.showMap = !this.showMap;
}
toggleHint(){
this.showHint = !this.showHint;
}
getMusics(){
return audio.music.game;
}
playMusic(){
let musics = this.getMusics();
this.songIndex = int(random(musics.length));
musics[this.songIndex].play();
updateVolume();
let self = this;
setTimeout(function(){
musics[self.songIndex].onended(function(){
if (!musics[self.songIndex].isPaused()) self.playMusic();
});
});
}
pauseMusic(){
let musics = this.getMusics();
if (musics[this.songIndex].isPaused()) return;
musics[this.songIndex].pause();
}
unpauseMusic(){
let musics = this.getMusics();
if (musics[this.songIndex].isPlaying()) return;
musics[this.songIndex].play();
updateVolume();
}
}
function checkViewPort(){
//Between 0.5 and 1
let limit = 0.5;
let player = game.player,
world = game.world;
if (player.pos.x > width * limit + viewPort.x && viewPort.x + width < world.pos.x + world.dim.x) viewPort.x = player.pos.x - width * limit;
if (player.pos.x < width * (1 - limit) + viewPort.x && viewPort.x > world.pos.x) viewPort.x = player.pos.x - width * (1 - limit);
if (player.pos.y > height * limit + viewPort.y && viewPort.y + height < world.pos.y + world.dim.y) viewPort.y = player.pos.y - height * limit;
if (player.pos.y < height * (1 - limit) + viewPort.y && viewPort.y > world.pos.y) viewPort.y = player.pos.y - height * (1 - limit);
}
function loadNewGame(){
game = new Game();
game.load();
}
function requestNewGame(){
game.pause();
new Message(Message.New()).display();
}