This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding `ClosestObstacleSystem` to tag any creatures near walls w/ Closest<Obstacle>. SeekSystem<Obstacle> will help the creature avoid that obstacle. * Adjusted SeekSystem behavior to be a little more configurable. - SeekSystem<Obstacle> now tries to follow the wall rather than run away. - Adjusted how close the creature can get to the wall.
- Loading branch information
Showing
4 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod decision; | ||
pub mod obstacle; | ||
pub mod wander; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use amethyst::core::nalgebra::Vector3; | ||
use amethyst::{ | ||
core::Transform, | ||
ecs::{join::Join, Entities, Read, ReadStorage, System, WriteStorage}, | ||
}; | ||
|
||
use std::cmp::Ordering; | ||
|
||
use crate::components::creatures::Movement; | ||
use crate::resources::world_bounds::WorldBounds; | ||
use crate::systems::behaviors::decision::Closest; | ||
|
||
#[derive(Default)] | ||
pub struct Obstacle; | ||
|
||
/// Determine the closest bounding wall based on a location | ||
fn closest_wall(location: &Vector3<f32>, bounds: &WorldBounds) -> Vector3<f32> { | ||
let mut bounds_left = location.clone(); | ||
bounds_left.x = bounds.left; | ||
let mut bounds_right = location.clone(); | ||
bounds_right.x = bounds.right; | ||
let mut bounds_top = location.clone(); | ||
bounds_top.y = bounds.top; | ||
let mut bounds_bottom = location.clone(); | ||
bounds_bottom.y = bounds.bottom; | ||
|
||
// Iterates through each bound | ||
[bounds_left, bounds_right, bounds_top, bounds_bottom] | ||
.iter() | ||
// Calculates the distance between the wall & the location | ||
.map(|v| v - location) | ||
// Returns the minimum distance | ||
.min_by(|a, b| { | ||
if a.magnitude_squared() < b.magnitude_squared() { | ||
Ordering::Less | ||
} else { | ||
Ordering::Greater | ||
} | ||
}) | ||
.unwrap() | ||
} | ||
|
||
pub struct ClosestObstacleSystem; | ||
impl<'s> System<'s> for ClosestObstacleSystem { | ||
type SystemData = ( | ||
Entities<'s>, | ||
ReadStorage<'s, Transform>, | ||
ReadStorage<'s, Movement>, | ||
Read<'s, WorldBounds>, | ||
WriteStorage<'s, Closest<Obstacle>>, | ||
); | ||
|
||
fn run( | ||
&mut self, | ||
(entities, transforms, movements, world_bounds, mut closest_obstacle): Self::SystemData, | ||
) { | ||
// Right now the only obstacles are the world bound walls, so it's | ||
// safe to clear this out. | ||
closest_obstacle.clear(); | ||
|
||
for (entity, transform, _) in (&entities, &transforms, &movements).join() { | ||
// Find the closest wall to this entity | ||
let wall_dir = closest_wall(&transform.translation(), &world_bounds); | ||
if wall_dir.magnitude_squared() < 3.0f32.powi(2) { | ||
closest_obstacle | ||
.insert(entity, Closest::<Obstacle>::new(wall_dir)) | ||
.expect("Unable to add obstacle to entity"); | ||
} | ||
} | ||
} | ||
} |