rem ==================================================================
rem---------------------Tiny space invaders---------------------------
rem-------------------------------------------------------------------
rem-------------------------------------------------------------------
rem-----A shooting game by Goettsch using a tutorial by Marcus--------
rem ==================================================================


rem Import libraries.
import "Speed.lib"
import "Keycodes.lib"

constant:
	rem Images.
	BG_IMAGE					0
	PLAYER_IMAGE			1
	ENEMY_IMAGE				2
	PLAYER_BULLET_IMAGE	3
	ENEMY_BULLET_IMAGE	4
   BACK_GROUND_IMAGE    5
   END_IMAGE            6
   MENU_IMAGE           7

	rem Sound effects.
	PLAYER_SHOOT_SOUND	0
	HIT_SOUND				1
	ENEMY_SHOOT_SOUND		2
	ENEMY_BOOM_SOUND		3
	

	rem Enemy array fields.
	ENM_ACTIVE		0
	ENM_BULLET_X	1
	ENM_BULLET_Y	2

hidden:

rem Create a window with an x4 scale and turn off automatic redraw.
set window 1, 1, 1024, 768, true, 2

set redraw off

rem Load enemies and set their color keys.
load image PLAYER_IMAGE, "data/player.bmp"
load image ENEMY_IMAGE, "data/enemies.bmp"
load image PLAYER_BULLET_IMAGE, "data/bullet.bmp"
load image ENEMY_BULLET_IMAGE, "data/enemy_bullet.bmp"
load image BACK_GROUND_IMAGE, "data/background.bmp"
load image END_IMAGE, "data/endscreen.bmp"
load image MENU_IMAGE, "data/menu.bmp"
set image colorkey PLAYER_IMAGE, 0, 0, 0
set image colorkey ENEMY_IMAGE, 0, 0 ,0
set image colorkey PLAYER_BULLET_IMAGE, 0, 0 ,0
set image colorkey ENEMY_BULLET_IMAGE, 0, 0 ,0
set image grid ENEMY_IMAGE, 0, 0

rem Load sound effects.
load sound PLAYER_SHOOT_SOUND, "data/player_shoot.wav"
load sound HIT_SOUND, "data/hit.wav"
load sound ENEMY_SHOOT_SOUND, "data/enemy_shoot.wav"
load sound ENEMY_BOOM_SOUND, "data/enemy_boom.wav"

rem Small menu
do
set color 0,0,0
cls
set color 255,255,255
      draw image MENU_IMAGE,0,0
redraw

until keydown(VK_SPACE)


rem Enemy array:
rem	enemies[x][y][ ENM_ACTIVE, ENM_BULLET_X, ENM_BULLET_Y]
enemies[21][3][3]

rem Init enemies.
for y = 0 to 2
	for x = 1 to 20
		enemies[x][y][ENM_ACTIVE] = true
		enemies[x][y][ENM_BULLET_X] = -1
	next
next

rem All enemies share a position and movement.
enemiesX = 0
enemiesY = 20
enemiesDX = 3
enemiesDropY = 0
enemyMoveTimer = 0
enemyFrame = 0
enemyShootTimer = 100

rem Player
playerX = (width(primary) - width(PLAYER_IMAGE))/2
playerY = 260
bulletX = -1
bulletY = 0
lives=2

do
	rem Update logic.

	rem Enemies.
	if enemiesDropY = 0
		enemyMoveTimer = (enemyMoveTimer + 1)%4
		if enemyMoveTimer = 0
			enemiesX = enemiesX + enemiesDX
			if enemiesX > 400 or enemiesX < 1
				enemiesDX = -enemiesDX
				enemiesDropY = 6
			endif
			enemyFrame = 5 - enemyFrame
		endif
	else
		enemiesY = enemiesY + 1
		enemiesDropY = enemiesDropY - 1
	endif

	rem Enemy bullets.
	enemyShootTimer = max(enemyShootTimer - 1, 0)
	if enemyShootTimer = 0
		rem Find possible shooters. Only the bottom enemies may shoot
		possible[21][2]
		possibleCount = 0
		for x = 0 to 20
			enmY = -1
			for y = 0 to 2
				if enemies[x][y][ENM_ACTIVE] and enemies[x][y][ENM_BULLET_X] = -1
					enmY = y
				endif
			next
			if enmY >= 0
				possible[possibleCount][0] = x
				possible[possibleCount][1] = enmY
				possibleCount = possibleCount + 1
			endif
		next

		if possibleCount > 0
			rem Chose an enemy.
			i = rnd(possibleCount)
			enemies[possible[i][0]][possible[i][1]][ENM_BULLET_X] = enemiesX + possible[i][0]*10 + 4
			enemies[possible[i][0]][possible[i][1]][ENM_BULLET_Y] = enemiesY + possible[i][1]*10 + 8
			enemyShootTimer = 60 + rnd(60)
			play sound ENEMY_SHOOT_SOUND
		endif
	endif
	
	for y = 0 to 2
		for x = 0 to 20
			if enemies[x][y][ENM_BULLET_X] >= 0
				enemies[x][y][ENM_BULLET_Y] = enemies[x][y][ENM_BULLET_Y] + 8
				rem Out of the window.
				if enemies[x][y][ENM_BULLET_Y] >= 450
					enemies[x][y][ENM_BULLET_X] = -1
				else
					rem Check for collision with player.and end the game if lives <0
					bx = enemies[x][y][ENM_BULLET_X] + 1
					by = enemies[x][y][ENM_BULLET_Y] + height(ENEMY_BULLET_IMAGE)
					if bx >= playerX and bx < playerX + width(PLAYER_IMAGE)
						if by >= playerY and by < playerY + height(PLAYER_IMAGE)
							enemies[x][y][ENM_BULLET_X] = -1
							       play sound HIT_SOUND
                                      lives=lives-1
                                            if lives<0 then

do
set color 0,0,0
cls
set color 255,255,255
      draw image END_IMAGE,0,0
redraw

until keydown(VK_ESC) or not running()
                      endif 
						endif
					endif
				endif
			endif
		next
	next

	rem Player.
	if keydown(VK_LEFT) then playerX = max(playerX - 2, 0)
	if keydown(VK_RIGHT) then playerX = min(playerX + 2, 645)

	rem Player bullet.
	if bulletX >= 0
		rem Move up.
		bulletY = bulletY - 7
		rem Check for collision with enemies.
		x = ((bulletX + 1) - enemiesX)/10
		y = (bulletY - enemiesY)/10
		if x >= 0 and x < 21 and y >= 0 and y < 3
			if enemies[x][y][ENM_ACTIVE]
				enemies[x][y][ENM_ACTIVE] = false
				bulletX = -1
				play sound ENEMY_BOOM_SOUND       
			endif
		endif
		rem Out of window?
		if bulletY < -height(PLAYER_BULLET_IMAGE) then bulletX = -1
	elseif keydown(VK_SPACE, true)
		rem Shoot.
		bulletX = playerX + 4
		bulletY = playerY - 5
		play sound PLAYER_SHOOT_SOUND
	endif	

	rem Draw.

	rem Background.
	draw image BACK_GROUND_IMAGE
	

	rem Enemies.
	set color 255, 255, 255
	for y = 0 to 2
		for x = 0 to 20
			if enemies[x][y][ENM_ACTIVE]
				draw image ENEMY_IMAGE, enemiesX + x*10, enemiesY + y*10, y + enemyFrame
			endif
			if enemies[x][y][ENM_BULLET_X] > 0
				draw image ENEMY_BULLET_IMAGE, enemies[x][y][ENM_BULLET_X], enemies[x][y][ENM_BULLET_Y]
			endif
		next
	next

	rem Player
	draw image PLAYER_IMAGE, playerX, playerY

	rem Player bullet.
	if bulletX >= 0
		draw image PLAYER_BULLET_IMAGE, bulletX, bulletY
	endif

set caret 100,5
set color 255,0,0
center "Lives left   ",lives
set color 255,255,255



	proc SPD_HoldFrame 30
	redraw
until keydown(VK_ESC) or not running()
