Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

First time posting on these forums so go easy on me!

I've been programming a race game which has been going quite well. I've just discovered now how hard it is to set up player positions!(As in 1st, 2nd, 3rd, etc). From research I've discovered that the easiest way of doing this is through checkpoints which I've successfully managed to set up. Each player races through the checkpoints in the game and this progress is stored as an int. There's 2 int, one for laps and one for checkpoints. When the checkpoint integer reaches a certain number it resets back to zero and the lap integer is incremented.

I was planning to compare these to work out player positions. Hopefully by putting them into arrays and then sorting them so it could tell me the player's positions in the race as the person with the high lap and checkpoint count would surely be the highest ranked.

Also to make the measuring of player positions more accurate I've measure the distance between checkpoints. At each checkpoint it works out how far the player is from the next point and using the distance from the previous to the next I'm able to generate a percentage which tells me how far the player is from the lap.

What I was hoping to do is create an integer worked out from the players race progress. So for example going through a checkpoint would increment the player's race progress integer by 100. If there were 12 checkpoint a complete lap would obviously be 1200. 2 laps would be 2400 and so on. By inverting the percentage I generated earlier it gives me the distance to the next checkpoint. so for example a player 75% towards the 4th checkpoint in their first lap would have an integer of 375, whereas a player 50% towards the first checkpoint in their second lap would have an integer of 1250.

I hope that logic made sense.

So what I'm essentially looking to create is a sort of table, using the player's object or even a unique player id as an index. And then to sort this table by the player position int.

I think this maybe possible through multidimensional arrays but I'm quite unsure. I've looked into associative arrays, dictionaries and hashtables but I'm still pretty unclear on what to do.

Would really appreciate being pointed in the right direction. Thanks!

share|improve this question
    
Checkpoints are a good start. However it will feel weird for the player, once he passes someone, not to see his better position immedietely, but only after he passes the next checkpoint. There are better ways to do this immedietely, depending on the representation of your track. Using splines as track representation for instance. –  Maik Semder Mar 29 '13 at 9:38

1 Answer 1

up vote 0 down vote accepted
public class Racer
{
    public int CheckPoints { get; set; }

    public int Progress
    {
        get
        {
            return CheckPoints * 100;
        }
    }
}

I am assuming that you have a list of "racers" that you maintain. If that is the case, either on the update loop, or maybe only when they pass through a checkpoint, you can then update that list and order it by Progress descending.

YourRacersList = YourRacersList.OrderByDescending(x => x.Progress).ToList();

I am sure this can probably be optimized, but hopefully this will at least give you a starting point.

share|improve this answer
    
Ah! I didn't have a list of racers which was stupid but I've just set one up! If I understand you're code correctly, once I've got all the players in a list I'm just going to access their progress integer and order the list by that?? And then somehow output their position? –  kidshenlong Mar 29 '13 at 0:32
    
Having a little trouble figuring out what "x => x.Progress" does. Am I suppose to exchange those values for the progress int? Or does x automatically equal the object it's attached to? Sorry if that's a stupid question, just very new to lists and don't get if I'm suppose to explicitly define 'x' EDIT: No worries! Figured it out –  kidshenlong Mar 29 '13 at 1:03
    
Why Linq, when List<T> has a perfectly fine Sort method? I like Linq and use it a lot, but it's also more likely to create garbage. Sort is also clearer as to what's happening. –  michael.bartnett Mar 29 '13 at 1:34
    
Garbage? Tbh I had a bit of trouble finding clear list examples, would you mind linking to any? –  kidshenlong Mar 29 '13 at 1:42
    
@kidshenlong "Garbage" as in, little bits of memory that the garbage collector has to find and clean up later which were allocated while enumerating through a collection using Linq. Here's the List<T> documentation: msdn.microsoft.com/en-us/library/w56d4y5z.aspx –  michael.bartnett Mar 29 '13 at 1:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.