home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / StarShipView.BackModule / Warp1.bproj / Warp1.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  5.8 KB  |  293 lines

  1. #import "Warp1.h"
  2. #import "Thinker.h"
  3. #define PI (3.141592653589)
  4.  
  5. @implementation Warp1
  6.  
  7.  
  8. - init
  9. {
  10.         [super init];
  11.  
  12.     return self;
  13.     
  14. }
  15. // Every module should have a first state
  16. //since class info only gets loaded once
  17. - setFirstState
  18. {
  19. int ii = 0;
  20.     starCounter = 0;
  21.     if(soundEnabled){
  22.         [pwrUpSnd play];
  23.     }
  24.     firstState = YES;
  25.     currentCycle = 1;
  26.     [self setCenter]; 
  27.  
  28.     [starsObject setStarsStopped];  //put out all stars at once
  29.     [starsObject startStars];
  30.     starsStopped = NO;
  31.     starsStopping = NO;
  32.     totalStopped = 0; 
  33.     for(ii = 0;ii < MAXLINES;ii++){
  34.         lines[ii].toggle = 1;
  35.         lines[ii].stopped = 0;
  36.         [self newLine:ii];
  37.         lines[ii].delay = randBetween(0.0,5.0);  //override set value
  38.     }
  39.     
  40.     return self;
  41. }
  42. - (BOOL)doUntilDone
  43. {
  44. int ii;
  45.  
  46. BOOL done;
  47.  
  48.     done = NO;
  49.     
  50.     
  51.     if(!starsStopped || totalStopped < MAXLINES){    
  52.         for(ii = 0;ii < MAXLINES;ii++){
  53.               if(lines[ii].delay < 0){
  54.                  if(!lines[ii].toggle){ //erase line
  55.  
  56.                     [self eraseLine:ii];     
  57.                     if((lines[ii].dist1 > ceil(radius)) && !lines[ii].stopped){
  58.  
  59.                         //add new one
  60.                         if(!starsStopping){
  61.                             starCounter++;
  62.                             lines[ii].toggle = 1;
  63.                             [self newLine:ii];
  64.                             //bring new one on screen
  65.                             [self drawLine:ii];
  66.                         }
  67.                         else{
  68.                             lines[ii].stopped = 1;
  69.                             totalStopped++;
  70.                         }
  71.                     }
  72.                     else {
  73.                         if(!lines[ii].stopped)
  74.                             [self newDist:ii];
  75.                     } 
  76.             
  77.                 }
  78.                 else{ //draw line
  79.                     [self drawLine:ii];
  80.                     [self newDist:ii]; 
  81.             
  82.                     if((lines[ii].dist1) > ceil(radius)){
  83.                         lines[ii].dist1 = lines[ii].oldDist1 =
  84.                          lines[ii].startDist;
  85.                         lines[ii].length = lines[ii].oldLength = 1;
  86.                         lines[ii].toggle = 0;
  87.                     }
  88.                 
  89.                 } 
  90.             }
  91.             else
  92.                 lines[ii].delay--;
  93.         }
  94.         if(!starsStopping){
  95.             if(starCounter > MAXLINES){ 
  96.                 currentCycle++;
  97.                 starCounter = 0;
  98.             }
  99.             if(currentCycle > cycles){
  100.                 if(soundEnabled)
  101.                     [pwrDownSnd play];
  102.                     [starsObject stopStars];  
  103.                     for(ii = 0;ii < MAXLINES;ii++){
  104.                     lines[ii].delay = 0;
  105.                 }
  106.                 totalStopped = 0;
  107.                 starsStopping = YES;
  108.             }
  109.         }
  110.     }    
  111.     else{ //stars are stopping
  112.         if(starsStopped){
  113.             PSsetlinewidth(0.0);  //reset
  114.             done = YES;
  115.         }
  116.     }
  117.     return done;
  118. }
  119.  
  120. //takes theta and distance and stuffs it into point x & y
  121. - convertToXY:(float)dist :(NXPoint *)point :(float)theta
  122. {
  123.     point->x = floor(centerOfScreen.x + (dist * cos(theta)));
  124.     point->y = floor(centerOfScreen.y  + (dist * sin(theta)));
  125.     return self;
  126. }
  127.  
  128. //calculates new length and updates everything
  129. - newDist:(int)index
  130. {
  131.     lines[index].oldDist1 = lines[index].dist1;
  132.     lines[index].oldLength = lines[index].length;
  133.     if(lines[index].toggle)
  134.         lines[index].dist1 = lines[index].dist1 + lines[index].length;
  135.     else //erasing
  136.         lines[index].dist1 = lines[index].dist1 + lines[index].length;
  137.     lines[index].length = lines[index].length
  138.      + objectSpeed;
  139.      
  140.     if(lines[index].dist1 + lines[index].length > radius)
  141.         lines[index].length = radius - lines[index].dist1 + 5;
  142.     lines[index].r = randBetween(0,1); //random colors
  143.     lines[index].g = randBetween(0,1);
  144.     lines[index].b = randBetween(0,1);
  145.     return self;
  146. }
  147. //generate new line values
  148. - newLine:(int)index
  149. {
  150.     lines[index].dist1 = lines[index].oldDist1 = randBetween(0,radius);
  151.     lines[index].startDist = lines[index].dist1 - 2;
  152.     if(lines[index].startDist < 0.0)
  153.         lines[index].startDist = 0.0;
  154.     lines[index].length = lines[index].oldLength = 1.0;
  155.     lines[index].delay = floor(randBetween(30,startInterval));
  156.     lines[index].theta = randBetween(0,(2*PI));
  157.     lines[index].r = 1.0;  //start out as white
  158.     lines[index].g = 1.0;
  159.     lines[index].b = 1.0;
  160.     return self;
  161. }
  162.  
  163. //between 1 - 2
  164. - setStarSpeed:sender
  165. {
  166.     starSpeed = ([sender floatValue]);
  167.     return self;     
  168. }
  169.  
  170. // between 0-500
  171. - setStartInterval: (Slider *)sender;
  172. {
  173.  
  174.     startInterval = [sender intValue];
  175.     return self;
  176.  
  177. }
  178. //between 1 - 200
  179. - setObjectSpeed: (Slider *)sender
  180. {
  181.     objectSpeed =[sender floatValue];
  182.     return self;
  183. }
  184.  
  185. - setBoundsRect:(NXRect *)r
  186. {
  187.     bounds.origin.x = r->origin.x;
  188.     bounds.origin.y  = r->origin.y;
  189.     bounds.size.width = r->size.width;
  190.     bounds.size.height = r->size.height;
  191.     return self;
  192. }
  193.  
  194. - setCenter
  195. {
  196. float x,y;
  197.  
  198.     centerOfScreen.x = (bounds.size.width / 2) + bounds.origin.x;
  199.      centerOfScreen.y = bounds.size.height / 2 + bounds.origin.y;
  200.     x = bounds.size.width;
  201.     y = bounds.size.height;
  202.     radius = (sqrt(x*x + y*y))/2;
  203.     return self;
  204. }
  205. - drawLine:(int)index
  206. {
  207. NXPoint p1,p2;
  208.     PSsetlinewidth(0.0);
  209.  
  210.     [self convertToXY:lines[index].dist1 :&p1 :lines[index].theta];
  211.     [self convertToXY:(lines[index].dist1+lines[index].length) 
  212.      :&p2 :lines[index].theta];
  213.     PSDrawLine(p1.x,p1.y,p2.x,p2.y,lines[index].r,
  214.      lines[index].g,lines[index].b); 
  215.     return self;
  216. }
  217. - eraseLine:(int)index
  218. {
  219. NXPoint p1,p2;
  220.  
  221.     PSsetlinewidth(4.0);
  222.  
  223.     [self convertToXY:lines[index].dist1 :&p1 :lines[index].theta];
  224.     [self convertToXY:(lines[index].dist1+lines[index].length) 
  225.      :&p2 :lines[index].theta];
  226.     PSDrawLine(p1.x,p1.y,p2.x,p2.y,0.0,0.0,0.0); 
  227.     return self;
  228. }
  229.  
  230. - setPwrDownSnd:(Sound *)theSound
  231. {
  232.     pwrDownSnd = theSound;
  233.     return self;
  234. }
  235. - setPwrUpSnd:(Sound *)theSound;
  236. {
  237.     pwrUpSnd = theSound;
  238.     return self;
  239. }
  240.  
  241. - starsStopped
  242. {
  243.     starsStopped = YES;
  244.     return self;
  245. }
  246. - setStarsOutlet:(id)starsOutlet;
  247. {
  248.     starsObject = starsOutlet;
  249.     return self;
  250. }
  251. - (int)setCycleValue:(int)value;
  252. {
  253. int mutiplier;
  254.     // since this one cycles very fast, make it 9 times the slider value
  255.     // could be zero
  256.     
  257.     mutiplier = 9;
  258.     if(!value)
  259.         value = 1;
  260.     else
  261.         value *= mutiplier;
  262.     cycles = value;
  263.     currentCycle = 1;
  264.     return mutiplier;
  265. }
  266.  
  267. - windowSizeChanged
  268. {
  269. int ii;
  270.     [self setCenter];
  271.     starsStopped = NO;
  272.     starsStopping = NO;
  273.     totalStopped = 0; 
  274.     for(ii = 0;ii < MAXLINES;ii++){
  275.         lines[ii].toggle = 1;
  276.         lines[ii].stopped = 0;
  277.         [self newLine:ii];
  278.         lines[ii].delay = randBetween(0.0,5.0);  //override set value
  279.     }
  280.     return self;
  281. }
  282. - freeResources
  283. {//not used in this module
  284.     return self;
  285. }
  286.  
  287. - setSoundEnabled:(BOOL)enabled
  288. {
  289.     soundEnabled = enabled;
  290.     return self;
  291. }
  292. @end
  293.