0

In my game i have many levels.Each level have 3 objectives.For completing one objective in each level the player gets one star.the value of all the stars player gets in all the levels is saved as totalstars.now the problem is i want increment the value of totalstars only once for each objective in each level.for example if players plays level 1 for first time and completes one objective he get 1 total star but when he again plays level one and completes only the same objective again the value of total stars should not increase.How can i do this? i hope i made it clear enough.Below is one of the objective.

void OnTriggerEnter2D (Collider2D Ball)
{
    EndGame.SetActive(true);
    WinStar.SetActive(true);
    PlayerPrefs.SetInt("Level" + CurrentLevel.ToString() + "_win", 1);  
    deactive();
    COuntAndTime();

    TotalStars.Totalstars += 1; //this value should increase only once for each objective in each level.
    TotalStars.totstar.Save();
}
zwcloud
  • 4,546
  • 3
  • 40
  • 69
Momin
  • 3
  • 1

3 Answers3

0

The most basic way to do this would be to create a N * 3 (n being the amount of levels) array. So with booleans. So it would like this

int[,] objectives = new bool[N, 3]
{true , false, true},
{false, false, false}

Then on increment just do a check whether they have already obtained the star or not

You could use a more dynamic structure or an object in each level which has these bools that you could query against.So in level you would have

CompletedObjectives objectives{get; set;}

and completed objectives would look like

Public class CompletedObjectives
{
bool ObjectiveOne {get ;set;}
bool ObjectiveTwo {get ;set;}
bool ObjectiveThree {get ;set;}
}

and same again query these before incrementing

I hope this helps

EDIT: EXTRA INFO

so c# is in a object oriented language so each level should be an object it would contain information about the level you will need to decide what makes information each level has such as level number and difficulty and stars. You will also want a virtual method called something like play level a virtual method is one that you can override in another class. If the method isnt implemented in the class the code below will throw an exception.

public class level
{
  public int level number {get; set;}

  public string difficulty {get ; set;}

  public CompletedObjectives objectives {get;set;}

  public virtual void PlayLevel()
  {
    System.NotImplementedException(); 
  }

}

so your next step it to make your levels

so example for level 1

public class Level1 : level
{
   this.levelnumber = 1;
   this.difficulty = "easy";

   public override void Playlevel()
   {
      //level logic goes here
   }

}

then your main would add all these levels to a List

List<Level> levels = new List<Level>{new Level1(), new Level2()}

so first time you call level1 from the list all the objectives are false, when they are completed you change their value to true and increment the total stars second time if they are true you dont increment.

If you need anymore help feel free to ask the above code is a rough outline to explain the basic way this can be done. Their are more advanced ways that provide more benefits but you have to start somewhere.

https://www.coursera.org/learn/game-programming

https://mva.microsoft.com/

youtube is also a great resource for learning basic programming. Their are a lot of books out their especially about object-oriented in general which will help you design your classes/interfaces etc better.

Ronan
  • 583
  • 5
  • 20
  • thanks for replying where can i get more information about this way as i am a beginner it will be difficult to implement this just by this – Momin Sep 13 '16 at 10:34
  • I added more explanations I will add links to learning resources in a moment – Ronan Sep 13 '16 at 13:29
0

There are many possible solutions. For example you can use three bool variables, so that each single star will have its own bool variable. Then create a read-only property int TotalStars and compute it as a number of true values in those three variables.

bool starA, starB, starC;

if(solved) starA = true; //an example

int TotalStars() {
  int sum = 0;
  if(starA) sum++;
  if(starB) sum++;
  if(starC) sum++;
  return sum;
}
Al Kepp
  • 5,831
  • 2
  • 28
  • 48
  • thanks for you suggestion maybe i might have used it wrong way but it wont work seperately for each level – Momin Sep 13 '16 at 10:39
-1

I would explore using Enums as Flags. It'll give you flexibility and speed while reducing the storage requirements and increasing readability.

The backing type for a enum is an integer. Integers are a series of bits, (0's or 1's). The basic premise is to use the bits as stores for the levels.

Example
00000000 - No bits are set, so no levels are set.
00000001 - The rightmost bit is set so first level is set.
00001001 - The 4th to right and rightmost bit is set so the 4th and first level is set.

https://msdn.microsoft.com/en-us/library/system.flagsattribute(v=vs.110).aspx

The below example uses a enum for storing the level state.

[Flags]
public enum Levels 
{
    Level01 = 1, /* note values are powers of 2*/
    Level02 = 2,
    Level03 = 4,
    Level04 = 8
}

void OnTriggerEnter2D (Collider2D Ball)
{
    EndGame.SetActive(true);
    WinStar.SetActive(true);

    /* Note enum value can be calculated as 2^Level 
       May want to internalize logic in method on PlayerPrefs class */
    PlayerPrefs.LevelState |= (int) Math.Pow(2, CurrentLevel);
    deactive();
    COuntAndTime();

    /* new method for determining TotalStars 
       May want to internalize logic in method on TotalStars Class */
    TotalStars.Totalstars = CountBits(PlayerPrefs.LevelState); 
    //this value should increase only once for each objective in each level.
    TotalStars.totstar.Save();
}

/* Method modified from http://stackoverflow.com/questions/12171584/what-is-the-fastest-way-to-count-set-bits-in-uint32-in-c-sharp 
Counts the number of set bits in Levels */
public static int CountBits(Levels value)
{    
    int count = 0;
    while (value != 0)
    {
        count++;
        value &= value - 1;
    }
    return count;
}
Paul Tsai
  • 893
  • 6
  • 16
  • thanks for your reply but it seems far too complicated for a beginner – Momin Sep 13 '16 at 10:36
  • I've removed the unit test and added links to make the solution more clear. In the original function, only 2 lines of code changes (note PlayerPrefs.LevelState is just an integer variable). The idea is to use an enum to represent the different levels. Learning how to use bitwise operators can greatly reduce complexity in your data structures. – Paul Tsai Sep 13 '16 at 16:25