Tuto tiny-ecs demo Part 10 Conclusion

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

So this is the last chapter.

Adding Buildings

Let's finish the tiny-ecs demo by adding some buildings to our level1.

In the entities folder "_E", let's add a file called "eBuilding.lua" for example.

This is the code for an EBuilding entity:

EBuilding = Core.class()

function EBuilding:init(xspritelayer, x, y, w, h)
	-- sprite
	self.spritelayer = xspritelayer
	self.sprite = Pixel.new(math.random(0xffffff), 1, w, h)
	self.sprite:setAnchorPoint(0.5, 1)
	-- params
	self.x = x
	self.y = y
end

The only thing here is to change the anchor point of the EBuilding entity.

Now in the "LevelX.lua" file we add some buildings to tiny-ecs world:

	-- ...
	-- tiny-ecs
	if self.tiny == nil then
		self.tiny = require "classes/tiny-ecs"
	end
	self.tiny.tworld = self.tiny.world()
	-- some deco (xspritelayer, x, y, w, h)
	for i = 1, 8 do -- 8 random buildings
		self.tiny.tworld:addEntity(
			EBuilding.new(self.camera,
				math.random(myappwidth), 14*16,
				math.random(4*16, myappwidth/4), math.random(8, 12)*16
			)
		)
	end
	-- some enemies (xspritelayer, x, y, dx, dy)
	local nmes = {}
	for i = 1, 10 do -- we create 10 enemies
	-- ...

We add 8 randomly positionned and sized EBuilding entities.

I hope you like the demo!

Gideros Project

In the project I added an SDebugDraw system.

Media:tiny-ecs_demo.zip (tip: right click and save link as)

Conclusion

This was my take on using tiny-ecs. To sum up:

  • tiny-ecs has a world
  • a world is made of entities and systems
  • entities have ids
  • entities can have components
  • components can serve as ids
  • a system filters the entities to act upon based on entities ids
  • a system can run once or every update

I really enjoy making games using the ECS paradigm. I hope to have given you the keys to experiment with it!


Thanks for reading!


Prev.: Tuto tiny-ecs demo Part 9 Player Shoots Enemies Die
END


Tutorial - tiny-ecs demo