JSFiddle - React, Tailwind, and code Playground
by angch
CSS
body {
font-family: Courier;
}
JavaScript
// simulate clicking nextwave at these times:
var simulatedClicksAt = [0, 2, 4];
var Generator = (function () {
function Generator(wave) {
this.wave = wave;
this.monster = 1;
this.monstersInWave = 60;
}
Generator.prototype.generate = function () {
m = "Wave" + this.wave + ".Monster" + this.monster;
this.monster++;
return m;
};
Generator.prototype.hasEnded = function () {
if (this.monster > this.monstersInWave) {
return true;
}
return false;
};
return Generator;
})();
var activeGenerators = [];
var gameTime = 0;
var currentWave = 1;
var maxWaves = 10;
function onclickNextWave() {
activeGenerators.push(new Generator(currentWave));
currentWave++;
}
// Tick
for (gameTime = 0;; gameTime++) {
if (simulatedClicksAt.indexOf(gameTime) >= 0) {
onclickNextWave();
}
// Exit now, just a demo, just in case.
if (gameTime >= 10000) {
break;
}
if (activeGenerators.length === 0) {
// Proper gameover:
if (currentWave > maxWaves) {
// Simplistic, since we need to check if there are spawns still on the map.
console.log("Game over");
break;
}
activeGenerators.push(new Generator(currentWave));
currentWave++;
}
var spawn = [];
for (var i = 0; i < activeGenerators.length; i++) {
spawn.push(activeGenerators[i].generate());
if (activeGenerators[i].hasEnded()) {
activeGenerators.splice(i, 1);
i--;
}
}
console.log(gameTime, spawn);
$("body").append(gameTime, " ", spawn.join(" "), "<br>");
}