// // Dot.m // Particles // // #import "Dot.h" #import "GL.h" @implementation Dot #define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI) #define RANDOM_SEED() srandom(time(NULL)) #define RANDOM_INT(__MIN__, __MAX__) ((__MIN__) + random() % ((__MAX__+1) - (__MIN__))) -(void)setPosition:(GLfloat)x y:(GLfloat)y { posx = x; posy = y; } -(void)setRadio:(GLfloat)value { radio = value; } -(void)setVy:(GLfloat)value { vy = value; } -(void)setFrame:(GLfloat)value { frame = value; } -(void)setColor:(int)r g:(int)g b:(int)b alpha:(int)alpha { colorR = r; colorG = g; colorB = b; colorA = alpha; } // -------------------- -(void)createNew:(bool)inScreen { // primero un random para las coordenadas XY (siempre creamos la particula en pantalla) int x = RANDOM_INT(0, 320) - 160; int y = RANDOM_INT(0, 600) - 300; // y si no la queriamos dentro, la bajamos 600 pixeles pabajo if(!inScreen) y += 600; // random para el radio int r = RANDOM_INT(4, 16); // la velocidad va en funcion del radio tb, asi los puntos // mas grandes se moverán mas lentamente float velocidad = 2.0f - r * 0.1f; int fr = RANDOM_INT(0, 4) ; [self setPosition:x y:y]; [self setRadio:r * (fr+2) * 2]; [self setVy:velocidad]; [self setFrame:fr]; [self setColor:RANDOM_INT(0, 0xFF) g:RANDOM_INT(0, 0xFF) b:RANDOM_INT(0, 0xFF) alpha:RANDOM_INT(0, 0x80)]; dx = 0; dy = 0; } -(void)setTouch:(GLfloat)x y:(GLfloat)y { GLfloat sepx = posx-x; GLfloat sepy = posy-y; GLfloat sep = sepx * sepx + sepy * sepy; if(sep < 2000) // esto parece mucho pero son 40 pixels { dx += sepx ; dy += sepy ; } } -(void)steep { posx += dx; posy += dy; dx*=0.7f; dy*=0.7f; posy -= vy; // cuando la particula se sale por arriba creamos otra, dandole un false para // que se cree fuera de pantalla, por abajo if(posy + radio < - 240) [self createNew:false]; } -(void)render { // ponemos el color de esta particula [GL setColor:colorR g:colorG b:colorB alpha:colorA]; // ponemos el frame de la textura [GL setFrame:frame]; // y dibujamos el sprite [GL drawSprite:posx-radio y1:posy-radio x2:posx+radio y2:posy+radio]; } @end