-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_boidcollection.py
83 lines (67 loc) · 2.1 KB
/
_boidcollection.py
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
74
75
76
77
78
79
80
81
82
83
from _boid import Boid, get_displacement
class BoidCollection:
def __init__(self,*args,**kwargs):
'''
Collection of Boids.
:param args:
:param kwargs:
'''
self.boids = set()
self._init_displacements()
def _init_displacements(self):
'''
Creates blank displacements container
'''
self.displacements = {boid:{} for boid in self.boids}
self.displacements_up_to_date = False
def add(self,number=None,**kwargs):
'''
add(): Adds a boid, passing kwargs to boid constructor
add(number): Adds number of boids, passing kwargs to boid constructor
:returns: list of boids created
'''
if not kwargs.get('collection'):
kwargs['collection'] = self
if not number:
number = 1
newboids = []
for i in range(number):
b = Boid(**kwargs)
self.boids.add(b)
newboids.append(b)
self._init_displacements()
return newboids
def remove(self,boid):
'''
Removes boid from collection
'''
self.boids.remove(boid)
self._init_displacements()
def _update_displacements(self):
'''
Updates displacements between boids (only needs calling if displacements are not up to date)
'''
for boid_1 in self.boids:
for boid_2 in self.boids:
if boid_1 != boid_2:
self.displacements[boid_1][boid_2] = get_displacement(boid_1,boid_2)
self.displacements_up_to_date = True
def get_displacements(self):
if not self.displacements_up_to_date:
self._update_displacements()
return self.displacements
def tick(self):
'''
Ticks every boid
'''
for boid in self.boids:
boid.decide_movement_strategy()
for boid in self.boids:
boid.tick()
self.displacements_up_to_date = False
def draw(self):
'''
Draws every boid
'''
for boid in self.boids:
boid.draw()