summaryrefslogtreecommitdiffhomepage
path: root/lib/02-apply_boid_calculations.rb
blob: 5b31b8ce7bc26fdd31490adf452a5b7845f9d84d (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

FF::Scn::BoidCalculations.add(FF::Sys.new('ApplyBoidCalculations', priority: 75) do
  @center ||= Camera::Circle.new(color: [0.25, 0.0, 0.5, 0.5],
                                 radius: 10,
                                 sectors: 10,
                                 z: -49)
  @center_vel ||= Camera::Line.new(color: [0.25, 0.0, 0.5, 0.5],
                                   width: 12,
                                   z: -50)
  unless $config.debug
    @center.remove
    @center_vel.remove
  end

  group_velocity = [0.0, 0.0]
  center_mass = [0.0, 0.0]
  boids_count = FF::Cmp::Boids.each.to_a.count

  FF::Cmp::Boids.each do |boid|
    boid.vx += boid.cx
    boid.vy += boid.cy
    FF::Sys::Limit.call
    boid.x += boid.vx
    boid.y += boid.vy

    if boid.vx.negative? && !boid.flipped
      boid.flipped = true
      spr = boid.entities.first.components[FF::Cmp::BoidVisuals].first.obj
      spr.width = -spr.width.abs
      # flip
    elsif boid.vx.positive? && boid.flipped
      # unflip
      boid.flipped = false
      spr = boid.entities.first.components[FF::Cmp::BoidVisuals].first.obj
      spr.width = spr.width.abs
    end

    boid.cx = 0.0
    boid.cy = 0.0

    center_mass[0] += boid.x
    center_mass[1] += boid.y
    group_velocity[0] += boid.vx
    group_velocity[1] += boid.vy
  end

  group_velocity = [0.0, 0.0]
  center_mass = [0.0, 0.0]
  boids_count = FF::Cmp::Fish[0].entities.count

  FF::Cmp::Fish[0].entities.each do |ent|
    boid = ent.components[FF::Cmp::Boids].first

    center_mass[0] += boid.x
    center_mass[1] += boid.y
    group_velocity[0] += boid.vx
    group_velocity[1] += boid.vy
  end

  @center_vel.x1 = @center.x = (center_mass[0] / boids_count)
  @center_vel.y1 = @center.y = (center_mass[1] / boids_count)

  @center_vel.x1 += @center.radius
  @center_vel.y1 += @center.radius

  if $follow
    Camera.x = @center_vel.x1 = @center.x + @center.radius
    Camera.y = @center_vel.y1 = @center.y + @center.radius
  end
  @center_vel.x2 = @center_vel.x1 + ((group_velocity[0] / boids_count) * 10.0)
  @center_vel.y2 = @center_vel.y1 + ((group_velocity[1] / boids_count) * 10.0)
end)