var crateIds = [10, 20, 30, 40, 50]; var platformIds = new Array();

var currentObjective = 0; var crateLoaded = 0;

var crate1 = gameControl.getObjectById(10); var platform1 = gameControl.getObjectById(11); var platform2 = gameControl.getObjectById(21); var platform3 = gameControl.getObjectById(31); var platform4 = gameControl.getObjectById(41); var platform5 = gameControl.getObjectById(51);

var speed = 0.025;

var xDiff = 0.0; var yDiff = 0.0;

function initNextObjective() { var crate = gameControl.getObjectById(crateIds[currentObjective]); var platform = gameControl.getObjectById(crate.getID() + 1);

addBringCrateToPlatformObjective(crate.getID(), platformIds[currentObjective]);

crate.setX(platform.getX() + xDiff); crate.setY(platform.getY() + yDiff); crate.show(); }

function getRandomSpeed() { return speed - 0.015 + 0.03*Math.random(); }

function setGroupSpeed(group, vx, vy) { for (var id = 10*group; id <= 10*group + 6; id++) { gameControl.getObjectById(id).setSpeed(vx, vy); } }

function init() { xDiff = crate1.getX() - platform1.getX(); yDiff = crate1.getY() - platform1.getY();

crateIds = shuffle(crateIds); for (var i=0; i<5; i++) { do { // The target platforms are selected random with the following constraints: // - The target platform must not be the crate's platform. // - The target platform must not be the next crate's platform.

var platformId = 10*(1+random(4)) + 1; } while (platformId-1 == crateIds[i] || (i < 4 && platformId-1 == crateIds[i+1])); platformIds[i] = platformId; } initNextObjective();

setGroupSpeed(1, 0, -getRandomSpeed()); setGroupSpeed(2, getRandomSpeed(), 0); setGroupSpeed(3, 0, getRandomSpeed()); setGroupSpeed(4, -getRandomSpeed(), 0); setGroupSpeed(5, getRandomSpeed(), 0); }

function onCratePickup(crate) { gameControl.setPopUpText("please", ((platformIds[currentObjective]-1)/10)); crateLoaded = 1; }

function update(elapsedTime) { if (platform1.getY() <= 64) { setGroupSpeed(1, 0, getRandomSpeed()); } if (platform1.getY() >= 336) { setGroupSpeed(1, 0, -getRandomSpeed()); }

if (platform2.getX() <= 160) { setGroupSpeed(2, getRandomSpeed(), 0); } if (platform2.getX() >= 400) { setGroupSpeed(2, -getRandomSpeed(), 0); }

if (platform3.getY() <= 64) { setGroupSpeed(3, 0, getRandomSpeed()); } if (platform3.getY() >= 336) { setGroupSpeed(3, 0, -getRandomSpeed()); }

if (platform4.getX() <= 160) { setGroupSpeed(4, getRandomSpeed(), 0); } if (platform4.getX() >= 400) { setGroupSpeed(4, -getRandomSpeed(), 0); }

if (platform5.getX() <= 160) { setGroupSpeed(5, getRandomSpeed(), 0); } if (platform5.getX() >= 400) { setGroupSpeed(5, -getRandomSpeed(), 0); }

var landedPlatform = player.getLandedPlatform(); if (landedPlatform != null) { // Always synchronize velocity of landed ship with platform. player.setSpeed(landedPlatform.getHorizontalSpeed(), landedPlatform.getVerticalSpeed()); } }

function onLanded(platform) { if (crateLoaded == 1 && platform.getID() != platformIds[currentObjective]) { gameControl.setPopUpText("angry", ((platformIds[currentObjective]-1)/10)); }

player.setSpeed(platform.getHorizontalSpeed(), platform.getVerticalSpeed()); }

function onObjectiveReached() { switch (++currentObjective) { case 5: if (player.isActive()) { gameControl.setPopUpText("thanks"); } gameControl.finish(); break;

default: if (player.isActive()) { gameControl.setPopUpText("thanks"); } crateLoaded = 0; initNextObjective(); } }