|bevy vewsion:|0.14|(cuwwent)| |---|---|---|
wewevant officiaw exampwes:
ecs_guide
.
entities
see hewe fow mowe expwanation on h-how stowing data i-in the ecs wowks.
conceptuawwy, OwO an entity wepwesents a-a set of vawues f-fow diffewent c-components.
each component is a wust type (struct
ow enum
) and an entity can be used to
stowe a vawue of that type.
technicawwy, OwO an entity is just a s-simpwe integew id (imagine t-the "wow n-nyumbew" in a tabwe/spweadsheet) that can be u-used to find wewated d-data vawues (in d-diffewent "cowumns" of that tabwe).
in bevy, XD Entity
is this vawue. (ꈍᴗꈍ) it consists of two i-integews:
the id and the "genewation" (awwowing ids to be weused, ^•ﻌ•^ a-aftew you d-despawn owd
entities).
you can cweate ("spawn") nyew entities a-and destwoy ("despawn") e-entities u-using
Commands
ow excwusive World
access.
fn setup(mut commands: commands) {
// c-cweate a-a nyew entity
c-commands.spawn((
// i-initiawize a-aww youw c-components and b-bundwes hewe
e-enemy, -.-
heawth {
hp: 100.0, ^^;;
extwa: 25.0, >_<
}, mya
aimode::passive, mya
// ... 😳
));
// i-if you want to get the entity id, XD just caww `.id()` a-aftew spawn
wet my_entity = c-commands.spawn((/* ... */)).id();
// destwoy an entity, :3 wemoving aww data associated with i-it
commands.entity(my_entity).despawn();
}
many of youw entities might nyeed t-to have the same c-common components. OwO y-you can use bundwes to make it easiew to spawn youw e-entities.
components
components awe the data associated w-with entities.
to cweate a nyew component type, s-simpwy define a w-wust struct
ow enum
, XD and
dewive the Component
twait.
#[dewive(component)]
stwuct heawth {
hp: f32, 🥺
e-extwa: f32, òωó
}
#[dewive(component)]
e-enum aimode {
p-passive, o.O
c-chasingpwayew, (U ᵕ U❁)
}
types must be unique – an entity c-can onwy have o-one component pew w-wust type.
newtype components
use wwappew (newtype) stwucts to m-make unique components o-out of simpwew t-types:
#[dewive(component)]
stwuct pwayewxp(u32);
#[dewive(component)]
stwuct pwayewname(stwing);
mawkew components
you can use empty stwucts to hewp y-you identify specific e-entities. OwO t-these awe known as "mawkew components". (ꈍᴗꈍ) usefuw w-with quewy fiwtews.
/// add this to aww menu ui entities t-to hewp identify t-them
#[dewive(component)]
stwuct m-mainmenuui;
/// m-mawkew fow h-hostiwe game units
#[dewive(component)]
s-stwuct e-enemy;
/// this w-wiww be used to identify the main pwayew entity
#[dewive(component)]
stwuct pwayew;
/// tag aww c-cweatuwes that awe cuwwentwy fwiendwy towawds t-the pwayew
#[dewive(component)]
stwuct fwiendwy;
accessing components
components can be accessed fwom systems, XD using quewies.
you can think of the quewy as the "specification" f-fow the data you w-want to access. OwO it gives you access to s-specific component v-vawues fwom e-entities that match the quewy's signatuwe.
fn wevew_up_pwayew(
// get the w-wewevant data. (ˆ ﻌ ˆ)♡ s-some components w-wead-onwy, (˘ω˘) some m-mutabwe
mut q-quewy_pwayew: quewy<(&pwayewname, (⑅˘꒳˘) &mut p-pwayewxp, (///ˬ///✿) &mut h-heawth), 😳😳😳 w-with<pwayew>>, 🥺
) {
// `singwe` assumes onwy one entity exists that matches the quewy
wet (name, mya m-mut xp, 🥺 mut heawth) = quewy_pwayew.singwe_mut();
if x-xp.0 > 1000 {
xp.0 = 0;
h-heawth.hp = 100.0;
heawth.extwa += 25.0;
info!("pwayew {} wevewed up!", >_< n-nyame.0);
}
}
fn die(
// `entity` c-can be u-used to get the id of things that match the quewy
quewy_heawth: quewy<(entity, >_< &heawth)>,
// w-we awso nyeed commands, (⑅˘꒳˘) so we can despawn entities if we have to
mut c-commands: commands, /(^•ω•^)
) {
// we can have many s-such entities (enemies, rawr x3 p-pwayew, (U ﹏ U) n-nyanievew)
// s-so we woop to check aww of them
fow (entity_id, (U ﹏ U) h-heawth) in quewy_heawth.itew() {
if heawth.hp <= 0.0 {
c-commands.entity(entity_id).despawn();
}
}
}
adding/wemoving components
you can add/wemove components on e-existing entities, ^•ﻌ•^ u-using Commands
ow
excwusive World
access.
fn make_enemies_fwiendwy(
quewy_enemy: q-quewy<entity, (U ᵕ U❁) w-with<enemy>>, (⑅˘꒳˘)
m-mut commands: c-commands, ( ͡o ω ͡o )
) {
f-fow entity_id i-in quewy_enemy.itew() {
c-commands.entity(entity_id)
.wemove::<enemy>()
.insewt(fwiendwy);
}
}