/
BoxesScene.m
171 lines (152 loc) · 6.9 KB
/
BoxesScene.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#import "BoxesScene.h"
#import <CoreMotion/CoreMotion.h>
#import "chipmunk.h"
#import "SKAssignableSpriteNode.h"
#import "SKTexture+Extras.h"
@implementation BoxesScene {
cpSpace *_chipmunkSpace;
CMMotionManager *_motionManager;
BOOL _circles;
}
- (instancetype)initWithSize:(CGSize)size image:(UIImage *)image circles:(BOOL)circles;
{
if (!(self = [super initWithSize:size])) { return nil; }
self.image = image;
_circles = circles;
_motionManager = CMMotionManager.new;
_motionManager.accelerometerUpdateInterval = 1.f / 60.f;
[_motionManager startAccelerometerUpdates];
return self;
}
- (void)didMoveToView:(SKView *)view;
{
self.view.showsFPS = YES;
self.view.showsNodeCount = YES;
self.view.ignoresSiblingOrder = YES;
[self createPhysicsWorld];
[self createSceneContents];
self.backgroundColor = UIColor.blackColor;
}
- (void)createPhysicsWorld;
{
_chipmunkSpace = cpSpaceNew();
cpFloat wallWidth = 100.f;
cpVect groundCorners[] = {
cpv(-wallWidth, -wallWidth),
cpv(self.view.bounds.size.width + wallWidth, -wallWidth),
cpv(self.view.bounds.size.width + wallWidth, self.view.bounds.size.height + wallWidth),
cpv(-wallWidth, self.view.bounds.size.height + wallWidth),
cpv(-wallWidth, -wallWidth)};
for (int i = 0; i < 4; i++) {
cpShape *shape = cpSegmentShapeNew(cpSpaceGetStaticBody(_chipmunkSpace),
groundCorners[i], groundCorners[i + 1], wallWidth + 16);
cpShapeSetFriction(shape, 1);
cpSpaceAddShape(_chipmunkSpace, shape);
}
}
- (void)createSceneContents;
{
CGFloat verticalOffset = 94;
CGFloat horizontalMargin = 16;
CGImageRef image = self.image.CGImage;
size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image);
size_t components = CGImageGetBitsPerPixel(image) / CGImageGetBitsPerComponent(image);
CFDataRef rawData = CGDataProviderCopyData(CGImageGetDataProvider(image));
UInt8 *imageBuffer = (UInt8 *)CFDataGetBytePtr(rawData);
CGFloat boxWidth = (self.size.width - horizontalMargin * 2) / width;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
NSInteger index = (x + (width * y)) * components;
UInt8 pixelComponentB = imageBuffer[index];
UInt8 pixelComponentG = imageBuffer[index + 1];
UInt8 pixelComponentR = imageBuffer[index + 2];
UIColor *color = [UIColor colorWithRed:pixelComponentR / 255.f green:pixelComponentG / 255.f blue:pixelComponentB / 255.f alpha:1];
CGSize boxSize = CGSizeMake(boxWidth, boxWidth);
SKAssignableSpriteNode *spriteNode = [SKAssignableSpriteNode shapeWithColor:color
size:boxSize
circular:_circles];
spriteNode.pixelXPosition = x;
spriteNode.pixelYPosition = y;
spriteNode.position = CGPointMake((x * boxWidth) + 16,
self.view.bounds.size.height - verticalOffset - (y * boxWidth));
spriteNode.mass = (pixelComponentG / 25.f) + 1;
[self addChild:spriteNode];
cpFloat moment;
cpBody *body;
cpShape *shape;
if (_circles) {
moment = cpMomentForCircle(spriteNode.mass, boxSize.width / 2.f, boxSize.height / 2.f, cpvzero);
body = cpBodyNew(spriteNode.mass, moment);
shape = cpCircleShapeNew(body, boxSize.width / 2.f, cpvzero);
} else {
moment = cpMomentForBox(spriteNode.mass, boxSize.width, boxSize.height);
body = cpBodyNew(spriteNode.mass, moment);
shape = cpBoxShapeNew(body, boxSize.width, boxSize.height, 0.f);
}
cpShapeSetFriction(shape, 0.5);
cpShapeSetElasticity(shape, 0.5);
cpBodySetPosition(body, spriteNode.position);
cpSpaceAddBody(_chipmunkSpace, body);
cpSpaceAddShape(_chipmunkSpace, shape);
cpBodySetUserData(body, (__bridge cpDataPointer)spriteNode);
spriteNode.body = body;
}
}
CFRelease(rawData);
}
- (void)update:(NSTimeInterval)currentTime;
{
CGImageRef image = self.image.CGImage;
CGDataProviderRef dataProvider = CGImageGetDataProvider(image);
CFDataRef rawData = CGDataProviderCopyData(dataProvider);
const UInt8 *buffer = CFDataGetBytePtr(rawData);
size_t bytesPerRow = CGImageGetBytesPerRow(image);
size_t components = CGImageGetBitsPerPixel(image) / CGImageGetBitsPerComponent(image);
cpSpaceSetGravity(_chipmunkSpace, cpv(self.platformSpecificAcceleration.x * 100,
self.platformSpecificAcceleration.y * 100));
cpSpaceStep(_chipmunkSpace, 1.f / 30.f);
BOOL useCircles = _circles;
BOOL liveColor = self.liveColor;
cpSpaceEachBody_b(_chipmunkSpace, ^(cpBody *body) {
SKAssignableSpriteNode *node = (__bridge SKAssignableSpriteNode *)cpBodyGetUserData(body);
node.position = cpBodyGetPosition(body);
node.zRotation = cpBodyGetAngle(body);
if (liveColor) {
size_t spritePixelX = node.pixelXPosition;
size_t spritePixelY = node.pixelYPosition;
size_t pixelIndex = (bytesPerRow * spritePixelY) + spritePixelX * components;
UInt8 pixelComponent = buffer[pixelIndex];
UInt8 pixelComponent2 = buffer[pixelIndex + 1];
UInt8 pixelComponent3 = buffer[pixelIndex + 2];
UIColor *color = [UIColor colorWithRed:pixelComponent3 / 255.f
green:pixelComponent2 / 255.f
blue:pixelComponent / 255.f alpha:1];
if (useCircles) {
node.texture = [SKTexture circleTextureWithColor:color size:node.size];
} else {
node.color = color;
}
}
});
CFRelease(rawData);
}
- (CMAcceleration)platformSpecificAcceleration;
{
#if (TARGET_OS_SIMULATOR)
return (CMAcceleration){
UIDevice.currentDevice.orientation == UIDeviceOrientationLandscapeLeft ? -1.f :
UIDevice.currentDevice.orientation == UIDeviceOrientationLandscapeRight ? 1.f : 0.f,
UIDevice.currentDevice.orientation == UIDeviceOrientationPortrait ? -1.f :
UIDevice.currentDevice.orientation == UIDeviceOrientationPortraitUpsideDown ? 1.f : 0.f, 0.f};
#endif
return _motionManager.accelerometerData.acceleration;
}
- (void)dealloc;
{
[_motionManager stopAccelerometerUpdates];
cpSpaceEachShape_b(_chipmunkSpace, ^(cpShape *shape) { cpShapeFree(shape); });
cpSpaceEachBody_b(_chipmunkSpace, ^(cpBody *body) { cpBodyFree(body); });
cpSpaceFree(_chipmunkSpace);
}
@end