------------------------------------------------------------------------
-- This file isn't used anymore, but contains some (ugly) example Lua
-- code! Do whatever with it. This was replaced by main.lua.
------------------------------------------------------------------------

include("inc.lua");
include("hud.lua");

--updateFrameNumber = 0;
--function worldUpdate()
--	updateFrameNumber = updateFrameNumber + 1;
--	--writeConsole("Worldupdate! "..updateFrameNumber.."\n");
--end

--function worldPostUpdate()
--	writeConsole("Worldpostupdate!\n");
--end

-- All the code here will execute upon loading the file - around the time the
-- game world is created. Use it to set up.
io.write("And so it begins...\n");

-- An example of subclassing off the main Entity class. Entity is constructed
-- in the C++ code and mostly consists of C++ function bindings so we can
-- fiddle around with the entities.
AClass = Entity:createSubClass();

------------------------------------------------------------------------------
-- An example of a child class of something else.

-- We don't need to reimplement createSubClass(). This will create a game
-- object WITHOUT actually instantiating it in the engine. Basically just sets
-- up metatables and all that good stuff.
BClass = AClass:createSubClass();

function BClass:setFrame()
	local vx, vy = self:getVel();
	local threshold = 6;
	
	if vx > threshold then
		-- Right
		self:setSpriteFrame(0, 0, 32, 16);
		
		if vy > threshold then
			-- Up-right
			self:setSpriteFrame(64, 0, 96, 32);
		elseif vy < threshold * -1 then
			-- Down-right
			self:setSpriteFrame(96, 0, 128, 32);
		end
		
	elseif vx < threshold * -1 then
		-- Left
		self:setSpriteFrame(0, 16, 32, 32);
		
		if vy > threshold then
			-- Up-left
			self:setSpriteFrame(128, 0, 160, 32);
		elseif vy < threshold * -1 then
			-- Down-left
			self:setSpriteFrame(160, 0, 192, 32);
		end
	else
		if vy > threshold then
			-- Up
			self:setSpriteFrame(32, 0, 48, 32);
		elseif vy < threshold * -1 then
			-- Down
			self:setSpriteFrame(48, 0, 64, 32);
		end
	end
end

-- All objects should have an update() function.
-- This is called every frame.
-- It is NOT called when the object is very far away (one or more
-- entire screens away). This non-calling functionality has yet to be
-- implemented, however. Use timer hooks for things that have to act
-- when not nearby (not implemented yet).
function BClass:update()
	--io.write("BClass ("..self:getName()..") update\n");

	self:setFrame();
	
	local age = self:getAge();
	--if age > 90 then
	if not self:getOnScreen() then
		self:die();
	end
	
	x, y = self:getPos();
	
	--if y < -256 then
	--	self:die()
	--end
end

-- init() is called when an object is created.
function BClass:init()
	-- Be sure to call init() in the parent class! I know the syntax is a
	-- little weird, but whatever. Be sure to pass self as a parameter and
	-- use the '.' instead of ':', or it'll have the un-instantiated AClass as
	-- its self variable.
	AClass.init(self);
	
	--io.write("BClass ("..self:getName()..") init\n");
	
	self:setTexture("data/lilyshots.png");
	
	self:setSize(16, 16);
	self:setSpriteFrame(0, 0, 0, 0);
	
	self:setBlockable(true);
	--self:setBlocker(true);
	self:setBlocker(false);
	
	--self:setForceUpdate(true);
end
------------------------------------------------------------------------------

CClass = BClass:createSubClass();

-- This is how actual (in-game) instances are created.
--cc = CClass:create();
--c2 = CClass:create();
--c3 = CClass:create();

--function c2:foobar()
	--io.write("c2:foobar()!\n");
--end

--function c3:update()
	--io.write("c3 calling c2...\n");
	--c2:foobar();
	--c2:testFunc();
--end

--for i = 1, 10, 1 do
--	local ab = CClass:create();
--	ab:setPos(i * 16, i * 16);
--	ab:setSize(16, 16);
--end


writeConsole("Lily Project 3 - Creating classes.\n");

include("spawner.lua");

include("player.lua");
include("camera.lua");

include("zombie.lua");
include("teleporter.lua");
include("bonuses.lua");
include("projectiles.lua");


-- TODO: When loading from XML (in C++ side) remake the spawner instances...
--   Make sure the init lua has run (or the base spawner and things to spawn won't be there)
--   To make an instance when loading...
--     Make a ScriptEntity...
--       Push the Spawner base class onto the stack
--       Call the luaCB_create(L, xml)
--         (And make an XML-friendly create function.)
--       ScriptEntity constructor must handle extra XML crap.
--       Set the base class for that (it'll be on the stack)



-- Add in some random Castlevania stuff
--SceneryChunk = Entity:createSubClass();
--function SceneryChunk:init()
	--self:setLayer(0);
	--self:setTexture("data/castlevania1.png");
	--self:setSpriteFrame(0, 368, 1536, 736);
	--self:setPos(0, 0);
	--self:setGravityInfluence(0);
	--self:setBlockable(false);
	--self:setBlocker(false);
--end
--function SceneryChunk:update()
--end
--function SceneryChunk:setSpriteFrame(x1, y1, x2, y2)
	--Entity.setSpriteFrame(self, x1, y1, x2, y2);
	--self:setSize(math.abs(x1 - x2), math.abs(y1 - y2));
--end

--burningBG = SceneryChunk:create();
--burningBG:setTexture("data/castlevania1.png");
--burningBG:setSpriteFrame(0, 368, 1536, 736);

--cvScreen = SceneryChunk:create();
--cvScreen:setTexture("data/castlevania1.png");
--cvScreen:setSpriteFrame(0, 0, 1536, 368);
--cvScreen:setLayer(2);
--cvScreen:setPos(0, 8);

--blackThings = SceneryChunk:create();
--blackThings:setTexture("data/castlevania1.png");
--blackThings:setSpriteFrame(0, 736, 1536, 1104);
--blackThings:setLayer(1);
--blackThings:setAlpha(0.5);





-- Some test scenery!
registerScenery("Merdude", "data/castlevania2stuff.png", 11, 138, 11 + 60, 138 + 31);
registerScenery("Scenery_cv1", "data/castlevania1.png", 0, 0, 1536, 368);
registerScenery("Scenery_cv2", "data/castlevania1.png", 0, 736, 1536, 1104);
registerScenery("Scenery_cv3", "data/castlevania1.png", 0, 368, 1536, 736);


local x;
local y;
for x = 0, 3, 1 do
	for y = 0, 3, 1 do
		registerScenery("CyborgHell1_"..x.."-"..y, "data/cyborghell1.png", x * 1024, y * 768, (x + 1) * 1024, (y + 1) * 768);
	end
end

registerScenery("CyborgHell_NearBG", "data/cyborghell_nearbg.png", 0, 0, 2048, 1536);
registerScenery("CyborgHell_FarBG",  "data/cyborghell_farbg.png", 0, 0, 1024, 768);

--for i = 1, 10, 1 do
--	local ab = CClass:create();
--	ab:setPos(i * 16, i * 16);
--	ab:setSize(16, 16);
--end






--thingBoo = Entity:create();
--thingBoo:setTexture("data/thingboo.png");
--thingBoo:setSpriteFrame(0, 0, 256, 256);
--thingBoo:setSize(16, 16);
--thingBoo:setSpriteBottomCenter(107, 139);


if initialRunDone == nil then
	loadTiles("cyborghell12.xml.tiles.gz");
	loadEntities("cyborghell12.xml");
	initialRunDone = true;
end

