Newer
Older
pub struct Score(pub(crate) f32);
impl Score {
pub fn set(&mut self, value: f32) {
if !(0.0..=100.0).contains(&value) {
panic!("Score value must be between 0.0 and 100.0");
}
self.0 = value;
}
}
This trait defines new Scorers. In general, you should use the [derive macro](derive.Scorer.html) instead.
pub trait Scorer: std::fmt::Debug + Sync + Send {
fn build(&self, entity: Entity, cmd: &mut Commands) -> ScorerEnt;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#[derive(Debug)]
pub struct FixedScore(f32);
pub fn fixed_score_system(mut query: Query<(&FixedScore, &mut Score)>) {
for (FixedScore(fixed), mut score) in query.iter_mut() {
score.set(*fixed);
}
}
mod fixed_score {
use super::*;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct FixedScore(f32);
#[typetag::deserialize]
impl Scorer for FixedScore {
fn build(&self, actor: Entity, cmd: &mut Commands) -> ScorerEnt {
let ent = ScorerEnt(cmd.spawn().id());
cmd.entity(ent.0)
.insert(Score::default())
.insert(super::FixedScore(self.0));
cmd.entity(actor).push_children(&[ent.0]);
ent
}
}
}
#[derive(Debug)]
pub struct AllOrNothing {
threshold: f32,
scorers: Vec<ScorerEnt>,
}
pub fn all_or_nothing_system(query: Query<(Entity, &AllOrNothing)>, mut scores: Query<&mut Score>) {
for (
aon_ent,
AllOrNothing {
threshold,
scorers: children,
},
) in query.iter()
{
let mut sum = 0.0;
for ScorerEnt(child) in children.iter() {
let score = scores.get_mut(*child).expect("where is it?");
if score.0 < *threshold {
sum = 0.0;
break;
} else {
sum += score.0;
}
}
let mut score = scores.get_mut(aon_ent).expect("where did it go?");
score.set(crate::evaluators::clamp(sum, 0.0, 100.0));
}
}
mod all_or_nothing {
use super::*;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct AllOrNothing {
threshold: f32,
scorers: Vec<Box<dyn Scorer>>,
}
#[typetag::deserialize]
impl Scorer for AllOrNothing {
fn build(&self, actor: Entity, cmd: &mut Commands) -> ScorerEnt {
let ent = ScorerEnt(cmd.spawn().id());
let scorers: Vec<_> = self
.scorers
.iter()
.map(|scorer| scorer.build(actor, cmd).0)
.collect();
cmd.entity(ent.0)
.insert(Score::default())
.insert(super::AllOrNothing {
threshold: self.threshold,
scorers: scorers.into_iter().map(ScorerEnt).collect(),
});
cmd.entity(actor).push_children(&[ent.0]);
ent
}
}
}
#[derive(Debug)]
pub struct SumOfScorers {
threshold: f32,
scorers: Vec<ScorerEnt>,
}
pub fn sum_of_scorers_system(query: Query<(Entity, &SumOfScorers)>, mut scores: Query<&mut Score>) {
for (
sos_ent,
SumOfScorers {
threshold,
scorers: children,
},
) in query.iter()
{
let mut sum = 0.0;
for ScorerEnt(child) in children.iter() {
let score = scores.get_mut(*child).expect("where is it?");
sum += score.0;
}
if sum < *threshold {
sum = 0.0;
}
let mut score = scores.get_mut(sos_ent).expect("where did it go?");
score.set(crate::evaluators::clamp(sum, 0.0, 100.0));
}
}
mod sum_of_scorers {
use super::*;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct SumOfScorers {
threshold: f32,
scorers: Vec<Box<dyn Scorer>>,
}
#[typetag::deserialize]
impl Scorer for SumOfScorers {
fn build(&self, actor: Entity, cmd: &mut Commands) -> ScorerEnt {
let ent = ScorerEnt(cmd.spawn().id());
let scorers: Vec<_> = self
.scorers
.iter()
.map(|scorer| scorer.build(actor, cmd).0)
.collect();
cmd.entity(ent.0)
.insert(Score::default())
.insert(super::AllOrNothing {
threshold: self.threshold,
scorers: scorers.into_iter().map(ScorerEnt).collect(),
});
cmd.entity(actor).push_children(&[ent.0]);
ent
}
}
}