first location is playable
enemies and so on
This commit is contained in:
1 parent
6fb38310bc
commit
2c6d4e500f
18 files changed
+2565
-31
No files matched your search
@@ -0,0 +1,114 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
using Code.Gameplay.Enemies;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Code.Gameplay.Player
|
||||
{
|
||||
public class PlayerHealth : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private int _maxHealth = 100;
|
||||
[SerializeField] private int _damageFromEnemy = 25;
|
||||
|
||||
[SerializeField] private float _invincibilityDuration = 2f;
|
||||
|
||||
[SerializeField] private float _attackCooldown = 0.5f;
|
||||
[SerializeField] private float _attackRange = 2f;
|
||||
[SerializeField] private LayerMask _enemyLayer;
|
||||
|
||||
[SerializeField] private Slider _healthBar;
|
||||
|
||||
private int _currentHealth;
|
||||
private bool _isInvincible;
|
||||
private float _invincibilityTimer;
|
||||
private float _lastAttackTime;
|
||||
public float nextAttackTime => _lastAttackTime + _attackCooldown;
|
||||
|
||||
public event Action<int> OnHealthChanged;
|
||||
public event Action OnDied;
|
||||
public event Action OnTookDamage;
|
||||
public event Action OnAttacked;
|
||||
|
||||
public bool IsAlive => _currentHealth > 0;
|
||||
public bool CanTakeDamage => !_isInvincible && IsAlive;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_currentHealth = _maxHealth;
|
||||
OnHealthChanged?.Invoke(_currentHealth);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateInvincibility();
|
||||
_healthBar.value = (float)_currentHealth / _maxHealth;
|
||||
}
|
||||
|
||||
public void TakeDamage(bool fatal = false)
|
||||
{
|
||||
if (!CanTakeDamage) return;
|
||||
|
||||
if (fatal)
|
||||
_currentHealth = 0;
|
||||
else
|
||||
_currentHealth -= _damageFromEnemy;
|
||||
OnHealthChanged?.Invoke(_currentHealth);
|
||||
OnTookDamage?.Invoke();
|
||||
|
||||
if (_currentHealth <= 0)
|
||||
{
|
||||
Die();
|
||||
return;
|
||||
}
|
||||
|
||||
StartInvincibility();
|
||||
}
|
||||
|
||||
public void TryAttack()
|
||||
{
|
||||
if (Time.time < nextAttackTime) return;
|
||||
|
||||
_lastAttackTime = Time.time;
|
||||
OnAttacked?.Invoke();
|
||||
|
||||
var hits = Physics2D.OverlapCircleAll(transform.position, _attackRange, _enemyLayer);
|
||||
foreach (var hit in hits)
|
||||
{
|
||||
if (hit.TryGetComponent(out IEnemy enemy))
|
||||
{
|
||||
enemy.TakeDamage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void StartInvincibility()
|
||||
{
|
||||
_isInvincible = true;
|
||||
_invincibilityTimer = _invincibilityDuration;
|
||||
}
|
||||
|
||||
private void UpdateInvincibility()
|
||||
{
|
||||
if (!_isInvincible) return;
|
||||
|
||||
_invincibilityTimer -= Time.deltaTime;
|
||||
if (_invincibilityTimer <= 0f)
|
||||
{
|
||||
_isInvincible = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
_healthBar.value = 0;
|
||||
OnDied?.Invoke();
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
Gizmos.color = Color.red;
|
||||
Gizmos.DrawWireSphere(transform.position, _attackRange);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 843803cd5581423b98437ac2b94fd754
|
||||
timeCreated: 1770111850
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Code.Input;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace Code.Gameplay.Player
|
||||
{
|
||||
@@ -9,6 +11,9 @@ namespace Code.Gameplay.Player
|
||||
[SerializeField] private Transform _groundCheck;
|
||||
[SerializeField] private LayerMask _groundLayer;
|
||||
[SerializeField] private Animator _animator;
|
||||
[SerializeField] private SpriteRenderer _spriteRenderer;
|
||||
|
||||
[SerializeField] private PlayerHealth _playerHealth;
|
||||
|
||||
[SerializeField] private float _moveSpeed = 5f;
|
||||
[SerializeField] private float _sprintSpeedMultiplier = 1.8f;
|
||||
@@ -20,10 +25,13 @@ namespace Code.Gameplay.Player
|
||||
private IInputService _input;
|
||||
private bool _isJumping;
|
||||
private float _jumpTimer;
|
||||
private bool _isAttacking;
|
||||
private float _attackEndTime;
|
||||
|
||||
private RuntimeAnimatorController _idleAnimator;
|
||||
private RuntimeAnimatorController _walkAnimator;
|
||||
private RuntimeAnimatorController _jumpAnimator;
|
||||
private RuntimeAnimatorController _attackAnimator;
|
||||
|
||||
public bool isGrounded { get; private set; }
|
||||
|
||||
@@ -34,6 +42,13 @@ namespace Code.Gameplay.Player
|
||||
_idleAnimator = Resources.Load<RuntimeAnimatorController>("Animations/penguin_idle_01");
|
||||
_walkAnimator = Resources.Load<RuntimeAnimatorController>("Animations/penguin_walk_01");
|
||||
_jumpAnimator = Resources.Load<RuntimeAnimatorController>("Animations/penguin_jump_01");
|
||||
_attackAnimator = Resources.Load<RuntimeAnimatorController>("Animations/penguin_atack_01");
|
||||
_playerHealth.OnTookDamage += StartBlinking;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
_playerHealth.OnTookDamage -= StartBlinking;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -65,6 +80,13 @@ namespace Code.Gameplay.Player
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_input.AttackPressed && !_isAttacking && Time.time >= _playerHealth.nextAttackTime)
|
||||
{
|
||||
_isAttacking = true;
|
||||
_attackEndTime = Time.time + 0.3f;
|
||||
_playerHealth?.TryAttack();
|
||||
}
|
||||
|
||||
UpdateFacingDirection();
|
||||
UpdateAnimations();
|
||||
@@ -82,6 +104,19 @@ namespace Code.Gameplay.Player
|
||||
|
||||
private void UpdateAnimations()
|
||||
{
|
||||
if (_isAttacking)
|
||||
{
|
||||
if (_animator.runtimeAnimatorController != _attackAnimator)
|
||||
_animator.runtimeAnimatorController = _attackAnimator;
|
||||
|
||||
if (Time.time >= _attackEndTime)
|
||||
{
|
||||
_isAttacking = false;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var isMoving = Mathf.Abs(_input.MoveInput) > 0.1f;
|
||||
var isJumping = !isGrounded;
|
||||
|
||||
@@ -124,6 +159,23 @@ namespace Code.Gameplay.Player
|
||||
}
|
||||
}
|
||||
|
||||
private async void StartBlinking()
|
||||
{
|
||||
_spriteRenderer.color = new Color(1, 1, 1, 0.5f); // полупрозрачность
|
||||
float blinkDuration = 2f;
|
||||
float blinkInterval = 0.1f;
|
||||
int blinkCount = (int)(blinkDuration / blinkInterval);
|
||||
|
||||
for (int i = 0; i < blinkCount; i++)
|
||||
{
|
||||
_spriteRenderer.enabled = !_spriteRenderer.enabled;
|
||||
await UniTask.Delay(TimeSpan.FromSeconds(blinkInterval));
|
||||
}
|
||||
|
||||
_spriteRenderer.enabled = true;
|
||||
_spriteRenderer.color = Color.white;
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
if (_groundCheck == null) return;
|
||||
|
||||
Reference in new issue
Block a user