summaryrefslogtreecommitdiffhomepage
path: root/app/systems/rules/cohesion.rb
blob: 7e339613b26f6c255dc5ddb0327c2a1f2cf1fd57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
FF::Scn::BoidRules.add(
  FF::Sys.new('BoidsCohesion', priority: 50) do
    center_mass = [0.0, 0.0]
    boids_count = FF::Cmp::BoidsCohesion.data.count

    FF::Cmp::BoidsCohesion.each do |cohesion|
      boid = cohesion.entities[0].components[FF::Cmp::Boid][0]
      center_mass[0] += boid.x
      center_mass[1] += boid.y
      #puts boid.x
      #puts boid.y
    end
    
    #puts center_mass
    FF::Cmp::BoidsCohesion.each do |cohesion|
      boid_update = cohesion.entities[0].components[FF::Cmp::Boid][0]
      move_boid = center_mass.dup
      move_boid[0] -= boid_update.x
      move_boid[1] -= boid_update.y
      move_boid[0] /= boids_count - 1.0
      move_boid[1] /= boids_count - 1.0

      boid_update.cx += (move_boid[0] - boid_update.x) / cohesion.strength.to_i
      boid_update.cy += (move_boid[1] - boid_update.y) / cohesion.strength.to_i
    end
  end
)