Reputation: 193
Hi I have a 2d topdown rpg game, and I wanted to know if there was a way of locking my game for a certain amount of time (countdown) when my player loses all of his hearts (lives). And no matter what the user can not access my game until the countdown has complete. Just like candy crush when you lose all of your lives. So far I've got a panel that slides down, saying GameOver, when the player dies (when all of his hearts are gone). But I'm not really sure how to put a lock on my game for a certain amount of time, I also want the user to sill go back to the main menu even if the game is still lock. Like I said before I want it similar to candy crush saga where if the player loses live then they will have to wait a certain amount of time to play the game.
This script is the health script of my player, as you can see my game over panel is linked to this as well as my UI manager:
public int curHealth;
public int maxHealth = 3;
Vector3 startPosition;
public PlayerHealth playerhealthRef;
float counter;
public Animator anima; // drag the panel in here again
private UI_ManagerScripts UIM;
private PlayerScript ps;
void Start ()
{
curHealth = maxHealth;
startPosition = transform.position;
ps = GameObject.FindGameObjectWithTag("PlayerScript").GetComponent <PlayerScript> ();
}
void Update ()
{
if (curHealth > maxHealth) {
curHealth = maxHealth;
}
if (curHealth <= 0) {
Die ();
}
}
void Awake()
{
UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts> ();
}
void Die(){
if (PlayerPrefs.HasKey ("Highscore")) {
if (PlayerPrefs.GetInt ("Highscore") < ps.Score) {
PlayerPrefs.SetInt ("Highscore", ps.Score);
}
} else
{
PlayerPrefs.SetInt ("Highscore", ps.Score);
}
UIM.EnableBoolAnimator(anima);
}
public void Damage(int dmg)
{
curHealth -= dmg;
Reset();
}
void Reset ()
{
transform.position = startPosition;
GotoMouse.target = startPosition;
}
}
This is my UImanager script:
public AudioClip swooshSound;
public void DisableBoolAnimator(Animator anim)
{
anim.SetBool ("IsDisplayed", false);
}
public void EnableBoolAnimator(Animator anim)
{
anim.SetBool ("IsDisplayed", true);
}
public void NavigateTo(int scene)
{
Application.LoadLevel (scene);
}
public void ExitGame()
{
Application.Quit ();
}
public void PauseGame()
{
AudioSource.PlayClipAtPoint (swooshSound, transform.position);
Time.timeScale = 0;
}
public void UnPauseGame()
{
Time.timeScale = 1;
}
}
Thank you :)
Upvotes: 1
Views: 215
Reputation: 612
If you want to have the player wait for a certain amount of time, you should save a DateTime
to a file. Then, when the player tries to play, have the game check if the date/time is later than the date/time saved to the file. Something like this:
//For saving the file when the player loses
File.WriteAllText("path_to_file", DateTime.Now.AddHours(1).ToString());
//For checking to see if the player can play
if(Convert.ToDateTime(File.ReadAllText("path_to_file")) < DateTime.Now)
{
//Allow player to play
}
else {
//Tell player they can't play
}
Then change DateTime.Now.AddHours(1)
to however many hours you want the player to wait, or you can change it to DateTime.Now.AddMinutes(1)
to however many minutes you want the player to wait. Hope this helped :)
Upvotes: 1