added endscreen

This commit is contained in:
MrTorex committed 2026-02-24 10:33:33 +03:00
1 parent a33eb50ca0
commit 2b01c50795
15 files changed
+989 -88

No files matched your search

@@ -24,9 +24,8 @@ namespace Code.Gameplay.Camera
private void Awake()
{
_camera = GetComponent<UnityEngine.Camera>();
if (_camera == null)
if (!_camera)
{
Debug.LogError("CameraController requires a Camera component!");
enabled = false;
return;
}
@@ -56,12 +55,12 @@ namespace Code.Gameplay.Camera
private void UpdateCamera(float deltaTime, Vector3 lookInput)
{
var targetRotation = HandleRotation(deltaTime, lookInput);
HandleRotation(deltaTime, lookInput);
HandlePosition(deltaTime, targetRotation);
HandlePosition(deltaTime);
}
private Quaternion HandleRotation(float deltaTime, Vector3 lookInput)
private void HandleRotation(float deltaTime, Vector3 lookInput)
{
var horizontalRotation = Quaternion.Euler(Vector3.up * (lookInput.x * rotationSpeed));
_planarDirection = horizontalRotation * _planarDirection;
@@ -73,11 +72,9 @@ namespace Code.Gameplay.Camera
var targetRotation = planarRotation * verticalRotation;
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, deltaTime * rotationSharpness);
return targetRotation;
}
private void HandlePosition(float deltaTime, Quaternion targetRotation)
private void HandlePosition(float deltaTime)
{
_currentFollowPosition = Vector3.Lerp(
_currentFollowPosition,
@@ -102,15 +99,5 @@ namespace Code.Gameplay.Camera
{
return _planarDirection;
}
public void SetFollowTransform(Transform target)
{
followTransform = target;
if (target == null) return;
_currentFollowPosition = target.position;
_planarDirection = target.forward;
}
}
}
@@ -10,6 +10,5 @@ namespace Code.Gameplay.Character.Core
public Vector3 CameraPlanarDirection;
public bool JumpPressed;
public bool SprintPressed;
public bool SlidePressed;
}
}
@@ -18,25 +18,12 @@ namespace Code.Gameplay.Character.Core
private IInputService _inputService;
private CameraController _cameraController;
public CustomCharacterController CustomCharacterController => customCharacterController;
public HealthSystem HealthSystem => healthSystem;
public WeaponInventory WeaponInventory => weaponInventory;
public Transform GetFollowTransform() => cameraFollowPoint;
private void Start()
{
_inputService = FindFirstObjectByType<DIContainer>()?.Resolve<IInputService>();
_cameraController = FindFirstObjectByType<CameraController>();
if (_inputService == null)
{
Debug.LogError("IInputService not found!");
}
if (_cameraController == null)
{
Debug.LogError("CameraController not found!");
}
if (healthSystem != null)
{
@@ -55,15 +42,6 @@ namespace Code.Gameplay.Character.Core
Debug.Log("Character died!");
}
public void Teleport(Vector3 position, Quaternion rotation)
{
if (customCharacterController != null)
{
customCharacterController.SetPosition(position);
customCharacterController.SetRotation(rotation);
}
}
private void HandleCharacterInput()
{
if (_inputService == null || _cameraController == null || customCharacterController == null)
@@ -24,10 +24,7 @@ namespace Code.Gameplay.Character.Controller
private float _jumpCooldownTimer;
private float JumpCooldownDuration => jumpCooldownDuration;
public bool CanJump => _jumpCooldownTimer <= 0f;
public void SetPosition(Vector3 position) => motor.SetPosition(position);
public void SetRotation(Quaternion rotation) => motor.SetRotation(rotation);
private void Start()
{
_inputHandler = new CharacterInputHandler(
@@ -7,10 +7,10 @@ namespace Code.Gameplay.Character.Inventory
{
public class WeaponInventory : MonoBehaviour
{
[SerializeField] private List<WeaponBase> weapons = new List<WeaponBase>();
[SerializeField] private List<WeaponBase> weapons = new();
[SerializeField] private Transform weaponHolder;
[SerializeField] private int currentWeaponIndex = 0;
[SerializeField] private int currentWeaponIndex;
public event Action<WeaponBase> OnWeaponSwitched;
public event Action OnWeaponsInitialized;
@@ -54,13 +54,13 @@ namespace Code.Gameplay.Character.Inventory
public void NextWeapon()
{
int nextIndex = (currentWeaponIndex + 1) % weapons.Count;
var nextIndex = (currentWeaponIndex + 1) % weapons.Count;
SwitchWeapon(nextIndex);
}
public void PreviousWeapon()
{
int prevIndex = (currentWeaponIndex - 1 + weapons.Count) % weapons.Count;
var prevIndex = (currentWeaponIndex - 1 + weapons.Count) % weapons.Count;
SwitchWeapon(prevIndex);
}
@@ -68,7 +68,7 @@ namespace Code.Gameplay.Character.Inventory
{
if (weapons.Contains(weapon))
{
int index = weapons.IndexOf(weapon);
var index = weapons.IndexOf(weapon);
weapons.Remove(weapon);
Destroy(weapon.gameObject);
+3 -3
View File
@@ -64,7 +64,7 @@ namespace Code.Gameplay.Enemy
private void Update()
{
if (!IsAlive || target == null) return;
if (!IsAlive || !target) return;
var distanceToTarget = Vector3.Distance(transform.position, target.position);
@@ -115,7 +115,7 @@ namespace Code.Gameplay.Enemy
private void Attack()
{
if (Time.time - lastAttackTime < attackCooldown) return;
if (weapon == null) return;
if (!weapon) return;
if (!HasLineOfSight()) return;
@@ -128,7 +128,7 @@ namespace Code.Gameplay.Enemy
private bool HasLineOfSight()
{
if (target == null) return false;
if (!target) return false;
var direction = (target.position - weaponPoint.position).normalized;
var distance = Vector3.Distance(weaponPoint.position, target.position);
+2 -2
View File
@@ -47,12 +47,12 @@ namespace Code.Gameplay.Weapons
var damageable = hit.collider.GetComponentInParent<IDamageable>();
damageable?.TakeDamage(_damage);
if (impactEffect != null)
if (impactEffect)
{
Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
if (impactSound != null && _audioSource != null)
if (impactSound && _audioSource)
{
_audioSource.PlayOneShot(impactSound);
}
-1
View File
@@ -2,7 +2,6 @@ namespace Code.Gameplay.Weapons
{
public class Rifle : WeaponBase
{
protected override void Awake()
{
base.Awake();
+6 -6
View File
@@ -104,10 +104,10 @@ namespace Code.Gameplay.Weapons
SpawnBullet();
if (muzzleFlash != null)
if (muzzleFlash)
muzzleFlash.Play();
if (shootSound != null)
if (shootSound)
audioSource.PlayOneShot(shootSound);
OnShoot();
@@ -115,7 +115,7 @@ namespace Code.Gameplay.Weapons
protected virtual void SpawnBullet()
{
if (bulletPrefab == null) return;
if (!bulletPrefab) return;
var spread = Random.insideUnitSphere * (spreadAngle / 100f);
var direction = (_firePoint.forward + spread).normalized;
@@ -123,7 +123,7 @@ namespace Code.Gameplay.Weapons
var bullet = Instantiate(bulletPrefab, _firePoint.position, Quaternion.LookRotation(direction));
var bulletComponent = bullet.GetComponent<Bullet>();
if (bulletComponent != null)
if (bulletComponent)
{
bulletComponent.Initialize(bulletSpeed, damage, direction);
}
@@ -159,7 +159,7 @@ namespace Code.Gameplay.Weapons
{
isReloading = true;
if (reloadSound != null)
if (reloadSound)
audioSource.PlayOneShot(reloadSound);
yield return new WaitForSeconds(reloadTime);
@@ -178,7 +178,7 @@ namespace Code.Gameplay.Weapons
if (Time.time - lastShotTryTime < fireRate)
return;
lastShotTryTime = Time.time;
if (emptySound != null)
if (emptySound)
audioSource.PlayOneShot(emptySound);
}
+4 -4
View File
@@ -45,7 +45,7 @@ namespace Code.UI
private void UpdateWeaponName()
{
if (weaponNameText == null || _weaponInventory?.CurrentWeapon == null) return;
if (!weaponNameText || !_weaponInventory?.CurrentWeapon) return;
var weaponName = _weaponInventory.CurrentWeapon.WeaponName;
weaponNameText.text = string.Format(weaponNameFormat, weaponName.ToUpper());
@@ -53,7 +53,7 @@ namespace Code.UI
private void UpdateHealth()
{
if (healthText == null || _playerHealth == null) return;
if (!healthText || !_playerHealth) return;
healthText.text = string.Format(healthFormat,
_playerHealth.CurrentHealth,
@@ -62,13 +62,13 @@ namespace Code.UI
private void UpdateHealth(float current, float max)
{
if (healthText == null) return;
if (!healthText) return;
healthText.text = string.Format(healthFormat, current, max);
}
private void UpdateAmmo()
{
if (ammoText == null || _weaponInventory?.CurrentWeapon == null) return;
if (!ammoText || !_weaponInventory?.CurrentWeapon) return;
var weapon = _weaponInventory.CurrentWeapon;
ammoText.text = string.Format(ammoFormat,
+107 -5
View File
@@ -1,25 +1,127 @@
using Code.DI;
using Code.Gameplay.Character.Core;
using Code.Gameplay.Enemy;
using Code.Input;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace Code.UI
{
public class GameplayController : MonoBehaviour
public class GameplayController : MonoBehaviour
{
[SerializeField] private GameObject endScreen;
[SerializeField] private TMP_Text endText;
[SerializeField] private Button restartButton;
[SerializeField] private Button exitButton;
[SerializeField] private float fadeDuration = 0.5f;
[SerializeField] private float buttonDelay = 0.2f;
[SerializeField] private Ease showEase = Ease.OutBack;
private IInputService _inputService;
private AudioSource _audioSource;
private CharacterProvider _player;
private void Start()
{
var container = FindFirstObjectByType<DIContainer>();
_inputService = container.Resolve<IInputService>();
_inputService.EnableMap(ActionMapping.Gameplay);
_audioSource = FindFirstObjectByType<AudioSource>();
}
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
_player = FindFirstObjectByType<CharacterProvider>();
if (_player != null && _player.HealthSystem != null)
{
_player.HealthSystem.OnDied += HandlePlayerDeath;
}
restartButton.onClick.AddListener(RestartGame);
exitButton.onClick.AddListener(ExitToMenu);
endScreen.SetActive(false);
}
private void Update()
{
if (!endScreen.activeInHierarchy)
{
CheckVictoryCondition();
}
}
private void CheckVictoryCondition()
{
var enemies = FindObjectsByType<SimpleEnemy>(FindObjectsSortMode.None);
if (enemies.Length == 0)
{
ShowEndScreen("You Won!", Color.green);
}
}
private void HandlePlayerDeath()
{
ShowEndScreen("You Died!", Color.red);
}
private void ShowEndScreen(string message, Color color)
{
_inputService.DisableMap(ActionMapping.Gameplay);
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
endScreen.SetActive(true);
endText.text = message;
endText.color = color;
var sequence = DOTween.Sequence();
endScreen.transform.localScale = Vector3.zero;
endText.rectTransform.localScale = Vector3.zero;
restartButton.transform.localScale = Vector3.zero;
exitButton.transform.localScale = Vector3.zero;
sequence.Append(endScreen.transform.DOScale(1f, fadeDuration).SetEase(showEase))
.Join(endText.rectTransform.DOScale(1f, fadeDuration).SetEase(showEase))
.Append(restartButton.transform.DOScale(1f, fadeDuration).SetEase(showEase))
.Append(exitButton.transform.DOScale(1f, fadeDuration).SetEase(showEase));
endText.rectTransform.DOScale(1.1f, 1f)
.SetLoops(-1, LoopType.Yoyo)
.SetEase(Ease.InOutSine);
}
private void RestartGame()
{
restartButton.transform.DOPunchScale(Vector3.one * 0.2f, 0.1f);
SceneManager.LoadScene("Gameplay");
}
private void ExitToMenu()
{
exitButton.transform.DOPunchScale(Vector3.one * 0.2f, 0.1f);
SceneManager.LoadScene("MainMenu");
}
private void OnDestroy()
{
_inputService.DisableMap(ActionMapping.Gameplay);
if (_player != null && _player.HealthSystem != null)
{
_player.HealthSystem.OnDied -= HandlePlayerDeath;
}
restartButton?.onClick.RemoveListener(RestartGame);
exitButton?.onClick.RemoveListener(ExitToMenu);
DOTween.KillAll();
}
}
}