This repository has been archived on 2025-04-27. You can view files and clone it, but cannot push or open issues or pull requests.
Servers-Under-Maintenance-G.../classes/Server.js

107 lines
2.7 KiB
JavaScript

class Server extends GameObject {
constructor(x, y) {
super(x, y);
this.clickable = true;
this.hitbox = new Hitbox(this.x, this.y, Game.TILESIZE, Game.TILESIZE);
this.sprite = Assets.server[Math.floor(Math.random() * Assets.server.length)];
this.framesSinceSpriteChange = 0;
this.failBlinkCycle = 0;
this.framesUntilNextFailure = Game.FPS * Game.MIN_TIME_UNTIL_FAIL + Math.floor(Math.random() * Game.FPS * Game.RANDOM_TIME_UNTIL_FAIL);
this.sparkFrames = -1;
this.numConnectors = 5;
this.numLines = 2;
this.ui = new ServerUI(this);
this.ui.visible = false;
this.load = 100;
}
draw() {
if (!this.failure) {
Game.c.drawImage(this.sprite, this.x, this.y - Game.TILESIZE, Game.TILESIZE, Game.TILESIZE * 2);
if (Math.random() < Math.pow(1.001, this.framesSinceSpriteChange) - 1) {
this.sprite = Assets.server[Math.floor(Math.random() * Assets.server.length)];
this.framesSinceSpriteChange = 0;
} else {
this.framesSinceSpriteChange++;
}
} else {
this.failBlinkCycle = (this.failBlinkCycle + 1) % (Game.FPS * 2);
Game.c.drawImage(Assets.serverFailure, this.x, this.y - Game.TILESIZE, Game.TILESIZE, Game.TILESIZE * 2);
Game.c.fillStyle = "red";
Game.c.globalAlpha = Math.abs(this.failBlinkCycle - Game.FPS) / (Game.FPS * 3);
Game.c.fillRect(this.x, this.y - Game.TILESIZE * 0.5, Game.TILESIZE, Game.TILESIZE * 1.5);
Game.c.globalAlpha = 1;
if (this.sparkFrames >= 0) {
Game.c.drawImage(Assets.spark[this.sparkFrames], this.x, this.y);
this.sparkFrames++;
if (this.sparkFrames >= Assets.spark.length) {
this.sparkFrames = -1;
}
} else {
if (Math.random() < 0.01) {
this.sparkFrames = 0;
Assets.sounds.server.spark.play();
}
}
}
}
click() {
if (this.failure && this.distance(Game.player) < Game.SERVER_ACCESS_DISTANCE) {
this.ui.visible = true;
Assets.sounds.server.open.play();
}
if (
Game.gamestate == "tutorial" && (
Game.tutorialStep == 3 ||
Game.tutorialStep == 4 ||
Game.tutorialStep == 13 ||
Game.tutorialStep == 18 ||
Game.tutorialStep == 27
)
) {
tutorialStep();
}
}
fix() {
this.framesUntilNextFailure = Game.FPS * Game.MIN_TIME_UNTIL_FAIL + Math.floor(Math.random() * Game.FPS * Game.RANDOM_TIME_UNTIL_FAIL);
Assets.sounds.server.fix.play();
Game.money += Game.FIX_SERVER_INCOME;
if (Game.gamestate == "tutorial") {
tutorialStep();
}
}
get failure() {
return this.framesUntilNextFailure <= 0;
}
update() {
if (!this.failure) {
this.framesUntilNextFailure--;
if (this.failure) {
this.ui.createFailure();
}
Game.serverLoad -= this.load;
}
}
}
class TutorialServer extends Server {
update() {
}
}