#include "Hspdx.as"

; StarShipII 『 Distance ver0.37 』

	randomize

; ■■■■■ 処理速度決定
*first
	sndload "start.wav"   , 0,0
	sndload "start.mid"   ,10,0
	sndload "title.mid"   ,11,1
	sndload "boss.mid"    ,12,1
	screen 0,150,200,1,300,200 : cls 4 : title "Distance ver0.37" : gsel 0,1
	pos 0,0 : objsize 100,25 : gamewait=0 : gamebgm=1
	font "MSゴシック",15
	pos 15, 20 : color 255,0,0 : mes "Distance ver0.37"
	pos 25, 50 : color 0,255,0 : mes "ゲーム速度"
	pos 25, 70 : combox gamewait,150,"NORMAL\n+1\n+2\nLIMITED"
	pos 25,100 : color 0,255,0 : mes "MIDI音楽"
	pos 25,120 : combox gamebgm ,150,"鳴らさない\n鳴らす"
	pos 25,155 : button "Game Start",*dxchk
stop


; ■■■■■ DirectX 初期化&チェック
*dxchk
	if gamewait=0 : gamewait=30
	if gamewait=1 : gamewait=25
	if gamewait=2 : gamewait=20
	if gamewait=3 : gamewait=15

	es_ini : es_screen 640,480,8
	if stat=1 : goto *dxerr1
	if stat=2 : goto *dxerr2
goto *start
;goto *before

*dxerr1
	dialog "DirectXの初期化に失敗しました。",1    : end
*dxerr2
	dialog "スクリーンの初期化に失敗しました。",1 : end
*dxerr3
	es_bye : wait 100
	dialog "VRAMの容量が不足しています。",1       : end


; ■■■■■ 初期設定
*start
	buffer 2,640,480,1 : picload "title.bmp",1 : es_buffer 0,0 : if stat=1 : goto *dxerr3
	titlepos=120 : titleposv=1

	es_size 10,10,0,0 : smax=200
	repeat 4
		sgx=cnt*10+360 : es_pat cnt,sgx,110
	loop
	repeat smax
		rnd stx,630 : rnd sty,470 : rnd stc,4 : sts=4-stc*200
		es_set cnt,stx,sty,stc : es_adir cnt,8,sts
	loop

	gsel 0 : es_cls : es_sync : wait 100
goto *gametitle


; ■■■■■ タイトルメイン ■■■■■
*gametitle
	fadelevel=-256 : if gamebgm=1 : snd 10
	repeat 288
		es_cls 4 : stick pat
		getkey gameend,123 : if gameend=1 : es_sync : sndoff : es_bye : end
		gmode 1,200,40 : font "Times New Roman Bold Italic",18 : color 0,255,255
		pos 0, 50 : es_copy 0,440,110 : pos 205, 60 : es_fmes "http://www.at-m.or.jp/~onitama/"
		pos 0,100 : es_copy 0,440,150 : pos 205,110 : es_fmes "http://www.doga.co.jp/ptdoga/"
		pos 0,150 : es_copy 0,440,190 : pos 205,160 : es_fmes "http://www.geocities.co.jp/Playtown-Spade/9909/"
		gmode 1,100,80
		pos 0,200 : es_copy 0,340,123 : pos 105,230 : es_fmes "http://www.tksys.co.jp/bgm/"
		gmode 1,300,65
		pos 0,290 : es_copy 0,340,230 : pos 305,315 : es_fmes "http://www.yamaha.co.jp/xg/pds/pds.html"
		if cnt<128 : fadelevel=fadelevel+2
		if cnt>160 : fadelevel=fadelevel-2
		es_palfade fadelevel
		es_sync gamewait : if frame\10=0 : fps=1000/stat
		await 0 : if pat&64>0 : break
	loop
	color 0,0,0 : es_boxf 0,0,640,480 : es_sync : es_palfade 0 : wait 50 : if gamebgm=1 : sndoff : wait 10 : snd 11

*titlemain
	es_cls 4 : stick pat
	if (pat&64>0)&(menuposition=0) : es_clear : goto *before
	getkey gameend,123 : if gameend=1 : es_sync : sndoff : es_bye : end

	gosub *titlestardust

	es_draw

	gosub *titleview
	gosub *titlemenu

	es_sync gamewait : if frame\10=0 : fps=1000/stat
	await 0 : frame++
goto *titlemain


; ■■■■■ タイトル星屑
*titlestardust
	repeat smax
		es_get stx,cnt,3 : es_get sty,cnt,5
		if stx>640 : es_pos cnt,-10,sty
		if sty>480 : es_pos cnt,stx,-10
	loop
return


; ■■■■■ タイトルビュー
*titleview
	pos 590,460      : color 255,255,255 : font "MSPゴシック",15,1 : es_fmes ""+fps+" FPS"
	pos   0,       0 : gmode 1,640,110 : es_copy 0,0,0
	pos 150,titlepos : gmode 1,340,226 : es_copy 0,0,110
	font "MSPゴシック",15,1
	dscore=highscore : str dscore,9
	pos   0,  0 : color 192,192,192 : es_fmes "HIGHSCORE  "+dscore
	font "DFP特太ゴシック体",15
	pos 250,350 : color 128,128,0   : es_fmes "GAME START"
	pos 250,380 : color 128,128,0   : es_fmes "SHOW HIGHSCORE"
	pos 250,410 : color 128,128,0   : es_fmes "TRACE MODE"
	pos 250,440 : color 128,128,0   : es_fmes "CONFIG"
	if titlepos>160 : titleposv=-1
	if titlepos<120 : titleposv= 1
	if frame\2=0 : titlepos=titlepos+titleposv
return


; ■■■■■ タイトルメニュー
*titlemenu
;	if pat&8>0 : menuposition++
;	if menuposition>3 : menuposition=0
;	if pat&2>0 : menuposition--
;	if menuposition<0 : menuposition=3
	font "DFP特太ゴシック体",15 : gmode 1,14,13
	if menuposition=0 : pos 248,348 : color 255,255,0 : es_fmes "GAME START"		: pos 220,350 : es_copy 0,340,110
	if menuposition=1 : pos 248,378 : color 255,255,0 : es_fmes "SHOW HIGHSCORE"	: pos 220,380 : es_copy 0,340,110
	if menuposition=2 : pos 248,408 : color 255,255,0 : es_fmes "TRACE MODE"		: pos 220,410 : es_copy 0,340,110
	if menuposition=3 : pos 248,438 : color 255,255,0 : es_fmes "CONFIG"			: pos 220,440 : es_copy 0,340,110
return


; ■■■■■ ゲーム前初期設定 ■■■■■
*before
	if gamebgm=1 : sndoff : wait 10 : snd 0
	buffer 2,640,480,1 : picload "myship.bmp",1 : es_buffer 0,0 : if stat=1 : goto *dxerr3

	smax=50 : dim stx,smax : dim sty,smax : dim sts,smax
	repeat smax : rnd stx.cnt,630 : rnd sty.cnt,470 : rnd sts.cnt,5 : loop

	es_size 50,50,10,0
	es_pat  0,100,0
	es_pat  1, 50,0
	es_pat  2,  0,0
	es_pat  3,150,0
	es_pat  4,200,0		; 0-4 自機

	es_size 20,20,80,0
	repeat 5 : es_pat 5+cnt,20*cnt+260,20 : loop
						; 5-9 自機弾

	es_size 20,20,50,0
	es_pat 16,260,0,4
	es_pat 17,280,0,4
	es_link 17,16		; 16-17 敵通常弾

	es_size 20,20,50,0
	repeat 32 : es_pat 18+cnt,20*cnt,50 : loop
	repeat 32 : es_pat 50+cnt,20*cnt,70 : loop
						; 18-81 敵64方向レーザー

	repeat 5  : es_pat 82+cnt,20*cnt+300,0,2 : loop
						; 82-86 敵弾爆風

	es_window 0,0,640,480 : es_area -50,-50,690,530

	mx=295 : my=215 : move=3 : dir=4 : ship=300		; ■■■■■ 自機のストライプ No.300 : Type 1
	es_set ship,mx,my,2 : es_type ship,1

	es_size 50,50,80,0
	es_pat 100,0,90

	es_set 400, 50, 50,100 : es_type 400,4
	es_set 401,200,100,100 : es_type 401,4
	es_set 402,390,100,100 : es_type 402,4
	es_set 403,540, 50,100 : es_type 403,4

	frame=0 : gsel 0 : es_cls : es_sync : wait 100 : if gamebgm=1 : snd 12
goto *main


; ■■■■■ メイン ■■■■■
*main
	es_cls 4
	getkey gameend,123 : if gameend=1 : es_sync : sndoff : es_bye : end

	gosub *stardust					; ●星屑描写

	gosub *myshipmove				; ○自機移動処理
	if shot=1 : gosub *shotmove		; ○自機弾発射処理
	gosub *clash					; ○自機  対敵弾の当り判定
	if frame\2=0 : gosub *tekitama	; ○敵弾処理
	if frame\100=5 : gosub *tama64	; ○敵弾64方向処理
	es_draw
	gosub *information				; ●点数、得点倍率、シールドなどゲーム情報描写
	es_sync gamewait : if frame\10=0 : fps=1000/stat
	await 0 : frame++
	if pat&128>0 : gosub *pause
goto *main


; ■■■■■ PAUSE
*pause
	pausetime=0
	repeat
		getkey gameend,123 : if gameend=1 : es_bye : end
		stick pat : if (pausetime>10)&(pat&128>0) : break
		pausetime++
		await 16
	loop
return


; ■■■■■ ゲーム情報描写
*information
	pos 590,460 : color 192,192,192 : font "MSPゴシック",15,1 : es_fmes ""+fps+" FPS"
return


; ■■■■■ 星屑スクロール表示
*stardust
	repeat smax
		gmode 1,10,10 : pos stx.cnt,sty.cnt : es_copy 0,sts.cnt*10+260,40
		sty.cnt=sty.cnt+15-sts.cnt : if sty.cnt>480 : sty.cnt=0
	loop
return

; ■■■■■ 自機移動と表示
*myshipmove
	stick pat,79
	if (frame\4=0)6(pat&64>0) : shot=1 : else : shot=0
	if (pat&16>0) : move=move+6
	if move>15 : move=3
	mxv=(pat>>2&1)-(pat&1)
	myv=(pat>>3&1)-(pat>>1&1)
	mx=mxv*move+mx : my=myv*move+my
	if mx<-10 : mx=-10
	if mx>600 : mx=600
	if my<20  : my=20
	if my>430 : my=430
	if mxv<0 : dir-- : if dir<0 : dir=0
	if mxv>0 : dir++ : if dir>8 : dir=8
	if mxv=0 : dir=4
	es_chr ship,dir/2 : es_pos ship,mx,my
return


; ■■■■■ 自機対敵弾衝突判定
*clash
	repeat 300
		es_find temp,8,cnt : if temp=-1 : continue
		es_check chk,temp,1 : es_get dx,temp,3 : es_get dy,temp,5
		if chk>=0 : es_kill temp : es_set temp,dx,dy,82 : es_type temp,16 : es_flag temp,$800a : gosub *allclash
	loop
return

*allclash
	repeat 300
		es_find temp,8,cnt : if temp=-1 : continue
		es_get dx,temp,3 : es_get dy,temp,5
		es_kill temp : es_set temp,dx,dy,82 : es_type temp,16 : es_flag temp,$800a
	loop
return

; ■■■■■ 自機弾移動と表示	スプライト No.301〜318 : Type 2
*shotmove
	shlf=20-move
	es_new shno,301
	if shno<304 : es_set shno,mx+5 ,my+10,7 : es_type shno,2 : es_adir shno,32,3000 : es_flag shno,$8300+shlf
	es_new shno,304
	if shno<307 : es_set shno,mx+25,my+10,7 : es_type shno,2 : es_adir shno,32,3000 : es_flag shno,$8300+shlf
		if move>9 : return
	es_new shno,307
	if shno<310 : es_set shno,mx+10,my+10,6 : es_type shno,2 : es_adir shno,36-mxv,3000 : es_flag shno,$8300+shlf
	es_new shno,310
	if shno<313 : es_set shno,mx+20,my+10,8 : es_type shno,2 : es_adir shno,28-mxv,3000 : es_flag shno,$8300+shlf
		if move>3 : return
	es_new shno,313
	if shno<316 : es_set shno,mx+15,my+5,5 : es_type shno,2 : es_adir shno,40-mxv,3000 : es_flag shno,$8300+shlf
	es_new shno,316
	if shno<319 : es_set shno,mx+15,my+5,9 : es_type shno,2 : es_adir shno,24-mxv,3000 : es_flag shno,$8300+shlf
return


; ■■■■■ 敵弾発生			スプライト NO.  0〜299 :Type 8
*tekitama
	rnd enemy,4 : enemy=enemy+400
	es_get ex,enemy,3 : es_get ey,enemy,5
	es_new tama,0 : if tama>299 : return
	rnd ts,4 : ts=ts+2*100 : es_set tama,ex+15,ey+15,16 : es_type tama,8 : es_aim tama,mx+15,my+15,ts
return

*tama64
	rnd enemy,4 : enemy=enemy+400
	es_get ex,enemy,3 : es_get ey,enemy,5 : dx=ex+15 : dy=ey+15
	repeat 64
		es_new tama,0 : if tama>299 : break
		es_set tama,dx,dy,18+cnt : es_type tama,8 : es_adir tama,cnt,300
	loop
return