Created
July 9, 2014 14:40
-
-
Save icodeforlove/a821c20d507a7d9d0d94 to your computer and use it in GitHub Desktop.
SpriteKit component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameControls.h | |
// Hiss | |
// | |
// Created by Chad Scira on 6/21/14. | |
// Copyright (c) 2014 Chad Scira. All rights reserved. | |
// | |
#import <SpriteKit/SpriteKit.h> | |
@class GameControls; | |
@protocol GameControlsDelegate <NSObject> | |
@optional | |
-(void)controlButtonPressed: (NSString *)button; | |
@end | |
@interface GameControls : SKNode <GameControlsDelegate> { | |
SKAction *fadePulseAction; | |
SKSpriteNode *upArrow; | |
SKSpriteNode *rightArrow; | |
SKSpriteNode *downArrow; | |
SKSpriteNode *leftArrow; | |
NSMutableArray *buttons; | |
} | |
@property (nonatomic, weak) id <GameControlsDelegate> delegate; | |
@property CGSize size; | |
+(GameControls*)controlsWithSize: (CGSize)size; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GameControls.m | |
// Hiss | |
// | |
// Created by Chad Scira on 6/21/14. | |
// Copyright (c) 2014 Chad Scira. All rights reserved. | |
// | |
#import "GameControls.h" | |
#import "SKTween.h" | |
@implementation GameControls | |
-(id)initWithSize: (CGSize)size { | |
if (self = [self init]) { | |
buttons = [[NSMutableArray alloc]init]; | |
fadePulseAction = [SKTween tweenSet:@[ | |
@{@"duration": @.2, @"finish": @{@"fadeAlphaTo": @.8}}, | |
@{@"delay": @.1, @"duration": @.2, @"finish": @{@"fadeAlphaTo": @.4}}]]; | |
self.userInteractionEnabled = YES; | |
self.size = size; | |
[self addArrows]; | |
} | |
return self; | |
} | |
+(GameControls*)controlsWithSize: (CGSize)size { | |
GameControls *controls = [[self alloc] initWithSize:size]; | |
return controls; | |
} | |
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
for (UITouch *touch in touches) { | |
CGPoint point= [touch locationInNode: self]; | |
NSString *name; | |
for (SKShapeNode *button in buttons) { | |
if ([[UIBezierPath bezierPathWithCGPath: button.path] containsPoint: CGPointMake(point.x - button.position.x, point.y - button.position.y)]) { | |
name = button.name; | |
break; | |
} | |
} | |
// NSLog(@"button pressed: %@ %f,%f %lu", name, point.x, point.y, (unsigned long)touches.count); | |
if ([name isEqualToString:@"up"]) { | |
[self.delegate controlButtonPressed: name]; | |
[upArrow runAction: fadePulseAction]; | |
} else if ([name isEqualToString:@"right"]) { | |
[self.delegate controlButtonPressed: name]; | |
[rightArrow runAction: fadePulseAction]; | |
} else if ([name isEqualToString:@"down"]) { | |
[self.delegate controlButtonPressed: name]; | |
[downArrow runAction: fadePulseAction]; | |
} else if ([name isEqualToString:@"left"]) { | |
[self.delegate controlButtonPressed: name]; | |
[leftArrow runAction: fadePulseAction]; | |
} | |
} | |
} | |
-(CGPoint)getNextPointWith: (CGPoint) pointA andPoint: (CGPoint) pointB atHeight: (float) height { | |
float slope = atan2(pointB.y - pointA.y, pointB.x - pointA.x); | |
float angle = M_PI - (slope + M_PI/2); | |
float opposite = height; | |
float adjacent = tan(angle) * opposite; | |
float hypotenuse = sqrt(pow(opposite, 2) + pow(adjacent, 2)); | |
return CGPointMake(pointA.x + cos(slope) * hypotenuse, pointB.y + sin(slope) * hypotenuse); | |
} | |
-(void)addArrows { | |
float scale = .25; | |
float xButtonMargin = 5; | |
SKShapeNode *overlayShape; | |
UIBezierPath *overlayBezierPath; | |
CGRect centerRect = CGRectMake(self.size.width/2 - 35/2, self.size.height/2 - 15, 35, 30); | |
CGPoint centerRectCenterPoint = CGPointMake(centerRect.origin.x + centerRect.size.width/2, centerRect.origin.y + centerRect.size.height/2); | |
// canculate points | |
CGPoint bottomRight = [self getNextPointWith:centerRectCenterPoint andPoint:CGPointMake(centerRectCenterPoint.x + centerRect.size.width/2, centerRectCenterPoint.y + centerRect.size.height/2) atHeight:self.size.height/2]; | |
CGPoint bottomLeft = [self getNextPointWith:centerRectCenterPoint andPoint:CGPointMake(centerRectCenterPoint.x - centerRect.size.width/2, centerRectCenterPoint.y + centerRect.size.height/2) atHeight:self.size.height/2]; | |
CGPoint topRight = [self getNextPointWith:centerRectCenterPoint andPoint:CGPointMake(centerRectCenterPoint.x + centerRect.size.width/2, centerRectCenterPoint.y - centerRect.size.height/2) atHeight:self.size.height/2]; | |
CGPoint topLeft = [self getNextPointWith:centerRectCenterPoint andPoint:CGPointMake(centerRectCenterPoint.x - centerRect.size.width/2, centerRectCenterPoint.y - centerRect.size.height/2) atHeight:self.size.height/2]; | |
// up arrow | |
upArrow = [SKSpriteNode spriteNodeWithImageNamed:@"pad-up-arrow"]; | |
[upArrow setScale: scale]; | |
upArrow.alpha = .6; | |
upArrow.name = @"up"; | |
upArrow.position = CGPointMake(self.size.width/2, self.size.height/2 + upArrow.size.width); | |
[self addChild: upArrow]; | |
overlayBezierPath = UIBezierPath.bezierPath; | |
[overlayBezierPath moveToPoint: bottomLeft]; | |
[overlayBezierPath addLineToPoint:centerRectCenterPoint]; | |
[overlayBezierPath addLineToPoint:bottomRight]; | |
[overlayBezierPath addLineToPoint: bottomLeft]; | |
overlayShape = [SKShapeNode node]; | |
overlayShape.path = overlayBezierPath.CGPath; | |
overlayShape.strokeColor = [UIColor clearColor]; | |
overlayShape.antialiased = NO; | |
overlayShape.name = @"up"; | |
overlayShape.zPosition = 1; | |
[self addChild:overlayShape]; | |
[buttons addObject: overlayShape]; | |
// right arrow | |
rightArrow = [SKSpriteNode spriteNodeWithImageNamed:@"pad-right-arrow"]; | |
rightArrow.zPosition = -1; | |
rightArrow.alpha = .6; | |
[rightArrow setScale: scale]; | |
rightArrow.position = CGPointMake(self.size.width/2 + upArrow.size.width + xButtonMargin, self.size.height/2); | |
[self addChild: rightArrow]; | |
overlayBezierPath = UIBezierPath.bezierPath; | |
[overlayBezierPath moveToPoint: topRight]; | |
[overlayBezierPath addLineToPoint:centerRectCenterPoint]; | |
[overlayBezierPath addLineToPoint:bottomRight]; | |
[overlayBezierPath addLineToPoint:CGPointMake(self.size.width, bottomRight.y)]; | |
[overlayBezierPath addLineToPoint:CGPointMake(self.size.width, 0)]; | |
[overlayBezierPath moveToPoint: topRight]; | |
overlayShape = [SKShapeNode node]; | |
overlayShape.path = overlayBezierPath.CGPath; | |
overlayShape.strokeColor = [UIColor clearColor]; | |
overlayShape.antialiased = NO; | |
overlayShape.name = @"right"; | |
overlayShape.zPosition = 1; | |
[self addChild:overlayShape]; | |
[buttons addObject: overlayShape]; | |
// down arrow | |
downArrow = [SKSpriteNode spriteNodeWithImageNamed:@"pad-down-arrow"]; | |
[downArrow setScale: scale]; | |
downArrow.name = @"down"; | |
downArrow.alpha = .6; | |
downArrow.position = CGPointMake(self.size.width/2, self.size.height/2 - downArrow.size.width); | |
[self addChild: downArrow]; | |
overlayBezierPath = UIBezierPath.bezierPath; | |
[overlayBezierPath moveToPoint: topLeft]; | |
[overlayBezierPath addLineToPoint:centerRectCenterPoint]; | |
[overlayBezierPath addLineToPoint:topRight]; | |
[overlayBezierPath addLineToPoint: topLeft]; | |
overlayShape = [SKShapeNode node]; | |
overlayShape.path = overlayBezierPath.CGPath; | |
overlayShape.strokeColor = [UIColor clearColor]; | |
overlayShape.antialiased = NO; | |
overlayShape.name = @"down"; | |
overlayShape.zPosition = 1; | |
[self addChild:overlayShape]; | |
[buttons addObject: overlayShape]; | |
// left arrow | |
leftArrow = [SKSpriteNode spriteNodeWithImageNamed:@"pad-left-arrow"]; | |
[leftArrow setScale: scale]; | |
leftArrow.alpha = .6; | |
leftArrow.name = @"left"; | |
leftArrow.position = CGPointMake(self.size.width/2 - upArrow.size.width - xButtonMargin, self.size.height/2 ); | |
[self addChild: leftArrow]; | |
overlayBezierPath = UIBezierPath.bezierPath; | |
[overlayBezierPath moveToPoint: topLeft]; | |
[overlayBezierPath addLineToPoint:centerRectCenterPoint]; | |
[overlayBezierPath addLineToPoint:bottomLeft]; | |
[overlayBezierPath addLineToPoint:CGPointMake(0, bottomLeft.y)]; | |
[overlayBezierPath addLineToPoint:CGPointMake(0, 0)]; | |
[overlayBezierPath moveToPoint: topLeft]; | |
overlayShape = [SKShapeNode node]; | |
overlayShape.path = overlayBezierPath.CGPath; | |
overlayShape.strokeColor = [UIColor clearColor]; | |
overlayShape.antialiased = NO; | |
overlayShape.name = @"left"; | |
overlayShape.zPosition = 1; | |
[self addChild:overlayShape]; | |
[buttons addObject: overlayShape]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment