presentation = Image.load("Sprites/stage11.png")
Resolution = { width = 480, height = 272 }

lefttable = { 
Image.load("./Sprites/Naruto/esquerda1.png"),
Image.load("./Sprites/Naruto/esquerda2.png"),
Image.load("./Sprites/Naruto/esquerda3.png"),
Image.load("./Sprites/Naruto/esquerda4.png"),
Image.load("./Sprites/Naruto/esquerda5.png"),
Image.load("./Sprites/Naruto/esquerda6.png"),
}

righttable = { 
Image.load("./Sprites/Naruto/direita1.png"),
Image.load("./Sprites/Naruto/direita2.png"),
Image.load("./Sprites/Naruto/direita3.png"),
Image.load("./Sprites/Naruto/direita4.png"),
Image.load("./Sprites/Naruto/direita5.png"),
Image.load("./Sprites/Naruto/direita6.png"),
}

standtable = { 
Image.load("./Sprites/Naruto/st1.png"),
Image.load("./Sprites/Naruto/st2.png"),
Image.load("./Sprites/Naruto/st3.png"),
Image.load("./Sprites/Naruto/st4.png"),
Image.load("./Sprites/Naruto/st5.png"),
Image.load("./Sprites/Naruto/st6.png"),
}

standtable2 = { 
Image.load("./Sprites/Naruto/st11.png"),
Image.load("./Sprites/Naruto/st22.png"),
Image.load("./Sprites/Naruto/st33.png"),
Image.load("./Sprites/Naruto/st44.png"),
Image.load("./Sprites/Naruto/st55.png"),
Image.load("./Sprites/Naruto/st66.png"),
}

s = Image.load("./Sprites/Naruto/st1.png")

--Tabela do jogador
Player = { x = 30, y = 215, img = s, d = u, direction = "right", state = "ground", jspeed = 10 }

--Variaveis
oldpad = Controls.read()
direction = "right"
animtimer = 0

--Funcoes

function playerjump()
	if pad:cross() and not oldpad:cross() and Player.state == "ground" then
		Player.state = "jump"
end
	if Player.state == "jump" then
		Player.jspeed = Player.jspeed - 0.5
		Player.y = Player.y - Player.jspeed
		Player.img = jump
end
	if Player.jspeed == 0 then
		Player.y = Player.y + (Player.jspeed + 3)
end
	if (Player.y) >= 210.5 then
		Player.state = "ground"
		Player.jspeed = 10
end
end

function animate(imgtable,interval) 
	local numFrames = table.getn(imgtable) 
		if animtimer >= numFrames*interval then animtimer = 0 end 
			animtimer = animtimer + 1 
		for i = 1, numFrames do 
		if animtimer > interval*(i-1) and animtimer <= interval*i then 
			img = imgtable[i] 
	break 
end
end
	return img 
end

--Loop principal
while true do
screen:clear()
pad = Controls.read()

screen:blit(0,0,presentation,false)

playerjump()

	if not pad:right() and not pad:left() and direction == "left" and Player.state == "ground" then
		Player.img = animate(standtable2,10)
end
	if not pad:right() and not pad:left() and direction == "right" and Player.state == "ground" then
		Player.img = animate(standtable,10)
end
	if pad:left() and Player.x > 0 and Player.state == "ground" then
		Player.x = Player.x - 1.5
		Player.img = animate(lefttable,5)
		direction="left"
end
	if pad:right() and Player.x < 480 and Player.state == "ground" then
		Player.x = Player.x + 1.5
		Player.img = animate(righttable,5)
		direction="right"
end
	if pad:down() then
		direction = "down"
end
	if pad:up() then
		direction = "up"
end
	if Player.x >= Resolution.width - Player.img:width()/2 then
		dofile ("./Script/Stage1/Stage1.txt")
end

screen:blit(Player.x,Player.y,Player.img)

screen.waitVblankStart()
screen.flip()
oldpad = pad
end 