Files
Gnome-s-Bounty/Assets/Scripts/Player.cs
T
2023-06-28 22:53:31 +03:00

68 lines
1.7 KiB
C#

using UnityEngine;
public class Player : Character
{
[SerializeField]
private Transform _hammerSpawnPoint;
[SerializeField]
private GameObject _hammerPrefab;
private int _hammerSpeed = 10;
private int _totalCoins = 0;
private bool _hasKey = false;
public static Player Instance { get; private set; }
private GameObject _hammer;
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
Debug.Log("There's more than one player instance");
return;
}
PlayerPrefs.SetString("lastExitName", string.Empty);
Instance = this;
DontDestroyOnLoad(gameObject);
}
public void AddCoin()
{
_totalCoins++;
print($"player have {_totalCoins} coins");
}
public void SetKey()
{
print($"player have key");
_hasKey = true;
}
public void RemoveKey()
{
_hasKey = false;
}
public bool IsHasKey()
{
return _hasKey;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && !_isFalling)
{
if (_hammer == null)
{
_hammer = Instantiate(_hammerPrefab, _hammerSpawnPoint.position, _hammerSpawnPoint.rotation);
_hammer.transform.localScale = new Vector2(_hammer.transform.localScale.x * (_facingRight ? 1 : -1), _hammer.transform.localScale.y);
_hammer.GetComponent<Rigidbody2D>().velocity = new Vector2(gameObject.transform.localScale.x * _hammerSpeed, 0);
}
}
base.MoveTo(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
}