Skip to content

Instantly share code, notes, and snippets.

@AlexanderBollbach
Created August 21, 2015 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderBollbach/b7391f6982be6c614572 to your computer and use it in GitHub Desktop.
Save AlexanderBollbach/b7391f6982be6c614572 to your computer and use it in GitHub Desktop.
//
// OrbMaster.m
// fieldTheory1
//
// Created by alexanderbollbach on 8/20/15.
// Copyright © 2015 alexanderbollbach. All rights reserved.
//
#import "OrbMaster.h"
@interface OrbMaster()
@property(nonatomic) double closestOrbDistance;
@property(nonatomic) NSString* closestOrbName;
@property(nonatomic,strong) CAShapeLayer* connectLayer;
@property(nonatomic) CGMutablePathRef connectPath;
@property(nonatomic,strong) OrbMaster* closestOrb;
@end
@implementation OrbMaster
-(instancetype)initWithFrame:(CGRect)frame andColor:(UIColor*)color ofSuperView:(UIView*)superView andName:(NSString*)name {
if (self = [super initWithFrame:frame]) {
// style
self.color = color;
self.name = name;
self.backgroundColor = [UIColor clearColor];
// CONFIGURE ORB IN MANAGER
OrbManager* temp = [OrbManager orbManager];
[temp.managedOrbs addObject:self];
// AK Setup
self.instrumentAB = [AKInstrument instrument];
self.oscillatorAB = [AKOscillator oscillator];
self.ampAB = [self.instrumentAB createPropertyWithValue:1 minimum:0 maximum:0];
self.oscillatorAB.amplitude = self.ampAB;
self.FreqAB = [self.instrumentAB createPropertyWithValue:0 minimum:0 maximum:0];
self.oscillatorAB.frequency = self.FreqAB;
[self.instrumentAB setAudioOutput:self.oscillatorAB];
[AKOrchestra addInstrument:self.instrumentAB];
[self.ampAB setFloatValue:0.0];
[self.instrumentAB start];
self.theSuperView = superView;
[superView addSubview:self];
// add connect layer
self.connectLayer = [[CAShapeLayer alloc] init];
[self.theSuperView.layer addSublayer:self.connectLayer];
self.closestOrbDistance = 1000;
}
return self;
}
-(void)checkDistance:(NSMutableArray*)managedOrbs {
// self center
float selfX = [[self.layer presentationLayer]frame].origin.x + self.frame.size.width/2;
float selfY = [[self.layer presentationLayer]frame].origin.y + self.frame.size.height/2;
// double closestX = 0;
// double closestY = 0;
// get closest Orb
for (OrbMaster* orb in managedOrbs) {
if (![orb.name isEqualToString:self.name]) {
// get dist of orb
float nearbyOrbX = [[orb.layer presentationLayer]frame].origin.x + orb.frame.size.width/2;
float nearbyOrbY = [[orb.layer presentationLayer]frame].origin.y + orb.frame.size.height/2;
float dx = (nearbyOrbX-selfX);
float dy = (nearbyOrbY-selfY);
float dist = sqrt(dx*dx + dy*dy);
// if in Zone
if (dist < self.closestOrbDistance && dist < 150) {
self.closestOrb = orb;
}
}
}
if (self.closestOrb) {
self.color = [UIColor redColor];
[self setNeedsDisplay];
[self.ampAB setFloatValue:0.03];
// get closest center
float closestX = [[self.closestOrb.layer presentationLayer]frame].origin.x + self.closestOrb.frame.size.width/2;
float cloesetY = [[self.closestOrb.layer presentationLayer]frame].origin.y + self.closestOrb.frame.size.height/2;
[self.FreqAB setValue:[[self.closestOrb.layer presentationLayer] frame].origin.y * 4];
// draw Connect Layer
self.connectPath = CGPathCreateMutable();
CGPathMoveToPoint(self.connectPath, NULL, selfX, selfY);
CGPathAddLineToPoint(self.connectPath, NULL,closestX,cloesetY);
CGPathCloseSubpath(self.connectPath);
self.connectLayer.strokeColor = [UIColor redColor].CGColor;
self.connectLayer.fillColor = [UIColor redColor].CGColor;
[self.connectLayer setPath:self.connectPath];
self.connectLayer.lineWidth = 1;
} else {
self.color = [UIColor whiteColor];
[self setNeedsDisplay];
[self.ampAB setFloatValue:0.0];
self.connectLayer.fillColor = [UIColor clearColor].CGColor;
self.connectLayer.strokeColor = [UIColor clearColor].CGColor;
}
//reset closest
self.closestOrbDistance = 1000;
self.closestOrb = nil;
}
-(void)setPath:(CGMutablePathRef)path {
CAKeyframeAnimation* ani = [CAKeyframeAnimation animationWithKeyPath:@"position"];
ani.calculationMode = kCAAnimationCubicPaced;
ani.fillMode = kCAFillModeBackwards;
ani.repeatCount = INFINITY;
ani.duration = 5;
ani.path = path;
[self.layer addAnimation:ani forKey:@""];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 3);
CGContextSetFillColorWithColor(ctx, self.color.CGColor);
UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(60, 60) ];
[path fill];
}
float interpolate(float originalMin, float originalMax,float newBegin,
float newEnd,float inputValue, float curve)
{
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
float invFlag = 0;
float result = 0;
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ;
curve = pow(10, curve);
if (inputValue < originalMin)
{
inputValue = originalMin;
}
if (inputValue > originalMax)
{
inputValue = originalMax;
}
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin)
{
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange;
if (originalMin > originalMax )
{
result = 0;
return result;
}
if (invFlag == 0)
{
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}else{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
result = rangedValue;
return result;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment