Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
big-brain
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Louis
big-brain
Commits
ad513b36
Unverified
Commit
ad513b36
authored
4 years ago
by
Kat Marchán
Browse files
Options
Downloads
Patches
Plain Diff
more example improvements
parent
e54e8d37
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/thirst.rs
+23
-15
23 additions, 15 deletions
examples/thirst.rs
with
23 additions
and
15 deletions
examples/thirst.rs
+
23
−
15
View file @
ad513b36
...
@@ -21,6 +21,9 @@ impl Thirst {
...
@@ -21,6 +21,9 @@ impl Thirst {
pub
fn
thirst_system
(
time
:
Res
<
Time
>
,
mut
thirsts
:
Query
<&
mut
Thirst
>
)
{
pub
fn
thirst_system
(
time
:
Res
<
Time
>
,
mut
thirsts
:
Query
<&
mut
Thirst
>
)
{
for
mut
thirst
in
thirsts
.iter_mut
()
{
for
mut
thirst
in
thirsts
.iter_mut
()
{
thirst
.thirst
+=
thirst
.per_second
*
(
time
.delta
()
.as_micros
()
as
f32
/
1000000.0
);
thirst
.thirst
+=
thirst
.per_second
*
(
time
.delta
()
.as_micros
()
as
f32
/
1000000.0
);
if
thirst
.thirst
>=
100.0
{
thirst
.thirst
=
100.0
;
}
println!
(
"Thirst: {}"
,
thirst
.thirst
);
println!
(
"Thirst: {}"
,
thirst
.thirst
);
}
}
}
}
...
@@ -34,15 +37,15 @@ pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) {
...
@@ -34,15 +37,15 @@ pub fn thirst_system(time: Res<Time>, mut thirsts: Query<&mut Thirst>) {
// These actions will be spawned and queued by the game engine when their
// These actions will be spawned and queued by the game engine when their
// conditions trigger (we'll configure what these are later).
// conditions trigger (we'll configure what these are later).
#[derive(Debug,
Action)]
#[derive(Debug,
Action)]
pub
struct
Drink
Action
;
pub
struct
Drink
;
// Associated with that DrinkAction, you then need to have a system that will
// Associated with that Drink
Action, you then need to have a system that will
// actually execute those actions when they're "spawned" by the Big Brain
// actually execute those actions when they're "spawned" by the Big Brain
// engine.
// engine.
//
//
// In our case, we want the Thirst components, since we'll be changing those.
// In our case, we want the Thirst components, since we'll be changing those.
// Additionally, we want to pick up the DrinkAction components, as well as
// Additionally, we want to pick up the DrinkAction components, as well as
// their associated ActionState. Note that the DrinkAction belongs to a
// their associated ActionState. Note that the Drink
Action belongs to a
// *separate entity* from the owner of the Thirst component!
// *separate entity* from the owner of the Thirst component!
fn
drink_action_system
(
fn
drink_action_system
(
mut
thirsts
:
Query
<&
mut
Thirst
>
,
mut
thirsts
:
Query
<&
mut
Thirst
>
,
...
@@ -55,9 +58,9 @@ fn drink_action_system(
...
@@ -55,9 +58,9 @@ fn drink_action_system(
// usually happen because the target action changed (due to a different
// usually happen because the target action changed (due to a different
// Scorer winning). But you can also cancel the actions yourself by
// Scorer winning). But you can also cancel the actions yourself by
// setting the state in the Action system.
// setting the state in the Action system.
mut
query
:
Query
<
(
&
Parent
,
&
DrinkAction
,
&
mut
ActionState
)
>
,
mut
query
:
Query
<
(
&
Parent
,
&
mut
ActionState
)
,
With
<
Drink
>
>
,
)
{
)
{
for
(
Parent
(
actor
),
_drink_action
,
mut
state
)
in
query
.iter_mut
()
{
for
(
Parent
(
actor
),
mut
state
)
in
query
.iter_mut
()
{
// Use the drink_action's actor to look up the corresponding Thirst.
// Use the drink_action's actor to look up the corresponding Thirst.
if
let
Ok
(
mut
thirst
)
=
thirsts
.get_mut
(
*
actor
)
{
if
let
Ok
(
mut
thirst
)
=
thirsts
.get_mut
(
*
actor
)
{
match
*
state
{
match
*
state
{
...
@@ -84,21 +87,26 @@ fn drink_action_system(
...
@@ -84,21 +87,26 @@ fn drink_action_system(
// in the docs (later), but for now, just put them in there and trust the
// in the docs (later), but for now, just put them in there and trust the
// system. :)
// system. :)
#[derive(Debug,
Scorer)]
#[derive(Debug,
Scorer)]
pub
struct
Score
Thirst
;
pub
struct
Thirst
y
;
// Look familiar? Similar dance to Actions here.
// Look familiar? Similar dance to Actions here.
pub
fn
score_
thirst_system
(
pub
fn
thirst
y_scorer
_system
(
thirsts
:
Query
<&
Thirst
>
,
thirsts
:
Query
<&
Thirst
>
,
// Same dance with the Parent here, but now we've added a
Utility
!
// Same dance with the Parent here, but now we've added a
Score
!
mut
query
:
Query
<
(
&
Parent
,
&
mut
Score
),
With
<
Score
Thirst
>>
,
mut
query
:
Query
<
(
&
Parent
,
&
mut
Score
),
With
<
Thirst
y
>>
,
)
{
)
{
for
(
Parent
(
actor
),
mut
score
)
in
query
.iter_mut
()
{
for
(
Parent
(
actor
),
mut
score
)
in
query
.iter_mut
()
{
if
let
Ok
(
thirst
)
=
thirsts
.get
(
*
actor
)
{
if
let
Ok
(
thirst
)
=
thirsts
.get
(
*
actor
)
{
// This is really what the job of a Scorer is. To calculate
// This is really what the job of a Scorer is. To calculate
a
//
a
generic Utility value that the Big Brain engine will compare
// generic Utility value that the Big Brain engine will compare
// against others, over time, and use to make decisions. This is
// against others, over time, and use to make decisions. This is
// generally "the higher the better", and "first across the finish
// generally "the higher the better", and "first across the finish
// line", but that's all configurable using Pickers!
// line", but that's all configurable using Pickers!
//
// In a real-world application, you might want to do a fancier
// calculation here, possibly to clamp the value to a range, add a
// curve, etc. In our case, we'll just assume thirst goes from
// 0.0..100.0, to keep things simple.
*
score
=
Score
(
thirst
.thirst
);
*
score
=
Score
(
thirst
.thirst
);
}
}
}
}
...
@@ -126,9 +134,9 @@ pub fn init_entities(mut cmd: Commands) {
...
@@ -126,9 +134,9 @@ pub fn init_entities(mut cmd: Commands) {
(
(
picker: {"FirstToScore": (threshold: 80.0)},
picker: {"FirstToScore": (threshold: 80.0)},
choices: [(
choices: [(
when: {"
Score
Thirst": ()},
when: {"Thirst
y
": ()},
// This action will fire when (and as long as)
Score
Thirst scores >=80.0.
// This action will fire when (and as long as) Thirst
y
scores >=80.0.
then: {"Drink
Action
": ()},
then: {"Drink": ()},
)],
)],
)
)
"#
,
"#
,
...
@@ -143,7 +151,7 @@ fn main() {
...
@@ -143,7 +151,7 @@ fn main() {
.add_plugin
(
BigBrainPlugin
)
.add_plugin
(
BigBrainPlugin
)
.add_startup_system
(
init_entities
.system
())
.add_startup_system
(
init_entities
.system
())
.add_system
(
thirst_system
.system
())
.add_system
(
thirst_system
.system
())
.add_system
(
score_thirst_system
.system
())
.add_system
(
drink_action_system
.system
())
.add_system
(
drink_action_system
.system
())
.add_system
(
thirsty_scorer_system
.system
())
.run
();
.run
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment