-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIYMainScene.m
125 lines (98 loc) · 3.05 KB
/
LIYMainScene.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// LIYMainScene.m
// Boids
//
// Created by Jason Cheladyn on 9/18/13.
// Copyright (c) 2013 Jason Cheladyn. All rights reserved.
//
#import "LIYMainScene.h"
@interface LIYMainScene()
@end
@implementation LIYMainScene
@synthesize _flockPointer;
@synthesize _currentTouch;
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
// self.physicsWorld.gravity = CGPointMake(0, 0);
// self.physicsWorld.contactDelegate = self;
[self setUp];
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
}
- (void)setUp
{
srandom(time(NULL));
CGRect screenSize = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenSize.size.width;
CGFloat screenHeight = screenSize.size.height;
self._currentTouch = CGPointZero;
CGSize boidSize = CGSizeMake(5, 5);
_flockPointer = [[LIYBoid alloc] init];
_flockPointer.color = [SKColor whiteColor];
_flockPointer.size = boidSize;
LIYBoid *previousBoid = _flockPointer;
LIYBoid *boid = _flockPointer;
float count = 80.0f;
for (int i = 0; i < count; i++) {
if (i != 0) {
boid = [[LIYBoid alloc] init];
boid.color = [SKColor whiteColor];
boid.size = boidSize;
previousBoid->_next = boid;
}
previousBoid = boid;
boid.doRotation = NO;
[boid setSpeedMax:1.2f withRandomRangeOf:0.15f andSteeringForceMax:1.0f withRandomRangeOf:0.15f];
[boid setWanderingRadius:16.0f lookAheadDistance:40.0f andMaxTurningAngle:0.2f];
[boid setEdgeBehavior:EDGE_WRAP];
// [boid setScale:0.5 + CCRANDOM_0_1()];
[boid setPos:ccp(CCRANDOM_MINUS1_1() * screenWidth, screenHeight/2)];
[self addChild:boid];
}
}
- (void)update:(NSTimeInterval)currentTime
{
[self tick];
}
- (void)tick
{
LIYBoid *boid = _flockPointer;
while (boid) {
LIYBoid *b = boid;
boid = b->_next;
[b wander:0.15f];
[b flock:_flockPointer
withSeparationWeight:0.1f
andAlignmentWight:0.1f
andCohesionWeight:0.2f
andSeparationDistance:10.0f
andAlignmentDistance:30.0f
andCoheasionDistance:20.0f];
if (CGPointEqualToPoint(_currentTouch, CGPointZero) == NO) {
[b flee:self._currentTouch panicAtDistance:65 usingMultiplier:0.25f];
}
[b update];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [[event allTouches] anyObject];
self._currentTouch = [touch locationInNode:self];
NSLog(@"Touch %f %f", self._currentTouch.x, self._currentTouch.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
self._currentTouch = [touch locationInNode:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self._currentTouch = CGPointZero;
}
@end