Tuto tiny-ecs demo Part 3 tiny-ecs World

From GiderosMobile
Revision as of 01:44, 18 December 2023 by MoKaLux (talk | contribs)

tiny-ecs World

We will create our tiny-ecs world in the LevelX scene (cf: scenemanager).

Please create the levelX.lua file in the "scenes" folder:

LevelX = Core.class(Sprite)

function LevelX:init()
	-- bg
	application:setBackgroundColor(0x5a002d)
	-- the camera
	self.camera = Sprite.new()
	-- tiny-ecs
	self.tiny = require "classes/tiny-ecs"
	self.tiny.world = self.tiny.world()
	-- order
	self:addChild(self.camera)
	-- listeners
	self:addEventListener("enterBegin", self.onTransitionInBegin, self)
	self:addEventListener("enterEnd", self.onTransitionInEnd, self)
	self:addEventListener("exitBegin", self.onTransitionOutBegin, self)
	self:addEventListener("exitEnd", self.onTransitionOutEnd, self)
end

-- game loop
local dt
function LevelX:onEnterFrame(e)
	dt = e.deltaTime
	self.tiny.world:update(dt) -- tiny world
end

-- event listeners
function LevelX:onTransitionInBegin() self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self) end
function LevelX:onTransitionInEnd() self:myKeysPressed() end
function LevelX:onTransitionOutBegin() self:removeAllListeners() end
function LevelX:onTransitionOutEnd() end

-- app keys handler
function LevelX:myKeysPressed()
	self:addEventListener(Event.KEY_DOWN, function(e)
		-- menu (mobiles and desktops)
		if e.keyCode == KeyCode.BACK or e.keyCode == KeyCode.ESC then
			scenemanager:changeScene("menu", 1, transitions[2], easings[2])
		end
	end)
end

The camera is a Sprite which will hold our actors (entities).

Then we initialize tiny-ecs and make a reference to its world.

In the game loop, we update our tiny-ecs world, but there is nothing to update yet!

All the rest is a classic scenemanager scene (SceneManager).

You can run the project and navigate to the Level 1 scene, using your finger, mouse or keyboard :-)

Next?

Next we will build our first Entity.


Prev.: Tuto tiny-ecs demo Part 2 Menu
Next: Tuto tiny-ecs demo Part 4 tiny-ecs Entity


Tutorial - tiny-ecs demo