iPhone ドア アンロック

マークを合わせて、板をバラバラに壊すiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@import SpriteKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createDoorPanels];

    [self createLock];

    [self createCamera];

}

– (void)setupScene {

    SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];

    sv.backgroundColor = [self color:0];

    sv.scene = [SCNScene scene];

    sv.scene.physicsWorld.gravity = SCNVector3Zero;

    sv.autoenablesDefaultLighting = YES;

    [self.view addSubview:sv];

    self.sceneView = sv;

}

– (void)createDoorPanels {

    

    float w = 4;

    for (int i=0; i<36; i++) {

        SCNBox *panel = [SCNBox boxWithWidth:w height:w length:0.4 chamferRadius:0];

        panel.firstMaterial.diffuse.contents = [self color:1];

        panel.firstMaterial.specular.contents = [self color:2];

        

        int row = (i % 6);

        int col = (i / 6);

        float x = w * row – w * 2.5;

        float y = w * col – w * 2.5;

        SCNNode *panelNode = [SCNNode nodeWithGeometry:panel];

        panelNode.name = @”panel”;

        panelNode.position = SCNVector3Make(x, y, 0);

        panelNode.physicsBody = [SCNPhysicsBody dynamicBody];

        [self.sceneView.scene.rootNode addChildNode:panelNode];

    }

}

– (void)createLock {

    SKScene *s = [SKScene sceneWithSize:self.view.bounds.size];

    self.sceneView.overlaySKScene = s;

    

    UIColor *color =[[UIColor orangeColor] colorWithAlphaComponent:0.6];

    SKShapeNode *ring = [SKShapeNode shapeNodeWithCircleOfRadius:50];

    ring.name = @”ring”;

    ring.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

    ring.fillColor = [UIColor clearColor];

    ring.lineWidth = 10;

    ring.strokeColor = color;

    [s addChild:ring];

    SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(20, 10)];

    bar.position = CGPointMake(36, 0);

    [ring addChild:bar];

    

    SKSpriteNode *line = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(4, 140)];

    line.position = ring.position;

    [s addChild:line];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    SKNode *lock = [self.sceneView.overlaySKScene childNodeWithName:@”ring”];

    [lock runAction:[SKAction rotateByAngle:M_PI/2.0 duration:3.0] completion:^{

        

        [self.sceneView.scene.rootNode enumerateChildNodesUsingBlock:^(SCNNode *child, BOOL *stop) {

            if ([child.name isEqual:@”panel”]) {

                int direct =  arc4random() % 4;

                switch (direct) {

                    case 0: [child.physicsBody applyForce:SCNVector3Make(10, 0, 0) impulse:YES];

                    case 1: [child.physicsBody applyForce:SCNVector3Make(0, 10, 0) impulse:YES];

                    case 2: [child.physicsBody applyForce:SCNVector3Make(-10, 0, 0) impulse:YES];

                    case 3: [child.physicsBody applyForce:SCNVector3Make(10, –10, 0) impulse:YES];

                }

            }

        }];

        

    }];

    

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 0, 40);

    [self.sceneView.scene.rootNode addChildNode:camera];

}

#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000)>>16)/255.0 green:((rgb & 0xFF00)>>8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]

– (UIColor *)color:(int)i {

    switch (i) {

        case 0: return ColorHex(0x6F91F2);

        case 1: return ColorHex(0x0F1841);

        case 2: return ColorHex(0x6F91F2);

        case 3: return ColorHex(0x0E0E0E);

        case 4: return ColorHex(0x0E0E0E);

    }

    return nil;

}

@end