Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
othello board
//
// ViewController.m
// OthelloBoard
//
// Created by MizushimaYusuke on 2017/10/06.
// Copyright © 2017 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBoard];
}
- (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor colorWithHue:0.3 saturation:0.9 brightness:0.6 alpha:1];
sv.scene = [SCNScene scene];
sv.allowsCameraControl = YES;
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
- (void)createBoard {
for (int i=0; i<8; i++) {
for (int j=0; j<8; j++) {
SCNNode *n = [self createOthello];
n.position = SCNVector3Make(2.2 * i, 0, 2.2 * j);
}
}
}
- (SCNNode *)createOthello {
SCNNode *disk = [SCNNode node];
disk.name = @"disk";
[self.sceneView.scene.rootNode addChildNode:disk];
SCNCylinder *black = [SCNCylinder cylinderWithRadius:1 height:0.1];
black.firstMaterial.diffuse.contents = [UIColor colorWithWhite:0.1 alpha:1];
SCNCylinder *white = [SCNCylinder cylinderWithRadius:1 height:0.1];
white.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *bnode = [SCNNode nodeWithGeometry: black];
bnode.position = SCNVector3Make(0, 0.05, 0);
[disk addChildNode:bnode];
SCNNode *wnode = [SCNNode nodeWithGeometry: white];
wnode.position = SCNVector3Make(0, -0.05, 0);
[disk addChildNode:wnode];
return disk;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[[self.sceneView.scene.rootNode childNodes] enumerateObjectsUsingBlock:^(SCNNode * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (arc4random_uniform(10) == 0) {
[obj runAction:[SCNAction rotateByX:M_PI y:0 z:0 duration:0.5]];
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment