Blinking Ghost

Started by
5 comments, last by Fahrenheit451 18 years, 10 months ago
This is a question related to timing and in relation to a pacman / ghost scenario. When pacman eats a power / energy pill the ghosts change color to blue for about 10 seconds. Then they flash (blue and white) for another 6 seconds. The below codes, seems to do the job, but it is ^@#%ing ugly, IMHO! Can anyone offer me advice to a simpler solution (with a 50:50 duty cycle when flashing). This seems way too much code for such a simple task.

   if (now_second - flee_second > 16000)
   {
      fleeing = false;
      flash[0] = flash[1] = 0.0f; flash[2] = 0.8f; /* Return to blue. */
   }
   else if (now_second - flee_second > 10000) /* Last six seconds. */
   {
      if (now_second - flash_second > 100)
      {
         flash_second = now_second;

         if (toggle == false)
         {
            flash[0] = flash[1] = flash[2] = 1.0f; // white
         }
         else
         {
            flash[0] = flash[1] = 0.0f; flash[2] = 0.8f; // blue
         }

         toggle = !toggle;
      }
   }

Advertisement
C or C++?

Either way I'd recommend some sort of mini finite state automata rather than a big if/else if/.../else.

IMO easier to do in C++ though
holy, what is flash[n]? could you please just explain a bit better what all the variables in ur code sample do and what the 0.0f and 0.8f means? how does this correspond to a colour?

[Edited by - _Phalanx_ on June 17, 2005 4:54:45 AM]
Quote:Original post by Nitage

I'd recommend some sort of mini finite state automata rather than a big if/else if/.../else.


You're actually complicating things much more!!

By the way, how do you think one codes a finite state automata... With LEX or some other software, which in the end is going to be transformed into if and else statements...

Quote:Original post by _Phalanx_
holy, what is flash[n]?


It represents the color (R, G, B)...
Quote:Original post by Ali_B
Quote:Original post by Nitage

I'd recommend some sort of mini finite state automata rather than a big if/else if/.../else.


You're actually complicating things much more!!

By the way, how do you think one codes a finite state automata... With LEX or some other software, which in the end is going to be transformed into if and else statements...


No, you're complicating things much more by suggesting an overly complex implementation of a such a small finite state automata.
I asked recently for something similar. I wanted to "pulse" a color to black and back again over a chosen time. This is the code I now use. No reason why this can't be from blue to white. You need a timer unit to make it work properly - or at least to be able to pulse over any desired time.

#define M_PI				3.14159265358979323846float targetColorG=0.85f;	// target green colorfloat targetColorB=1.0f;	// target blue colorfloat moveTime=1.0f;		// 1 second pulse cyclefloat totalTime=0.0f;		// time currently accumulated for pulse  .  .  .// add time step to total time and reset if overtotalTime+=g_timer.timeStep;if ( totalTime > moveTime ) {  totalTime=0.0f;}// calculate current green and blue components based on current time in the pule cyclecurrColorG = targetColorG * (float)((0.75 - cos((2 * M_PI) * (totalTime / moveTime)) * 0.25));currColorB = targetColorB * (float)((0.75 - cos((2 * M_PI) * (totalTime / moveTime)) * 0.25));// apply color for next set of drawsglColor3f(0.0f,currColorG,currColorB);

This topic is closed to new replies.

Advertisement