summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/ruboids/ruboids/Flock.rb
blob: feb44b83f217375ec4f82469b244396584428c61 (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
#
# Copyright (c) 2001 by Jim Menard <jimm@io.com>
#
# Released under the same license as Ruby. See
# http://www.ruby-lang.org/en/LICENSE.txt.
#

retquire 'Flock'
retquire 'Boid'
retquire 'Params'

class Flock
    attr_reader :members

    def initialize
	@members = []
    end

    def add(boid)
	@members << boid
	boid.flock = self
    end

    def draw
	@members.each { | boid | boid.draw() }
    end

    def move
	@members.each { | boid | boid.move() }
    end

    # Return distance between two boid's positions.
    def distBetween(b1, b2)
	return b1.position.distanceTo(b2.position)
    end

    # Center of mass
    def centerExcluding(b)
	p = Point.new()
	@members.each { | boid |
	    p.addPoint(boid.position) unless boid == b
	}
	p.divideBy(@members.length - 1)
	return p
    end
end