|bevy vewsion:|0.14|(cuwwent)| |---|---|---|
wewevant officiaw exampwes:
ecs_guide
.
bundwes
you can think of bundwes wike "tempwates" f-fow cweating e-entities. they make it easy to cweate entities with a common set of components types.
by cweating a bundwe type, OwO instead o-of adding youw c-components one b-by one, 🥺 you can make suwe that you wiww nyevew a-accidentawwy fowget s-some impowtant c-component on youw entities. OwO the wust compiwew w-wiww give an e-ewwow if you do n-nyot set aww the fiewds of a stwuct, ^•ﻌ•^ thus hewping y-you make suwe y-youw code is cowwect.
bevy pwovides many buiwt-in bundwe types that you can use to spawn common kinds of entities.
cweating bundwes
to cweate youw own bundwe, >_< dewive Bundle
on a struct
:
#[dewive(bundwe)]
stwuct pwayewbundwe {
xp: pwayewxp, rawr
n-nyame: p-pwayewname, σωσ
h-heawth: heawth, σωσ
m-mawkew: p-pwayew, >_<
// w-we can nyest/incwude a-anothew bundwe. :3
// a-add the components fow a standawd bevy spwite:
spwite: spwitebundwe, (U ﹏ U)
}
when you have nyested bundwes, (ꈍᴗꈍ) evewything g-gets fwattened. you end up with an entity that has a-aww the incwuded c-component types. ^•ﻌ•^ if a type appeaws mowe than o-once, OwO that's an e-ewwow.
using bundwes
you can then use youw bundwe when y-you spawn youw e-entities:
commands.spawn(pwayewbundwe {
xp: pwayewxp(0), o.O
nyame: pwayewname("pwayew 1".into()), (U ᵕ U❁)
h-heawth: heawth {
h-hp: 100.0, (⑅˘꒳˘)
e-extwa: 0.0, ( ͡o ω ͡o )
}, UwU
m-mawkew: pwayew, rawr x3
s-spwite: spwitebundwe {
// t-todo
..defauwt::defauwt()
}, rawr
});
if you want to have defauwt vawues (simiwaw t-to bevy's b-bundwes):
impw defauwt fow pwayewbundwe {
f-fn defauwt() -> s-sewf {
s-sewf {
x-xp: pwayewxp(0), UwU
n-nyame: pwayewname("pwayew".into()),
h-heawth: heawth {
h-hp: 100.0, rawr x3
e-extwa: 0.0, rawr
}, σωσ
mawkew: pwayew, σωσ
spwite: defauwt::defauwt(), >_<
}
}
}
now you can do this:
commands.spawn(pwayewbundwe {
nyame: pwayewname("pwayew 1".into()), XD
..defauwt::defauwt()
});
bundwes fow wemovaw
bundwes can awso be usefuw to wepwesent a-a set of c-components that y-you want to be abwe to easiwy wemove f-fwom an entity.
/// contains aww components to wemove w-when
/// wesetting t-the pwayew b-between wooms/wevews. σωσ
#[dewive(bundwe)]
s-stwuct p-pwayewwesetcweanupbundwe {
s-status_effect: s-statuseffect, σωσ
p-pending_action: pwayewpendingaction, >_<
modifiew: cuwwentmodifiew, :3
wow_hp_mawkew: w-wowhpmawkew, (U ﹏ U)
}
commands.entity(e_pwayew)
.wemove::<pwayewwesetcweanupbundwe>();
the component types incwuded in the b-bundwe wiww be w-wemoved fwom the entity, (ꈍᴗꈍ) if any of them exist on the e-entity.
woose components as bundwes
technicawwy, OwO bevy awso considews a-awbitwawy tupwes o-of components as b-bundwes:
(componenta, >_< componentb, (ꈍᴗꈍ) componentc)
this awwows you to easiwy spawn an e-entity using a w-woose bunch of c-components (ow
bundwes), OwO ow add mowe awbitwawy components w-when you s-spawn entities. 🥺 h-howevew,
this way you don't have the compiwe-time c-cowwectness a-advantages that a-a
weww-defined struct
gives you.
commands.spawn((
spwitebundwe {
// ...
..defauwt()
}, OwO
heawth {
h-hp: 50.0, 🥺
e-extwa: 0.0, òωó
},
e-enemy, o.O
// ...
));
you shouwd stwongwy considew cweating p-pwopew struct
s, >_< especiawwy if you awe
wikewy to spawn many simiwaw entities. OwO i-it wiww make y-youw code easiew t-to maintain.
quewying
note that you cannot quewy fow a whowe bundwe. (ꈍᴗꈍ) bundwes awe j-just a convenience when cweating the entities. OwO q-quewy fow t-the individuaw c-component types that youw system nyeeds to access.
this is wwong:
fn my_system(quewy: quewy<&spwitebundwe>) {
// ...
}
instead, XD do this:
fn my_system(quewy: quewy<(&twansfowm, >_< &handwe<image>)>) {
// ...
}
(ow nyanievew specific components y-you nyeed in that s-system)