// Protect the workers and animals! // Defend these two positions: var defend = []; defend[0] = { x: 98, y: 28 }; defend[1] = { x: 84, y: 7 }; var soldiers = []; var friends = this.findFriends(); for(var i=0; i < friends.length; i++) { var friend = friends[i]; if(friend.type == "soldier") { soldiers.push(friend); } else { // Defend the workers: defend.push(friend); } }
loop { // Use a for-loop to assign each soldier to a corresponding defend[] target // Use command(soldier, "defend", thang) or command(soldier, "defend", position) for(var y = 0; y < soldiers.length; y ++) { var f = soldiers[y]; this.command(f, "defend", defend[y]); }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19、The Bane of Soldiers
// Robobombs explode when they die or touch an enemy. // Split up your soldiers so that they don‘t all get exploded together.
loop { var friends = this.findFriends(); for(var index = 0; index < friends.length; index++) { var friend = friends[index]; // Use the index to decide where to command each soldier to move. if (friend && index == "0") { this.command(friend, "move", {x:58,y:46}); } else { this.command(friend, "move", {x:friend.pos.x-1,y:friend.pos.y}); }
} }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20、Ring Bearer
// You must escort a powerful magical ring back to town to be studied. // The goal is to escape, not fight. More ogres lurk in the surrounding mountains! // Make a circle of soldiers around the peasant! // We give you two functions to help with this:
// findSoldierOffset figures out the position a soldier should stand at in relation to the peasant. // The first argument ‘soldiers‘ should be an array of your soldiers. // The second argument ‘i‘ is the index of the soldier (in soldiers) you want to find the position for. function findSoldierOffset(soldiers, i) { var soldier = soldiers[i]; var angle = i * 360 / soldiers.length; return radialToCartesian(5, angle); } // This function does the math to determine the offset a soldier should stand at. function radialToCartesian(radius, degrees) { var radians = Math.PI / 180 * degrees; var xOffset = radius * Math.cos(radians); var yOffset = radius * Math.sin(radians); return {x: xOffset, y: yOffset}; } var peasant = this.findByType("peasant")[0]; // Use findByType to get an array of your soldiers. loop { // Use a for-loop to iterate over your array of soldiers var f = this.findFriends(); for(var index = 0; index < f.length; index ++){ var friend = f[index]; if (friend.type == "soldier") { var offset = findSoldierOffset(f,index); moveTo = {x: peasant.pos.x + offset.x,y:peasant.pos.y + offset.y}; this.command(friend, "move", moveTo); } } // Find the offset for a soldier. // Add the offset.x and offset.y to the peasant‘s pos.x and pos.y. // Command the soldier to move to the new offset position.
// The hero should keep pace with the peasant! this.move({x: this.pos.x + 0.2, y: this.pos.y}); }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21、双生花
// 如果花匠受伤了,双生花会缩小! this.summonSoldiers = function() { if (this.gold >= this.costOf("soldier")) { this.summon("soldier"); } }; // 定义函数:commandSoldiers this.commandSoldiers = function() { var friends = this.findFriends(); for(var index = 0; index < friends.length; index ++) { var friend = friends[index]; var enemy = friend.findNearestEnemy(); if (enemy && friend.type == "soldier") { this.command(friend, "attack", enemy); } }
//多试几次就有好运气啦 // Command the peasants to prevent the ogres from scoring. // The fireball is type "ball". var friends = this.findFriends(); var i = 0;var j = -1; loop { if (i%10 === 0) { j = -j; } this.command(friends[1], "move", {x:17,y:friends[1].pos.y+j}); this.command(friends[0], "move", {x:17,y:friends[0].pos.y-j}); i++; }