
// Project: Followers Revenge
// Created: 2018-12-28

// show all errors
SetErrorMode(2)

#constant screenwidth=640
#constant screenheight=480
#constant fullscreen=1
#constant screenrate=0

// set window properties
SetWindowTitle( "Followers Revenge" )
SetWindowSize( screenwidth, screenheight, fullscreen )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( screenrate, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

global timetext,actiontext,actiontextshadow,intext,outtext // createtext variables

global mapsprite,smallermapsprite,mapspritememblock, mouse, mapspriteimage, explosionparticle // map and memblock image variables


global objects as integer[] // title 3D objects from a PNG file


global worldsizex, worldsizey, worldpositionx // world positioning variables

global time, in, out, frametimer#, homex, homey, currentactionselected
global game_mode=0 // 0=game start/title, -1 running, 1 - paused (switched with game_mode=-game_mode

global leveluptext, levelreset, level // level variables

type _actionimages
	spr
	counter
	countertextid
	
endtype
global actionimages as _actionimages[10]

type _follower
	spr
	x#
	y#
	speed#
	explodingtext
	explodingtime
	explosionparticle
	expldingframetime#
	direction // 0=left, 1 = right
	action$
	actioncount // counter to perform each action - each follow only has a certain amount of tools / energy to comeplete an action
	actionframestep# // this is a timer of when to do the actions - maybe perhaps ever half a second
endtype
global followers as _follower[]

type _levelimagecolours
	tcred
	tcgreen
	tcblue
	bcred
	bcgreen
	bcblue
	howmanyfollowersthislevel
endtype
global levelimagecolours as _levelimagecolours[]

worldsizey=155

runtitle()


level=1


initialise()



do
	
	if GAME_MODE=0
		print("DD")
	else
		if GAME_MODE=-1

			runningcode()

		else
			pausecode()
		endif
	endif


	Render2DFront()
    Print( ScreenFPS() )
    
    Sync()
loop

function runtitle()

	music = LoadMusic("\media\overworld.mp3")
	PlayMusic(music)
	SetMusicFileVolume(music,0)

	title as integer[]
	AGKImage1 = CreateSprite(LoadImage("\media\Made-with-AppGameKit-Logo-64px.png"))
	AGKImage2 = CreateSprite(LoadImage("\media\Made-with-AppGameKit-Logo-64px.png"))

	title.insert(CreateText("An Hobbiest Re-Incarnation"))
	title.insert(CreateText("An App Game Kit Reproduction"))
	title.insert(CreateText("AGK Programming By: Puzzler2018"))
	title.insert(CreateText("Inspired By: DMA Design"))
	title.insert(CreateText("With help from the TGC World at"))
	title.insert(CreateText("https://forum.thegamecreators.com/"))
	title.insert(CreateText("Music: Overture By Kevin McCleoud"))
	title.insert(CreateText("Proudly Presents"))
	skip=CreateText("Press Left Mouse Button to Skip")
	SetTextSize(skip,8)
	for a=0 to title.length
		SetTextSize(title[a],30)
		SetTextVisible(title[a],0)
	next
	
	a=0
	alphafade=0
	musicfade=0
	alphaup=1
	repeat
		SetTextPosition(title[a],screenwidth/2-GetTextTotalWidth(title[a])/2,screenheight/2-GetTextTotalHeight(title[a]))
		SetTextPosition(skip, screenwidth/2-GetTextTotalWidth(skip)/2,screenheight-10)
		SetSpritePosition(AGKImage1,50,screenheight/2-GetSpriteHeight(agkimage1)/2)
		SetSpritePosition(AGKImage2,520,screenheight/2-GetSpriteHeight(agkimage2)/2)
		SetMusicFileVolume(music,musicfade)

		SetTextColorAlpha(title[a],alphafade)
		
		if a=1
			SetSpriteColorAlpha(AGKImage1,alphafade)
			SetSpriteColorAlpha(AGKImage2,alphafade)
		else
			SetSpriteColorAlpha(AGKImage1,0)
			SetSpriteColorAlpha(AGKImage2,0)
		endif	
		SetTextVisible(title[a],1)
		if alphaup=0 then dec alphafade
		if alphaup=1 
			inc alphafade
			inc musicfade
		endif
		if alphafade>255 and timer()-framestep#>4
			alphafade=255
			musicfade=100
			framestep#=timer()
			alphaup=0
		
		endif
		
		
		
		if alphafade<0
			alphafade=0
			SetTextVisible(title[a],0)
			inc a
			alphaup=1
		endif
		
		
		sync()
	until alphafade<0 or a>title.length or GetRawMouseLeftPressed()
// carry on fading if it left mouse button pressed and is half way through a fade
	if alphafade>0
		repeat 
			SetTextPosition(title[a],screenwidth/2-GetTextTotalWidth(title[a])/2,screenheight/2-GetTextTotalHeight(title[a]))
			SetTextPosition(skip, 0,screenheight-10)
			SetSpritePosition(AGKImage1,50,screenheight/2-GetSpriteHeight(agkimage1)/2)
			SetSpritePosition(AGKImage2,520,screenheight/2-GetSpriteHeight(agkimage2)/2)

			SetTextColorAlpha(title[a],alphafade)
			SetTextColorAlpha(skip,alphafade)
			if a=1
				SetSpriteColorAlpha(AGKImage1,alphafade)
				SetSpriteColorAlpha(AGKImage2,alphafade)
			else
				SetSpriteColorAlpha(AGKImage1,0)
				SetSpriteColorAlpha(AGKImage2,0)
			endif	
			SetTextVisible(title[a],1)

			dec alphafade
				sync()
		until alphafade<0
	endif
// load main title
	level=1
	showtitle()
endfunction


function showtitle()
	
	grabinfo("\media\board.png")

	camerax#=100
	cameray#=20
	cameraz#=-207

	mousetocontinue = CreateText("Press Left Mouse Button to Start")
	settextsize	(mousetocontinue, 30)
	repeat
		SetCameraPosition(1,camerax#,cameray#,cameraz#)
		
		SetTextPosition(mousetocontinue,screenwidth/2-GetTextTotalWidth(mousetocontinue)/2,screenheight-100)
		sync()
	until GetRawMouseLeftPressed()
	
	levelup()





	game_mode=-1
endfunction
function grabinfo(filename$)
    img = LoadImage(filename$)
    imgmemblock = CreateMemblockFromImage(img)
     
    width = GetMemblockInt(imgmemblock,0)
    height = GetMemblockInt(imgmemblock,4)
    objnumber=0
        for x=width-1 to 0 step -1
    for y=height-1 to 0 step -1
            offset = (12+((y * width) + x) * 4) - 4
            r=GetMemblockByte(imgmemblock,offset)
            g=GetMemblockByte(imgmemblock,offset+1)
            b=GetMemblockByte(imgmemblock,offset+2)
            a=GetMemblockByte(imgmemblock,offset+3)
             
            //DrawBox(x,y,x+1,y+1,MakeColor(r,g,b),MakeColor(r,g,b),MakeColor(r,g,b),MakeColor(r,g,b),1)
 
            if r<255 and g<255 and b<255
    objects.insert( CreateObjectBox(1,1,10))
    SetObjectPosition(objects[objnumber],x, -y , 5)
    SetObjectColor(objects[objnumber],r,g,b,a)
    inc objnumber
 
endif
        next
    next
     
     
endfunction



function runningcode()
			// this app works on the X axis
		displaymap(worldpositionx)
	
		// updpate and move followers
		updatefollowers()
	
	
		// checkmousecollisionchecks and set actions
		checkcollisionwithmouse()
	
	
		checkkeypresses()
	
	
	
		checktime()
	
		checkleveluporreset()
endfunction
function pausecode()
	
			// paused
		if GetTextExists(pause)=0 then pause = createtext("Paused")
		if GetTextExists(cont)=0 then cont = createtext("Press Left Mouse Button to Continue")
		settextsize(pause,60)
		SetTextSize(cont,30)
		alphafade=255
		repeat
			SetSpriteColorAlpha(mapsprite,alphafade)
			SetSpriteColorAlpha(smallermapsprite,alphafade)
			
			for a=0 to followers.length
				SetSpriteColorAlpha(followers[a].spr,alphafade)
			next
			
			for a=0 to 9
				SetSpriteColorAlpha(actionimages[a].spr,alphafade)
			next
			
			
			SetTextPosition(pause,screenwidth/2-GetTextTotalWidth(pause)/2,screenheight/2-GetTextTotalHeight(pause)/2)
			SetTextPosition(cont,screenwidth/2-GetTextTotalWidth(cont)/2,screenheight-100)
			dec alphafade,10
			if alphafade<50 then alphafade=50
			sync()
		until GetRawMouseLeftPressed()
		DeleteText(pause)
		DeleteText(cont)

		game_mode=-game_mode
// fade up
		alphafade=50
		repeat
			SetSpriteColorAlpha(mapsprite,alphafade)
			SetSpriteColorAlpha(smallermapsprite,alphafade)
			
			for a=0 to followers.length
				SetSpriteColorAlpha(followers[a].spr,alphafade)
			next
			
			for a=0 to 9
				SetSpriteColorAlpha(actionimages[a].spr,alphafade)
			next
			
			
			inc alphafade,10
			sync()
		until alphafade>255


			
		

endfunction
function checkleveluporreset()
	
	if levelreset=0 // in play
		if out=0 and in>0 
			print("LEVELUP")
			sync()
			sleep(2000)
		endif
	else
		if out=0 // all destroyed
			dispose()
			initialise()
			buildlevel(level)
			levelreset=0
					
		endif
	endif
		
	
endfunction

function checkcollisionwithmouse()

	for act=0 to 9
		if GetSpriteCollision(mouse,actionimages[0].spr) and GetRawMouseLeftPressed() // Explode all and reset level
			for a=0 to followers.length
				followers[a].action$="Exploder"
			next
			levelreset=1
		endif
		if GetSpriteCollision(mouse,actionimages[act].spr)
	print(a)
			ax=GetSpriteX(actionimages[act].spr)-2
			ay=GetSpriteY(actionimages[act].spr)-2
			tax=GetSpriteWidth(actionimages[act].spr)+4
			tay=GetSpriteHeight(actionimages[act].spr)+4
		
			col=MakeColor(255,0,0)

			DrawBox(ax,ay,ax+tax,ay+tay,col,col,col,col,1)
			
		endif
		if GetSpriteCollision(mouse,actionimages[act].spr) and GetRawMouseLeftPressed()
			currentactionselected=act
			if currentactionselected=9 // Pause 
				GAME_MODE=-GAME_MODE
			endif

		endif
		
	next
	// draw box around the current selected action 
	ax=GetSpriteX(actionimages[currentactionselected].spr)-2
	ay=GetSpriteY(actionimages[currentactionselected].spr)-2
	tax=GetSpriteWidth(actionimages[currentactionselected].spr)+4
	tay=GetSpriteHeight(actionimages[currentactionselected].spr)+4
		
	col=MakeColor(255,0,0)

	DrawBox(ax,ay,ax+tax,ay+tay,col,col,col,col,1)
	
	
	for a=0 to followers.length
		
		if GetSpriteCollision(mouse,followers[a].spr) = 1
			//update the name of the follower
	
			SetTextString(actiontext,followers[a].action$ + " " + str(a))
			SetTextString(actiontextshadow,followers[a].action$ + " " + str(a))
		
			SetTextPosition(actiontext,10,350)
			SetTextPosition(actiontextshadow,8,348)
			
			
			// if we left click then change action to a digger as long as not floating
			if currentactionselected=1 and followers[a].action$<>"Floater" and GetRawMouseLeftPressed()and GetSpriteVisible(followers[a].spr)=1 and followers[a].action$<>"Digger"
				followers[a].action$="Digger"
				dec actionimages[1].counter
			endif

			// if we right click then change action to a builder as long as not floating
			if currentactionselected=2 and followers[a].action$<>"Floater" and GetRawMouseLeftPressed()and GetSpriteVisible(followers[a].spr)=1 and followers[a].action$<>"Builder"
				followers[a].action$="Builder"
				dec actionimages[2].counter
			endif


			// if we press Space whilst on them - they explode
			if currentactionselected=3 and followers[a].action$<>"Floater" and GetRawMouseLeftPressed() and GetSpriteVisible(followers[a].spr)=1 and followers[a].action$<>"Exploder"
				followers[a].action$="Exploder"
				dec actionimages[3].counter
			endif




		else
//			SetTextString(actiontext,"")
	//		SetTextString(actiontextshadow,"")
			
		endif
		
	next
	
endfunction

function updatefollowers()
	
	for a=0 to followers.length


		// make a blue pixel color of r=0, g=200, b=200 // if hit this color, it will signify that the follow as escaped the level	
		if GetSpriteVisible(followers[a].spr) =1 and checkpixels(followers[a].x# + GetSpriteWidth(followers[a].spr)/2,followers[a].y# + GetSpriteHeight(followers[a].spr)) = MakeColor(0,200,200) // in space
			
			SetSpriteVisible(followers[a].spr,0)
			inc in
			dec out
			
			
		endif

		// check the centre X and the bottom of the sprite
		if checkpixels(followers[a].x# + GetSpriteWidth(followers[a].spr)/2,followers[a].y# + GetSpriteHeight(followers[a].spr)) = MakeColor(0,0,0) // in space
			
			// if follower is "Building" then the position is moved by the Building routine
			if followers[a].action$<>"Builder" or followers[a].action$="Walker"
				inc followers[a].y#,followers[a].speed#
			endif
			
		else
			// if was just been floating then change a walker, cause dont want to upset the other actions
			if followers[a].action$="Floater" then followers[a].action$="Walker"
			
		endif

		if followers[a].action$="Walker"
			// move left or right
			if followers[a].direction=1
			// go right
				inc followers[a].x#,followers[a].speed#
			else
			// go left
				dec followers[a].x#,followers[a].speed#
			endif
		endif



		/// hit something so dont carry on going down - automatically go right

		if followers[a].action$="Digger"
		// dig earth directly down
					
			
			if timer()-followers[a].actionframestep#>1 // every half a second perform a build
					
				
				followers[a].actionframestep#=timer()
				dighole(followers[a].x#,followers[a].y#+GetSpriteHeight(followers[a].spr))
				
			endif
			
		// destroy earth
		
		endif


		if followers[a].action$="Exploder" and GetSpriteVisible(followers[a].spr)=1
		// Explode the little fellow
					
			SetTextPosition(followers[a].explodingtext, followers[a].x#+worldpositionx,followers[a].y#-10)

			if GetParticlesExists(followers[a].explosionparticle) then SetParticlesPosition(followers[a].explosionparticle,followers[a].x#+worldpositionx+GetSpriteWidth(followers[a].spr)/2,followers[a].y#+GetSpriteHeight(followers[a].spr)/2)
			
			if timer()-followers[a].actionframestep#>1 // every half a second perform a build
				SetSpriteColor(followers[a].spr,255,0,0,255)	
				SetTextString(followers[a].explodingtext,str(followers[a].explodingtime))
				followers[a].actionframestep#=timer()
//				dighole(followers[a].x#,followers[a].y#+GetSpriteHeight(followers[a].spr))
				if followers[a].explodingtime=0
					followers[a].explodingtime=1000
					explodearea(followers[a].x#,followers[a].y#+GetSpriteHeight(followers[a].spr))

					generateexplosion(a, followers[a].x#+worldpositionx,followers[a].y#)
					

			//		SetTextVisible(followers[a].explodingtext,0)
					
			//		SetSpriteVisible(followers[a].spr,0)
				endif
				dec followers[a].explodingtime

			endif
			
		// destroy earth
		
		endif

		
		
		if followers[a].action$="Builder"
		// build new platforms in the direction is going and up one pixel

			
				dec followers[a].y#,followers[a].speed#/50.0
				
				if followers[a].direction=-1
							
					dec followers[a].x#,followers[a].speed#/10
			
				else
					inc followers[a].x#,followers[a].speed#/10
			
			
				endif



			// add a platform undernear of follower
			if timer()-followers[a].actionframestep#>1 // every half a second perform a build
						
				followers[a].actionframestep#=timer()
				setplatform(followers[a].x#+GetSpriteWidth(followers[a].spr),followers[a].y#+GetSpriteHeight(followers[a].spr))
				inc followers[a].actioncount
				
			endif

			if followers[a].actioncount>30 // can only do this 30 times before gives up and carries on walking
				followers[a].action$="Walker"
			endif
			
		endif
			


		// check the centre Y and the left of the sprite to see if hit a wall on the left
		if checkpixels(followers[a].x#,followers[a].y# + GetSpriteHeight(followers[a].spr)/2) = MakeColor(0,0,0) // in space
			
			// it has so change direction
			
			followers[a].direction =- followers[a].direction
		endif


		// check the centre Y and the right of the sprite to see if hit a wall on the right
		if checkpixels(followers[a].x#+GetSpriteWidth(followers[a].spr),followers[a].y# + GetSpriteHeight(followers[a].spr)/2) = MakeColor(0,0,0) // in space
			
			// it has so change direction
			
			followers[a].direction =- followers[a].direction
		endif


		
		
	next
endfunction

function generateexplosion(followerid, x,y)
//sleep(100)
	followers[followerid].explosionparticle = CreateParticles(x,y)
			life#=2
			
			SetParticlesSize(followers[followerid].explosionparticle,4)
			SetParticlesLife(followers[followerid].explosionparticle,life#)
			SetParticlesColorInterpolation(followers[followerid].explosionparticle,1)
			SetParticlesFrequency(followers[followerid].explosionparticle,1000)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.1,0,0,255,255)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.2,255,255,0,200)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.3,255,0,0,100)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.4,255,255,255,50)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.5,255,255,255,50)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.6,200,200,200,50)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.7,100,100,100,50)
			AddParticlesColorKeyFrame(followers[followerid].explosionparticle,.8,0,0,0,0)
			SetParticlesVelocityRange(followers[followerid].explosionparticle,20,24)
	followers[followerid].expldingframetime#=timer()
			
endfunction
function explodearea(x,y)
	
	//mapspritememblock
	width = GetMemblockInt(mapspritememblock,0)
// make a nice depth platform of 2 pi+xls down
	a=30

	for xx=-a to a
		for yy=y to y+20

			offset = 12 + (((yy) * width) + xx+x) * 4
			if offset<GetMemblockSize(mapspritememblock)

				SetMemblockInt(mapspritememblock,offset,0)
				SetMemblockInt(mapspritememblock,offset+1,0) 
				SetMemblockInt(mapspritememblock,offset+2,0)
				SetMemblockInt(mapspritememblock,offset+3,255)
			endif
		next
		dec a
	next
	
		
		DeleteImage(mapspriteimage)
	CreateImageFromMemblock(mapspriteimage, mapspritememblock)
	SetSpriteImage(mapsprite,mapspriteimage)
	SetSpriteImage(smallermapsprite,mapspriteimage)
	
endfunction 


function dighole(x,y)
	
	//mapspritememblock
	width = GetMemblockInt(mapspritememblock,0)
// make a nice depth platform of 2 pixls down
	a=10
	for yy=y to y+3 

		for xx=-a to a
			offset = 12 + (((yy) * width) + xx+x) * 4
			if offset<GetMemblockSize(mapspritememblock)

				SetMemblockInt(mapspritememblock,offset,0)
				SetMemblockInt(mapspritememblock,offset+1,0)
				SetMemblockInt(mapspritememblock,offset+2,0)
				SetMemblockInt(mapspritememblock,offset+3,255)
			endif
		next
		dec a
	next
	
		
		DeleteImage(mapspriteimage)
	CreateImageFromMemblock(mapspriteimage, mapspritememblock)
	SetSpriteImage(mapsprite,mapspriteimage)
	SetSpriteImage(smallermapsprite,mapspriteimage)
	
endfunction 

	
function setplatform(x,y)
	
	//mapspritememblock
	width = GetMemblockInt(mapspritememblock,0)

// make a nice depth platform of 2 pixls down
	for xx=x-5 to x+5	
		for yy=y to y+2
			offset = 12 + ((yy * width) + xx) * 4
	
			SetMemblockInt(mapspritememblock,offset,0)
			SetMemblockInt(mapspritememblock,offset+1,random(200,255)) // a nice shade of green
			SetMemblockInt(mapspritememblock,offset+2,0)
			SetMemblockInt(mapspritememblock,offset+3,255)
		next
	next
		
		DeleteImage(mapspriteimage)
	CreateImageFromMemblock(mapspriteimage, mapspritememblock)
	SetSpriteImage(mapsprite,mapspriteimage)
	SetSpriteImage(smallermapsprite,mapspriteimage)
	
endfunction

function checkpixels(x,y)
	
	//mapspritememblock
	width = GetMemblockInt(mapspritememblock,0)
	
	offset = 12 + ((y * width) + x) * 4
	if offset<GetMemblockSize(mapspritememblock)
		r=GetMemblockInt(mapspritememblock,offset)
		g=GetMemblockInt(mapspritememblock,offset+1)
		b=GetMemblockInt(mapspritememblock,offset+2)
		a=GetMemblockInt(mapspritememblock,offset+3)
	
		col = MakeColor(r,g,b)
	
		
	endif
	
endfunction col

function addfollower()

	if out<levelimagecolours[level-1].howmanyfollowersthislevel
		follower as _follower
		follower.spr =CreateSprite(0)
		follower.x# = screenwidth/2 
		follower.y# = homey
		follower.direction=1 // automatically go right when start
		follower.speed#=.2
		follower.action$="Floater"
		follower.actionframestep#=timer()
		follower.explodingtime=5
		follower.explodingtext = CreateText(str(follower.explodingtime))
		SetTextSize(follower.explodingtext,10)
		SetSpriteColor(follower.spr,0,0,255,255)
		SetSpritePosition(follower.spr,follower.x#,follower.y#)
		
		followers.insert(follower)

		inc out
	endif
endfunction

function initialisetime()
	time=500

//	worldpositionx=-screenwidth/2
	frametimer#=timer()
endfunction

function checktime()

	for a=0 to followers.length

		if (timer()-followers[a].expldingframetime#)>.5


			if GetParticlesExists(followers[a].explosionparticle)
				SetParticlesFrequency(followers[a].explosionparticle,0)
				DeleteParticles(followers[a].explosionparticle)
				SetSpriteVisible(followers[a].spr,0)
				SetTextVisible(followers[a].explodingtext,0)
				dec out
			endif
		endif
	next

	if (levelreset=0) and (timer()-frametimer#)>1 
		frametimer#=timer()
		addfollower()
		
		dec time
		
		settextstring(timetext,"Time " + str(time))
	endif
endfunction


function checkkeypresses()

	// if move mouse to the left or right side of screen then move the viewport - same if press the < or > cursors
	
	if getpointerx()<10 then inc worldpositionx
	if getpointerx()>screenwidth-10 then dec worldpositionx
	if GetRawKeyState(37) then dec worldpositionx
	if GetRawKeyState(39) then inc worldpositionx

	if GetRawKeyPressed(76) //L
		inc level
		levelup()
	endif


// press space to add a follower
	if GetRawKeyPressed(32)
		addfollower()
	endif

	
	
	if GetRawKeyPressed(27) then end

endfunction

function levelup()

// fade current level down
	alphafade=255
	
	repeat
		if GetSpriteExists(mapsprite)=1 then SetSpriteColor(mapsprite,GetSpriteColorRed(mapsprite),GetSpriteColorGreen(mapsprite),GetSpriteColorBlue(mapsprite),alphafade)
		if GetSpriteExists(smallermapsprite)=1 then SetSpriteColor(smallermapsprite,GetSpriteColorRed(smallermapsprite),GetSpriteColorGreen(smallermapsprite),GetSpriteColorBlue(smallermapsprite),alphafade)
		
		dec alphafade,2
		sync()
	until alphafade<0
	
	
	
	
	// build new level with 0 alpha
	
	dispose()
	initialise()
	buildlevel(level)
	
	
	// generate information between screens and fade it up
	
	if GetTextExists(line1)=0 then line1=CreateText("Level " + str(level))
	if GetTextExists(line2)=0 then line2=CreateText("Number of Followers : " + str(levelimagecolours[level-1].howmanyfollowersthislevel))
	if GetTextExists(line3)=0 then line3=CreateText("Rating              : Fun")
	if GetTextExists(line4)=0 then line4=CreateText("Realease Rate       : 1 second")
	if GetTextExists(line9)=0 then line9=CreateText("Press Mouse Button to Continue")
	
	settextsize(line1,30)
	SetTextColorAlpha(line1,0)
	settextsize(line2,30)
	SetTextColorAlpha(line2,0)
	settextsize(line3,30)
	SetTextColorAlpha(line3,0)
	settextsize(line4,30)
	SetTextColorAlpha(line4,0)
	settextsize(line9,20)
	SetTextColorAlpha(line9,0)


	SetSpriteSize(smallermapsprite,screenwidth-50,70)
	sync()
		SetSpritePosition(smallermapsprite,screenwidth/2-GetSpriteWidth(smallermapsprite)/2,10)
		SetTextPosition(line1,screenwidth/2-GetTextTotalWidth(line1)/2,100)
		SetTextPosition(line2,screenwidth/2-GetTextTotalWidth(line2)/2,150)
		SetTextPosition(line3,screenwidth/2-GetTextTotalWidth(line3)/2,200)
		SetTextPosition(line4,screenwidth/2-GetTextTotalWidth(line4)/2,250)
		SetTextPosition(line9,screenwidth/2-GetTextTotalWidth(line9)/2,screenheight-50)

	sync()
	
	alphafade=0
	repeat
		SetSpriteColorAlpha(smallermapsprite,alphafade)
		SetTextColorAlpha(line1,alphafade)
		SetTextColorAlpha(line2,alphafade)
		SetTextColorAlpha(line3,alphafade)
		SetTextColorAlpha(line4,alphafade)

		SetTextColorAlpha(line9,alphafade)
		inc alphafade
		sync()
	until alphafade>255
	
	
	
	// repeat the above screen unil Space bar is pressed
	repeat
		sync()
	until GetRawMouseLeftPressed()
	
	// fade down the information screen
	alphafade=255
	repeat
		SetSpriteColorAlpha(smallermapsprite,alphafade)
		SetTextColorAlpha(line1,alphafade)
		SetTextColorAlpha(line2,alphafade)
		SetTextColorAlpha(line3,alphafade)
		SetTextColorAlpha(line4,alphafade)
		SetTextColorAlpha(line9,alphafade)
		dec alphafade
		sync()
	until alphafade<0
	
	// fade up the new level screen
	repeat
		SetSpriteColor(mapsprite,GetSpriteColorRed(mapsprite),GetSpriteColorGreen(mapsprite),GetSpriteColorBlue(mapsprite),alphafade)
		SetSpriteColor(smallermapsprite,GetSpriteColorRed(smallermapsprite),GetSpriteColorGreen(smallermapsprite),GetSpriteColorBlue(smallermapsprite),alphafade)
		
		inc alphafade,2
		sync()
	until alphafade>255
	
	
	
	
endfunction

function displaymap(worldpositionx)
	
	SetSpritePosition(mouse,getpointerx(),getpointery())
	SetSpritePosition(mapsprite,worldpositionx,0)


	SetTextString(outtext,"Out " + str(out))
	SetTextPosition(outtext,200,350)
	
	
	SetTextString(intext,"In " + str(in))
	SetTextPosition(intext,400,350)
	SetTextPosition(timetext,550,350)


// move the followers according the position X on where the map is
	for a=0 to followers.length
		SetSpritePosition(followers[a].spr,followers[a].x#+worldpositionx,followers[a].y#)
	next

	SetTextPosition(leveluptext,screenwidth*2+100 + worldpositionx,315)

	
	SetSpritePosition(smallermapsprite,500,400)
	SetSpriteSize(smallermapsprite,200,50)

// draw box around the smaller map according to where you are on the map
	col=MakeColor(255,255,255)
	
	DrawBox(500+worldpositionx,400,500+worldpositionx+50,450,col,col,col,col,0)
	

	for a=0 to 9

		SetSpritePosition(actionimages[a].spr,10+a*38,400)
		
		SetTextString(actionimages[a].countertextid,str(actionimages[a].counter))
		SetTextPosition(actionimages[a].countertextid,10+a*38,380)
		SetSpriteSize(actionimages[a].spr,32,32)
		SetSpriteDepth(actionimages[a].spr,0)
	next
	


endfunction


function initialise()

	setuplevelimages()

	swap()
		DrawLine(0,4,8,4,MakeColor(255,255,255),MakeColor(255,255,255))
		DrawLine(4,0,4,8,MakeColor(255,255,255),MakeColor(255,255,255))
	render()
	mouse=CreateSprite(GetImage(0,0,8,8))	
	SetSpriteDepth(mouse,0)
	SetRawMouseVisible(0)



	leveluptext = CreateText("Level Up")
	SetTextSize(leveluptext,12)
	
	
	actiontext= CreateText("Digger 01")
	SetTextSize(actiontext,30)
	SetTextColor(actiontext,0,200,0,255)
	SetTextBold(actiontext,1)

	actiontextshadow= CreateText("Digger 01")
	SetTextSize(actiontextshadow,31)
	SetTextColor(actiontextshadow,0,255,0,255)
	SetTextBold(actiontextshadow,1)
	SetTextDepth(actiontext,0)
	SetTextDepth(actiontextshadow,1)



	outtext= CreateText("Out 0")
	SetTextSize(outtext,30)
	SetTextColor(outtext,0,200,0,255)
	SetTextBold(outtext,1)


	intext= CreateText("In 0%")
	SetTextSize(intext,30)
	SetTextColor(intext,0,200,0,255)
	SetTextBold(intext,1)

	timetext= CreateText("Time")
	SetTextSize(timetext,30)
	SetTextColor(timetext,0,200,0,255)
	SetTextBold(timetext,1)


	
	actionimages[0].spr=createsprite(LoadImage("\media\action_explodeall.png"))
	actionimages[1].spr=createsprite(LoadImage("\media\action_dig.png"))
	actionimages[2].spr=createsprite(LoadImage("\media\action_build.png"))
	actionimages[3].spr=createsprite(LoadImage("\media\action_explode.png"))
	actionimages[4].spr=createsprite(LoadImage("\media\action_climb.png"))
	actionimages[5].spr=createsprite(LoadImage("\media\action_float.png"))
	actionimages[6].spr=createsprite(LoadImage("\media\action_stopper.png"))
	actionimages[7].spr=createsprite(LoadImage("\media\action_bazooker.png"))
	actionimages[8].spr=createsprite(LoadImage("\media\action_mine.png"))
	actionimages[9].spr=createsprite(LoadImage("\media\action_pause.png"))
	
	for a=0 to 9
		actionimages[a].counter=levelimagecolours[level-1].howmanyfollowersthislevel
		actionimages[a].countertextid=CreateText(str(actionimages[a].counter))
		SetTextSize(actionimages[a].countertextid,12)
	next



endfunction

function dispose()
	
	DeleteAllImages()
	DeleteAllObjects()
	DeleteAllSprites()
	DeleteAllText()

	followers.length=-1

endfunction

function buildlevel(level)
initialisetime()
	
	
	ground=30
	swap()
	// blank out the mouse we just made
		DrawBox(0,0,screenwidth,worldsizey,MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),MakeColor(0,0,0),1)
	for a=0 to level * 30
		rx=random(0,screenwidth)
		ry=random(100-(level*2),worldsizey)
		
		size=random(10,11+(level*2))
		
		tlc=MakeColor(0,200,0)
		trc=tlc
		blc=tlc
		brc=tlc
		
		// draw random boxes in the air	
		for x=rx to rx+size
			for y=ry to ry+size
				if y<ry+random(5,7)
					
					r1=random(levelimagecolours[level-1].tcred,levelimagecolours[level-1].tcred+20)
					g1=random(levelimagecolours[level-1].tcgreen,levelimagecolours[level-1].tcgreen+20)
					b1=random(levelimagecolours[level-1].tcblue,levelimagecolours[level-1].tcblue+20)
					
					col = MakeColor(r1,g1,b1)
					DrawBox(x,y,x+1,y+1,col,col,col,col,1)
				else
					r1=random(levelimagecolours[level-1].bcred,levelimagecolours[level-1].bcred+20)
					g1=random(levelimagecolours[level-1].bcgreen,levelimagecolours[level-1].bcgreen+20)
					b1=random(levelimagecolours[level-1].bcblue,levelimagecolours[level-1].bcblue+20)
					
					col=MakeColor(r1,g1,b1)
					DrawBox(x,y,x+1,y+1,col,col,col,col,1)
				
					
				endif	
			next
		next
		
		

	next
	
	
		// draw the ground		
		for x=5 to screenwidth-5
			for y=worldsizey-ground to worldsizey
				if y<worldsizey-ground+random(5,7)
					r1=random(levelimagecolours[level-1].tcred,levelimagecolours[level-1].tcred+20)
					g1=random(levelimagecolours[level-1].tcgreen,levelimagecolours[level-1].tcgreen+20)
					b1=random(levelimagecolours[level-1].tcblue,levelimagecolours[level-1].tcblue+20)
					col=MakeColor(r1,g1,b1)
					DrawBox(x,y,x+1,y+1,col,col,col,col,1)
				else
					r1=random(levelimagecolours[level-1].bcred,levelimagecolours[level-1].bcred+20)
					g1=random(levelimagecolours[level-1].bcgreen,levelimagecolours[level-1].bcgreen+20)
					b1=random(levelimagecolours[level-1].bcblue,levelimagecolours[level-1].bcblue+20)
					col=MakeColor(r1,g1,b1)
					DrawBox(x,y,x+1,y+1,col,col,col,col,1)
				
					
				endif	
			next
		next
	


// create entry point
	rx=random(5,screenwidth-50)

	for x=rx to rx+5
		for y= worldsizey-ground to worldsizey-5
			col=MakeColor(random(200,255),random(200,255),0)
			DrawBox(x,y,x+1,y+1,col,col,col,col,1)
		next
	next

// make a blue pixel color of r=0, g=200, b=200 // if hit this color, it will signify that the follow as escaped the level	
	for x=rx+5 to rx+15
		for y= worldsizey-ground to worldsizey-ground
			col=MakeColor(0,200,200)
			DrawBox(x,y,x+1,y+1,col,col,col,col,1)
		next
	next

	for x=rx+5 to rx+15
		for y= worldsizey-ground+1 to worldsizey-5
			col=MakeColor(0,random(150,250),random(150,250))
			DrawBox(x,y,x+1,y+1,col,col,col,col,1)
		next
	next

// levelup path
	for x=rx+5 to screenwidth-5
		for y= worldsizey-10 to worldsizey-5
			col=MakeColor(0,random(150,250),random(150,250))
			DrawBox(x,y,x+1,y+1,col,col,col,col,1)
		next
	next

	for x=rx+ground-15 to rx+ground-10
		for y= worldsizey-ground to worldsizey-10
			col=MakeColor(random(200,255),random(200,255),0)
			DrawBox(x,y,x+1,y+1,col,col,col,col,1)
		next
	next



	for x=rx to screenwidth-5
		for y= worldsizey-5 to worldsizey-2
			col=MakeColor(random(200,255),random(200,255),0)
			DrawBox(x,y,x+1,y+1,col,col,col,col,1)
			
			DrawBox(x+15,y-10,x+15+1,y-10+1,col,col,col,col,1)

		next
	next




	homex=rx+5
	homey=ry+10
	
	// reset position of the world sprite
	worldpositionx=-homex

render()

mapspriteimage = getimage(0,0,screenwidth,worldsizey)

mapsprite = CreateSprite(mapspriteimage)
SetSpriteColorAlpha(mapsprite,0)
SetSpritePosition(mapsprite,worldpositionx,0)

smallermapsprite=CloneSprite(mapsprite)

mapspritememblock = CreateMemblockFromImage(mapspriteimage) // for pixel work
endfunction



function setuplevelimages()
	i as _levelimagecolours

	for a=0 to 10 // 10 levels

// top shade
		i.tcred=random(200,255)
		i.tcgreen=random(200,255)
		i.tcblue=random(200,255)

// a darker shade of the top	
		i.bcred=i.tcred-50
		i.bcgreen=i.tcgreen-50
		i.bcblue=i.tcblue-50
	
		i.howmanyfollowersthislevel=(a+1) * 10
		levelimagecolours.insert(i)

	next
	out=0
	in=0


	
	
	
endfunction
