first location is playable

enemies and so on
This commit is contained in:
MrTorex committed 2026-02-03 14:40:52 +03:00
1 parent 6fb38310bc
commit 2c6d4e500f
18 files changed
+2565 -31

No files matched your search

+3
View File
@@ -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
+7
View File
@@ -0,0 +1,7 @@
namespace Code.Gameplay.Enemies
{
public interface IEnemy
{
void TakeDamage();
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a15b885d0f044bddb4669eb39d19eb8e
timeCreated: 1770111890
+19
View File
@@ -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
+114
View File
@@ -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()
@@ -66,6 +81,13 @@ namespace Code.Gameplay.Player
}
}
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;
@@ -7,7 +7,7 @@ AnimationClip:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: penguin_atack
serializedVersion: 6
serializedVersion: 7
m_Legacy: 0
m_Compressed: 0
m_UseHighQualityCurve: 1
@@ -18,7 +18,8 @@ AnimationClip:
m_ScaleCurves: []
m_FloatCurves: []
m_PPtrCurves:
- curve:
- serializedVersion: 2
curve:
- time: 0
value: {fileID: 21300000, guid: 6059ad9a67fda844aa2d256b0f868934, type: 3}
- time: 0.083333336
@@ -29,6 +30,7 @@ AnimationClip:
path:
classID: 212
script: {fileID: 0}
flags: 2
m_SampleRate: 12
m_WrapMode: 0
m_Bounds:
@@ -43,6 +45,8 @@ AnimationClip:
typeID: 212
customType: 23
isPPtrCurve: 1
isIntCurve: 0
isSerializeReferenceCurve: 0
pptrCurveMapping:
- {fileID: 21300000, guid: 6059ad9a67fda844aa2d256b0f868934, type: 3}
- {fileID: 21300000, guid: 0dda9efeece02c3488735399dbffb0cd, type: 3}
@@ -57,7 +61,7 @@ AnimationClip:
m_Level: 0
m_CycleOffset: 0
m_HasAdditiveReferencePose: 0
m_LoopTime: 1
m_LoopTime: 0
m_LoopBlend: 0
m_LoopBlendOrientation: 0
m_LoopBlendPositionY: 0
@@ -1,27 +1,5 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1101 &-6514154243687919828
AnimatorStateTransition:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_Conditions: []
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 0}
m_Solo: 0
m_Mute: 0
m_IsExit: 1
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionOffset: 0
m_ExitTime: 0
m_HasExitTime: 1
m_HasFixedDuration: 1
m_InterruptionSource: 0
m_OrderedInterruption: 1
m_CanTransitionToSelf: 1
--- !u!91 &9100000
AnimatorController:
m_ObjectHideFlags: 0
@@ -76,8 +54,7 @@ AnimatorState:
m_Name: penguin_atack
m_Speed: 1
m_CycleOffset: 0
m_Transitions:
- {fileID: -6514154243687919828}
m_Transitions: []
m_StateMachineBehaviours: []
m_Position: {x: 50, y: 50, z: 0}
m_IKOnFeet: 0
File diff suppressed because it is too large. Load diff
Binary file not shown.
@@ -0,0 +1,143 @@
fileFormatVersion: 2
guid: 4e5626dd7e4d37a769dcea63a298b16d
TextureImporter:
internalIDToNameTable:
- first:
213: -7088430268270048823
second: HealthbarBorder_0
externalObjects: {}
serializedVersion: 13
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
flipGreenChannel: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
ignoreMipmapLimit: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 0
aniso: 1
mipBias: 0
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 2
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
swizzle: 50462976
cookieLightType: 0
platformSettings:
- serializedVersion: 4
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 4
buildTarget: Standalone
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
ignorePlatformSupport: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites:
- serializedVersion: 2
name: HealthbarBorder_0
rect:
serializedVersion: 2
x: 0
y: 0
width: 64
height: 64
alignment: 0
pivot: {x: 0.5, y: 0.5}
border: {x: 15, y: 15, z: 15, w: 15}
customData:
outline: []
physicsShape: []
tessellationDetail: -1
bones: []
spriteID: 9c903a42a36d0ad90800000000000000
internalID: -7088430268270048823
vertices: []
indices:
edges: []
weights: []
outline: []
customData:
physicsShape: []
bones: []
spriteID: 0af393af07ddf3aa4ae9604d61560fe1
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spriteCustomMetadata:
entries: []
nameFileIdTable:
HealthbarBorder_0: -7088430268270048823
mipmapLimitGroupName:
pSDRemoveMatte: 0
userData:
assetBundleName:
assetBundleVariant:
+1
View File
@@ -1,5 +1,6 @@
{
"dependencies": {
"com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask",
"com.unity.2d.animation": "13.0.4",
"com.unity.2d.aseprite": "3.0.1",
"com.unity.2d.psdimporter": "12.0.1",
+7
View File
@@ -1,5 +1,12 @@
{
"dependencies": {
"com.cysharp.unitask": {
"version": "https://github.com/Cysharp/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "73a63b7f672b88f7e9992f6917eb458a8cbb6fa9"
},
"com.unity.2d.animation": {
"version": "13.0.4",
"depth": 0,
+1 -1
View File
@@ -11,7 +11,7 @@ TagManager:
- Provides Contact
- Water
- UI
-
- Enemy
-
-
-