Player.cs 1.84 KB
Newer Older
Naliwe GS committed
1 2
using Assets.Scripts.API;
using UnityEngine;
Raphael committed
3
using UnityEngine.SceneManagement;
Naliwe GS committed
4 5 6 7 8 9

namespace Assets.Scripts
{
    public class Player : MonoBehaviour
    {
        private Transform _pos;
Naliwe GS committed
10 11
        private float _blinkTimer = 0f;
        private int _blinkCount = 0;
Naliwe GS committed
12

Naliwe GS committed
13
        public int Score = 0;
Naliwe GS committed
14
        public int Battery = 100;
Naliwe GS committed
15
        public int Progression = 0;
Naliwe GS committed
16
        public bool IsBlinking = false;
Raphael committed
17
        public int NbVials = 0;
Naliwe GS committed
18

Naliwe GS committed
19 20 21 22 23 24 25 26
        void Start()
        {
            _pos = GetComponent<Transform>();
        }

        void Update()
        {
            var newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
27
            newPos.z = -1;
Naliwe GS committed
28

Naliwe GS committed
29
            _pos.position = Vector3.Lerp(_pos.position, newPos, 10*Time.deltaTime);
Naliwe GS committed
30 31
            if (IsBlinking)
                Blink();
Raphael committed
32 33

            if (Battery < 12)
Naliwe GS committed
34
            {
Naliwe GS committed
35 36
                if (PlayerPrefs.GetInt("highscore") < Score)
                    PlayerPrefs.SetInt("highscore", Score);
37
				PlayerPrefs.SetInt("lastscore", Score);
PLN (Algolia) committed
38 39
				SceneManager.LoadScene("GameOver");

Naliwe GS committed
40
            }
Raphael committed
41
            if (Progression == 25)
Naliwe GS committed
42
            {
Naliwe GS committed
43
                Score += 200;
PLN (Algolia) committed
44
			}
Naliwe GS committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        }

        public void Blink()
        {
            _blinkTimer += Time.deltaTime;

            if (_blinkTimer > .16f)
            {
                GetComponent<SpriteRenderer>().enabled = !GetComponent<SpriteRenderer>().enabled;
                _blinkCount++;
                _blinkTimer = 0f;
            }

            if (_blinkCount != 7) return;
            _blinkCount = 0;
            _blinkTimer = 0f;
Naliwe GS committed
61
            GetComponent<BoxCollider2D>().enabled = true;
Naliwe GS committed
62 63 64 65 66
            IsBlinking = false;
        }

        public void SetBlinking()
        {
Naliwe GS committed
67
            GetComponent<BoxCollider2D>().enabled = false;
Naliwe GS committed
68 69
            GetComponent<SpriteRenderer>().enabled = false;
            IsBlinking = true;
Naliwe GS committed
70 71 72
        }
    }
}