using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class PlayerController : MonoBehaviour
{
private float moveSpeed = 7f;
private Rigidbody2D _rb;
public Animator anim;
public SpriteRenderer sprite;
public TMP_Text chocoText;
public GameObject youLose;
public Joystick joy;
public static int choco;
public static bool stop;
public static int lives = 1;
public TMP_Text livesText;
public static int timerSecondsRecord;
public static int timerMinutesRecord;
public static int timerHoursRecord;
public TMP_Text timerRecordText;
private static string timerSecondsRecordToString;
private static string timerMinutesRecordToString;
private static string timerHoursRecordToString;
void Start()
{
_rb = GetComponent<Rigidbody2D>();
if (PlayerPrefs.HasKey("TimerSecondsRecord"))
{
timerSecondsRecord = PlayerPrefs.GetInt("TimerSecondsRecord");
}
if (PlayerPrefs.HasKey("TimerMinutesRecord"))
{
timerMinutesRecord = PlayerPrefs.GetInt("TimerMinutesRecord");
}
if (PlayerPrefs.HasKey("TimerHoursRecord"))
{
timerMinutesRecord = PlayerPrefs.GetInt("TimerHoursRecord");
}
}
void Update()
{
livesText.text = "Жизней:" + lives.ToString();
TimerRecord();
CheckLifes();
CheckChoco();
Stop();
chocoText.text = "Шоколадок:" + choco.ToString();
}
void FixedUpdate()
{
float move = joy.Horizontal;
_rb.velocity = new Vector2(move * moveSpeed, _rb.velocity.y);
if (move > 0)
{
sprite.flipX = false;
}
if (move < 0)
{
sprite.flipX = true;
}
if (move == 0)
{
anim.SetBool("Walk", false);
}
else
{
anim.SetBool("Walk", true);
}
}
private void Stop()
{
if (stop)
{
Time.timeScale = 0f;
}
else
{
Time.timeScale = 1f;
}
}
private void CheckLifes()
{
if (lives > 3)
{
lives = 3;
}
if (lives <= 0)
{
youLose.SetActive(true);
stop = true;
}
}
private void CheckChoco()
{
if (choco < 0)
{
lives -= 1;
choco = 0;
}
if (choco >= 15 && lives < 3)
{
lives++;
choco = 0;
}
if (choco >= 15 & lives >= 3)
{
choco = 15;
}
}
void TimerRecord()
{
int timerSecondsRecordPlusTimerMinutesRecord = (timerMinutesRecord * 60) + timerSecondsRecord;
Debug.Log(timerSecondsRecordPlusTimerMinutesRecord);
if (Timer.secondsRecord > timerSecondsRecordPlusTimerMinutesRecord)
{
timerSecondsRecord = Timer.secondsRecord;
PlayerPrefs.SetInt("TimerSecondsRecord", timerSecondsRecord);
}
if (timerSecondsRecord >= 60)
{
timerMinutesRecord += 1;
timerSecondsRecord = 0;
Timer.secondsRecord -= 60;
PlayerPrefs.SetInt("TimerMinutesRecord", timerMinutesRecord);
PlayerPrefs.SetInt("TimerSecondsRecord", timerSecondsRecord);
}
if (timerMinutesRecord >= 60)
{
timerHoursRecord += 1;
timerMinutesRecord -= 60;
PlayerPrefs.SetInt("TimerHoursRecord", timerHoursRecord);
PlayerPrefs.SetInt("TimerMinutesRecord", timerSecondsRecord);
}
timerSecondsRecordToString = PlayerPrefs.GetInt("TimerSecondsRecord").ToString();
timerMinutesRecordToString = PlayerPrefs.GetInt("TimerMinutesRecord").ToString();
timerHoursRecordToString = PlayerPrefs.GetInt("TimerHoursRecord").ToString();
if (timerSecondsRecord < 10)
{
timerSecondsRecordToString = "0" + timerSecondsRecordToString;
}
if (timerMinutesRecord < 10)
{
timerMinutesRecordToString = "0" + timerMinutesRecordToString;
}
if (timerHoursRecord < 10)
{
timerHoursRecordToString = "0" + timerHoursRecordToString;
}
timerRecordText.text = "Ваш рекорд:" + timerHoursRecordToString + ":" + timerMinutesRecordToString + ":" + timerSecondsRecordToString;
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Brick")
{
lives -= 1;
}
if (other.gameObject.tag == "Chocolate")
{
choco += 1;
}
}
}