• ### What is your GameDev Story?

This topic is 4965 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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;
}
}



##### Share on other sites
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

##### Share on other sites
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]

##### Share on other sites
Quote:
 Original post by NitageI'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...

##### Share on other sites
Quote:
 Original post by _Phalanx_holy, what is flash[n]?

It represents the color (R, G, B)...

##### Share on other sites
Quote:
Original post by Ali_B
Quote:
 Original post by NitageI'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.

##### Share on other sites
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);

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 34
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015644
×