#import #import "sprite.h" @implementation Sprite -(id) initWithX:(double)initX y:(double)initY radius:(double)r { [super init]; x = initX; y = initY; vx = 0.0; vy = 0.0; radius = r; color = [[UIColor blackColor] retain]; state = 0; stateTime = 0.0; return self; } -(id) initWithX:(double)initX y:(double)initY vx:(double)initVx vy:(double)initVy radius:(double)r { [super init]; x = initX; y = initY; vx = initVx; vy = initVy; radius = r; color = [[UIColor blackColor] retain]; state = STATE_NORMAL; stateTime = 0.0; return self; } -(id) initWithX:(double)initX y:(double)initY vx:(double)initVx vy:(double)initVy radius:(double)r color:(UIColor *)c { [super init]; x = initX; y = initY; vx = initVx; vy = initVy; radius = r; color = [c retain]; state = 0; stateTime = 0.0; return self; } -(double) getX { return x; } -(double) getY { return y; } -(UIColor*) getColor { return color; } -(double) getMaximumSpeed { return 1e+10; } -(void) setVelocityX:(double)newVx y:(double)newVy { vx = newVx; vy = newVy; // limit speed to max velocity double v = sqrt(vx * vx + vy * vy); double maxV = [self getMaximumSpeed]; if (v > maxV) { vx = vx / (v / maxV); vy = vy / (v / maxV); } } -(double) getBoundingRadius { return radius; } -(BOOL) collidesWith:(Sprite*)s { double dx = x - [s getX]; double dy = y - [s getY]; double dist = sqrt(dx * dx + dy * dy); return (dist < radius + [s getBoundingRadius]); } -(void) updateForTime:(double)t model:(GameModel*)m { stateTime += t; x += vx * t; y += vy * t; } -(int) getState { return state; } -(void) setState:(int)s { state = s; stateTime = 0.0; } -(double) getStateTime { return stateTime; } -(void) dealloc { [color release]; [super dealloc]; } @end