RANDOMIZE Timer()
SYNC ON : SYNC RATE 33
 
 
`======================
`		INIT VARIABLES
`======================
GOSUB Arrays
GOSUB Vars
 
`======================
`		GEN IMAGES 
`======================
GOSUB Create_Gfx
 
`======================
`			MAIN LOOP
`======================
GOSUB GenMap
Create bitmap 1,640,480
Do
	REM State variable controls where the program should go for a main loop
	REM 3=Menu
	REM 1=The level design (gets images 1-30,000)
	REM 2=The game (gets images 30,001-60,000)
	IF state=1 THEN GOSUB S1_Lvl_Design
	IF state=2 THEN GOSUB S2_Play
	IF State=3 THEN GOSUB S3_Menu
	copy bitmap 1,0
	SYNC
	set current bitmap 1
	CLS
LOOP
 
END
`=====================
`		INIT SUBS
`=====================
Arrays:
	REM General State Arrays
		DIM Stars(1000,2)
	REM State 1 Arrays
		DIM S1_A1(2,2)
		DIM S1_A2(2,2)
		DIM S1_A3(2,2)
		DIM S1_Map(33,33)
		DIM S1_Tiles(2,6)
		DIM S1_Menu(3)
 
	REM State 2 Arrays
		DIM S2_MAP(33,33)	
 
	REM State 3 Arrays
		DIM S3_Menu$(5)
RETURN
 
Vars:
	REM PLAYER LOCATION
	Player_X=0
	Player_Y=0
	LAND=0
	STATE=3
	CurrentLevel=1
	grav#=.1
	Land=0
 
	REM State 1 Variables
	REM Area 1
	S1_A1(1,1)=0
	S1_A1(1,2)=639
	S1_A1(2,1)=0
	S1_A1(2,2)=20
 
	REM Area 2
	S1_A2(1,1)=0
	S1_A2(1,2)=100
	S1_A2(2,1)=21
	S1_A2(2,2)=479
 
	REM Area 3
	S1_A3(1,1)=150
	S1_A3(1,2)=598
	S1_A3(2,1)=25
	S1_A3(2,2)=473
 
	REM State 3 Variables
	S3_Menu$(0)="Star Catcher"
	S3_Menu$(1)="Play"
	S3_Menu$(2)="Quit"
RETURN
 
Create_Gfx:
	GET IMAGE 20,0,0,13,13
	REM Tiles
	REM Floor
	INK RGB(255,255,255),0
	BOX 0,0,14,14
	GET IMAGE 21,0,0,14,14
	CLS
	REM Player
	SET TEXT SIZE 14
	INK rgb(255,0,0),0
	CENTER TEXT 7,0,"Y"
	GET IMAGE 22,0,0,13,13
	CLS
	REM COIN
	INK RGB(255,255,0),0
	CENTER TEXT 7,0,"*"
	GET IMAGE 23,0,0,13,13
	CLS
 
 
	REM STATE 1 Stuff
	REM Area 1
	INK RGB(255,0,0),0
	BOX S1_A1(1,1),S1_A1(2,1),S1_A1(1,2),S1_A1(2,2)
	INK 0,0
	CENTER TEXT S1_A1(1,1)+(S1_A1(1,2)-S1_A1(1,1))/6,S1_A1(2,1),"START/STOP"
	CENTER TEXT S1_A1(1,1)+(S1_A1(1,2)-S1_A1(1,1))/2,S1_A1(2,1),"Reset"
	CENTER TEXT S1_A1(1,1)+5*(S1_A1(1,2)-S1_A1(1,1))/6,S1_A1(2,1),"END"
	GET IMAGE 1,S1_A1(1,1),S1_A1(2,1),S1_A1(1,2),S1_A1(2,2)
	CLS
 
	REM Area 2
	INK RGB(0,255,0),0
	BOX S1_A2(1,1),S1_A2(2,1),S1_A2(1,2),S1_A2(2,2)
	GET IMAGE 2,S1_A2(1,1),S1_A2(2,1),S1_A2(1,2),S1_A2(2,2)
	CLS
 
	REM Area 3
	INK RGB(0,0,255),0
	BOX S1_A3(1,1),S1_A3(2,1),S1_A3(1,2),S1_A3(2,2)
	GET IMAGE 3,S1_A3(1,1),S1_A3(2,1),S1_A3(1,2),S1_A3(2,2)
	CLS
 
 
	REM State 3 Stuff
	REM Menu Background
	INK 0,RGB(127,127,127)
	CLS
	SET TEXT FONT "Arial",0
	SET TEXT SIZE 50
	CENTER TEXT 320,20,S3_Menu$(0)
 
	INK RGB(255,255,255),0
	SET TEXT SIZE 24
	BOX 320-10-(Text Width(S3_Menu$(1))/2),190,330+(Text Width(S3_Menu$(1))/2),200+Text Size()+20
	BOX 320-10-(Text Width(S3_Menu$(2))/2),200+Text Size()+45,330+(Text Width(S3_Menu$(2))/2),200+(4*Text Size())+45
 
	INK 0,0
	CENTER TEXT 320,200,S3_Menu$(1)
	CENTER TEXT 320,200+Text Size()+50,S3_Menu$(2)
	GET IMAGE 4,0,0,640,480
	CLS
RETURN
 
`=========================
`	RANDOM STAR PLACEMENT
`=========================
GenMap:
	FOR i=1 to CurrentLevel
		StarX=Rnd(31)+1
		StarY=Rnd(31)+1
		S1_Map(StarX,StarY)=3		
		Stars(i,1)=StarX
		Stars(i,2)=StarY
		Stars(i,0)=1
	NEXT i
RETURN
 
 
`===================================
`		STATE 1 (LEVEL DESIGN) SUBS
`===================================
 
S1_Lvl_Design:
	REM Parts:
	REM Gradient Map DONE
	REM Coin Loc. Generator DONE (See GenMap subroutine)
	REM Tile Selection (DONE)
	REM Tile placement (DONE)
	REM Drawing tools (take from DNG)
 
	`===============
	`		GUI
	`===============
	REM Generates the gui and saves it as image 
	GOSUB 1_Gen_Gui
 
	`===============
	`	LOC CHECK
	`===============
	REM MENU AREA
	IF MouseX()>=S1_A1(1,1) AND MouseX()<=S1_A1(1,2)
		IF MouseY()>=S1_A1(2,1) AND MouseY()<=S1_A1(2,2)
			s1_loc=1
		ENDIF
	ENDIF
	REM TILE SELECTION AREA
	IF MouseX()>=S1_A2(1,1) AND MouseX()<=S1_A2(1,2)
		IF MouseY()>=S1_A2(2,1) AND MouseY()<=S1_A2(2,2)
			s1_loc=2
		ENDIF
	ENDIF
	REM MAP AREA
	IF MouseX()>=S1_A3(1,1) AND MouseX()<=S1_A3(1,2)
		IF MouseY()>=S1_A3(2,1) AND MouseY()<=S1_A3(2,2)
			s1_loc=3
		ENDIF
	ENDIF
 
	`===============
	`   SELECTION
	`===============
	IF MouseClick()>0
		click=MouseClick()
		SELECT s1_loc
			CASE 1 : GOSUB 1_Menu : ENDCASE
			CASE 2 : GOSUB 1_TileSelect : ENDCASE
			CASE 3 : GOSUB 1_MapArea : ENDCASE
		ENDSELECT
	ENDIF
 
	`===============
	`	MAP REGEN
	`===============
	GOSUB 1_Regen
 
	`===============
	`	CLEAN UP
	`===============
	GOSUB 1_Clean
 
RETURN
 
1_Gen_Gui:
	PASTE IMAGE 1,S1_A1(1,1),S1_A1(2,1)
	PASTE IMAGE 2,S1_A2(1,1),S1_A2(2,1)
	PASTE IMAGE 3,S1_A3(1,1),S1_A3(2,1)
 
	SPRITE 21,S1_A2(1,1)+1,S1_A2(2,1)+1,21
	SCALE SPRITE 21,700
	SPRITE 22,S1_A2(1,1)+1,S1_A2(2,1)+98,22
	SCALE SPRITE 22,700
RETURN
 
1_Menu:
REM Options: Start/Stop,Reset,Quit
	IF Click=1
		X=MouseX()-S1_A1(1,1)
		Selection=X/213
		INC Selection
		IF Selection=1
			GOSUB Setup_Map2
			State=2
		ENDIF
		IF Selection=2
			GOSUB 1_Clear
		ENDIF
		IF Selection=3
			INK 0,0
			CLS
			END
		ENDIF
	ENDIF
RETURN
 
1_TileSelect:
	Y=MouseY()-S1_A2(2,1)
	Tile=Y/98
	INC Tile
	IF Tile>2 THEN Tile=2
RETURN
 
1_MapArea:
	LocX=1+(MouseX()-S1_A3(1,1))/14
	LocY=1+(MouseY()-S1_A3(2,1))/14
	IF LocX<=32 AND LocY<=32 AND S1_Map(LocX,LocY)<3
		IF Click=1
			IF Tile=2 
				S1_Map(Player_X,Player_Y)=0
				Player_X=LocX : Player_Y=LocY
			ENDIF
			S1_Map(LocX,LocY)=Tile
		ENDIF
		IF Click=2
			S1_Map(LocX,LocY)=0
		ENDIF
	ENDIF
 
RETURN
 
1_Regen:
FOR X=1 to 32
	FOR Y=1 to 32
		PASTE IMAGE 20+S1_Map(X,Y),(X-1)*14+S1_A3(1,1),(Y-1)*14+S1_A3(2,1)
	NEXT Y
NEXT X
RETURN
 
1_Clean:
RETURN
 
1_Clear:
	FOR X=1 TO 32
		FOR Y=1 TO 32
			IF S1_Map(X,Y)<3
				S1_Map(X,Y)=0
			ENDIF
		NEXT Y
	NEXT X
	Player_X=0
	Player_Y=0
RETURN
 
Setup_Map2:
	CLS
	1_Player_X=Player_X
	1_Player_Y=Player_Y
	FOR X=1 TO 32
		FOR Y=1 TO 32
			S2_Map(X,Y)=S1_Map(X,Y)
		NEXT Y
	NEXT X
	REPEAT
	UNTIL MOUSECLICK()=0
RETURN
 
 
 
`========================
`	STATE 2 (PLAY) SUBS
`========================
S2_Play:
`KEY CONTROLS
`GRAVITY
`COLLISION CHECKS
scale=1
 
	GOSUB 1_GEN_GUI
	IF MouseClick()=1
		Repeat
		until mouseclick()=0
		State=1
		Player_X=1_Player_X
		Player_Y=1_Player_Y
	ELSE
		GOSUB 2_MOVE
	ENDIF
	GOSUB 2_REGEN
 
RETURN
 
2_Move:
	IF RightKey()=1 THEN GOSUB 2_MoveRight
	IF LeftKey()=1 THEN Gosub 2_MoveLeft
	IF RightKey()=0 AND LeftKey()=0 THEN HORVEL=0
	IF Upkey()=1 then gosub 2_jump
 
 
	S2_MAP(Player_X,Player_Y)=0
 
		IF VertVel<=0
			IF S2_MAP(Player_X,Player_Y+1)<>1
				Inc Player_Y
				Land=0
			Else
				Land=1
				VertVel=0
			ENDIF
		ELSE
			If S2_Map(Player_X,Player_Y-1)<>1
				Dec Player_Y
				Land=0
				Dec VertVel
			Else
				VertVel=0
			ENDIF
		ENDIF
	If S2_Map(Player_X+HorVel,Player_Y)<>1
		INC Player_X,HorVel
	ENDIF
	If Player_X<1 then Player_X=1
	If Player_X>32 then Player_X=32
	If Player_Y<1 then Player_Y=1
	If Player_Y>32 then player_Y=32
	S2_Map(Player_X,Player_Y)=1
 
	For I=1 to CurrentLevel
		If Player_X=Stars(I,1) and Player_Y=Stars(I,2) and Stars(I,0)=1
			Inc Stars_Collected
			Stars(I,0)=0
		Endif
	Next I
 
	If Stars_Collected=CurrentLevel
		Gosub 2_Complete_Level
	Endif
RETURN
 
 
2_MoveRight:
	HorVel=1
RETURN
 
2_MoveLeft:
	HorVel=-1
RETURN
 
2_Jump:
If Land=1
	land=0
	VertVel=5
Endif
RETURN
 
2_REGEN:
S2_MAP(PLAYER_X,PLAYER_Y)=2
`ClsPrint Land
FOR X=1 to 32
	FOR Y=1 to 32
		PASTE IMAGE 20+S2_Map(X,Y),(X-1)*14+S1_A3(1,1),(Y-1)*14+S1_A3(2,1)
	NEXT Y
NEXT X
RETURN
 
2_Complete_Level:
	Stars_Collected=0
	Inc CurrentLevel
	State=1
	Player_X=1_Player_X
	Player_Y=1_Player_Y
	GOSUB Clear_Map_1
	Gosub GenMap
RETURN
 
 
Clear_Map_1:
	For X=0 to 32
		For Y=0 to 32
			S1_Map(x,y)=0
		Next Y
	Next X
RETURN
S4:
	PLAYER_X=16
	PLAYER_Y=3
	S2_MAP(16,3)=2
	FOR I=0 TO 32
		S2_MAP(I,32)=1
	NEXT I
 
	For i=0 to 32
		S2_Map(17,i)=1
	next i
	STATE=2
RETURN
 
`===================================
`				State 3-Menu
`===================================
S3_Menu:
	GOSUB 3_Gen_Menu
	Area=0
	GOSUB 3_Check_Area
	Click=MouseClick()
	GOSUB 3_Check_Click
RETURN
 
3_Gen_Menu:
	PASTE IMAGE 4,0,0
RETURN
 
3_Check_Area:
	IF MouseX()>=320-10-(Text Width(S3_Menu$(1))/2) AND MouseX()<=330+(Text Width(S3_Menu$(1))/2)
		IF MouseY()>=190 AND MouseY()<=200+Text Size()+20
			Area=1
		ENDIF
	ENDIF
 
	IF MouseX()>=320-10-(Text Width(S3_Menu$(2))/2) AND MouseX()<=330+(Text Width(S3_Menu$(2))/2)
		IF MouseY()>=200+Text Size()+45 AND MouseY()<=200+(4*Text Size())+45
			Area=2			
		ENDIF
	ENDIF
RETURN
 
3_Check_Click:
	IF Click=1
		SELECT Area
			CASE 1 : State=1 : CLS : SET TEXT SIZE 12 : ENDCASE
			CASE 2 : END : ENDCASE
		ENDSELECT
	ENDIF
RETURN
`====================================
`					FUNCTIONS
`====================================
FUNCTION Sign(x)
	IF x<0 THEN retval=-1
	If X>0 then retval=1
	If X=0 then retval=0
ENDFUNCTION retval