Jump to content
  • Advertisement
Sign in to follow this  
Prog101

credit system

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys i am abit stuck on my credit system, at the moment what happens is i add the type of credit and the score to an array and then shuffle the array every time a new credit is added, shifting the array down by one, the credits then render in order and fade out. what i now want to do is to have the new credit fly in from the right and shift the other credits down can anyone help
#define LIST_MAX 3

/****** ENUMS *****/
typedef enum CreditTypeRace
{
	RESPECT_CREDITS,
	CLEAN_LAP_CREDITS,
	LAP_LEADER_CREDITS,
	OVERTAKE_CREDITS,
	NB_CREDIT_TYPES
}CreditTypeRace;

/****** STRUCTS *****/
typedef struct CreditList 
{
	_Time StartTime;
	CreditTypeRace Type;
	int Score;
}CreditList;

CreditList cList[LIST_MAX];

void BJCredSys_AddCreditsToList(int Score,int Type)
{
	int i = 0;
	int NbCreditsInList = 0;

	/* lets see if we have any outstanding credits */
	for(i = 0; i < LIST_MAX; i++)
	{
		if(cList.Type != -1)
		{
			/*if we are here then we have some credits already in the array*/
			NbCreditsInList++;
		}
	}

	if (NbCreditsInList == 0)
	{
		/* the list is empty so lets add one in */
		cList[0].Type  = Type;
		cList[0].StartTime = gGameTime;
		cList[0].Score = Score;
	}
	else
	{
		/* the list is not empty so lets shuffle them down one */
		for (i = 0; i < LIST_MAX; i++)
		{
			int list1 = LIST_MAX-(i+1);
			int list2 = list1-1;

			if (list2 >= 0)
			{
				cList[list1].Type = cList[list2].Type;
				cList[list1].StartTime = cList[list2].StartTime;
				cList[list1].Score = cList[list2].Score;
			}
			
		}
		/* now lets add the new credit type */
		cList[0].Type  = Type;
		cList[0].StartTime = gGameTime;
		cList[0].Score = Score;
	}
}


void RenderCreditsList(int X, int Y)
{
	int i;
	int NbCreditsInList = 0;
	char creditString[64];
	int Xpos = X;
	int Ypos = Y;
	int yOffset = 30;

	/* lets see if we have any outstanding credits */
	for(i = 0; i < LIST_MAX; i++)
	{
		if(cList.Type != -1)
		{
			/*if we are here then we have some credits already in the array*/
			NbCreditsInList++;
		}
	}

	if (NbCreditsInList == 0)
	{
		/* no credits to render */
		return;
	}

	for (i = 0; i < NbCreditsInList; i++)
	{
		if ( gGameTime - cList.StartTime <= 20000)
		{
			float oppacitcyTemp = (gGameTime - cList.StartTime)/20000.f;
			float oppacitcy = (1 - oppacitcyTemp)*255.f;
			
			sprintf(creditString,"%s = %d", STRING(ST_CREDIT_RESPECT + cList.Type) ,cList.Score);
			BjTextPrint(creditString, Xpos,Ypos, BJColourMake(255, 255, 255, (int)oppacitcy), FONT, CENTRE);
			Ypos+=yOffset;
		}
		else
		{
			cList.Type = -1;
			cList.StartTime = 0;
			cList.Score = 0;
		}
	}
}



Share this post


Link to post
Share on other sites
Advertisement
Any particular reason you're restricting yourself to C? Just curious (I assume this is C - I didn't see any C++ in your code).

Anyway, a good way to approach problems of this sort is just to describe (verbally to yourself, on paper, or whatever) the behavior that you want. For example:

When a new credit is added, I want all the old credits to scroll down one line, and for the new credit to move in from the right and 'take over' the top spot. I want this to happen over a time span of 1 second, and I'd like for the animation to be smoothed out a bit.

We can see that the addition of a new credit begins an animation sequence in which each credit moves from one position to another. All the credits except for the new one move from their current position to a new position whose y value is one line's worth greater (or lesser, depending). The new credit moves to the position currently held by the previous top credit in the list, from a position with the same y value but off to the right a ways. Make sense?

At this point you start a timer (and perhaps set a flag of some sort). As long as the timer is running, you compute the position of each credit by interpolating its old and new positions, using the percentage of elapsed time as your t value. When the timer runs out, you reset the flag, change the state, or whatever, to indicate that the animation is no longer active.

Finally, if you want to smooth things out a bit, you run your t value though an 'ease-in, ease-out' function before performing the interpolation. And voila, you have a cool graphical effect (well, at least it sounds cool to me :).

Again, note that the description of a specific desired behavior can often serve as a blueprint for the code that implements that behavior.

Share this post


Link to post
Share on other sites
I would recommend a different approach entirely. Allow me to explain:

Games usually have a two step cycle. Step 1 is to update the game logic and move characters / other things. Step 2 is to draw. No drawing should occur in ste 1 and no updating in step 2.

Also, at an abstract level what you want is in fact two seperate systemsm both using a list.
A message system, whereby when you get a credit, somebody trash talks, you crash, you finish a lap, etc, a message appears on the screen in the way you described. Messages expire.

A credit system, only storing a list of the last few credits you got.

I would have the following system for the credit system:



/****** ENUMS *****/
typedef enum CreditTypeRace
{
RESPECT_CREDITS,
CLEAN_LAP_CREDITS,
LAP_LEADER_CREDITS,
OVERTAKE_CREDITS,
NB_CREDIT_TYPES
}CreditTypeRace;

/****** STRUCTS *****/
typedef struct CreditList
{
_Time StartTime;
CreditTypeRace Type;
int Score;
}CreditList;

typedef struct MessageList{
char * text;
_Time start_time; //when did it get created?
}MessageList;

CreditList cList[LIST_MAX];
MessageList mList[LIST_MAX];

void add_credit(int score, int type)
{
move_other_credits_up_1_slot();
add_credit_to_start_of_list();
//side effect of this is that the oldest credit gets fragged.

switch(type){
case (RESPECT_CREDITS):
player.respect_credits++;
char message[50];
sprintf("You got a respect credit worth %d!",score);
add_message(message);
break;

case (CLEAN_LAP_CREDITS):
player.clean_lap_credits++;
char message[50];
sprintf("You got a clean lap credit worth %d!",score);
add_message(message);
break;

//case statements for all the others
}
}
//thats it. credits in their abstract sense are inanimate objects.


//its assumed that credits expire and / or get replaced by new credits. Apart from that, they are seperate from the message system.

void add_message(char * text){
move_all_messages_up();
strcpy(mList[0].text,text);
mList[0].start_time = gGameTime;

//basically, do the stuff you used to do inside addMessage.

}

void draw_messages{
_Time age_of_first_message = gGameTime - mList[0].StartTime;

for(int index = 0; index < LIST_MAX; index++)
{
int Xpos;
int Ypos;
/*
xpos and ypos are dependent on age_of_message.
its up to you to decide any behaviour you want here, and its now going
to be far simpler as the code doesn't need to contain anything to do
with credits, only messages
*/

draw_message(mList[index],Xpos,Ypos);
}
};


void update()
{
//you probably have this already. so, just ensure that whenever you get a new credit, you call the function add_credit
}

void draw_HUD()
{
draw_other_stuff(); //e.g. player stats, FPS, etc.
draw_messages();
}


void draw()
{
//draw level
//draw characters
draw_HUD()

}

void game_loop()
{
while(we dont want to quit)
{
update();
draw();
}
}





and there you have it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!