screeny = 230
screenx = 20
collisionx = 20
collisiony = 230
black = Color.new(0,0,0)
mapx = 1
mapy = 1


dofile("loadlevel.lua")

--define globals
PLAYER_HEIGHT = 32
PLAYER_WIDTH = 24

dir = {}
dir.vx = 0
dir.vy = 0
dir.ax = 0
dir.ay = 1  --constant gravity


--timers and frames

W_FRAME = 1
W_TIMER = 4
J_FRAME = 1
J_TIMER = 4

SCROLLX = 0
SCROLLY = 0

--Collision colors
Solid_Ground = Color.new(0,255,255)
Semi_Solid_Ground = Color.new(255,255,0)
Air = Color.new(255,255,255)


--Load Ryu Sprites

--Lefts
Stand_l = Image.load("ryu_l/stand.png")
Run_l = {}
Run_l[1] = Image.load("ryu_l/run1.png")
Run_l[2] = Image.load("ryu_l/run2.png")
Run_l[3] = Image.load("ryu_l/run3.png")
SideJump_l = {}
SideJump_l[1] = Image.load("ryu_l/sidejump1.png")
SideJump_l[2] = Image.load("ryu_l/sidejump2.png")
SideJump_l[3] = Image.load("ryu_l/sidejump3.png")
SideJump_l[4] = Image.load("ryu_l/sidejump4.png")
Jump_l = {}
Jump_l[0] = Image.load("ryu_l/jump0.png")
Jump_l[1] = Image.load("ryu_l/jump1.png")

--Rights
Stand_r = {}
Stand_r[1] = Image.load("ryu_r/stand1.png")
Stand_r[2] = Image.load("ryu_r/stand2.png")
Stand_r[3] = Image.load("ryu_r/stand3.png")
Stand_r[4] = Image.load("ryu_r/stand4.png")
Run_r = {}
Run_r[1] = Image.load("ryu_r/run1.png")
Run_r[2] = Image.load("ryu_r/run2.png")
Run_r[3] = Image.load("ryu_r/run3.png")
Run_r[4] = Image.load("ryu_r/run4.png")
Run_r[5] = Image.load("ryu_r/run5.png")
Run_r[6] = Image.load("ryu_r/run6.png")
Run_r[7] = Image.load("ryu_r/run7.png")
Run_r[8] = Image.load("ryu_r/run8.png")
Run_r[9] = Image.load("ryu_r/run9.png")
Run_r[10] = Image.load("ryu_r/run10.png")
Run_r[11] = Image.load("ryu_r/run11.png")
Run_r[12] = Image.load("ryu_r/run12.png")
Run_r[13] = Image.load("ryu_r/run13.png")
Run_r[14] = Image.load("ryu_r/run14.png")
Run_r[15] = Image.load("ryu_r/run15.png")
Run_r[16] = Image.load("ryu_r/run16.png")
Run_r[17] = Image.load("ryu_r/run17.png")
SideJump_r = {}
SideJump_r[1] = Image.load("ryu_r/sidejump1.png")
SideJump_r[2] = Image.load("ryu_r/sidejump2.png")
SideJump_r[3] = Image.load("ryu_r/sidejump3.png")
SideJump_r[4] = Image.load("ryu_r/sidejump4.png")
Jump_r = {}
Jump_r[0] = Image.load("ryu_r/jump0.png")
Jump_r[1] = Image.load("ryu_r/jump1.png")

state = Stand_r [W_FRAME]

LoadLevel(01)

function collision()

	if(collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony - 1) == Solid_Ground)  then
		dir.vy = 3
	end

	while(collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 33) == Solid_Ground and collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 32) == Solid_Ground) do
		scroll()
		OnGround = 1
		collisiony = collisiony - 1
	end
		
	while(collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 33) == Semi_Solid_Ground and collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 32) == Semi_Solid_Ground and dir.vy >= 0) do
		scroll()
		OnGround = 1
		collisiony = collisiony - 1
	end

	while(collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 33) == Semi_Solid_Ground and collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 32) ~= Semi_Solid_Ground) do
		scroll()
		OnGround = 1
		collisiony = collisiony + 2
	end

	while(collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 33) == Solid_Ground and collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 32) ~= Solid_Ground) do
		scroll()
		OnGround = 1
		collisiony = collisiony + 2
	end

	if(collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 32) == Air and collisionbackground[mapy][mapx]:pixel(collisionx + 12,collisiony + 33) == Air) then
		scroll()
		OnGround = 0
	end
end

function scroll()


	
	if(collisionx >= 450 and lastdir == "right")  then
		mapx = mapx + 1
		collisionx = 0
	end
	if(collisionx <= 4 and mapx ~= 1 and lastdir == "left")  then
		mapx = mapx - 1
		collisionx = 450
	end
--	mapy = math.floor(collisiony/230) + 1

	screenx = collisionx - SCROLLX + ((mapx - 1) * 450)
	screeny = collisiony - SCROLLY

	if(screenx > 300)  then
		SCROLLX = SCROLLX + 3
	end

	if(screenx < 100)  then
		SCROLLX = SCROLLX - 3
	end
	if(SCROLLX <= 0)  then
		SCROLLX = 0
	end
end
	

function input()
	pad = Controls.read()

	if lastdir == "left"  then
		state = Stand_l
	end
	
	if lastdir == "right"  then
		state = Stand_r[W_FRAME]
		W_TIMER = W_TIMER - 1
			if W_TIMER == 0  then
				W_FRAME = W_FRAME + 1
				W_TIMER = 4
			end
			if W_FRAME == 4  then
				W_FRAME = 1
			end
	end

	if(OnGround == 1) then
		if pad:right()  then
			dir.ax = 3
			state = Run_r[W_FRAME]
			W_TIMER = W_TIMER - 1
			if W_TIMER == 0  then
				W_FRAME = W_FRAME + 1
				W_TIMER = 17
			end
			if W_FRAME == 17  then
				W_FRAME = 1
			end
			lastdir = "right"
			if pad:cross()  then
				dir.vy = -13
				collisiony = collisiony - 2
				jump1 = 0
			end
		end

		if pad:left()  then
			dir.ax = -3
			state = Run_l[W_FRAME]
			W_TIMER = W_TIMER - 1
			if W_TIMER == 0  then
				W_FRAME = W_FRAME + 1
				W_TIMER = 4
			end
			if W_FRAME == 4  then
				W_FRAME = 1
			end
			lastdir = "left"
			if pad:cross()  then
				dir.vy = -13
				collisiony = collisiony - 2
				jump1 = 0
			end
		end
		
		if pad:cross()  then
			dir.vy = -10
			collisiony = collisiony - 3
			jump1 = 0
		end
	end

	if(OnGround == 0)  then
		if(lastdir == "left" and dir.vx == 0)  then
			state = Jump_l[0]
			if(dir.vy > -7)  then
				state = Jump_l[1]
			end
		end

		if(lastdir == "right" and dir.vx == 0)  then
			state = Jump_r[0]
			if(dir.vy > -7)  then
				state = Jump_r[1]
			end
		end


		if(lastdir == "left" and dir.vx < 0)  then
			state = SideJump_l[J_FRAME]
			J_TIMER = J_TIMER - 1
			if J_TIMER == 0  then
				J_FRAME = J_FRAME + 1
				J_TIMER = 4
			end
			if J_FRAME == 4  then
				J_FRAME = 1
			end
		end

		if(lastdir == "right" and dir.vx > 0)  then
			state = SideJump_r[J_FRAME]
			J_TIMER = J_TIMER - 1
			if J_TIMER == 0  then
				J_FRAME = J_FRAME + 1
				J_TIMER = 4
			end
			if J_FRAME == 4  then
				J_FRAME = 1
			end
		end
	end

end



function movement()
	
	dir.vx = dir.vx + dir.ax
	dir.vy = dir.vy + dir.ay
	if(dir.vy >= 5)  then
		dir.vy = 4
	end

	dir.ax = 0
	if(OnGround == 1 and dir.vy >= 0)  then
		collisionx = collisionx + dir.vx
		dir.vx = 0
	elseif(OnGround == 1)  then
		collisionx = collisionx + dir.vx
		dir.vx = 0
		dir.vy = 0
	end

	if(OnGround == 0)  then
		collisiony = collisiony + 2
		collisiony = collisiony + dir.vy
		collisionx = collisionx + dir.vx
	end
end

function draw()

	if(mapx ~= 1)  then
		screen:blit((450 * (mapx - 2)) - SCROLLX,0,background[1][mapx - 1])
	end

	if(mapx <= MAP_WIDTH)  then
	--or mapx ~= MAP_WIDTH)  then
		screen:blit((450 * (mapx - 1)) - SCROLLX,0,background[1][mapx])
	end

	if(mapx <= MAP_WIDTH - 1)  then
		screen:blit((450 * mapx) - SCROLLX,0,background[1][mapx + 1])
	end

	screen:blit(screenx,screeny,state)
end




while true do

	input()
	collision()
	movement()
	draw()

	
	if(lastdir == "right")  then

	elseif(lastdir == "left")  then
	
	end

	screen.waitVblankStart()
	screen.flip()
end