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,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9d1a832c72cd4f69b786f17086a70a46
|
||||
timeCreated: 1770111882
|
||||
@@ -0,0 +1,99 @@
|
||||
using UnityEngine;
|
||||
using Code.Gameplay.Player;
|
||||
using DG.Tweening;
|
||||
|
||||
namespace Code.Gameplay.Enemies
|
||||
{
|
||||
public class BasicEnemy : MonoBehaviour, IEnemy
|
||||
{
|
||||
[SerializeField] private float _speed = 2f;
|
||||
[SerializeField] private float _patrolDistance = 3f;
|
||||
|
||||
[SerializeField] private int _health = 2;
|
||||
|
||||
[SerializeField] private SpriteRenderer _spriteRenderer;
|
||||
|
||||
[SerializeField] private float _blinkDuration = 0.2f;
|
||||
[SerializeField] private Color _hitColor = Color.red;
|
||||
|
||||
private Color _originalColor;
|
||||
|
||||
private Vector3 _startPosition;
|
||||
private bool _facingRight = true;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_startPosition = transform.position;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
Patrol();
|
||||
}
|
||||
|
||||
private void Patrol()
|
||||
{
|
||||
var targetX = _facingRight
|
||||
? _startPosition.x + _patrolDistance
|
||||
: _startPosition.x - _patrolDistance;
|
||||
|
||||
transform.position = Vector3.MoveTowards(
|
||||
transform.position,
|
||||
new Vector3(targetX, transform.position.y, transform.position.z),
|
||||
_speed * Time.deltaTime
|
||||
);
|
||||
|
||||
if (Mathf.Approximately(transform.position.x, targetX))
|
||||
{
|
||||
Flip();
|
||||
}
|
||||
}
|
||||
|
||||
private void Flip()
|
||||
{
|
||||
_facingRight = !_facingRight;
|
||||
transform.localScale = new Vector3(
|
||||
-transform.localScale.x,
|
||||
transform.localScale.y,
|
||||
transform.localScale.z
|
||||
);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (!other.CompareTag("Player")) return;
|
||||
|
||||
var playerHealth = other.GetComponent<PlayerHealth>();
|
||||
playerHealth?.TakeDamage();
|
||||
}
|
||||
|
||||
public void TakeDamage()
|
||||
{
|
||||
_health--;
|
||||
BlinkHit();
|
||||
if (_health <= 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
private void BlinkHit()
|
||||
{
|
||||
var originalColor = _spriteRenderer.color;
|
||||
var hitColor = new Color(_hitColor.r, _hitColor.g, _hitColor.b, originalColor.a);
|
||||
|
||||
DOTween.Sequence()
|
||||
.Append(_spriteRenderer.DOColor(hitColor, _blinkDuration / 4))
|
||||
.Append(_spriteRenderer.DOColor(originalColor, _blinkDuration / 4))
|
||||
.Append(_spriteRenderer.DOColor(hitColor, _blinkDuration / 4))
|
||||
.Append(_spriteRenderer.DOColor(originalColor, _blinkDuration / 4))
|
||||
.SetEase(Ease.Linear)
|
||||
.SetLink(gameObject);
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1dd5d179d7dc4551ad044f2f8a8986ae
|
||||
timeCreated: 1770111900
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Code.Gameplay.Enemies
|
||||
{
|
||||
public interface IEnemy
|
||||
{
|
||||
void TakeDamage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a15b885d0f044bddb4669eb39d19eb8e
|
||||
timeCreated: 1770111890
|
||||
@@ -0,0 +1,19 @@
|
||||
using Code.Gameplay.Player;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Gameplay.Enemies
|
||||
{
|
||||
public class VoidEnemy : MonoBehaviour, IEnemy
|
||||
{
|
||||
public void TakeDamage() { }
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D other)
|
||||
{
|
||||
if (other.CompareTag("Player"))
|
||||
{
|
||||
var playerHealth = other.GetComponent<PlayerHealth>();
|
||||
playerHealth?.TakeDamage(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 64a245a9516a4ec6a617ded117483d32
|
||||
timeCreated: 1770112280
|
||||
@@ -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