Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use ggez::*;
- use crate::vector_2d::Vector2D;
- use crate::Drawable;
- use crate::Updatable;
- #[derive(Copy, Clone)]
- pub struct Friend {
- position: Vector2D,
- velocity: Vector2D,
- size: f32,
- }
- impl Friend {
- pub fn new(x: f32, y: f32, dx: f32, dy: f32) -> Self {
- Friend {
- position: Vector2D { x: x, y: y },
- velocity: Vector2D { x: dx, y: dy},
- size: 5.0,
- }
- }
- pub fn steer(&self, friends: &Vec<Self>) -> Vector2D {
- let position = self.position;
- let separate = self.separate(&friends);
- let align = self.align(&friends);
- let cohere = self.cohere(&friends);
- let mut steer = Vector2D::zero();
- steer += separate * 0.35;
- steer += align * 0.25;
- steer += cohere * 0.25;
- steer = steer.normalise();
- steer = steer * 2.0;
- (self.velocity * 0.9 + steer * 0.1).normalise()
- }
- pub fn separate(&self, friends: &Vec<Friend>) -> Vector2D {
- let mut steer = Vector2D::default();
- let mut count: u32 = 0;
- for friend in friends {
- if self.position.distance(friend.position) < 30.0 {
- steer += self.position - friend.position;
- count += 1;
- }
- }
- steer = steer.normalise();
- // if count > 0 {
- // steer /= count as f32;
- // }
- steer
- }
- pub fn align(&self, friends: &Vec<Friend>) -> Vector2D {
- let mut steer = Vector2D::default();
- let mut count: u32 = 0;
- for friend in friends {
- if self.position.distance(friend.position) < 50.0 {
- steer += friend.velocity;
- count += 1;
- }
- }
- steer = steer.normalise();
- // if count > 0 {
- // steer /= count as f32;
- // }
- steer
- }
- pub fn cohere(&self, friends: &Vec<Friend>) -> Vector2D {
- let mut steer = Vector2D::default();
- let mut count: u32 = 0;
- for friend in friends {
- if self.position.distance(friend.position) < 50.0 {
- steer += friend.position - self.position;
- count += 1;
- }
- }
- steer = steer.normalise();
- // if count > 0 {
- // steer /= count as f32;
- // }
- steer
- }
- }
- impl Default for Friend {
- fn default() -> Self {
- Friend {
- position: Vector2D::random(),
- velocity: Vector2D::random_unit(),
- size: 5.0
- }
- }
- }
- impl Updatable for Friend {
- fn update(&mut self, ctx: &mut Context, state: &mut crate::State) {
- self.velocity = self.steer(&state.friends);
- self.position += self.velocity;
- while self.position.x < 0.0 { self.position.x += crate::WIDTH; }
- while self.position.y < 0.0 { self.position.y += crate::HEIGHT; }
- while self.position.x > crate::WIDTH { self.position.x -= crate::WIDTH; }
- while self.position.y > crate::HEIGHT { self.position.y -= crate::HEIGHT; }
- }
- }
- impl Drawable for Friend {
- fn draw(&self, ctx: &mut Context) -> GameResult<()> {
- let position = self.position.to_array();
- let circle = graphics::Mesh::new_circle(
- ctx,
- graphics::DrawMode::fill(),
- position,
- self.size,
- 0.1,
- graphics::Color::from_rgb(255, 195, 18)
- )?;
- let line = graphics::Mesh::new_line(
- ctx,
- &[position, (self.position + self.velocity * 20.0).to_array()],
- 1.0,
- graphics::Color::from_rgb(255, 195, 18)
- )?;
- graphics::draw(ctx, &circle, graphics::DrawParam::default())?;
- graphics::draw(ctx, &line, graphics::DrawParam::default())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement