This is my code so far in Final Project Level
// Spawn a player with spawnPlayerXY()
var hero=game.spawnPlayerXY("samurai", 34, 8);
hero.maxHealth=200;
hero.attackDamage=70;
hero.maxSpeed=20;
// Add at least one goal!
game.addSurviveGoal();
var scoutGoal=game.addManualGoal("Defeat the three big ogres");
var skeletonGoal=game.addManualGoal("Defeat the skeleton using the lightstone");
var generatorGoal=game.addManualGoal("Destroy the generator! Have archers protect you from behind the fence.");
// Counters
game.collectedChests = 0;
ui.track(game, "collectedChests");
game.lightstoneCollected = 0;
ui.track(game, "lightstoneCollected");
var lightExists=false;
var lightDuration=6;
var lightEnd=0;
var heroGotLight=false;
// Animated units
var generator=game.spawnXY("generator", 65, 55);
generator.spawnType="scout";
generator.spawnTime=6;
generator.spawnAI="Scampers";
var skeleton=game.spawnXY("skeleton", 13, 56);
skeleton.maxHealth=1000;
game.spawnXY("ogre-f", 39, 56);
game.spawnXY("ogre-f", 41, 33);
game.spawnXY("ogre-f", 12, 10);
var archer1=game.spawnXY("archer", 10, 60);
var archer2=game.spawnXY("archer", 11, 59);
var archer3=game.spawnXY("archer", 8, 55);
// Pathways and keys
var lightX=game.spawnXY("x-mark-bones", 11, 26);
var fenceDoor1=game.spawnXY("fence", 52, 36);
var fenceDoor2=game.spawnXY("fence", 52, 32);
var openX1=game.spawnXY("x-mark-stone", 48, 35);
var closeX1=game.spawnXY("x-mark-wood", 43, 40);
var openX2=game.spawnXY("x-mark-stone", 58, 40);
var closeX2=game.spawnXY("x-mark-wood", 58, 35);
//var forestDoor1 = game.spawnXY("forest", 64, 28);
//var forestDoor1 = game.spawnXY("forest", 72, 28);
// Forest and fences
game.spawnXY("forest", 23, 8);
game.spawnXY("forest", 23, 14);
game.spawnXY("forest", 23, 22);
game.spawnXY("fence", 33, 46);
game.spawnXY("fence", 38, 46);
game.spawnXY("fence", 33, 50);
game.spawnXY("fence", 33, 54);
game.spawnXY("forest", 8, 48);
game.spawnXY("forest", 16, 48);
game.spawnXY("forest", 50, 60);
game.spawnXY("forest", 50, 54);
game.spawnXY("forest", 50, 48);
game.spawnXY("forest", 50, 42);
game.spawnXY("forest", 50, 28);
game.spawnXY("forest", 56, 28);
game.spawnXY("forest", 44, 22);
game.spawnXY("forest", 44, 16);
game.spawnXY("forest", 44, 10);
game.spawnXY("forest", 44, 6);
// Fire traps
game.spawnXY("fire-trap", 57, 20);
game.spawnXY("fire-trap", 60, 20);
game.spawnXY("fire-trap", 63, 20);
game.spawnXY("fire-trap", 66, 20);
game.spawnXY("fire-trap", 69, 20);
game.spawnXY("fire-trap", 57, 17);
game.spawnXY("fire-trap", 60, 17);
game.spawnXY("fire-trap", 63, 17);
game.spawnXY("fire-trap", 66, 17);
game.spawnXY("fire-trap", 69, 17);
game.spawnXY("fire-trap", 57, 14);
game.spawnXY("fire-trap", 60, 14);
game.spawnXY("fire-trap", 63, 14);
game.spawnXY("fire-trap", 66, 14);
game.spawnXY("fire-trap", 69, 14);
game.spawnXY("fire-trap", 57, 11);
game.spawnXY("fire-trap", 60, 11);
game.spawnXY("fire-trap", 63, 11);
game.spawnXY("fire-trap", 66, 11);
game.spawnXY("fire-trap", 69, 11);
function onSpawn(event){
var unit=event.target;
if (unit.type=="ogre"){
unit.maxHealth=15;
unit.attackDamage=40;
unit.say("Good morning");
while(true){
if (unit.distanceTo(hero) < 20){
unit.say("Hero within range" );
unit.attack(hero);
}
}
}
if (unit.type=="skeleton"){
while(true){
if (hero.distanceTo(unit)<20 && heroGotLight === false){
unit.say("Hero got weapon!");
unit.attack(hero);
} else if (hero.distanceTo(unit)<8 && heroGotLight === true){
unit.defeat();
} else if (hero.distanceTo(unit)<20 && heroGotLight === true){
unit.behavior="RunsAway";
}
}
}
}
function goToBorder(unit){
var offset=game.randomInteger(-2,2);
unit.moveXY(25+offset, 58+offset);
unit.moveXY(26+offset, 36+offset);
unit.moveXY(44+offset, 36+offset);
//unit.behavior="Defends";
}
function onDefeat(event){
var unit = event.target;
if (unit.type=="ogre"){
game.spawnXY("potion-large", 32, 39);
game.spawnXY("chest", 35, 38);
}
if (unit.type=="skeleton"){
archer1.say("Skeleton down");
goToBorder(archer1);
goToBorder(archer2);
goToBorder(archer3);
}
}
function onCollect(event){
var item=event.other;
hero.say("Look what I've got! " + item.type);
if (item.type=="gem"){
game.collectedChests +=1;
hero.say("Chests collected " + game.collectedChests);
if (game.collectedChests >= 3){
var lightstone=game.spawnXY("lightstone", 11, 26);
lightExists=true;
hero.say("Lightstone is spawned " + lightExists );
}
}
}
game.setActionFor("ogre", "spawn", onSpawn);
game.setActionFor("skeleton", "spawn", onSpawn);
game.setActionFor("ogre", "defeat", onDefeat);
game.setActionFor("skeleton", "defeat", onDefeat);
hero.on("collect", onCollect);
function checkLight(event){
while(true){
if (lightExists === true && hero.distanceTo(lightX) < 3 && lightEnd ===0){
game.lightstoneCollected = 1;
hero.say("I've got Light!");
lightEnd=game.time+lightDuration;
heroGotLight=true;
}
if (lightEnd < game.time && game.lightstoneCollected === 1) {
lightExists=false;
hero.say("Ooops, no Light!");
heroGotLight=false;
}
}
}
game.setActionFor("lightstone", "spawn", checkLight);
It’s a kind of a quest.
You defeat the three big ogres and collect the three gem chests to spawn a lightstone. Pick up a lightstone to defeat the skeleton. On defeat of the skeleton, the function goToBorder()
is executed with archers as arguments.
They generally get to the point, however somehow not nicely. But if I set their behavior after the .moveXY(), they no longer move on defeat of the skeleton. So I decided to test it separately while preserving the existing code 