-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flock.m
54 lines (45 loc) · 838 Bytes
/
Flock.m
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
//
// Flock.m
// Boids
//
// Created by David Nolen on 11/3/09.
// Copyright 2009 David Nolen. All rights reserved.
//
#import "Flock.h"
#import "Boid.h"
#import "Vector2D.h"
@implementation Flock
- (Flock*) init
{
return [self initWithCount:BOID_COUNT];
}
- (Flock*) initWithCount:(int)n
{
self = [super init];
if(nil != self)
{
boids = [NSMutableArray new];
for(int i = 0; i < n; i++)
{
[boids addObject:[[Boid alloc] init]];
}
}
return self;
}
- (void) update
{
// could optimize the following by calculating distances upfront
for(Boid *b in boids)
{
Vector2D *sep = [b separation:boids];
Vector2D *ali = [b alignment:boids];
Vector2D *coh = [b cohesion:boids];
[[[b->acc add:sep] add:ali] add:coh];
[b update];
}
}
- (NSArray*) boids
{
return boids;
}
@end