<level>
  <playground map="blank.map">
    <decorations>
      <platform id="1" x="16" y="24" w="8"/>
      
      <crate id="10" x="6" y="7" type="big" visible="false"/>
      <platform id="11" x="4" y="7" w="5" left="1" right="1"/>
      <tile id="12" x="4" y="8" category="danger" tileId="02"/>
      <tile id="13" x="5" y="8" category="danger" tileId="03"/>
      <tile id="14" x="6" y="8" category="danger" tileId="03"/>
      <tile id="15" x="7" y="8" category="danger" tileId="03"/>
      <tile id="16" x="8" y="8" category="danger" tileId="04"/>

      <crate id="20" x="14" y="6" type="big" visible="false"/>
      <platform id="21" x="12" y="6" w="5" left="2" right="2"/>
      <tile id="22" x="12" y="7" category="danger" tileId="02"/>
      <tile id="23" x="13" y="7" category="danger" tileId="03"/>
      <tile id="24" x="14" y="7" category="danger" tileId="03"/>
      <tile id="25" x="15" y="7" category="danger" tileId="03"/>
      <tile id="26" x="16" y="7" category="danger" tileId="04"/>

      <crate id="30" x="33" y="15" type="big" visible="false"/>
      <platform id="31" x="31" y="15" w="5" left="3" right="3"/>
      <tile id="32" x="31" y="16" category="danger" tileId="02"/>
      <tile id="33" x="32" y="16" category="danger" tileId="03"/>
      <tile id="34" x="33" y="16" category="danger" tileId="03"/>
      <tile id="35" x="34" y="16" category="danger" tileId="03"/>
      <tile id="36" x="35" y="16" category="danger" tileId="04"/>

      <crate id="40" x="24" y="12" type="big" visible="false"/>
      <platform id="41" x="22" y="12" w="5" left="4" right="4"/>
      <tile id="42" x="22" y="13" category="danger" tileId="02"/>
      <tile id="43" x="23" y="13" category="danger" tileId="03"/>
      <tile id="44" x="24" y="13" category="danger" tileId="03"/>
      <tile id="45" x="25" y="13" category="danger" tileId="03"/>
      <tile id="46" x="26" y="13" category="danger" tileId="04"/>

      <crate id="50" x="20" y="18" type="big" visible="false"/>
      <platform id="51" x="18" y="18" w="5" left="5" right="5"/>
      <tile id="52" x="18" y="19" category="danger" tileId="02"/>
      <tile id="53" x="19" y="19" category="danger" tileId="03"/>
      <tile id="54" x="20" y="19" category="danger" tileId="03"/>
      <tile id="55" x="21" y="19" category="danger" tileId="03"/>
      <tile id="56" x="22" y="19" category="danger" tileId="04"/>
    </decorations>
  </playground>

  <startposition platform="1"/>

  <gamecontrol>
  <![CDATA[
	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();
		}
	}
  ]]>
  </gamecontrol>
</level>
