/*
OO
*/
#const double GRAVITY       1
#const double FRICTION      0.6
#const double MAX_SPEED     10.0
#const double ACCELERATION  0.8
#const KABE_LEFT        0
#const KABE_RIGHT       608
#const YUKA             400
#const JUMP_SPEED       -8
#const BULLET_MAX 1   // e̍ő吔
#const soala_chargeTime 5  // xˏoĂ玟Ɏˏo܂ł̍Œmێ
#const soala_BULLET_MAX 1    // e̍ő吔
title "SCx[_["

*init //  ϐ̏
	randomize				;
	enemy = 1
	dim enemy_x,enemy : dim enemy_y,enemy	;G̏ʒu̕ϐ
	dim enemy_dx,enemy : dim enemy_dy,enemy	;G̈ړ
	;G̏ʒuAړʂ𗐐Ō
	repeat enemy
		enemy_x =cnt,KABE_RIGHT-32
		enemy_y =cnt,YUKA/2
		kari = rnd (10) : enemy_dx(cnt) = kari-5
		kari = rnd (10) : enemy_dy(cnt) = kari-5
	loop

    dim bulletFlag, BULLET_MAX
	dim soala_bulletFlag, soala_BULLET_MAX


	buffer 1,96,64
	picload "hikoza.bmp"
	buffer 2,64,64
	picload "fire.bmp"
	buffer 3,96,96
	picload "soala.bmp"
	screen 0,640,480
	gmode 2
	a = 1 : b = 1
	fire_a = 0
	damage = 50
	soala_damage = 100
	player_x = double( KABE_LEFT + KABE_RIGHT ) / 2.0   // vC[XW
	player_y = double( YUKA )                     // vC[YW
	player_sx = 0.0
	player_sy = 0.0

//  C[v
*main
	redraw 0
	stick keys, 1 + 4
	gosub *shot //eo
	gosub *soala_shot
	gosub *move_fire//ëړ
	gosub *move_soalabeam
	gosub *move_hiko //qRUړ
	gosub *enemy_move  //Gړ
	gosub *kurai_hentai //H炢
	gosub *hiko_draw //qRU`
	gosub *tama_draw //e`
	gosub *drawsoalabeam
	gosub *enemy_draw //G`
	pos 60,440 : color 0,0,0 :font "lr@SVbN" ,20 : mes "HP"+damage
	pos 60,460 : color 0,0,0 :font "lr@SVbN" ,20 : mes "GHP"+soala_damage
	if damage <= 0 {
	cls
	pos 100,100
	color 255,0,0
	font "lr SVbN" ,100
	mes "game over"
	pos 300,200
	button goto "蒼", *init
	stop
	}
	if soala_damage <= 0 {
	cls
	pos 20,100
	color 255,0,0
	font "lr SVbN" ,100
	mes "߂łƂI"
	pos 300,200
	button goto "蒼", *init
	stop
	}
	redraw 1
	await 16
	goto *main

*shot
    getkey pushed, 'W'  // WL[Ŕ
    if pushed {
		if bulletFlag(0) : return
		fire_a = 1
        gosub *makeBullet
    }
    
    getkey pushed, 'D'  // DL[Ŕ
    if pushed {
		if bulletFlag(0) : return
	    fire_a = 2
        gosub *makeBullet
    }   
    
    getkey pushed, 'A'  // AL[Ŕ
    if pushed {
		if bulletFlag(0) : return
	    fire_a = 3
        gosub *makeBullet
    }

    getkey pushed, 'S'  // SL[Ŕ
    if pushed {
	    player_y = player_y-15
		if bulletFlag(0) : return
	    fire_a = 4
        gosub *makeBullet
    }
    
    return
    
*soala_shot
    if soala_charge > 0 : soala_charge-- : return
        soala_charge = soala_chargeTime
        gosub *soala_makeBullet
    return
 

*makeBullet
	repeat BULLET_MAX
		bulletFlag(cnt) = 1
		if fire_a = 1 {  //
		bulletFlag(cnt) = 1
        bulletX(cnt)  = player_x
        bulletY(cnt)  = player_y
		bulletVX(cnt) = 0
        bulletVY(cnt) = -30
		}
		if fire_a = 2 {  //E
		bulletX(cnt)  = player_x
        bulletY(cnt)  = player_y-16
		bulletVX(cnt) = 30
        bulletVY(cnt) = 0
		}
		if fire_a = 3 {  //
        bulletX(cnt)  = player_x
        bulletY(cnt)  = player_y-16
		bulletVX(cnt) = -30
        bulletVY(cnt) = 0
        }
		if fire_a = 4 {  //
        bulletX(cnt)  = player_x
        bulletY(cnt)  = player_y-16
		bulletVX(cnt) = 0
		bulletVY(cnt) = 30
		}
		break
	loop
	return

 
*soala_makeBullet
    repeat soala_BULLET_MAX
        if soala_bulletFlag(cnt) : continue      // eˏoς
        soala_bulletFlag(cnt) = 1
        soala_bulletX(cnt)  = enemy_x + 32
        soala_bulletY(cnt)  = enemy_y + 32
        soala_bulletVX(cnt) = (player_x - enemy_x)/10
        soala_bulletVY(cnt) = (player_y - enemy_y)/10
        break
    loop
    return
    
*move_fire
    repeat BULLET_MAX
        if bulletFlag(cnt) == 0 : continue
        bulletX(cnt) += bulletVX
        bulletY(cnt) += bulletVY
        if (bulletX(cnt) < 0)|(ginfo_winx < bulletX(cnt))|(bulletY(cnt) < 0)|(ginfo_winy < bulletY(cnt)) {
            bulletFlag(cnt) = 0
        }
        if (enemy_x+(96*3/4) >bulletX(cnt))&(bulletX(cnt)+(32*3/4) > enemy_x){
        if(enemy_y+(96*3/4) >bulletY(cnt))&(bulletY(cnt)+(32*3/4) > enemy_y) {
            bulletFlag(cnt) = 0
            soala_damage = soala_damage -1
        }}
    loop
    return

*move_soalabeam
    repeat soala_BULLET_MAX
        if soala_bulletFlag(cnt) == 0 : continue
        soala_bulletX(cnt) += soala_bulletVX
        soala_bulletY(cnt) += soala_bulletVY
        if (soala_bulletX(cnt) < 0)|(ginfo_winx < soala_bulletX(cnt))|(soala_bulletY(cnt) < 0)|(ginfo_winy < soala_bulletY(cnt)) {
            soala_bulletFlag(cnt) = 0
        }
        if (player_x+(32*3/4) >soala_bulletX(cnt))&(soala_bulletX(cnt)+(32*3/4) > player_x){
        if(player_y+(32*3/4) >soala_bulletY(cnt))&(soala_bulletY(cnt)+(32*3/4) > player_y) {
            soala_bulletFlag(cnt) = 0
            damage = damage-1 
        }}
    loop
    return
    
*move_hiko
	// L[͂ɏ]X̑x𑝌
	if ( keys & 1 ) : player_sx -= ACCELERATION : a + 1 : b = 0
	if a > 9 : a = 1
	if ( keys & 4 ) : player_sx += ACCELERATION : b + 1 : a = 0
	if b > 9 : b = 1
	player_sx = limitf( player_sx, -MAX_SPEED, MAX_SPEED )

	// n
	if ( player_y >= YUKA ) {
		// nĂꍇ
		on_ground = 1
	} else {
		// 󒆂ɂꍇcd͂ɂ藎
		on_ground = 0
		player_sy += GRAVITY		
	}

	// n̂݁AƂ̖Cɂ錸
	if ( on_ground == 1 ) {
		if ( player_sx > 0.0 ) {
			player_sx -= limitf(  player_sx, 0.0, FRICTION )
		} else : if ( player_sx < 0.0 ) {
			player_sx += limitf( -player_sx, 0.0, FRICTION )
		}
	}

	// n̂݁AƃWv
	if ( keys >> 1 & 1 ) { // "( on_ground == 1 ) & "΁A󒆂łWv\
		player_sy = double( JUMP_SPEED )
	}

	// vC[̈ړ
	player_x += player_sx
	player_y += player_sy

	// ǁEƂ̏Փ˔
	if ( player_x < KABE_LEFT ) {
		// ̕ǂƏՓ
		player_x = - player_x + KABE_LEFT * 2
		player_sx = -player_sx
	} else : if ( player_x > KABE_RIGHT ) {
		// E̕ǂƏՓ
		player_x = - player_x + KABE_RIGHT * 2
		player_sx = -player_sx
	}
	// Ƃ̏Փ˔
	if ( player_y > YUKA ) {
		player_y = double( YUKA )
		player_sy = 0.0
	}
	//V
	if (player_y < 0) {
		player_y = - player_y
	}
	return

*enemy_move
	repeat enemy(cnt)
		enemy_x(cnt)+=enemy_dx(cnt)
		enemy_y(cnt)+=enemy_dy(cnt)
		if (enemy_x(cnt) < 0)|(enemy_x(cnt) > (640-96)) : enemy_dx(cnt)=-enemy_dx(cnt)
		if (enemy_y(cnt) < 0)|(enemy_y(cnt) > (432-96)) : enemy_dy(cnt)=-enemy_dy(cnt)
	loop
	return

*kurai_hentai
	repeat enemy(cnt)
		if (player_x+(32*3/4) >enemy_x(cnt))&(enemy_x(cnt)+(96*3/4) > player_x) {
		if (player_y+(32*3/4) >enemy_y(cnt))&(enemy_y(cnt)+(96*3/4) > player_y) {
			damage = damage - 1
		}}
	loop
	return

//  e`
*hiko_draw
	// ʂ𔒂hԂ
	color 255, 255, 255
	boxf
	color 192,192,192
	boxf 0,432,ginfo_winx,ginfo_winy
	
	// vC[`悷
	pos player_x,player_y
	if 5 < a & a < 10 : gcopy 1,32,32,32,32
	if 5 < b & a < 10 : gcopy 1,32,0,32,32
	if 6 > a & a > 0 : gcopy 1,0,32,32,32
	if 6 > b & b > 0 : gcopy 1,0,0,32,32
	return
*tama_draw
	repeat BULLET_MAX
    if bulletFlag(cnt) == 0 : continue
    pos bulletX(cnt),bulletY(cnt)+16
	if fire_a = 1 : gcopy 2,0,32,32,32
	if fire_a = 2 : gcopy 2,0,0,32,32
	if fire_a = 3 : gcopy 2,32,0,32,32
	if fire_a = 4 : gcopy 2,32,32,32,32
	if fire_a = 5 : gcopy 2,64,0,32,32
	if fire_a = 6 : gcopy 2,64,32,32,32
	loop
    return
*enemy_draw
	repeat enemy
		pos enemy_x(cnt),enemy_y(cnt)
		gcopy 3,0,0,96,96
	loop
	return
*drawsoalabeam
    color 0,0,255
    repeat soala_BULLET_MAX
        if soala_bulletFlag(cnt) == 0 : continue
    pos soala_bulletX(cnt),soala_bulletY(cnt)
    mes ""
    loop
    return