Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted by Maurilio ( 14 years ago )
//
// Container.h
// Beer
//
// Created by Maurilio Henrique Reis Campos on 2/15/12.
// Copyright (c) 2012 Kiwi Tecnologia. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ContainerDelegate.h"
#import "DropViewController.h"
#include <stdlib.h>
#define M_PI 3.14159265358979323846264338327950288
#define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)
@interface Container : UIViewController <KWGame>
{
int level;
int count;
float graus;
BOOL left;
BOOL right;
}
-(id) initWithDelegate:(id)delegate left:(BOOL)isLeft right:(BOOL)isRight position:(CGPoint)point scene:(UIView *)view color:(NSString *) color;
-(void)startAnimation;
-(void)dropLiquid;
-(void)changeLiquid:(NSTimer *)sender;
-(void)pauseAnimations;
-(void)continueAnimations;
@property (nonatomic, retain) NSString *color;
@property (nonatomic, readwrite) BOOL left;
@property (nonatomic, readwrite) BOOL right;
@property (nonatomic, retain) UIView *scene;
@property (nonatomic, retain) NSTimer *borderAnimationTimer;
@property (nonatomic, retain) NSTimer *shootAnimationTimer;
@property (nonatomic, retain) IBOutlet UIImageView *imgBorder;
@property (nonatomic, retain) IBOutlet UIImageView *imgLiquid;
@property (nonatomic, retain) id delegate;
@end
@interface Container (private)
-(void)animateLevel:(int) from to: (int)value;
-(void)animateFirstLevel;
-(void)animateSecondLevel;
-(void)animateBorder;
-(void)shootAnimate:(BOOL)animate level: (int)value;
-(void)display:(UIView *) view position:(CGPoint) pos;
@end
@implementation Container
@synthesize delegate = _delegate;
@synthesize scene = _scene;
@synthesize imgBorder = _imgBorder;
@synthesize imgLiquid = _imgLiquid;
@synthesize left = _left;
@synthesize right = _right;
@synthesize borderAnimationTimer = _borderAnimationTimer;
@synthesize shootAnimationTimer = _shootAnimationTimer;
@synthesize color = _color;
-(id) initWithDelegate:(id)delegate left:(BOOL)isLeft right:(BOOL)isRight position:(CGPoint)point scene:(UIView *)view color:(NSString *)color
{
if (self = [super initWithNibName:@"Container" bundle:nil]) {
self.left = isLeft;
self.right = isRight;
self.delegate = delegate;
self.color = color;
[self display:view position:point];
NSString *startImage = [NSString stringWithFormat:@"%@_1.png", color];
_imgLiquid.image = [UIImage imageNamed:startImage];
}
return self;
}
-(void)animateLevel:(int)from :(int)to
{
NSMutableArray *liquidSequence = [NSMutableArray array];
NSString *finalImage = [NSString stringWithFormat:@"%@_%d.png", _color, to];
_imgLiquid.image = [UIImage imageNamed:finalImage];
for (;from <= to; from++) {
NSString *imgName = [NSString stringWithFormat:@"%@_%d.png", _color, from];
[liquidSequence addObject:[UIImage imageNamed:imgName]];
}
_imgLiquid.animationImages = liquidSequence;
_imgLiquid.animationDuration = 1.5f;
_imgLiquid.animationRepeatCount = 1;
[_imgLiquid startAnimating];
[NSTimer scheduledTimerWithTimeInterval:1.7f target:self selector:@selector(changeLiquid:) userInfo:finalImage repeats:NO];
}
-(void)changeLiquid:(NSTimer *)sender
{
NSString *finalImage = (NSString *)[sender userInfo];
NSLog(@"Final Image: %@", finalImage);
_imgLiquid.image = [UIImage imageNamed:finalImage];
}
-(void)animateFirstLevel
{
[self animateLevel:1 :4];
}
-(void)animateSecondLevel
{
[self animateLevel:5 :7];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_left = NO;
_right = NO;
level = 0;
count = 5;
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
-(void)display:(UIView *)view position:(CGPoint)pos
{
[view addSubview: self.view];
_scene = view;
self.view.center = pos;
[self startAnimation];
}
-(void)startAnimation
{
int interval = (arc4random() % 15)+1;
NSLog(@"Shoot interval: %i", interval);
_borderAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:0.6f
target:self
selector:@selector(animateBorder)
userInfo:nil
repeats:YES];
_shootAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(dropLiquid)
userInfo:nil repeats:YES];
}
-(void)dropLiquid
{
if (level <= 2) {
if (count > 0) {
[self shootAnimate:NO level:level];
count--;
} else {
count = 5;
switch (level) {
case 0:
[self animateFirstLevel];
break;
case 1:
[self animateSecondLevel];
break;
default:
break;
}
[self shootAnimate:NO level:++level];
}
} else {
_imgLiquid.image = nil;
[_borderAnimationTimer invalidate];
[_shootAnimationTimer invalidate];
if ([_delegate conformsToProtocol:@protocol(ContainerDelegate)]) {
[_delegate emptyContainer:self];
}
}
}
-(void) shootAnimate:(BOOL)animate level:(int)value
{
DropViewController *drop = [[DropViewController alloc] initWithNibName:@"DropViewController" bundle:nil];
[_scene addSubview:drop.view];
if ([_delegate conformsToProtocol:@protocol(ContainerDelegate)]) {
[_delegate liquidContainerWillDrop:drop];
}
CGPoint startPosition = CGPointMake(self.view.center.x, _imgBorder.bounds.size.height + 15);
CGPoint finalPosition;
drop.view.center = startPosition;
CGFloat radians = atan2f(_imgBorder.transform.b, _imgBorder.transform.a);
CGFloat degrees = radians * (180 / M_PI);
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
drop.view.transform = transform;
CGFloat mRadians = (degrees * M_PI / 180);
CGFloat fX = (1000 * sin(mRadians)) * -1;
CGFloat fY = (1000 * cos(mRadians));
finalPosition = CGPointMake(startPosition.x + fX, startPosition.y + fY);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatCount: 0];
[UIView setAnimationRepeatAutoreverses:NO];
drop.view.center = finalPosition;
[UIView commitAnimations];
}
-(void)animateBorder
{
if (_left) {
graus = ((arc4random() % 31) * -1.0f);
} else if (_right) {
graus = (arc4random() % 31);
} else {
int direction = -1;
if (arc4random() % 2 == 0) {
direction = 1;
}
graus = (float) arc4random_uniform(26) * direction;
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:YES];
CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(graus));
_imgBorder.transform = transform;
[UIView commitAnimations];
}
//KWGame Protocol
-(void)pauseAnimations
{
}
-(void)continueAnimations
{
}
@end
Revise this Paste