Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
data grid
//
// ViewController.m
// DiaGrid
//
// Created by MizushimaYusuke on 2017/06/19.
// Copyright © 2017 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createDiaGrid];
}
- (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
[sv presentScene:s];
[self.view addSubview:sv];
self.scene = s;
}
- (void)createDiaGrid {
float l = 55;
for (int i=0; i<20; i++) {
for (int j=0; j<12; j++) {
float x = l * i;
float y = l * j;
SKNode *dia = ((i + (j % 2)) % 2) == 0 ? [self shapeA] : [self shapeB];
dia.position = CGPointMake(x, y);
[self.scene addChild:dia];
}
}
}
- (SKNode *)shapeA {
int shape[][5] = {
{0,0,1,0,0},
{0,1,1,1,0},
{1,1,0,1,1},
{0,1,1,1,0},
{0,0,1,0,0},
};
return [self createShape:shape color:[UIColor yellowColor]];
}
- (SKNode *)shapeB {
int shape[][5] = {
{0,0,1,0,0},
{0,1,0,1,0},
{1,0,1,0,1},
{0,1,0,1,0},
{0,0,1,0,0},
};
return [self createShape:shape color:[UIColor whiteColor]];
}
- (SKNode *)createShape:(int[][5])shape color:(UIColor *)color {
SKNode *a = [SKNode node];
a.name = @"dia";
float l = 10;
for (int i=0; i<5; i++) {
for (int j=0; j<5; j++) {
if (shape[i][j] == 1) {
float x = l * i;
float y = l * j;
SKShapeNode *dot = [SKShapeNode shapeNodeWithRect:CGRectMake(x, y, l, l)];
if (i==2 && j==2) {
dot = [SKShapeNode shapeNodeWithRect:CGRectMake(x + 2, y + 2, l - 4, l - 4)];
}
dot.lineWidth = 0;
dot.fillColor = color;
[a addChild:dot];
}
}
}
return a;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.scene enumerateChildNodesWithName:@"dia" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
if (arc4random_uniform(5) == 0) {
[node runAction:[SKAction sequence:@[[SKAction scaleTo:0.5 duration:1.0], [SKAction scaleTo:1.0 duration:1.0]]]];
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment