// Use manual goals to specify which ogres to defeat.

function createGenerator(spawnType, x, y, spawnAI) {
    var generator = game.spawnXY("generator", x, y);
    generator.spawnType = spawnType;
    generator.spawnAI = spawnAI;
}

// Scouts are aggressive and munchkins are just walking.
createGenerator("scout", 12, 12, "AttacksNearest");
createGenerator("scout", 68, 56, "AttacksNearest");
createGenerator("munchkin", 12, 56, "Scampers");
createGenerator("munchkin", 68, 12, "Scampers");

var player = game.spawnPlayerXY("duelist", 40, 34);
player.maxHealth = 1000;
player.attackDamage = 20;
player.maxSpeed = 20;

// These are our goals. Notice we save them in variables!
var spawnMunchkinsGoal = game.addManualGoal("Let 6 munchkins spawn.");
var dontTouchGoal = game.addManualGoal("Don't attack munchkins.");
var defeatScoutsGoal = game.addManualGoal("Defeat 6 scouts.");
// game properties used to count new and defeated ogres.
game.spawnedMunchkins = 0;
game.defeatedScouts = 0;
ui.track(game, "spawnedMunchkins");
ui.track(game, "defeatedScouts");

function onSpawn(event) {
    game.spawnedMunchkins += 1;
}

function onDefeat(event) {
    var unit = event.target;
    if (unit.type == "scout") {
        game.defeatedScouts += 1;
    }
    if (unit.type == "munchkin") {
        // dontTouchGoal is failed if a munchkin is defeated.
        game.setGoalState(dontTouchGoal, false);
        player.say("Oops.");
    }
}

// Block passages with forest tiles.
// Then destroy them when the player defeats some ogres.

// Set up the player.
var player = game.spawnPlayerXY("duelist", 6, 34);
player.attackDamage = 35;
player.maxHealth = 750;
player.maxSpeed = 15;
// The player should move through the forest to win.
game.addMoveGoalXY(76, 34);

// For each defeated ogre, spawn two new ogres.

var player = game.spawnPlayerXY("knight", 40, 34);
player.attackDamage = 20;

// Use this to count defeated ogres.
game.defeated = 0;
// How many ogres the player needs to defeat
game.toDefeat = 16;
game.addDefeatGoal(game.toDefeat);
// Show these useful variables to the player using ui.track
ui.track(player, "health");
ui.track(game, "defeated");
ui.track(game, "toDefeat");

// Starter enemies.
game.spawnXY("munchkin", 30, 14);
game.spawnXY("munchkin", 50, 54);

// This makes new enemies aggresive.
function onSpawn(event) {
    var unit = event.target;
    unit.behavior = "AttacksNearest";
}

<!-- JavaScript can be used to show and hide elements! -->

<script>
    var showButton = $("#showButton");
    // Create the variable 'hideButton' to store #hideButton
    var hideButton = $("#hideButton");
    var image = $("#image");
    function showElement() {
        image.show();
    }

// Now you can create a custom goal for your game
// using the game.addManualGoal(description) method!

// Spawn a few scouts, a boss, and a maze.
game.spawnMaze(5);
game.spawnXY("scout", 60, 58);
game.spawnXY("scout", 28, 29);
game.spawnXY("scout", 61, 24);
var ogref = game.spawnXY("ogre-f", 60, 12);