#include "hostAPI.txt"

#include "global.txt"

var count;

var i;
var bOnwayBoss;
var bKilledOnwayBoss;
var iEndCount;
var iSCCount;
var bLastSC;
var bZeta;

var bStageAVG;
//如果是道中boss,则加分
//如果是Zeta，则开始结束计时，显示记分板，GE
func onBossDie() {
	if(bOnwayBoss == false) {
		iEndCount = 0;
		
		htClearBackgroundCover();
	}
	else {
		bOnwayBoss = false;
		if(bKilledOnwayBoss == true) {
			bKilledOnwayBoss = false;
			htAddScore(10000000);
			
			var text;
			text = "Bonus: 10000000";
			htAddFloatingText(text, "DFGirl.ttf", 30, 1.0, 0.6, 0.0, 1.0, 400.0, 200.0, 400.0, 0.0, 90, 30, 30);
		}
	}
}

var tmpEnemyID;
var px;
var py;

var tpx;
var tpy;

#define MAX_ENEMY_COUNT 256;

var iEnemyIDs[MAX_ENEMY_COUNT];

func updateEndState() {	
	if(iEndCount != -1) {
		++iEndCount;
		if(iEndCount > 90) {
			htStageEnd();
			iEndCount = -1;
		}
	}
}


func onBossLossLife() {
	if(bOnwayBoss == true) {
		return;
	}
	
	++iSCCount;
	if(iSCCount == 5) {
		htPlayBossEnergyAnim(90);
		htShakeScreen(2, 60, 15);
		htSetSCBackground("zeta_bg_sc2.png", 450, 600, 1);
		htRunAVGScriptInSTG("scripts/stage3/daozhong3_2.script");
		htClearBullet();
	}
}

func onAVGEnd() {
	if(bStageAVG == true)
		htPlayEndTheme();
}


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

func onPlayerDie() {
	if(bZeta == true)
		htRunAVGScript("scripts/AVG/be.script");
}


func attackToPlayerWithCircle_20_36_rg(id, x, y, time) {
	for(i=0; i<20; ++i) {
		htShootBulletToPlayer(3, 5, x, y, 4+i*0.3);
	}
	
	for(i=0; i<36; ++i) {
		htInsertBulletVec(5, 2, x, y, 3, i*10);
	}
}

func cakeAttackToPlayer_3_3(id, x, y, time) {
	htShootBulletToPlayer(3, 3, x, y, 5);
}

func cakeAttackCircle1_8(id, x, y, time) {
	for(i=0; i<12; ++i) {
		htInsertBulletVec(3, 7, x, y, 3, i*30);
	}
}

func deflection_way_1(x, y, dgr) {
	for(i=0; i<36; ++i) {
		htInsertBulletVec(5, 6, x, y, 3, i*10);
	}
}

func deflectAttackToPlayer(id, x, y, time) {
	var dgr;

	dgr = GetDegreeBetweenTwoPoints(x, y, px, py);
	
	htInsertBulletVec(5, 5, x, y, 3, dgr);
	htBindScriptFuncToBullet("deflection_way_1", 30, 1);
	htSetBulletDeflectionEx(1, 0, 0, 30);
	htInsertBulletVec(5, 5, x, y, 3, dgr);
	htBindScriptFuncToBullet("deflection_way_1", 30, 1);
	htSetBulletDeflectionEx(-1, 0, 0, 30);
}

func deflection_24_1_r1(id, x, y, time) {
	for(i=0; i<24; ++i) {
		htInsertBulletVec(5, 13, x, y, 3, i*15);
		htSetBulletReflectTime(1, false);
	}
}

func deflection_24_2_r1(id, x, y, time) {
	for(i=0; i<24; ++i) {
		htInsertBulletVec(5, 14, x, y, 3, i*15);
		htSetBulletReflectTime(1, false);
	}
}

func circle_f(id, x, y, time) {
	htInsertBulletVec(5, 8, x+20*sin(time*10), y, 5, 90);
	htInsertBulletVec(5, 9, x+20*sin(time*10+180), y, 5, 90);
}

func circle_f_toplayer(id, x, y, time) {
	var dgr;
	dgr = GetDegreeBetweenTwoPoints(x, y, tpx, tpy);
	htInsertBulletVec(5, 5, x+20*sin(time*10), y, 5, dgr);
	htInsertBulletVec(5, 6, x+20*sin(time*10+180), y, 5, dgr);
}

func circle_f_fan(id, x, y, time) {
	if(time % 30 == 0) {
		for(i=0; i<36; ++i) {
			htInsertBulletVec(5, 2, x, y, 3, i*10);
		}
	}
	htInsertBulletVec(5, 0, x+20*sin(time*6), y, 3, 90);
	htInsertBulletVec(5, 1, x+20*sin(time*6+180), y, 3, 90);
}

func init() {
	count = 0;
	i = 0;
	iEndCount = -1;
	
	//是否道中boss
	bOnwayBoss = false;
	//是否杀掉了道中boss
	bKilledOnwayBoss = true;
	
	bZeta = false;

	htStopSeaSimulation();
	htSet2DBackground("sl.png", 3, 0);
	htSetBackgroundAlpha(0.7);
	htEnable2DFog1(true, 4);
	
	htSetBackgroundMusic("Rosemary.mp3");
	
	iEnemyIDs[0] = createIcecream(200, -50, 30, "attackToPlayerWithCircle_20_36_rg", 60, 1, 120);
	htMoveEnemyTo(iEnemyIDs[0], 270, 150, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[0], "item_p");	
	iEnemyIDs[1] = createIcecream(600, -50, 30, "attackToPlayerWithCircle_20_36_rg", 60, 1, 120);
	htMoveEnemyTo(iEnemyIDs[1], 530, 150, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[1], "item_b");	
	iEnemyIDs[2] = createIcecream(300, -50, 30, "attackToPlayerWithCircle_20_36_rg", 60, 1, 120);
	htMoveEnemyTo(iEnemyIDs[2], 350, 150, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[2], "item_c");	
	iEnemyIDs[3] = createIcecream(500, -50, 30, "attackToPlayerWithCircle_20_36_rg", 60, 1, 120);
	htMoveEnemyTo(iEnemyIDs[3], 450, 150, 0, 60);
	htBindDieFuncToEnemy(iEnemyIDs[3], "item_h");	

	for(i=0; i<10; ++i) {
		var rf;
		rf = randomFloat(200, 600);
		tmpEnemyID = createCake(rf, -50, 10, "cakeAttackToPlayer_3_3", 2, 30, 240+i*20);
		
		if(i % 2 == 0) {
			htMoveEnemyTo(tmpEnemyID, 680, 100+i*10, 0, 60);
		} else {
			htMoveEnemyTo(tmpEnemyID, 120, 100+i*10, 0, 60);
		}
	}
	
	iEnemyIDs[4] = createIcecream(200, -50, 30, "deflectAttackToPlayer", 30, 1, 480);
	htMoveEnemyTo(iEnemyIDs[4], 270, 150, 0, 60);
	iEnemyIDs[5] = createIcecream(600, -50, 30, "deflectAttackToPlayer", 45, 1, 480);
	htMoveEnemyTo(iEnemyIDs[5], 530, 150, 0, 60);
	iEnemyIDs[6] = createIcecream(300, -50, 30, "deflectAttackToPlayer", 45, 1, 480);
	htMoveEnemyTo(iEnemyIDs[6], 350, 150, 0, 60);
	iEnemyIDs[7] = createIcecream(500, -50, 30, "deflectAttackToPlayer", 30, 1, 480);
	htMoveEnemyTo(iEnemyIDs[7], 450, 150, 0, 60);
	
	for(i=0; i<10; ++i) {
		rf = randomFloat(200, 600);
		tmpEnemyID = createCake(rf, -50, 10, "cakeAttackToPlayer_3_3", 2, 30, 720+i*20);
		
		if(i % 2 == 0) {
			htMoveEnemyTo(tmpEnemyID, 680, 100+i*10, 0, 60);
		} else {
			htMoveEnemyTo(tmpEnemyID, 120, 100+i*10, 0, 60);
		}
	}
	for(i=0; i<10; ++i) {
		rf = randomFloat(200, 600);
		tmpEnemyID = createCake(rf, -50, 10, "cakeAttackToPlayer_3_3", 4, 15, 900+i*20);
		
		if(i % 2 == 0) {
			htMoveEnemyTo(tmpEnemyID, 680, 100+i*10, 0, 60);
		} else {
			htMoveEnemyTo(tmpEnemyID, 120, 100+i*10, 0, 60);
		}
	}
	for(i=0; i<10; ++i) {
		rf = randomFloat(200, 600);
		tmpEnemyID = createCake(rf, -50, 10, "cakeAttackCircle1_8", 4, 15, 840+i*20);
		
		if(i % 2 == 0) {
			htMoveEnemyTo(tmpEnemyID, rf, 650, 0, 180);
		} else {
			htMoveEnemyTo(tmpEnemyID, rf, 650, 0, 180);
		}
	}
	
	tmpEnemyID = createIcecream_Fade(200, 100, 30, "deflection_24_1_r1", 30, 1, 30, 1080);
	htMoveEnemyTo(tmpEnemyID, 200, -50, 60, 60);
	htBindDieFuncToEnemy(tmpEnemyID, "item_p");	
	tmpEnemyID = createIcecream_Fade(600, 150, 30, "deflection_24_2_r1", 45, 1, 30, 1080);
	htMoveEnemyTo(tmpEnemyID, 600, -50, 60, 60);
	htBindDieFuncToEnemy(tmpEnemyID, "item_b");	
	tmpEnemyID = createIcecream_Fade(300, 150, 30, "deflection_24_2_r1", 45, 1, 30, 1080);
	htMoveEnemyTo(tmpEnemyID, 300, -50, 60, 60);
	htBindDieFuncToEnemy(tmpEnemyID, "item_c");	
	tmpEnemyID = createIcecream_Fade(500, 100, 30, "deflection_24_1_r1", 30, 1, 30, 1080);
	htMoveEnemyTo(tmpEnemyID, 500, -50, 60, 60);
	htBindDieFuncToEnemy(tmpEnemyID, "item_h");	
	
	for(i=0; i<10; ++i) {
		tmpEnemyID = createCake(150, 100, 8, "cakeAttackToPlayer_3_3", 2, 30, 1380+i*20);
		htMoveEnemyTo(tmpEnemyID, 700, 100+i*10, 0, 180);
		tmpEnemyID = createCake(650, 150, 8, "cakeAttackCircle1_8", 10, 6, 1380+i*20);
		htMoveEnemyTo(tmpEnemyID, 120, 200-i*10, 0, 180);
	}
	for(i=0; i<10; ++i) {
		tmpEnemyID = createCake(650, 100, 8, "cakeAttackToPlayer_3_3", 2, 30, 1680+i*20);
		htMoveEnemyTo(tmpEnemyID, 120, 100+i*10, 0, 180);
		tmpEnemyID = createCake(150, 150, 8, "cakeAttackCircle1_8", 10, 6, 1680+i*20);
		htMoveEnemyTo(tmpEnemyID, 700, 200-i*10, 0, 180);
	}
	for(i=0; i<10; ++i) {
		tmpEnemyID = createCake(150, 100, 8, "cakeAttackToPlayer_3_3", 2, 30, 1980+i*20);
		htMoveEnemyTo(tmpEnemyID, 700, 100+i*10, 0, 180);
		tmpEnemyID = createCake(650, 150, 8, "cakeAttackCircle1_8", 10, 6, 1980+i*20);
		htMoveEnemyTo(tmpEnemyID, 120, 200-i*10, 0, 180);
	}
	for(i=0; i<10; ++i) {
		tmpEnemyID = createCake(650, 100, 8, "cakeAttackToPlayer_3_3", 2, 30, 2280+i*20);
		htMoveEnemyTo(tmpEnemyID, 120, 100+i*10, 0, 180);
		tmpEnemyID = createCake(150, 150, 8, "cakeAttackCircle1_8", 10, 6, 2280+i*20);
		htMoveEnemyTo(tmpEnemyID, 700, 200-i*10, 0, 180);
	}
	for(i=0; i<10; ++i) {
		tmpEnemyID = createCake(650, 100, 8, "cakeAttackToPlayer_3_3", 2, 30, 2580+i*20);
		htMoveEnemyTo(tmpEnemyID, 120, 100+i*10, 0, 180);
		tmpEnemyID = createCake(150, 150, 8, "cakeAttackCircle1_8", 10, 6, 2580+i*20);
		htMoveEnemyTo(tmpEnemyID, 700, 200-i*10, 0, 180);
	}
}

func createEnemy2() {
	//直线向下螺旋
	tmpEnemyID = createIcecream_Fade(200, 100, 30, "circle_f", 2, 90, 30, 60);
	htMoveEnemyTo(tmpEnemyID, 200, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(600, 100, 30, "circle_f", 2, 90, 30, 60);
	htMoveEnemyTo(tmpEnemyID, 600, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(300, 150, 30, "circle_f", 2, 90, 30, 60);
	htMoveEnemyTo(tmpEnemyID, 300, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(500, 150, 30, "circle_f", 2, 90, 30, 60);
	htMoveEnemyTo(tmpEnemyID, 500, -50, 180, 60);
	
	//直线向下螺旋+圆环
	tmpEnemyID = createIcecream_Fade(250, 150, 30, "circle_f_fan", 2, 90, 30, 210);
	htMoveEnemyTo(tmpEnemyID, 250, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(550, 150, 30, "circle_f_fan", 2, 90, 30, 210);
	htMoveEnemyTo(tmpEnemyID, 550, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(350, 100, 30, "circle_f_fan", 2, 90, 30, 210);
	htMoveEnemyTo(tmpEnemyID, 350, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(450, 100, 30, "circle_f_fan", 2, 90, 30, 210);
	htMoveEnemyTo(tmpEnemyID, 450, -50, 180, 60);
	
	//直线自机狙螺旋
	tmpEnemyID = createIcecream_Fade(200, 100, 30, "circle_f_toplayer", 2, 90, 30, 400);
	htMoveEnemyTo(tmpEnemyID, 200, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(600, 100, 30, "circle_f_toplayer", 2, 90, 30, 400);
	htMoveEnemyTo(tmpEnemyID, 600, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(300, 150, 30, "circle_f_toplayer", 2, 90, 30, 400);
	htMoveEnemyTo(tmpEnemyID, 300, -50, 180, 60);
	tmpEnemyID = createIcecream_Fade(500, 150, 30, "circle_f_toplayer", 2, 90, 30, 400);
	htMoveEnemyTo(tmpEnemyID, 500, -50, 180, 60);
	
	tmpEnemyID = createIcecream_Fade(200, 100, 30, "deflection_24_1_r1", 30, 1, 30, 640);
	htMoveEnemyTo(tmpEnemyID, 200, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(600, 150, 30, "deflection_24_2_r1", 30, 1, 30, 640);
	htMoveEnemyTo(tmpEnemyID, 600, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(300, 150, 30, "deflection_24_2_r1", 30, 1, 30, 640);
	htMoveEnemyTo(tmpEnemyID, 300, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(500, 100, 30, "deflection_24_1_r1", 30, 1, 30, 640);
	htMoveEnemyTo(tmpEnemyID, 500, -50, 60, 60);
	
	tmpEnemyID = createIcecream_Fade(250, 100, 30, "deflection_24_1_r1", 30, 1, 30, 700);
	htMoveEnemyTo(tmpEnemyID, 250, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(550, 150, 30, "deflection_24_2_r1", 30, 1, 30, 700);
	htMoveEnemyTo(tmpEnemyID, 550, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(350, 150, 30, "deflection_24_2_r1", 30, 1, 30, 700);
	htMoveEnemyTo(tmpEnemyID, 350, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(450, 100, 30, "deflection_24_1_r1", 30, 1, 30, 700);
	htMoveEnemyTo(tmpEnemyID, 450, -50, 60, 60);
	
	tmpEnemyID = createIcecream_Fade(220, 100, 30, "deflection_24_1_r1", 30, 1, 30, 760);
	htMoveEnemyTo(tmpEnemyID, 220, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(340, 150, 30, "deflection_24_2_r1", 30, 1, 30, 760);
	htMoveEnemyTo(tmpEnemyID, 340, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(460, 150, 30, "deflection_24_2_r1", 30, 1, 30, 760);
	htMoveEnemyTo(tmpEnemyID, 460, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(580, 100, 30, "deflection_24_1_r1", 30, 1, 30, 760);
	htMoveEnemyTo(tmpEnemyID, 580, -50, 60, 60);
	
	tmpEnemyID = createIcecream_Fade(240, 100, 30, "deflection_24_1_r1", 30, 1, 30, 820);
	htMoveEnemyTo(tmpEnemyID, 240, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(320, 150, 30, "deflection_24_2_r1", 30, 1, 30, 820);
	htMoveEnemyTo(tmpEnemyID, 320, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(480, 150, 30, "deflection_24_2_r1", 30, 1, 30, 820);
	htMoveEnemyTo(tmpEnemyID, 480, -50, 60, 60);
	tmpEnemyID = createIcecream_Fade(560, 100, 30, "deflection_24_1_r1", 30, 1, 30, 820);
	htMoveEnemyTo(tmpEnemyID, 560, -50, 60, 60);
}	

func createBossZeta() {
	htCreateBoss("Zeta", 10, "zeta.png", 0, 0, 128, 128, 1);
	htSetBossPos(400, 100);
	htSetBossMovementRect(384, 0, 128, 128, 2, 512, 0, 128, 128, 2);

	htSetBossDieAnim(640, 0, 128, 128, 1);
	htAddBossAnim(0, 128, 0, 128, 128, 4);
	htAddBossAnim(1, 256, 0, 128, 128, 3);
	htAddBossAnim(2, 768, 0, 128, 128, 2);
	htAddBossAnim(3, 896, 0, 128, 128, 2);
	
	htSetBossMagicCircleColor(0.0, 0.0, 0.5, 1.0);
	
	htAddBossAttackScript("scripts/stage3/zeta/zeta-1-hard.txt", 1000, 1000, 2000000, true);
	htAddBossAttackScript("scripts/stage3/zeta/zeta-2-hard.txt", 1000, 1000, 2200000, true);
	htAddBossAttackScript("scripts/stage3/zeta/zeta-3-hard.txt", 1000, 1000, 2400000, true);
	htAddBossAttackScript("scripts/stage3/zeta/zeta-4-hard.txt", 1000, 1000, 2600000, true);
	htAddBossAttackScript("scripts/stage3/zeta/zeta-5-hard.txt", 1500, 1000, 2800000, true);
	htAddBossAttackScript("scripts/stage3/zeta/zeta-6-hard.txt", 2000, 1000, 20000000, true);

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

func createWayBoss() {
	htCreateBoss("Icecream", 10, "icecream.png", 64, 64, 64, 64, 1);
	htSetBossPos(400, 200);
	htSetBossMovementRect(64, 0, 64, 64, 1, 0, 0, 64, 64, 1);
	
	htSetBossDieAnim(0, 64, 64, 64, 1);
	
	htAddBossAttackScript("scripts/stage3/icecream1_hard.txt", 500, 1000, 1000000, false);
	htAddBossAttackScript("scripts/stage3/Icecream2_hard.txt", 600, 1000, 2000000, true);
	
	htSetBossNameBoxColor(1.0, 0.6, 0.0, 1.0);
}

func update() {
	if(bOnwayBoss == false)
		++count;
	
	px = htGetPlayerX();
	py = htGetPlayerY();
	
	if(count == 100)
		htCreatePlayer();
		
	//小冰冰
	if(count == 2820) {
		createWayBoss();
		htSetBackgroundSpeed(4);
		htSetFogSpeed(5);
	}
	if(count == 2880) {
		htBossStart();
		bOnwayBoss = true;
		++count;
	}
	
	if(count == 3020) {
		createEnemy2();
	}
	else if(count == 3450) {
		tpx = px;
		tpy = py;
	}
	
	//触手Zeta！！
	if(count == 4160) {
		htDisplayBossTitle("zeta-title.png", 60);	
		htSetBackgroundSpeed(5);
		htSetFogSpeed(7);
	}
	else if(count == 4280) {
		createBossZeta();
	}
	else if(count == 4310) {
		htRunAVGScriptInSTG("scripts/stage3/daozhong3.script");
	}
	else if(count == 4340) {
		htSetBackgroundMusic("天魔降伏.mp3");
		htPlayBossEnergyAnim(90);
		htBossStart();
		
		bStageAVG = true;
		bZeta = true;
		htEnable2DFog1(false, 0);
		htSetSCBackground("zeta_bg_sc1.jpg", 450, 600, 1);
		htAddBackgroundCover("zeta_bg_cover_2.png", 0.01, 412, 260, 30, BACKGROUND_TYPE_TURN);
		htAddBackgroundCover("zeta_bg_cover_3.png", 0.0, 412, 260, 30, BACKGROUND_TYPE_TURN);
	}
		
	if(iEndCount != -1)		
		updateEndState();
}
