-- copy all globals into locals, some locals are prefixed with a G to reduce name clashes
local coroutine,package,string,table,math,io,os,debug,assert,dofile,error,_G,getfenv,getmetatable,ipairs,Gload,loadfile,loadstring,next,pairs,pcall,print,rawequal,rawget,rawset,select,setfenv,setmetatable,tonumber,tostring,type,unpack,_VERSION,xpcall,module,require=coroutine,package,string,table,math,io,os,debug,assert,dofile,error,_G,getfenv,getmetatable,ipairs,load,loadfile,loadstring,next,pairs,pcall,print,rawequal,rawget,rawset,select,setfenv,setmetatable,tonumber,tostring,type,unpack,_VERSION,xpcall,module,require

local apps=apps
local win=win


local function print(...) _G.print(...) end

local wstr=require("wetgenes.string")


module("bulb.field")

function bake(state,field)
	local field=field or {}
	
	local cake=state.cake
	local game=cake.game
	local canvas=cake.canvas
	local flat=canvas.flat
	
	local ball=state.rebake("bulb.ball")
	local cells=state.rebake("bulb.cells")

	function field.loads()
--		cake.images.loads({
--			"game",
--			"well",
--		})
	end
	
	function field.setup()

		field.loads()
		
--		field.hx=8.5*game.unit -- 340
--		field.hy=12.5*game.unit -- 500
--		field.px=math.floor((320-field.hx)+game.unit*0.25) -- -10
--		field.py=math.floor((480-field.hy)+game.unit*0.5)  -- 0

--print("FIELD",field.px,field.py)
		
--		cake.sheets.createimg("game"):chop(1,1)
--		cake.sheets.createimg("well"):chop(1,1)



		-- the playing area

		field.px=30
		field.hx=260

		field.py=20
		field.hy=440
			
	end


	function field.update()
	
--[[
		local d=cells.maxy-(ball.swing.cy+ball.dip+(game.unit*1))
		d=d or 0
		d=1-(d/470)
		if d<0 then d=0 end
		if d>1 then d=1 end
		
		field.danger=d
]]		
	end

	function field.draw()

		cake.sheets.get("game"):draw(1,160,240,0,960,960)


--[[		
flat.tristrip("xyzrgba",{
			field.px,field.py,0,						1,1,1,1,
			field.px+field.hx,field.py,0,				1,1,1,1,
			field.px+0,field.py+field.hy,0,				1,1,1,1,
			field.px+field.hx,field.py+field.hy,0,		1,1,1,1,
			})
]]	
	end

	return field
end
