//

//  MRArkaView.m

//  SimpleArka

//

//  Created by Marco Rotatori on 12/02/10.

//  Copyright 2010 Marco Rotatori. All rights reserved.

//


#import "MRArkaView.h"



@implementation MRArkaView


- (id)initWithFrame:(NSRect)frame {

self = [super initWithFrame:frame];

if (self) {

velocity = 1.0;

xDirection = -2.0;

yDirection = 5.0;

ballRect = NSMakeRect([self frame].size.width / 2 - 10.0, 10.0, 10.0, 10.0);

barRect = NSMakeRect(([self frame].size.width - 100.0) / 2, 0.0, 100.0, 10.0);

barIsMoving = NO;

bricks = [[NSMutableArray alloc] init];

[self fillBricks];

timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(update:) userInfo:nil repeats:YES];

}

return self;

}



- (void)dealloc {

[bricks release];

[super dealloc];

}



- (BOOL)acceptsFirstResponder {

return TRUE;

}



- (void)drawRect:(NSRect)rect {

[[NSColor whiteColor] set];

NSRectFill(rect);

int i;

for(i=0; i<[bricks count]; i++) {

NSRect brickRect = [[bricks objectAtIndex:i] rectValue];

[[NSColor yellowColor] set];

[NSBezierPath fillRect:brickRect];

[[NSColor redColor] set];

[NSBezierPath strokeRect:brickRect];

}

[[NSColor blackColor] set];

[NSBezierPath fillRect:barRect];

[[NSColor blueColor] set];

[[NSBezierPath bezierPathWithOvalInRect:ballRect] fill];

}



- (void)mouseDown:(NSEvent *)theEvent {

NSPoint mouseLoc = [theEvent locationInWindow];

NSLog(@"%f, %f", mouseLoc.x, mouseLoc.y);

NSLog(@"%f, %f, %f, %f", barRect.origin.x, barRect.origin.y, barRect.size.width, barRect.size.height);

if(NSPointInRect(mouseLoc, barRect)) {

barIsMoving = YES;

}

}


- (void)mouseDragged:(NSEvent *)theEvent {

if(barIsMoving) {

float movement = [theEvent deltaX];

barRect.origin.x += movement;

if(barRect.origin.x < 0.0) {

barRect.origin.x = 0.0;

}

if(barRect.origin.x + barRect.size.width > [self frame].size.width) {

barRect.origin.x = [self frame].size.width - barRect.size.width;

}

}

}


- (void)mouseUp:(NSEvent *)theEvent {

barIsMoving = NO;

}



- (void)fillBricks {

[bricks removeAllObjects];

NSSize brickSize = NSMakeSize([self frame].size.width / 10, 20.0);

int i, n;

for(n=1; n<=4; n++) {

if(n%2 != 0) {

for(i=0; i<10; i++) {

NSRect brickRect = NSZeroRect;

brickRect.origin = NSMakePoint(brickSize.width * i, [self frame].size.height - brickSize.height * n);

brickRect.size = brickSize;

[bricks addObject:[NSValue valueWithRect:brickRect]];

}

}

else {

for(i=0; i<9; i++) {

NSRect brickRect = NSZeroRect;

brickRect.origin = NSMakePoint(brickSize.width / 2 + brickSize.width * i, [self frame].size.height - brickSize.height * n);

brickRect.size = brickSize;

[bricks addObject:[NSValue valueWithRect:brickRect]];

}

}


}

}



- (void)update:(NSTimer*)theTimer {

ballRect.origin.x += xDirection * velocity;

ballRect.origin.y += yDirection * velocity;

if(ballRect.origin.x <= 0.0 || ballRect.origin.x + ballRect.size.width >= [self frame].size.width) {

xDirection = -xDirection;

}

if(ballRect.origin.y <= 0.0 || ballRect.origin.y + ballRect.size.height >= [self frame].size.height) {

yDirection = -yDirection;

}

if(NSIntersectsRect(ballRect, barRect)) {

yDirection = -yDirection;

}

int i;

for(i=0; i<[bricks count]; i++) {

if(NSIntersectsRect(ballRect, [[bricks objectAtIndex:i] rectValue])) {

yDirection = -yDirection;

[bricks removeObjectAtIndex:i];

}

}

[self setNeedsDisplay:YES];

}


@end