Difference between revisions of "Tuto tiny-ecs demo Part 10 Conclusion"
From GiderosMobile
(Created page with "__TOC__ In this chapter we will make the player1 able to "shoot" and "hurt" enemies. This will be done in two parts: first enabling the player1 "shoot" action, then hurting...") |
|||
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
− | + | 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: | |
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | + | EBuilding = Core.class() | |
− | function | + | function EBuilding:init(xspritelayer, x, y, w, h) |
− | self. | + | -- sprite |
− | self. | + | self.spritelayer = xspritelayer |
− | self. | + | self.sprite = Pixel.new(math.random(0xffffff), 1, w, h) |
+ | self.sprite:setAnchorPoint(0.5, 1) | ||
+ | -- params | ||
+ | self.x = x | ||
+ | self.y = y | ||
end | end | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 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: | |
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
− | |||
-- ... | -- ... | ||
− | -- | + | -- tiny-ecs |
− | if | + | if self.tiny == nil then |
− | + | self.tiny = require "classes/tiny-ecs" | |
− | |||
− | |||
− | |||
− | |||
− | |||
end | 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 | |
− | + | ) | |
− | + | ) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | for i = | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | -- some enemies (xspritelayer, x, y, dx, dy) | |
+ | local nmes = {} | ||
+ | for i = 1, 10 do -- we create 10 enemies | ||
+ | -- ... | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | We add 8 randomly positionned and sized EBuilding entities. | |
− | + | '''I hope you like the demo'''! | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | == 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 be run once or every update | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | '''I really enjoy making games using the ECS paradigm, and I hope to have given you the keys to experiment with it'''! | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | ''' | + | '''Thanks for reading'''! |
− | + | == Gideros Project == | |
− | |||
Revision as of 01:30, 22 December 2023
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!
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 be run once or every update
I really enjoy making games using the ECS paradigm, and I hope to have given you the keys to experiment with it!
Thanks for reading!
Gideros Project
Prev.: Tuto tiny-ecs demo Part 9 Player Shoots Enemies Die
END