xxxxxxxxxx
450
class Game
{
constructor(assetDictionary)
{
this.assetDictionary = assetDictionary;
this.wallsGroup = new Group();
this.player = null;
this.enemies = [];
this.room = new Room(0, 0, 25, 25, this.assetDictionary.get("roomTiles"), this.wallsGroup);
this.totalDebt = 100;
this.numberOfCoins = 0;
this.numberOfPotions = 5;
this.cooldownTimer = 0; // Basic framecount based timer
this.sceneIndex = -1;
this.levelsCleared = 0;
this.difficultySlider = createSlider(1.01, 1.25, 1.08, 0.01);
this.difficultySlider.hide();
this.difficultySlider.style('width', '150px');
this.interestRate = this.difficultySlider.value();
}
healPlayer()
{
if (this.cooldownTimer > 0 || this.player.health >= this.player.totalHealth || this.numberOfPotions < 1)
return;
this.cooldownTimer = 20;
--this.numberOfPotions;
this.assetDictionary.get("drinkSound").play();
const healingPerPotion = 33;
this.player.health += healingPerPotion;
if (this.player.health > this.player.totalHealth)
this.player.health = this.player.totalHealth
}
update()
{
switch(this.sceneIndex)
{
case -1:
this.updateTitle();
break;
case 0:
this.updateIntro();
break;
case 1:
this.updateArena();
break;
case 2:
this.updateSummary();
break;
case 3:
this.updateWin();
break;
case 4:
this.updateLose();
break;
}
if (this.cooldownTimer > 0)
--this.cooldownTimer;
}
updateTitle()
{
// Text
const margin = 60;
fill(255);
stroke(0);
textFont(this.assetDictionary.get("font"));
textSize(50);
textAlign(CENTER, CENTER);
text("Arena Grinder", width/2, margin);
textSize(30);
textAlign(CENTER, TOP);
let titleText = "A game about the troubles of paying off debt\nCreated By Max Calhoun\n\nCONTROLS:\nWASD - Movement\n E - Heal\nLeft Mouse Button - Attack";
text(titleText, margin, 2*margin, width - 2 * margin, height - 2 * margin);
// Slider
this.difficultySlider.show();
this.difficultySlider.position(width/2 - 75, height/2 + 45);
this.interestRate = this.difficultySlider.value();
text("Interest Rate\n(Difficulty)\n" + int((this.interestRate - 1)* 100) + "%", width/2, height/2 + 50);
// Button
let w = 200
let h = w/16*9;
let x = width/2 - w/2;
let y = 5*height/6;
drawButton("Play Game", x, y, w, h);
if (this.cooldownTimer == 0 && mouseIsPressed && mouseX > x && mouseX <= x + w && mouseY > y && mouseY <= y + h)
{
// reset
this.reset();
// play game
this.cooldownTimer = 20;
this.sceneIndex = 0;
this.difficultySlider.hide();
// sound effect
this.assetDictionary.get("buttonClick").play();
}
}
updateIntro()
{
// Text
fill(255)
stroke(0);
textSize(30);
textAlign(CENTER, TOP);
textFont(this.assetDictionary.get("font"));
let introText = "Several months ago, you took out a small loan to purchase some decorations for your house. You forgot to make the payments and now you owe 100 gold coins. Lately you have been receiving threatening messages from the bank. They claim they will seize everything you own if you do not start to pay off the debt soon. The only job that can give you the money you need in a short timeframe is fighting as a combatant in the arena. While the pay is good, the fighting could lead to your impending doom.";
const margin = 60;
text(introText, margin, margin, width - 2 * margin, height - 2 * margin);
// Button
let w = 200;
let h = w/16*9;
let x = width/2 - w/2;
let y = 5*height/6;
drawButton("Enter\nthe Arena", x, y, w, h);
if (this.cooldownTimer == 0 && mouseIsPressed && mouseX > x && mouseX <= x + w && mouseY > y && mouseY <= y + h)
{
camera.zoom = 4;
this.sceneIndex = 1;
this.createPlayer(createVector(200, 200));
this.spawnEnemies();
this.assetDictionary.get("music").setVolume(0.1);
this.assetDictionary.get("music").loop();
this.assetDictionary.get("music").play();
// sound effect
this.assetDictionary.get("buttonClick").play();
}
}
updateLose()
{
// Text
const margin = 60;
fill(255);
stroke(0);
textFont(this.assetDictionary.get("font"));
textSize(50);
textAlign(CENTER, CENTER);
text("Game Over!", width/2, margin);
textSize(30);
textAlign(CENTER, TOP);
let loseText = "You meet your doom while attempting to free yourself of debt. In retrospect, taking out that loan was a costly decision. In the future, you should consider the long-term consequences of the choices you make.\n\nYou were able clear " + this.levelsCleared + " arena battles!";
text(loseText, margin, 2*margin, width - 2 * margin, height - 2 * margin);
// Button
let w = 200;
let h = w/16*9;
let x = width/2 - w/2;
let y = 5*height/6;
drawButton("Main Menu", x, y, w, h);
if (this.cooldownTimer == 0 && mouseIsPressed && mouseX > x && mouseX <= x + w && mouseY > y && mouseY <= y + h)
{
this.sceneIndex = -1;
this.cooldownTimer = 20;
// sound effect
this.assetDictionary.get("buttonClick").play();
}
}
updateWin()
{
// Text
const margin = 60;
fill(255);
stroke(0);
textFont(this.assetDictionary.get("font"));
textSize(50);
textAlign(CENTER, CENTER);
text("You Win!", width/2, margin);
textSize(30);
textAlign(CENTER, TOP);
let winText = "After numerous battles in the arena, you were able to win enough gold to clear your debt. While you gain a sense of satisfaction knowing that you freed yourself of debt, a part of you feels frustrated knowing that you created more work for yourself than necessary.\n\nYou were able clear " + this.levelsCleared + " arena battles!";
text(winText, margin, 2*margin, width - 2 * margin, height - 2 * margin);
// Button
let w = 200;
let h = w/16*9;
let x = width/2 - w/2;
let y = 5*height/6;
drawButton("Main Menu", x, y, w, h);
if (this.cooldownTimer == 0 && mouseIsPressed && mouseX > x && mouseX <= x + w && mouseY > y && mouseY <= y + h)
{
this.cooldownTimer = 20;
this.sceneIndex = -1;
// sound effect
this.assetDictionary.get("buttonClick").play();
}
}
updateSummary()
{
// Text
fill(255)
stroke(0);
textSize(30);
textAlign(CENTER, TOP);
textFont(this.assetDictionary.get("font"));
let summaryText = "You defeated your competitors and will advance to the next round. Your debt has accrued interest.\n\nYou now owe " + this.totalDebt + " gold.\nYou currently have " + this.numberOfCoins + " gold.\nYou have " + this.player.health + "/" + this.player.totalHealth + " health remaining.\nYou currently have " + this.numberOfPotions + " potions.";
const margin = 60;
text(summaryText, margin, margin, width - 2 * margin, height - 2 * margin);
// Fight Button
let w = 200
let h = w/16*9;
let x = width/2 - w/2;
let y = 5*height/6;
drawButton("FIGHT!", x, y, w, h);
if (this.cooldownTimer == 0 && mouseIsPressed && mouseX > x && mouseX <= x + w && mouseY > y && mouseY <= y + h)
{
this.player.sprite.position.x = 200;
this.player.sprite.position.y = 200;
camera.zoom = 4;
this.sceneIndex = 1;
this.spawnEnemies();
// sound effect
this.assetDictionary.get("buttonClick").play();
}
// Buy Potion Button
const pricePerPotion = 8;
if (this.numberOfCoins >= pricePerPotion)
{
x = width/5 - w/2;
drawButton("Buy Potion", x, y, w, h);
if (this.cooldownTimer == 0 && mouseIsPressed && mouseX > x && mouseX <= x + w && mouseY > y && mouseY <= y + h)
{
this.numberOfCoins -= pricePerPotion;
++this.numberOfPotions;
this.cooldownTimer = 20;
// sound effect
this.assetDictionary.get("buttonClick").play();
}
}
// Pay Debt Button
if (this.numberOfCoins > 0)
{
x = 4*width/5 - w/2;
drawButton("Pay Debt", x, y, w, h);
if (this.cooldownTimer == 0 && mouseIsPressed && mouseX > x && mouseX <= x + w && mouseY > y && mouseY <= y + h)
{
this.totalDebt -= this.numberOfCoins;
this.numberOfCoins = 0;
if (this.totalDebt <= 0)
{
this.sceneIndex = 3;
this.assetDictionary.get("music").fade(0, 1);
}
// sound effect
this.assetDictionary.get("buttonClick").play();
}
}
}
updateArena()
{
this.room.draw();
drawSprites();
const healKey = 69;
if (keyIsDown(healKey))
this.healPlayer();
this.drawUI();
this.player.update();
for (let i = this.enemies.length - 1; i >= 0 ; --i)
{
// Enemy is dead
if (this.enemies[i].health <= 0)
{
this.enemies.splice(i, 1);
this.numberOfCoins += int(random(10, 16));
}
// Enemy is alive
else
this.enemies[i].update();
}
// Player is dead
if (this.player.health <= 0)
{
this.sceneIndex = 4;
camera.zoom = 1;
camera.position = createVector(width/2, height/2);
this.assetDictionary.get("music").fade(0, 1);
}
// Player Won match
else if (this.enemies.length == 0)
{
this.cooldownTimer = 20;
this.sceneIndex = 2;
++this.levelsCleared;
camera.zoom = 1;
camera.position = createVector(width/2, height/2);
this.totalDebt = int(this.totalDebt * this.interestRate);
}
}
// draws UI elements in Arena fights
drawUI()
{
const size = 16;
// anchors to top left corner
let x = camera.position.x - (width/2)/camera.zoom;
let y = camera.position.y - (height/2)/camera.zoom;
fill(255)
stroke(0);
textAlign(LEFT, BOTTOM);
textFont(this.assetDictionary.get("font"));
image(this.assetDictionary.get("potion"), x, y, size, size);
text(this.numberOfPotions, x + size, y, 4 * size, size);
image(this.assetDictionary.get("coin"), x, y + size, size, size);
text(this.numberOfCoins, x + size, y + size, 4 * size, size);
}
createPlayer(spawnPosition)
{
let playerAnimations = [];
playerAnimations.push(this.assetDictionary.get("playerIdle"));
playerAnimations.push(this.assetDictionary.get("playerRun"));
playerAnimations.push(this.assetDictionary.get("playerHit"));
let weapon = new Weapon(this.assetDictionary.get("swordWeapon"), 19, this.assetDictionary.get("swordSwing"));
this.player = new Player(playerAnimations, spawnPosition, 100, weapon, this.wallsGroup, this.enemies);
}
// fills room with multiple enemies
spawnEnemies()
{
let spawnPosition = createVector(330, 330);
this.createEnemy(spawnPosition);
spawnPosition = createVector(40, 330);
this.createEnemy(spawnPosition);
spawnPosition = createVector(330, 40);
this.createEnemy(spawnPosition);
spawnPosition = createVector(40, 40);
this.createEnemy(spawnPosition);
}
// creates single enemy
createEnemy(spawnPosition)
{
if (this.player == null)
throw new Exception("Need to create player before enemies are created");
let enemyAnimations = [];
enemyAnimations.push(this.assetDictionary.get("enemyIdle"));
enemyAnimations.push(this.assetDictionary.get("enemyRun"));
let weapon = new Weapon(this.assetDictionary.get("maceWeapon"), 10, this.assetDictionary.get("maceSwing"));
let newEnemy = new AI(enemyAnimations, spawnPosition, this.player, 75, weapon, this.wallsGroup);
this.enemies.push(newEnemy);
}
reset()
{
// need to remove any existing sprites becuase they are stored in a separate container, meaning they will still show up even if a new player/enemy replaces them
if (this.player != null)
{
this.player.sprite.remove();
this.player.weapon.sprite.remove();
}
for (let i = 0; i < this.enemies.length; ++i)
{
this.enemies[i].sprite.remove();
this.enemies[i].weapon.sprite.remove();
}
this.player = null;
this.enemies = [];
this.totalDebt = 100;
this.numberOfCoins = 0;
this.numberOfPotions = 5;
this.levelsCleared = 0;
this.assetDictionary.get("music").stop();
}
}