import "Speed.lib"

rem Moon Lander.

constant:
	BG_IMAGE				0
	SHIP_IMAGE			1
	STATION_IMAGE		2
	EXPLOSION_IMAGE	3
	HEADS_IMAGE			4
	CLICK_IMAGE			5
	TMP_IMAGE			6

hidden:

rem Window.
set window 32, 32, 120, 90, false, 5
set redraw off

rem Images.
load image BG_IMAGE, "bg.bmp"
load image SHIP_IMAGE, "ship.bmp"
load image STATION_IMAGE, "station.bmp"
load image EXPLOSION_IMAGE, "explosion.bmp"
load image HEADS_IMAGE, "heads.bmp"
load image CLICK_IMAGE, "click.bmp"
set image grid SHIP_IMAGE, 3, 1
set image grid EXPLOSION_IMAGE, 6, 1
set image grid HEADS_IMAGE, 1, 3
set image colorkey SHIP_IMAGE, 255, 0, 255
set image colorkey STATION_IMAGE, 255, 0, 255
set image colorkey EXPLOSION_IMAGE, 0, 0, 0
set image colorkey HEADS_IMAGE, 0, 0 ,0
create image TMP_IMAGE, 120, 90

do
	t = 0
	do
		t = (t + 1)%60
		set color 255, 255, 255
		draw image BG_IMAGE, 0, 0
		draw image HEADS_IMAGE, 60 - width(HEADS_IMAGE)/2, 32, 2
		if t < 30
			draw image CLICK_IMAGE, 60 - width(CLICK_IMAGE)/2, 82
		endif
		proc SPD_HoldFrame 30
		redraw
	until mousebutton(0)
	stationSpeed = 1

	do
		speed# = 0.0
		shipX = 60
		shipY# = 68.0
		shipFrame
		stationX = 60
		levelCompleteTimer = -1
		levelFailedTimer = -1
		explosionFrame = -1
		fuel# = 60.0
		do
			rem Move station.
			stationX = stationX + stationSpeed
			if stationX > 140 then stationX = -20

			if levelCompleteTimer < 0 and levelFailedTimer < 0
				rem Control.
				if keydown(" ")
					speed = max#(speed - 0.05, -1.0)
					shipAnim = true
					fuel = max#(fuel - 0.25, 0.0)
				else
					speed = min#(speed + 0.1, 2.0)
					shipAnim = false
				endif
				shipY = min#(shipY + speed, 68.0)

				shipFrame = 1 - shipFrame
			elseif levelFailedTimer < 0
				shipX = stationX + 1
			endif

			rem Out of fuel?
			if fuel# = 0.0 and levelFailedTimer < 0
				levelFailedTimer = 30
				explosionFrame = 0
			endif

			if levelCompleteTimer < 0 and levelFailedTimer < 0
				if int(shipY) < 4 + height(STATION_IMAGE)	
					if int(shipY) > 0 + height(STATION_IMAGE)
						rem Dock?
						if abs(60 - stationX) <= 4
							levelCompleteTimer = 60
							shipAnim = false
						endif
					else
						rem Collision?
						if abs(60 - stationX) < 14
							levelFailedTimer = 30
							explosionFrame = 0
						endif
					endif
				endif
			endif

			if levelCompleteTimer > 0 then levelCompleteTimer = levelCompleteTimer - 1
			if levelFailedTimer > 0 then levelFailedTimer = levelFailedTimer - 1
			if explosionFrame >= 0
				explosionFrame = explosionFrame + 1
				if explosionFrame = 6 then explosionFrame = -1
			endif
	
			rem Draw.
			set color 255, 255, 255
			draw image BG_IMAGE, 0, 0
			if shipAnim
				cel = 1 + shipFrame
			else
				cel = 0
			endif
			if levelFailedTimer < 0
				draw image SHIP_IMAGE, shipX - width(SHIP_IMAGE)/2, int(shipY), cel
			endif
			draw image STATION_IMAGE, stationX - width(STATION_IMAGE)/2, 4
			if explosionFrame >= 0
				draw image EXPLOSION_IMAGE, shipX - 4, int(shipY) + 4, explosionFrame
			endif

			if fuel > 0.0
				set color 0, 192, 0
				draw rect 2, 84, int(fuel), 3, true
			endif
	
			proc SPD_HoldFrame 30
			redraw
		until levelCompleteTimer = 0 or levelFailedTimer = 0

		rem Show message.
		if levelCompleteTimer = 0; cel = 0; else; cel = 1; endif

		set color 255, 255, 255
		set image TMP_IMAGE
		draw image primary, 0, 0
		set image primary
		t = 125
		do
			t = t - 1
			draw image TMP_IMAGE, 0, 0
			if t%50 < 25
				draw image HEADS_IMAGE, 60 - width(HEADS_IMAGE)/2, 32, cel
			endif
			proc SPD_HoldFrame 30
			redraw
		until t = 0
		set color 0, 0, 0
		cls
		redraw
		wait 50
		if cel = 1 then break
		stationSpeed = stationSpeed + 1
	loop
loop
