Difference between revisions of "Tuto tiny-ecs demo Part 4 tiny-ecs Entity"

From GiderosMobile
 
Line 43: Line 43:
 
-- tiny-ecs
 
-- tiny-ecs
 
self.tiny = require "classes/tiny-ecs"
 
self.tiny = require "classes/tiny-ecs"
self.tiny.world = self.tiny.world()
+
self.tiny.tworld = self.tiny.world()
 
-- the player (xspritelayer, x, y)
 
-- the player (xspritelayer, x, y)
 
self.player1 = EPlayer1.new(self.camera, 12*16, 10*16)
 
self.player1 = EPlayer1.new(self.camera, 12*16, 10*16)
self.tiny.world:addEntity(self.player1)
+
self.tiny.tworld:addEntity(self.player1)
 
-- ...
 
-- ...
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 07:39, 21 December 2023

Entity

It is time to create our first Entity.

In your project create three folders which you can call "_E", "_C", "_S".

In the entities folder "_E", create a file called "ePlayer1.lua" for example. This will be the player1 entity:

EPlayer1 = Core.class()

function EPlayer1:init(xspritelayer, x, y)
	-- ids
	self.isplayer1 = true -- id
	self.isactor = true -- id
	-- sprite
	self.spritelayer = xspritelayer
	self.sprite = Pixel.new(0x0000ff, 1, 32, 64)
	-- params
	self.x = x
	self.y = y
end

The key here is the entity id.

An 'id' will serve as a filter for a tiny-ecs System. That is, a System will only apply to an entity with a specific id.

An entity can have multiple ids to make it available to more than one System.

After the id, we define a Sprite layer to draw our entity to. In this case our entity is a blue Pixel, with a width of 32 pixels and a height of 64 pixels.

All the entity variables (spritelayer, sprite, x and y position, ...) will be available to the Systems.

In fact, all the entity variables can serve as an id, as we will see later.

That's it, we created our first Entity, easy!

World addEntity

Now we need to add our first entity to tiny-ecs world.

	-- ...
	-- tiny-ecs
	self.tiny = require "classes/tiny-ecs"
	self.tiny.tworld = self.tiny.world()
	-- the player (xspritelayer, x, y)
	self.player1 = EPlayer1.new(self.camera, 12*16, 10*16)
	self.tiny.tworld:addEntity(self.player1)
	-- ...

First, we set up our player1 as being a new EPlayer1 entity, and we add it to tiny-ecs world.

You can run the project but the player1 will not display yet in Level1!

Next?

In the next part, we will create a System to display our current and all future entities.


Prev.: Tuto tiny-ecs demo Part 3 tiny-ecs World
Next: Tuto tiny-ecs demo Part 5 tiny-ecs System


Tutorial - tiny-ecs demo