<level>
  <playground map="barriers.map">
    <decorations>
      <platform id="1" x="16" y="23" w="8" left="yellow" right="yellow"/>
      <platform id="21" x="3" y="6" w="11" left="1"/>
      <platform id="22" x="26" y="6" w="11" right="2"/>
      <platform id="23" x="3" y="13" w="11" left="3"/>
      <platform id="24" x="26" y="13" w="11" right="4"/>
      <platform id="25" x="3" y="20" w="11" left="5"/>
      <platform id="26" x="26" y="20" w="11" right="6"/>

      <crate id="11" type="big" x="8" y="6" visible="false"/>
      <crate id="12" type="big" x="31" y="6" visible="false"/>
      <crate id="13" type="big" x="8" y="13" visible="false"/>
      <crate id="14" type="big" x="31" y="13" visible="false"/>
      <crate id="15" type="big" x="8" y="20" visible="false"/>
      <crate id="16" type="big" x="31" y="20" visible="false"/>
    </decorations>
  </playground>

  <startposition platform="1"/>

  <gamecontrol>
  <![CDATA[
	var crateIds =    [11, 12, 13, 14, 15, 16];
	var platformIds = new Array();

	var currentObjective = 0;
	var crateLoaded = 0;
	
	function initNextObjective() {
		addBringCrateToPlatformObjective(crateIds[currentObjective], platformIds[currentObjective]);
		gameControl.getObjectById(crateIds[currentObjective]).show();
	}

	function init() {
		crateIds = shuffle(crateIds);
		for (var i=0; i<6; 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 = 21 + random(5);
			} while (platformId-10 == crateIds[i] || (i < 5 && platformId-10 == crateIds[i+1]));
			platformIds[i] = platformId;
		}
		
		initNextObjective();
	}

	function onCratePickup(crate) {
		gameControl.setPopUpText("please", (platformIds[currentObjective]-20));
		crateLoaded = 1;
	}
	
	function onLanded(platform) {
		if (crateLoaded == 1 && platform.getID() != platformIds[currentObjective]) {
			gameControl.setPopUpText("angry", (platformIds[currentObjective]-20));
		}
	}
	
	function onObjectiveReached() {
		switch (++currentObjective) {
		case 6:
			addLandOnPlatformObjective(1);
			if (player.isActive()) {
				gameControl.setPopUpText("thanks_finish");
			}
			crateLoaded = 0;
			break;
		
		case 7:
			gameControl.finish();
			break;
		
		default:
			if (player.isActive()) {
				gameControl.setPopUpText("thanks");
			}
			crateLoaded = 0;
			initNextObjective();
		}
	}
  ]]>
  </gamecontrol>
</level>
