InitNewGame()

sprites = {}
frames = {}
pos = {}

function KeyDown( key )
	for i=1,9 do
		if key == keys[i] then
			active = sprites[i]
			return false
		end
	end
	if key == keys.lbracket then
		frame = frame - 1
		SetObjAnim( active, frame, false )
		frames[active] = frame
	elseif key == keys.rbracket then
		frame = frame + 1
		SetObjAnim( active, frame, false )
		frames[active] = frame
	elseif key == keys.enter then
		print("w!")
		for key, value in pairs(sprites) do
			if frames[value] == 0 then
				glx, gly = pos[value][1], pos[value][2]
			end
		end
		for key, value in pairs(sprites) do
			print( frames[value], ":", pos[value][1]-glx, pos[value][2]-gly )
		end
	end
	return false
end

function MouseKeyDown( key )
	if active then
		pos[active] = { x, y }
		active = nil
	else
		active = CreateSprite( "galactic_pear", x, y )
		table.insert( sprites, active )
		SetObjPos( active, x, y )
		frame = 0
		frames[active] = 0
	end
end

Resume( NewThread( function() 
		while true do
			x,y = GetMousePos() 
			if active then 
				SetObjPos(active, x, y) 
			end 
			Wait(1)
		end
	end))

--[[
0	:	0	0
1	:	1	-69
2	:	-57	28
4	:	55	28
6	:	105	31
8	:	0	170
--]]

spr = CreateSprite( "galactic_pear", 0, 0 )
SetObjPos( spr, 0, 0 )
SetObjAnim( spr, 0, false )
spr = CreateSprite( "galactic_pear", 0, 0 )
SetObjPos( spr, 1, -69 )
SetObjAnim( spr, 1, false )
spr = CreateSprite( "galactic_pear", 0, 0 )
SetObjPos( spr, -57, 28 )
SetObjAnim( spr, 2, false )
spr = CreateSprite( "galactic_pear", 0, 0 )
SetObjPos( spr, 55, 28 )
SetObjAnim( spr, 4, false )
spr = CreateSprite( "galactic_pear", 0, 0 )
SetObjPos( spr, 105, 31 )
SetObjAnim( spr, 6, false )
spr = CreateSprite( "galactic_pear", 0, 0 )
SetObjPos( spr, -75, 31 )
SetObjAnim( spr, 6, false )
SetObjSpriteMirrored( spr, true )
spr = CreateSprite( "galactic_pear", 0, 0 )
SetObjPos( spr, 0, 170 )
SetObjAnim( spr, 8, false )

GlobalSetKeyDownProc( KeyDown )
--GlobalSetKeyReleaseProc( KeyUp )
--GlobalSetMouseKeyReleaseProc( MouseKeyUp )
GlobalSetMouseKeyDownProc( MouseKeyDown )
