test + cleanup agent direction code
This commit is contained in:
parent
8dd01ab105
commit
a60847ad6f
21
src/agent.rs
21
src/agent.rs
@ -67,20 +67,19 @@ impl Agent {
|
||||
let right = buf.get_buf(xr, yr);
|
||||
|
||||
// Rotate and move logic
|
||||
let mut rng = rand::thread_rng();
|
||||
let mut direction: f32 = 0.0;
|
||||
|
||||
if (center > left) && (center > right) {
|
||||
direction = 0.0;
|
||||
let direction = if (center > left) && (center > right) {
|
||||
0.0
|
||||
} else if (center < left) && (center < right) {
|
||||
direction = *[-1.0, 1.0]
|
||||
.choose(&mut rng)
|
||||
.expect("unable to choose random direction");
|
||||
*[-1.0, 1.0]
|
||||
.choose(&mut rand::thread_rng())
|
||||
.expect("unable to choose random direction")
|
||||
} else if left < right {
|
||||
direction = 1.0;
|
||||
1.0
|
||||
} else if right < left {
|
||||
direction = -1.0;
|
||||
}
|
||||
-1.0
|
||||
} else {
|
||||
0.0
|
||||
};
|
||||
|
||||
let delta_angle = rotation_angle * direction;
|
||||
|
||||
|
||||
12
src/util.rs
12
src/util.rs
@ -11,3 +11,15 @@ pub const fn index(width: usize, height: usize, x: f32, y: f32) -> usize {
|
||||
let j = (y + height as f32) as usize & (height - 1);
|
||||
j * width + i
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn wrap_test() {
|
||||
assert_eq!(wrap(1.1, 1.0), 0.100000024);
|
||||
assert_eq!(wrap(0.5, 1.0), 0.5);
|
||||
assert_eq!(wrap(-1.0, 2.0), 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user