<level>
  <playground map="tutorial01.map">
    <decorations>
      <platform id="1" x="3" y="24" w="10"/>
      <platform id="2" x="26" y="24" w="10" left="yellow" right="yellow"/>
      
      <crate x="10" y="24" type="double"/>
      
      
      <tile id="10" x="30" y="3" category="hexagonal" tileId="23" hitpoints="10000"/>
      <tile id="11" x="33" y="3" category="hexagonal" tileId="23" hitpoints="10000"/>
      <tile id="12" x="36" y="3" category="hexagonal" tileId="23" hitpoints="10000"/>

      <tile id="13" x="30" y="4" category="hexagonal" tileId="35" hitpoints="10000"/>
      <tile id="14" x="31" y="4" category="hexagonal" tileId="39" hitpoints="10000"/>
      <tile id="15" x="32" y="4" category="hexagonal" tileId="31" hitpoints="10000"/>
      <tile id="16" x="33" y="4" category="hexagonal" tileId="18" hitpoints="10000"/>
      <tile id="17" x="34" y="4" category="hexagonal" tileId="39" hitpoints="10000"/>
      <tile id="18" x="35" y="4" category="hexagonal" tileId="31" hitpoints="10000"/>
      <tile id="19" x="36" y="4" category="hexagonal" tileId="37" hitpoints="10000"/>

      <tile id="20" x="31" y="5" category="hexagonal" tileId="24" hitpoints="10000"/>
      <tile id="21" x="32" y="5" category="hexagonal" tileId="20" hitpoints="10000"/>
      <tile id="22" x="33" y="5" category="hexagonal" tileId="22" hitpoints="10000"/>
      <tile id="23" x="34" y="5" category="hexagonal" tileId="24" hitpoints="10000"/>
      <tile id="24" x="35" y="5" category="hexagonal" tileId="20" hitpoints="10000"/>

      <tile id="25" x="33" y="5" category="hexagonal" tileId="15" hitpoints="10000"/>
      <tile id="26" x="30" y="4" category="hexagonal" tileId="16" hitpoints="10000"/>
      <tile id="27" x="36" y="4" category="hexagonal" tileId="14" hitpoints="10000"/>

      <turret id="28" orientation="top" x="33" y="6" type="dome">
        <barrel weapon="laser" type="smart" delay="10"/>
      </turret>
      <turret id="29" orientation="top" x="30" y="5" type="dome">
        <barrel type="smart" delay="12"/>
      </turret>
      <turret id="30" orientation="top" x="36" y="5" type="dome">
        <barrel type="smart" delay="12"/>
      </turret>
    </decorations>
  </playground>

  <startposition platform="1"/>

  <gamecontrol>
  <![CDATA[
	var centerTile = gameControl.getObjectById(16);
	var explodingTileIds = shuffle([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]);
	var numExplodingTiles = 15;
	var explodingTilesCount = 0;
	
    var turretsDestroyed = 0;
    var speed = 0.05001;
    var timer = createTimer(30);
    
    function setBossSpeed(vx) {
		for (var id = 10; id <= 30; id++) {
			gameControl.getObjectById(id).setSpeed(vx, 0);
		}
	}

	function init() {
		// This is a hack to prevent the turrets from shooting themselves
		// in case the ship is flying above the boss.
		for (var x = 0; x < 40; x++) {
			gameControl.getLevel().getPlayGround().getMap().markAsForegroundTile(x, 4);
		}
		
		setBossSpeed(-speed);
	}

	function update(elapsedTime) {
		if (centerTile == null)
			return;
		
		if (centerTile.getX() < 96) {
			setBossSpeed(speed);
		}
		if (centerTile.getX() > 640-96) {
			setBossSpeed(-speed);
		}
		
		if (turretsDestroyed == 3 && explodingTilesCount < numExplodingTiles) {
			if (timer.action(elapsedTime)) {
				gameControl.getObjectById(explodingTileIds[explodingTilesCount++]).destroy();
				timer.refresh();
			}
		}
	}
	
	function onDestroyed(object) {
		switch (object.getID()) {
		case 28:
			turretsDestroyed++;
			gameControl.getObjectById(25).destroy();
			break;
		case 29:
			turretsDestroyed++;
			gameControl.getObjectById(26).destroy();
			break;
		case 30:
			turretsDestroyed++;
			gameControl.getObjectById(27).destroy();
			break;
		}			

		if (turretsDestroyed == 3) {
			addLandOnPlatformObjective(2);
		}
	}
		
	function onObjectiveReached() {
		gameControl.finish();
	}
  ]]>
  </gamecontrol>
</level>
