mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 01:20:04 +00:00
Update example with relations
This commit is contained in:
parent
35f92a79d0
commit
4bd44693e8
1 changed files with 14 additions and 8 deletions
22
README.md
22
README.md
|
@ -19,15 +19,18 @@ jecs is a stupidly fast Entity Component System (ECS).
|
|||
### Example
|
||||
|
||||
```lua
|
||||
local world = Jecs.World.new()
|
||||
|
||||
local Health = world:component()
|
||||
local Damage = world:component()
|
||||
local Position = world:component()
|
||||
local world = World.new()
|
||||
|
||||
local player = world:entity()
|
||||
local opponent = world:entity()
|
||||
|
||||
local Health = world:component()
|
||||
local Position = world:component()
|
||||
-- Notice how components can just be entities as well?
|
||||
-- It allows you to model relationships easily!
|
||||
local Damage = world:entity()
|
||||
local DamagedBy = world:entity()
|
||||
|
||||
world:set(player, Health, 100)
|
||||
world:set(player, Damage, 8)
|
||||
world:set(player, Position, Vector3.new(0, 5, 0))
|
||||
|
@ -38,17 +41,20 @@ world:set(opponent, Position, Vector3.new(0, 5, 3))
|
|||
|
||||
for playerId, playerPosition, health in world:query(Position, Health) do
|
||||
local totalDamage = 0
|
||||
for _, opponentPosition, damage in world:query(Position, Damage) do
|
||||
for opponentId, opponentPosition, damage in world:query(Position, Damage) do
|
||||
if (playerPosition - opponentPosition).Magnitude < 5 then
|
||||
totalDamage += damage
|
||||
end
|
||||
world:set(playerId, ECS_PAIR(DamagedBy, opponentId), totalDamage)
|
||||
end
|
||||
end
|
||||
|
||||
world:set(playerId, Health, health - totalDamage)
|
||||
-- Gets the damage inflicted by our specific opponent!
|
||||
for playerId, health, inflicted in world:query(Health, ECS_PAIR(DamagedBy, opponent)) do
|
||||
world:set(playerId, health - inflicted)
|
||||
end
|
||||
|
||||
assert(world:get(playerId, Health) == 79)
|
||||
assert(world:get(opponentId, Health) == 92)
|
||||
```
|
||||
|
||||
125 archetypes, 4 random components queried.
|
||||
|
|
Loading…
Reference in a new issue