iPhoneドミノ3

ドミノを倒して旗を立てるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [self color:1];

    [self setupScene];

    [self createBar];

    [self createFlag];

    [self createDomino];

    [self createCamera];

}

– (void)setupScene {

    float w = CGRectGetMaxX(self.view.bounds);

    SCNView *sv = [[SCNView alloc] initWithFrame:CGRectMake(0, 0, w, w)];

    sv.backgroundColor = [self color:0];

    sv.scene = [SCNScene scene];

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createBar {

    SCNBox *box = [SCNBox boxWithWidth:30 height:2 length:10 chamferRadius:0];

    box.firstMaterial.diffuse.contents = [self color:2];

    SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

    boxNode.position = SCNVector3Make(15, 10, 0);

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

    boxNode.physicsBody = [SCNPhysicsBody staticBody];

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

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

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

}

– (void)createFlag {

    

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

        SCNBox *box = nil;

        SCNNode *boxNode = nil;

        if (i < 2) {

            box = [SCNBox boxWithWidth:25 height:5 length:10 chamferRadius:0];

            boxNode = [SCNNode nodeWithGeometry:box];

            boxNode.position = SCNVector3Make(1030 * i, –20, 0);

        } else {

            box = [SCNBox boxWithWidth:50 height:1 length:8 chamferRadius:0];

            boxNode = [SCNNode nodeWithGeometry:box];

            boxNode.position = SCNVector3Make(-5, –24, 0);

        }

        box.firstMaterial.diffuse.contents = [self color:3];

        boxNode.physicsBody = [SCNPhysicsBody staticBody];

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

    }

    

    SCNCylinder *pole = [SCNCylinder cylinderWithRadius:0.5 height:20];

    pole.firstMaterial.diffuse.contents = [self color:4];

    SCNNode *poleNode = [SCNNode nodeWithGeometry:pole];

    poleNode.position = SCNVector3Make(-13, –15, 0);

    poleNode.rotation = SCNVector4Make(0, 0, 1, M_PI/2.0);

    poleNode.physicsBody = [SCNPhysicsBody dynamicBody];

    poleNode.physicsBody.velocityFactor = SCNVector3Make(1, 1, 0);

    poleNode.physicsBody.angularVelocityFactor = SCNVector3Make(0, 0, 1);

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

    

    SCNBox *flag = [SCNBox boxWithWidth:5 height:5 length:0.2 chamferRadius:0];

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

    SCNNode *flagNode = [SCNNode nodeWithGeometry:flag];

    flagNode.position = SCNVector3Make(2.5, 7.5, 0);

    [poleNode addChildNode:flagNode];

}

– (void)createDomino {

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

        SCNBox *domino = [SCNBox boxWithWidth:1.5 height:6.4 length:4 chamferRadius:0];

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

        SCNNode *dominoNode = [SCNNode nodeWithGeometry:domino];

        dominoNode.name = [NSString stringWithFormat:@”domino%d”, i];

        dominoNode.position = SCNVector3Make(i * 5 + 3, 15, 0);

        dominoNode.physicsBody = [SCNPhysicsBody dynamicBody];

        dominoNode.physicsBody.mass = 40.0;

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

    }

}

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

{

    SCNNode *domino = [self.sceneView.scene.rootNode childNodeWithName:@”domino5″ recursively:NO];

    [domino.physicsBody applyForce:SCNVector3Make(-2, 0, 0) atPosition:SCNVector3Make(0, 100, 0) impulse:YES];

}

#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(0x054C51);

        case 1: return ColorHex(0x229C89);

        case 2: return ColorHex(0xC21800);

        case 3: return ColorHex(0xFF3C20);

        case 4: return ColorHex(0xFFCC7C);

    }

    return nil;

}

@end