Difference between revisions of "Tuto tiny-ecs demo Part 10 Conclusion"
(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...") |
|||
(3 intermediate revisions by the same user not shown) | |||
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 | ||
+ | ) | ||
+ | ) | ||
+ | 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'''! | |
− | + | == 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'''! |
− | + | == Further reading == | |
+ | One last link if you want to know more about ECS: | ||
− | ''' | + | '''[https://github.com/SanderMertens/ecs-faq#what-is-ecs github SanderMertens what is ecs]''' |
Latest revision as of 14:29, 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!
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!
Further reading
One last link if you want to know more about ECS:
github SanderMertens what is ecs
Prev.: Tuto tiny-ecs demo Part 9 Player Shoots Enemies Die
END