initial weapons
This commit is contained in:
1 parent
b87010d2e5
commit
afcd8761e6
66 files changed
+6388
-49
No files matched your search
@@ -1,6 +1,8 @@
|
||||
using Code.DI;
|
||||
using Code.Gameplay.Camera;
|
||||
using Code.Gameplay.Character.Controller;
|
||||
using Code.Gameplay.Character.Inventory;
|
||||
using Code.Gameplay.Character.Stats;
|
||||
using Code.Input;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -9,25 +11,64 @@ namespace Code.Gameplay.Character.Core
|
||||
public class CharacterProvider : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private CustomCharacterController customCharacterController;
|
||||
public CustomCharacterController CustomCharacterController => customCharacterController;
|
||||
|
||||
[SerializeField] private Transform cameraFollowPoint;
|
||||
public Transform GetFollowTransform() => cameraFollowPoint;
|
||||
[SerializeField] private HealthSystem healthSystem;
|
||||
[SerializeField] private WeaponInventory weaponInventory;
|
||||
|
||||
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>();
|
||||
_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)
|
||||
{
|
||||
healthSystem.OnDied += HandleDeath;
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleDeath()
|
||||
{
|
||||
enabled = false;
|
||||
if (customCharacterController != null)
|
||||
{
|
||||
customCharacterController.enabled = false;
|
||||
}
|
||||
|
||||
Debug.Log("Character died!");
|
||||
}
|
||||
|
||||
public void Teleport(Vector3 position, Quaternion rotation)
|
||||
{
|
||||
customCharacterController.SetPosition(position);
|
||||
customCharacterController.SetRotation(rotation);
|
||||
if (customCharacterController != null)
|
||||
{
|
||||
customCharacterController.SetPosition(position);
|
||||
customCharacterController.SetRotation(rotation);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCharacterInput()
|
||||
{
|
||||
if (_inputService == null || _cameraController == null || customCharacterController == null)
|
||||
return;
|
||||
|
||||
var inputs = new CharacterInputs();
|
||||
var moveInput = _inputService.MoveInput;
|
||||
inputs.MoveAxisForward = moveInput.y;
|
||||
@@ -35,15 +76,63 @@ namespace Code.Gameplay.Character.Core
|
||||
inputs.SprintPressed = _inputService.SprintHeld;
|
||||
inputs.JumpPressed = _inputService.JumpPressed;
|
||||
|
||||
inputs.CameraRotation = FindFirstObjectByType<CameraController>().GetCameraRotation();
|
||||
inputs.CameraPlanarDirection = FindFirstObjectByType<CameraController>().GetPlanarDirection();
|
||||
inputs.CameraRotation = _cameraController.GetCameraRotation();
|
||||
inputs.CameraPlanarDirection = _cameraController.GetPlanarDirection();
|
||||
|
||||
customCharacterController.SetInputs(ref inputs);
|
||||
|
||||
HandleWeaponInput();
|
||||
}
|
||||
|
||||
private void HandleWeaponInput()
|
||||
{
|
||||
if (weaponInventory == null) return;
|
||||
|
||||
if (_inputService.AttackPressed)
|
||||
{
|
||||
weaponInventory.ShootCurrentWeapon();
|
||||
}
|
||||
else
|
||||
{
|
||||
weaponInventory.StopShootingCurrentWeapon();
|
||||
}
|
||||
|
||||
var scroll = UnityEngine.Input.GetAxis("Mouse ScrollWheel");
|
||||
switch (scroll)
|
||||
{
|
||||
case > 0f:
|
||||
weaponInventory.NextWeapon();
|
||||
break;
|
||||
case < 0f:
|
||||
weaponInventory.PreviousWeapon();
|
||||
break;
|
||||
}
|
||||
|
||||
if (UnityEngine.Input.GetKeyDown(KeyCode.R))
|
||||
{
|
||||
weaponInventory.ReloadCurrentWeapon();
|
||||
}
|
||||
|
||||
for (var i = 0; i < 9; i++)
|
||||
{
|
||||
if (UnityEngine.Input.GetKeyDown(KeyCode.Alpha1 + i))
|
||||
{
|
||||
weaponInventory.SwitchWeapon(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleCharacterInput();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (healthSystem != null)
|
||||
{
|
||||
healthSystem.OnDied -= HandleDeath;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 21d89144fc42461e9132c47c9abc8144
|
||||
timeCreated: 1771856381
|
||||
@@ -0,0 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Code.Gameplay.Weapons;
|
||||
using System;
|
||||
|
||||
namespace Code.Gameplay.Character.Inventory
|
||||
{
|
||||
public class WeaponInventory : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private List<WeaponBase> weapons = new List<WeaponBase>();
|
||||
[SerializeField] private Transform weaponHolder;
|
||||
|
||||
[SerializeField] private int currentWeaponIndex = 0;
|
||||
|
||||
public event Action<WeaponBase> OnWeaponSwitched;
|
||||
public event Action OnWeaponsInitialized;
|
||||
|
||||
public WeaponBase CurrentWeapon => weapons.Count > 0 ? weapons[currentWeaponIndex] : null;
|
||||
public int WeaponCount => weapons.Count;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InitializeWeapons();
|
||||
}
|
||||
|
||||
private void InitializeWeapons()
|
||||
{
|
||||
for (var i = 0; i < weapons.Count; i++)
|
||||
{
|
||||
weapons[i] = Instantiate(weapons[i].gameObject, weaponHolder).GetComponent<WeaponBase>();
|
||||
weapons[i].gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
if (weapons.Count > 0 && currentWeaponIndex < weapons.Count)
|
||||
{
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(true);
|
||||
OnWeaponSwitched?.Invoke(weapons[currentWeaponIndex]);
|
||||
}
|
||||
|
||||
OnWeaponsInitialized?.Invoke();
|
||||
}
|
||||
|
||||
public void SwitchWeapon(int index)
|
||||
{
|
||||
if (index < 0 || index >= weapons.Count) return;
|
||||
if (index == currentWeaponIndex) return;
|
||||
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(false);
|
||||
currentWeaponIndex = index;
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(true);
|
||||
|
||||
OnWeaponSwitched?.Invoke(weapons[currentWeaponIndex]);
|
||||
}
|
||||
|
||||
public void NextWeapon()
|
||||
{
|
||||
int nextIndex = (currentWeaponIndex + 1) % weapons.Count;
|
||||
SwitchWeapon(nextIndex);
|
||||
}
|
||||
|
||||
public void PreviousWeapon()
|
||||
{
|
||||
int prevIndex = (currentWeaponIndex - 1 + weapons.Count) % weapons.Count;
|
||||
SwitchWeapon(prevIndex);
|
||||
}
|
||||
|
||||
public void RemoveWeapon(WeaponBase weapon)
|
||||
{
|
||||
if (weapons.Contains(weapon))
|
||||
{
|
||||
int index = weapons.IndexOf(weapon);
|
||||
weapons.Remove(weapon);
|
||||
Destroy(weapon.gameObject);
|
||||
|
||||
if (index <= currentWeaponIndex)
|
||||
{
|
||||
currentWeaponIndex = Mathf.Max(0, currentWeaponIndex - 1);
|
||||
}
|
||||
|
||||
if (weapons.Count > 0)
|
||||
{
|
||||
weapons[currentWeaponIndex].gameObject.SetActive(true);
|
||||
OnWeaponSwitched?.Invoke(weapons[currentWeaponIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ReloadCurrentWeapon()
|
||||
{
|
||||
CurrentWeapon?.Reload();
|
||||
}
|
||||
|
||||
public void ShootCurrentWeapon()
|
||||
{
|
||||
CurrentWeapon?.StartShooting();
|
||||
}
|
||||
|
||||
public void StopShootingCurrentWeapon()
|
||||
{
|
||||
CurrentWeapon?.StopShooting();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c24ec1ac73384bd6a84bdff6028a6479
|
||||
timeCreated: 1771856390
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 916b1a7d923b4f95b9215a2c89deca6c
|
||||
timeCreated: 1771856166
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Code.Gameplay.Core;
|
||||
|
||||
namespace Code.Gameplay.Character.Stats
|
||||
{
|
||||
public class HealthSystem : MonoBehaviour, IDamageable
|
||||
{
|
||||
[SerializeField] private float maxHealth = 100f;
|
||||
[SerializeField] private float currentHealth;
|
||||
|
||||
public event Action<float, float> OnHealthChanged;
|
||||
public event Action OnDied;
|
||||
|
||||
public float CurrentHealth => currentHealth;
|
||||
public float MaxHealth => maxHealth;
|
||||
public float HealthPercentage => currentHealth / maxHealth;
|
||||
public bool IsAlive => currentHealth > 0;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
currentHealth = maxHealth;
|
||||
}
|
||||
|
||||
public void TakeDamage(float damage)
|
||||
{
|
||||
if (!IsAlive) return;
|
||||
|
||||
currentHealth = Mathf.Max(0, currentHealth - damage);
|
||||
OnHealthChanged?.Invoke(currentHealth, maxHealth);
|
||||
|
||||
if (currentHealth <= 0)
|
||||
{
|
||||
Die();
|
||||
}
|
||||
}
|
||||
|
||||
public void Heal(float amount)
|
||||
{
|
||||
if (!IsAlive) return;
|
||||
|
||||
currentHealth = Mathf.Min(maxHealth, currentHealth + amount);
|
||||
OnHealthChanged?.Invoke(currentHealth, maxHealth);
|
||||
}
|
||||
|
||||
private void Die()
|
||||
{
|
||||
OnDied?.Invoke();
|
||||
}
|
||||
|
||||
public Transform GetTransform() => transform;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0d0a69fde097419bb4933773d22d739b
|
||||
timeCreated: 1771856171
|
||||
Reference in new issue
Block a user