#include "hostAPI.txt"

#include "global.txt"

var count;

var i;

var bOnwayBoss;
var bKilledOnwayBoss;
var iEndCount;
var bStageAVG;

func onBossDie() {
	if(bOnwayBoss == false) {
		iEndCount = 0;
		htClearBackgroundCover();
	}
	else {
		bOnwayBoss = false;
		if(bKilledOnwayBoss == true) {
			bKilledOnwayBoss = false;
			htAddScore(1000000);
			
			htInsertItem(ITEM_LIFE, 400, 200);
			
			var text;
			text = "Bonus: 1000000";
			htAddFloatingText(text, "DFGirl.ttf", 30, 1.0, 0.6, 0.0, 1.0, 400.0, 200.0, 400.0, 0.0, 90, 30, 30);
		}
	}
}

func onAVGEnd() {
	if(bStageAVG == true)
		htRunSTGScript("scripts/stage3/stage3_normal.txt");
}

func onScoreBoardEnd() {
	htRunAVGScript("scripts/AVG/part3.script");
}

func createBossGreen() {
	htCreateBoss("Green", 10, "green.png", 0, 0, 128, 128, 5);
	htSetBossPos(400, 100);
	htSetBossMovementRect(128, 0, 128, 128, 5, 256, 0, 128, 128, 5);

	htSetBossDieAnim(512, 0, 128, 128, 17);
	htAddBossAnim(0, 384, 0, 128, 128, 5);
	
	htSetBossMagicCircleColor(0.0, 0.0, 0.5, 1.0);
	
	htAddBossAttackScript("scripts/stage2/green/green-1-normal.txt", 500, 1000, 500000, true);
	htAddBossAttackScript("scripts/stage2/green/green-2-normal.txt", 1000, 1000, 550000, true);
	htAddBossAttackScript("scripts/stage2/green/green-3-normal.txt", 800, 1000, 600000, true);
	htAddBossAttackScript("scripts/stage2/green/green-4-normal.txt", 850, 1000, 650000, true);
	htAddBossAttackScript("scripts/stage2/green/green-5-normal.txt", 2000, 1000, 700000, true);

//	htPlayBossAnimEx(-2, 20, true);

	htSetBossNameboxColor(0.0, 1.0, 0.0, 1.0);
}

func createOnwayBoss() {
	htCreateBoss("Egra", 10, "egra.png", 0, 0, 64, 64, 1);
	htSetBossPos(400, 200);
	htSetBossMovementRect(0, 0, 64, 64, 1, 0, 0, 64, 64, 1);
	
	htSetBossDieAnim(0, 64, 64, 64, 1);
	htAddBossAnim(0, 64, 0, 64, 64, 1);
	
	htAddBossAttackScript("scripts/stage2/onway1_normal.txt", 500, 1000, 100000, false);
	
	htSetBossNameBoxColor(1.0, 0.6, 0.0, 1.0);
}

func cakeAttackCircle1(id, x, y, timeCount) {
	for(i=0; i<8; ++i) {
		htInsertBulletVec(0, 0, x, y, 4, i*30+randomFloat(0, 60));
	}
}  	

func cakeAttackCircle1_green(id, x, y, timeCount) {
	for(i=0; i<6; ++i) {
		htInsertBulletVec(0, 2, x, y, 4, i*30+randomFloat(0, 60));
	}
}  	

func cakeAttackCircle2(id, x, y, timeCount) {
	for(i=0; i<18; ++i) {
		htInsertBulletVec(3, 0, x, y, 4, i*15);
	}
}  

func cakeAttackCircle2_random(id, x, y, timeCount) {
	for(i=0; i<18; ++i) {
		htInsertBulletVec(3, 0, x, y, 4, i*15+randomFloat(0, 60));
	}
}  


func cakeFanAttack1(id, x, y, timeCount) {
	if(timeCount >= 30) {
		for(i=0; i<=6; ++i) {
			htInsertBulletVec(2, 6, x, y, 6, 60+i*15);
		}
	}
}

func cakeFanAttack2(id, x, y, timeCount) {
	for(i=1; i<=3; ++i) {
		htInsertBulletVec(2, 4, x, y, 6, 30+i*30);
	}
}

func cakeAttackToPlayer(id, x, y, timeCount) {
	htShootBulletToPlayer(3, 0, x, y, 5);
}

func cakeAttackToPlayer_waterRed(id, x, y, timeCount) {
	htShootBulletToPlayer(0, 0, x, y, 5);
}

func cakeAttackToPlayerWithCircle(id, x, y, timeCount) {
	htShootBulletToPlayer(3, 0, x, y, 5);
	
	if(timeCount % 20 == 0) {
		var px;
		var py;
		px = htGetPlayerX();
		py = htGetPlayerY();
		var dgr;
		dgr = GetDegreeBetweenTwoPoints(x, y, px, py);
		for(i=0; i<16; ++i) {
			htInsertBulletVec(5, 2, x, y, 5, dgr+20+i*20);
		}
	}
}

func cakeAttackToPlayer_Line(id, x, y, timeCount) {
	if(timeCount % 30 == 0) {
		for(i=0; i<10; ++i) 
			htShootBulletToPlayer(5, 1, x, y, 3+i*0.4);
	}
	if(timeCount % 45 == 0) {
		for(i=0; i<36; ++i)
			htInsertBulletVec(0, 0, x, y, 4, i*10+timeCount);
	}
}

func cakeAttackToPlayer_Line_De(id, x, y, timeCount) {
//	if(timeCount % 30 == 0) {
		var degree;
		var px;
		var py;
		
		px = htGetPlayerX();
		py = htGetPlayerY();
		
		degree = GetDegreeBetweenTwoPoints(x, y, px, py);
		
		for(i=0; i<20; ++i) 
			htInsertBulletVec(5, 0, x, y, 1+i*0.2, degree+10);
		for(i=0; i<20; ++i) 
			htInsertBulletVec(5, 0, x, y, 1+i*0.2, degree-10);
//	}
}

func dieBullet1(x, y) {
	for(i=0; i<24; ++i) {
		htInsertBulletVec(5, 1, x, y, 4, i*15);
	}
}

func dieBullet2(x, y) {
	for(i=0; i<24; ++i) {
		htInsertBulletVec(5, 0, x, y, 4, i*15);
	}
}
	

func update() {
	++count;
	
	var tmpCakeID;
	
	if(count == 100)
		htCreatePlayer();
		
	if(count == 2760) {
		bOnwayBoss = true;
		createOnwayBoss();
		htBossStart();
	}
	
	//算是增援吧
	//如果boss杀得够快。。。
	if(count > 2760 && (count < 3720)) {
		if(bOnwayBoss == false) {
			if(count % 240 == 0) {
				for(i=0; i<6; ++i) {
					tmpCakeID = createCake(300, -50, 8, "cakeAttackToPlayer", 30, 6, i*20);
					htSetEnemySpeed(tmpCakeID, 1, 3);
					htSetEnemyDeflection(tmpCakeID, -0.2, 120, 480);
					tmpCakeID = createCake(500, -50, 8, "cakeAttackToPlayer", 30, 6, i*20);
					htSetEnemySpeed(tmpCakeID, -1, 3);
					htSetEnemyDeflection(tmpCakeID, 0.2, 120, 480);
				}
				
	
				for(i=0; i<6; ++i) {
					tmpCakeID = createCake(150, 100, 8, "cakeAttackCircle1", 20, 12, i*20);
					htSetEnemySpeed(tmpCakeID, 3, 0);
		
					tmpCakeID = createCake(650, 150, 8, "cakeAttackCircle1", 20, 12, i*20);
					htSetEnemySpeed(tmpCakeID, -3, 0);
				}
			}
		}
	}
	
	//小boss最大持续时间到了。。。
	//强制挂掉boss恩恩
	if(count == 3720 && (bOnwayBoss == true)) {
		htEndBoss();
		bKilledOnwayBoss = false;
	}

	if(count == 6060) {
		htDisplayBossTitle("green-title.png", 60);
		
		htSetBackgroundSpeed(5);
		htSetFogSpeed(7);
	}

	if(count == 6180) {
		createBossGreen();
	}
	if(count == 6240) {	
		htRunAVGScriptInSTG("scripts/stage2/daozhong2.script");
	}
	if(count == 6270) {
		htBossStart();
		
		bStageAVG = true;
				
		htEnable2DFog1(false, 0);
		htSetBackgroundMusic("天魔降伏.mp3");
		htSetSCBackground("green_bg_sc1.png", 450, 600, 1);
	}
	
	if(iEndCount != -1) {
		++iEndCount;
		if(iEndCount > 90) {
			htStageEnd();
			iEndCount = -1;
		}
	}

}

func init() {
	count = 0;
	i = 0;

	var tmpCakeID;
	
	bStageAVG = false;

	htClearBackgroundCover();
	htEnable2DFog1(false, 5);
	htEnableSeaSimulation();
	
	htEnableSeaSimulation();

	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(300, -50, 5, "cakeAttackToPlayer", 30, 6, 300+i*20);
		htSetEnemySpeed(tmpCakeID, 1, 3);
		htSetEnemyDeflection(tmpCakeID, -0.2, 120, 480);
		tmpCakeID = createCake(500, -50, 5, "cakeAttackToPlayer", 30, 6, 300+i*20);
		htSetEnemySpeed(tmpCakeID, -1, 3);
		htSetEnemyDeflection(tmpCakeID, 0.2, 120, 480);
	}
	
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(150, 100, 5, "cakeAttackCircle1", 20, 12, 600+i*20);
		htSetEnemySpeed(tmpCakeID, 3, 0);
		
		tmpCakeID = createCake(650, 150, 5, "cakeAttackCircle1", 20, 12, 600+i*20);
		htSetEnemySpeed(tmpCakeID, -3, 0);
	}
	
	
	tmpCakeID = createIcecream_Fade(400, 100, 50, "cakeAttackCircle2_random", 5, 30, 10, 940);
	htMoveEnemyTo(tmpCakeID, 400, 650, 180, 180);
	htBindDieFuncToEnemy(tmpCakeID, "item_c");
	
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(300, -50, 5, "cakeAttackToPlayer_waterRed", 15, 4, 1020+i*20);
		htSetEnemySpeed(tmpCakeID, 0, 5);
		tmpCakeID = createCake(500, -50, 5, "cakeAttackToPlayer_waterRed", 15, 4, 1020+i*20);
		htSetEnemySpeed(tmpCakeID, 0, 5);
	}

	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(350, -50, 5, "cakeAttackCircle1", 20, 9, 1360+i*20);
		htSetEnemySpeed(tmpCakeID, 0, 3);
		htSetEnemyDeflection(tmpCakeID, -0.4, 120, 480);
		tmpCakeID = createCake(450, -50, 5, "cakeAttackCircle1", 20, 9, 1360+i*20);
		htSetEnemySpeed(tmpCakeID, 0, 3);
		htSetEnemyDeflection(tmpCakeID, 0.4, 120, 480);
	}
	
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(150, 150, 5, "cakeAttackCircle1_green", 15, 18, 1540+i*20);
		htSetEnemySpeed(tmpCakeID, 3, 0);
		
		tmpCakeID = createCake(650, 100, 5, "cakeAttackCircle1_green", 15, 18, 1540+i*20);
		htSetEnemySpeed(tmpCakeID, -3, 0);
	}
	
	tmpCakeID = createIcecream_Fade(300, 100, 40, "cakeAttackToPlayer_Line", 15, 3, 10, 1940);
	htMoveEnemyTo(tmpCakeID, 300, 650, 60, 180);
	htBindDieFuncToEnemy(tmpCakeID, "item_p");
	tmpCakeID = createIcecream_Fade(500, 100, 40, "cakeAttackToPlayer_Line", 15, 3, 10, 1940);
	htMoveEnemyTo(tmpCakeID, 500, 650, 60, 180);
	htBindDieFuncToEnemy(tmpCakeID, "item_p");
	
	tmpCakeID = createIcecream_Fade(250, 150, 40, "cakeAttackToPlayer_Line", 15, 3, 10, 2120);
	htMoveEnemyTo(tmpCakeID, 100, 400, 60, 180);
	tmpCakeID = createIcecream_Fade(550, 150, 40, "cakeAttackToPlayer_Line", 15, 3, 10, 2120);
	htMoveEnemyTo(tmpCakeID, 700, 400, 60, 180);
	
	tmpCakeID = createIcecream_Fade(350, 150, 40, "cakeAttackToPlayer_Line", 15, 3, 10, 2300);
	htMoveEnemyTo(tmpCakeID, 350, -50, 60, 180);
	htBindDieFuncToEnemy(tmpCakeID, "item_c");
	tmpCakeID = createIcecream_Fade(450, 150, 40, "cakeAttackToPlayer_Line", 15, 3, 10, 2300);
	htMoveEnemyTo(tmpCakeID, 450, -50, 60, 180);
	htBindDieFuncToEnemy(tmpCakeID, "item_h");
	
	
	for(i=0; i<6; ++i) {
		if(i % 2 == 0) {
			tmpCakeID = createCake(170, -10, 8, "cakeAttackCircle2", 45, 4, 3820+i*20);
			htMoveEnemyTo(tmpCakeID, 650, 630, 0, 300);
			htBindDieFuncToEnemy(tmpCakeID, "item_p");
	
			tmpCakeID = createCake(630, -10, 8, "cakeAttackCircle2", 45, 4, 3820+i*20);
			htMoveEnemyTo(tmpCakeID, 150, 630, 0, 300);
		} else {
			tmpCakeID = createCake(170, -10, 8, "cakeAttackToPlayer_WaterRed", 10, 24, 3820+i*20);
			htMoveEnemyTo(tmpCakeID, 650, 630, 0, 300);
	
			tmpCakeID = createCake(630, -10, 8, "cakeAttackToPlayer_WaterRed", 10, 24, 3820+i*20);
			htMoveEnemyTo(tmpCakeID, 150, 630, 0, 300);
			htBindDieFuncToEnemy(tmpCakeID, "item_p");
		}
	}
	
	
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(150, 100, 5, "cakeAttackCircle1_green", 20, 12, 4120+i*20);
		htSetEnemySpeed(tmpCakeID, 3, 0);
		
		tmpCakeID = createCake(650, 150, 5, "cakeAttackCircle1_green", 20, 12, 4120+i*20);
		htSetEnemySpeed(tmpCakeID, -3, 0);
	}
	
	tmpCakeID = createIcecream_Fade(300, 150, 60, "cakeAttackToPlayer_Line_De", 30, 3, 10, 4420);
	htMoveEnemyTo(tmpCakeID, 100, 150, 30, 120);
	tmpCakeID = createIcecream_Fade(500, 150, 60, "cakeAttackToPlayer_Line_De", 30, 3, 10, 4420);
	htMoveEnemyTo(tmpCakeID, 700, 150, 30, 120);
	tmpCakeID = createIcecream_Fade(400, 100, 100, "cakeAttackCircle2_Random", 10, 10, 10, 4480);
	htMoveEnemyTo(tmpCakeID, 400, -50, 90, 120);
	htBindDieFuncToEnemy(tmpCakeID, "item_b");
	
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake_fade(randomFloat(250, 350), randomFloat(40, 100), 5, "", 0, 0, 30, 4900+i*20);
		htSetEnemySpeed(tmpCakeID, 0.1, 2);
		htBindDieFuncToEnemy(tmpCakeID, "dieBullet1");
		
		tmpCakeID = createCake_fade(randomFloat(450, 550), randomFloat(40, 100), 5, "", 0, 0, 30, 4900+i*20);
		htSetEnemySpeed(tmpCakeID, -0.1, 2);
		htBindDieFuncToEnemy(tmpCakeID, "dieBullet2");
	}
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake_fade(randomFloat(250, 350), randomFloat(40, 100), 5, "", 0, 0, 30, 5200+i*20);
		htSetEnemySpeed(tmpCakeID, 0.1, 2);
		htBindDieFuncToEnemy(tmpCakeID, "dieBullet1");
		
		tmpCakeID = createCake_fade(randomFloat(450, 550), randomFloat(40, 100), 5, "", 0, 0, 30, 5200+i*20);
		htSetEnemySpeed(tmpCakeID, -0.1, 2);
		htBindDieFuncToEnemy(tmpCakeID, "dieBullet2");
	}
	
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(300, -50, 15, "cakeAttackToPlayerWithCircle", 10, 10, 5500+i*20);
		htSetEnemySpeed(tmpCakeID, 0.5, 2);
		htSetEnemyDeflection(tmpCakeID, -0.2, 120, 480);
		tmpCakeID = createCake(500, -50, 15, "cakeAttackToPlayerWithCircle", 10, 10, 5500+i*20);
		htSetEnemySpeed(tmpCakeID, -0.5, 2);
		htSetEnemyDeflection(tmpCakeID, 0.2, 120, 480);
	}
	
	for(i=0; i<6; ++i) {
		tmpCakeID = createCake(150, 100, 15, "cakeAttackToPlayerWithCircle", 10, 10, 5780+i*20);
		htSetEnemySpeed(tmpCakeID, 2, 0.5);
		htSetEnemyDeflection(tmpCakeID, -0.2, 120, 480);
		tmpCakeID = createCake(650, 150, 15, "cakeAttackToPlayerWithCircle", 10, 10, 5780+i*20);
		htSetEnemySpeed(tmpCakeID, -2, 0.5);
		htSetEnemyDeflection(tmpCakeID, 0.2, 120, 480);
	}
	
	bOnwayBoss = false;
	bKilledOnwayBoss = true;
	iEndCount = -1;
	
	htSetBackgroundMusic("魔法少女-出击!.mp3");
}