Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.86 KB | None | 0 0
  1. use ggez::*;
  2.  
  3. use crate::vector_2d::Vector2D;
  4.  
  5. use crate::Drawable;
  6. use crate::Updatable;
  7.  
  8. #[derive(Copy, Clone)]
  9. pub struct Friend {
  10.     position: Vector2D,
  11.     velocity: Vector2D,
  12.     size: f32,
  13. }
  14.  
  15. impl Friend {
  16.     pub fn new(x: f32, y: f32, dx: f32, dy: f32) -> Self {
  17.         Friend {
  18.             position: Vector2D { x:  x, y:  y },
  19.             velocity: Vector2D { x: dx, y: dy},
  20.             size: 5.0,
  21.         }
  22.     }
  23.  
  24.     pub fn steer(&self, friends: &Vec<Self>) -> Vector2D {
  25.         let position = self.position;
  26.         let separate = self.separate(&friends);
  27.         let align = self.align(&friends);
  28.         let cohere = self.cohere(&friends);
  29.  
  30.         let mut steer = Vector2D::zero();
  31.         steer += separate * 0.35;
  32.         steer += align * 0.25;
  33.         steer += cohere * 0.25;
  34.         steer = steer.normalise();
  35.         steer = steer * 2.0;
  36.  
  37.         (self.velocity * 0.9 + steer * 0.1).normalise()
  38.     }
  39.  
  40.     pub fn separate(&self, friends: &Vec<Friend>) -> Vector2D {
  41.         let mut steer = Vector2D::default();
  42.         let mut count: u32 = 0;
  43.  
  44.         for friend in friends {
  45.             if self.position.distance(friend.position) < 30.0 {
  46.                 steer += self.position - friend.position;
  47.                 count += 1;
  48.             }
  49.         }
  50.  
  51.         steer = steer.normalise();
  52.         // if count > 0 {
  53.         //     steer /= count as f32;
  54.         // }
  55.  
  56.         steer
  57.     }
  58.  
  59.     pub fn align(&self, friends: &Vec<Friend>) -> Vector2D {
  60.         let mut steer = Vector2D::default();
  61.         let mut count: u32 = 0;
  62.  
  63.         for friend in friends {
  64.             if self.position.distance(friend.position) < 50.0 {
  65.                 steer += friend.velocity;
  66.                 count += 1;
  67.             }
  68.         }
  69.  
  70.         steer = steer.normalise();
  71.         // if count > 0 {
  72.         //     steer /= count as f32;
  73.         // }
  74.  
  75.         steer
  76.     }
  77.  
  78.     pub fn cohere(&self, friends: &Vec<Friend>) -> Vector2D {
  79.         let mut steer = Vector2D::default();
  80.         let mut count: u32 = 0;
  81.  
  82.         for friend in friends {
  83.             if self.position.distance(friend.position) < 50.0 {
  84.                 steer += friend.position - self.position;
  85.                 count += 1;
  86.             }
  87.         }
  88.  
  89.         steer = steer.normalise();
  90.         // if count > 0 {
  91.         //     steer /= count as f32;
  92.         // }
  93.  
  94.         steer
  95.     }
  96. }
  97.  
  98. impl Default for Friend {
  99.     fn default() -> Self {
  100.         Friend {
  101.             position: Vector2D::random(),
  102.             velocity: Vector2D::random_unit(),
  103.             size: 5.0
  104.         }
  105.     }
  106. }
  107.  
  108. impl Updatable for Friend {  
  109.     fn update(&mut self, ctx: &mut Context, state: &mut crate::State) {
  110.         self.velocity = self.steer(&state.friends);
  111.         self.position += self.velocity;
  112.  
  113.         while self.position.x < 0.0 { self.position.x += crate::WIDTH; }
  114.         while self.position.y < 0.0 { self.position.y += crate::HEIGHT; }
  115.  
  116.         while self.position.x > crate::WIDTH { self.position.x -= crate::WIDTH; }
  117.         while self.position.y > crate::HEIGHT { self.position.y -= crate::HEIGHT; }
  118.     }
  119. }
  120.  
  121. impl Drawable for Friend {
  122.     fn draw(&self, ctx: &mut Context) -> GameResult<()> {
  123.         let position = self.position.to_array();
  124.  
  125.         let circle = graphics::Mesh::new_circle(
  126.             ctx,
  127.             graphics::DrawMode::fill(),
  128.             position,
  129.             self.size,
  130.             0.1,
  131.             graphics::Color::from_rgb(255, 195, 18)
  132.         )?;
  133.  
  134.         let line = graphics::Mesh::new_line(
  135.             ctx,
  136.             &[position, (self.position + self.velocity * 20.0).to_array()],
  137.             1.0,
  138.             graphics::Color::from_rgb(255, 195, 18)
  139.         )?;
  140.  
  141.         graphics::draw(ctx, &circle, graphics::DrawParam::default())?;
  142.         graphics::draw(ctx, &line, graphics::DrawParam::default())
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement