Tuto tiny-ecs demo Part 3 tiny-ecs World

From GiderosMobile
Revision as of 04:58, 17 December 2023 by MoKaLux (talk | contribs) (Created page with "__TOC__ == tiny-ecs World == We will create our tiny-ecs world in the LevelX scene. Please create the ''levelX.lua'' file in the "'''scenes'''" folder: <syntaxhighlight lang...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

tiny-ecs World

We will create our tiny-ecs world in the LevelX scene.

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

Here we simply 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!

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

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

Entity

where we will add our actors (Entities) to.

Create the main.lua file which contains the scenemanager and a list of scene transitions and easings:

Menu scene

The Menu is a scene, so to better organise our code, please create a folder called "scenes" for example and create the menu.lua file inside it.

Next?

Next we will build our Entities, their Components and the Systems.


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


Tutorial - tiny-ecs demo