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 databaseAhem = [
    { idle: 2, monsters: 10 },
    { idle: 2, monsters: 20 },
    { idle: 2, monsters: 30 },
    { idle: 2, monsters: 20 },
    { idle: 2, monsters: 20 },
    { idle: 2, monsters: 20 }
];

var Generator = (function () {
    function Generator(wave, monsters) {
        this.wave = wave;
        this.monster = 1;
        this.monstersInWave = monsters;
    }

    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;
var ticksBetweenWaves = 2;
var idleTicks = 0;

function onclickNextWave() {
    activeGenerators.push(new Generator(currentWave, databaseAhem[currentWave-1].monsters));
    currentWave++;
}

// Tick
for (gameTime = 0;; gameTime++) {
    if (simulatedClicksAt.indexOf(gameTime) >= 0) {
        onclickNextWave();
    }
    
    // Exit now, just a demo, just in case.
    if (gameTime >= 1000) {
        break;
    }
    
    if (activeGenerators.length === 0) {
        // Proper gameover:
        if (currentWave > databaseAhem.length) {
            // Simplistic, since we need to check if there are spawns still on the map.
            console.log("Game over");
            break;
        }
        
        idleTicks++;
        
        // Or lookup in the "database" for a variable ticksBetweenWaves
        if (idleTicks > databaseAhem[currentWave-1].idle) {
            activeGenerators.push(new Generator(currentWave, databaseAhem[currentWave-1].monsters));
            currentWave++;
        }
    }

    var spawn = [];
    for (var i = 0; i < activeGenerators.length; i++) {
       ...