first commit
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
public class ExperiencePickup : MonoBehaviour
|
||||
{
|
||||
public int experienceAmount = 50;
|
||||
public bool destroyOnCollect = true;
|
||||
public float respawnCooldown = 12f;
|
||||
public float rotationSpeed = 70f;
|
||||
|
||||
public LevelSystem levelSystem;
|
||||
public NotificationUI notificationUI;
|
||||
|
||||
private bool playerInRange;
|
||||
private bool collected;
|
||||
private Renderer[] renderers;
|
||||
private Collider[] colliders;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
renderers = GetComponentsInChildren<Renderer>();
|
||||
colliders = GetComponentsInChildren<Collider>();
|
||||
|
||||
if (levelSystem == null)
|
||||
{
|
||||
levelSystem = FindObjectOfType<LevelSystem>();
|
||||
}
|
||||
|
||||
if (notificationUI == null)
|
||||
{
|
||||
notificationUI = FindObjectOfType<NotificationUI>();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime, Space.World);
|
||||
|
||||
if (playerInRange && !collected && Input.GetKeyDown(KeyCode.E))
|
||||
{
|
||||
Collect();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.GetComponentInParent<SimplePlayerController>() != null)
|
||||
{
|
||||
playerInRange = true;
|
||||
if (notificationUI != null)
|
||||
{
|
||||
notificationUI.Show("Нажмите E, чтобы забрать кристалл опыта");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (other.GetComponentInParent<SimplePlayerController>() != null)
|
||||
{
|
||||
playerInRange = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void Collect()
|
||||
{
|
||||
collected = true;
|
||||
|
||||
if (levelSystem != null)
|
||||
{
|
||||
levelSystem.AddExperience(experienceAmount);
|
||||
}
|
||||
|
||||
if (notificationUI != null)
|
||||
{
|
||||
notificationUI.Show($"Кристалл опыта: +{experienceAmount} XP");
|
||||
}
|
||||
|
||||
if (destroyOnCollect)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
StartCoroutine(RespawnRoutine());
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator RespawnRoutine()
|
||||
{
|
||||
SetVisible(false);
|
||||
yield return new WaitForSeconds(Mathf.Max(1f, respawnCooldown));
|
||||
collected = false;
|
||||
SetVisible(true);
|
||||
}
|
||||
|
||||
private void SetVisible(bool visible)
|
||||
{
|
||||
for (int i = 0; i < renderers.Length; i++)
|
||||
{
|
||||
if (renderers[i] != null)
|
||||
{
|
||||
renderers[i].enabled = visible;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < colliders.Length; i++)
|
||||
{
|
||||
if (colliders[i] != null)
|
||||
{
|
||||
colliders[i].enabled = visible;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 128b26ace3a81b67c825108a47f2a42c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class LevelSystem : MonoBehaviour
|
||||
{
|
||||
[Header("Progression")]
|
||||
public int level = 1;
|
||||
public int currentExp = 0;
|
||||
public int expToNextLevel = 100;
|
||||
|
||||
[Header("Dependencies")]
|
||||
public TalentManager talentManager;
|
||||
public NotificationUI notificationUI;
|
||||
|
||||
[Header("HUD")]
|
||||
public Text levelText;
|
||||
public Text expText;
|
||||
public Image expFillImage;
|
||||
|
||||
public event Action OnExperienceChanged;
|
||||
public event Action OnLevelChanged;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (talentManager == null)
|
||||
{
|
||||
talentManager = FindObjectOfType<TalentManager>();
|
||||
}
|
||||
|
||||
if (notificationUI == null)
|
||||
{
|
||||
notificationUI = FindObjectOfType<NotificationUI>();
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
RefreshUI();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.X))
|
||||
{
|
||||
AddExperience(50);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddExperience(int amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
currentExp += amount;
|
||||
Notify($"Получено опыта: {amount}");
|
||||
|
||||
while (currentExp >= expToNextLevel)
|
||||
{
|
||||
currentExp -= expToNextLevel;
|
||||
LevelUp();
|
||||
}
|
||||
|
||||
RefreshUI();
|
||||
OnExperienceChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void LevelUp()
|
||||
{
|
||||
level = Mathf.Max(1, level + 1);
|
||||
expToNextLevel = Mathf.Max(1, Mathf.RoundToInt(expToNextLevel * 1.25f + 25f));
|
||||
|
||||
if (talentManager != null)
|
||||
{
|
||||
talentManager.AddTalentPoints(1);
|
||||
}
|
||||
|
||||
Notify($"Уровень {level}! Получено очко талантов");
|
||||
RefreshUI();
|
||||
OnLevelChanged?.Invoke();
|
||||
}
|
||||
|
||||
public float GetExpNormalized()
|
||||
{
|
||||
if (expToNextLevel <= 0)
|
||||
{
|
||||
return 0f;
|
||||
}
|
||||
|
||||
return Mathf.Clamp01((float)currentExp / expToNextLevel);
|
||||
}
|
||||
|
||||
private void RefreshUI()
|
||||
{
|
||||
if (levelText != null)
|
||||
{
|
||||
levelText.text = $"Уровень {level}";
|
||||
}
|
||||
|
||||
if (expText != null)
|
||||
{
|
||||
expText.text = $"{currentExp} / {expToNextLevel} XP";
|
||||
}
|
||||
|
||||
if (expFillImage != null)
|
||||
{
|
||||
expFillImage.fillAmount = GetExpNormalized();
|
||||
}
|
||||
}
|
||||
|
||||
private void Notify(string message)
|
||||
{
|
||||
if (notificationUI != null)
|
||||
{
|
||||
notificationUI.Show(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca4263d1e4dbc27e59409154d02bbb6d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,83 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class SimplePlayerController : MonoBehaviour
|
||||
{
|
||||
public PlayerStats playerStats;
|
||||
public CharacterController characterController;
|
||||
public Transform cameraTransform;
|
||||
|
||||
[Header("Movement")]
|
||||
public float gravity = -25f;
|
||||
public float rotationSpeed = 12f;
|
||||
|
||||
[Header("Camera")]
|
||||
public Vector3 cameraOffset = new Vector3(0f, 9f, -8f);
|
||||
public float cameraYaw = 45f;
|
||||
public float cameraSmooth = 12f;
|
||||
|
||||
private float verticalVelocity;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (characterController == null)
|
||||
{
|
||||
characterController = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
if (playerStats == null)
|
||||
{
|
||||
playerStats = GetComponent<PlayerStats>();
|
||||
}
|
||||
|
||||
if (cameraTransform == null && Camera.main != null)
|
||||
{
|
||||
cameraTransform = Camera.main.transform;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
cameraYaw += Input.GetAxis("Mouse X") * 120f * Time.deltaTime;
|
||||
}
|
||||
|
||||
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
|
||||
input = Vector3.ClampMagnitude(input, 1f);
|
||||
|
||||
Quaternion yawRotation = Quaternion.Euler(0f, cameraYaw, 0f);
|
||||
Vector3 moveDirection = yawRotation * input;
|
||||
|
||||
if (characterController.isGrounded && verticalVelocity < 0f)
|
||||
{
|
||||
verticalVelocity = -1f;
|
||||
}
|
||||
|
||||
verticalVelocity += gravity * Time.deltaTime;
|
||||
|
||||
float speed = playerStats != null ? playerStats.MoveSpeed : 5f;
|
||||
Vector3 velocity = moveDirection * speed;
|
||||
velocity.y = verticalVelocity;
|
||||
characterController.Move(velocity * Time.deltaTime);
|
||||
|
||||
if (moveDirection.sqrMagnitude > 0.001f)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (cameraTransform == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Quaternion yawRotation = Quaternion.Euler(0f, cameraYaw, 0f);
|
||||
Vector3 desiredPosition = transform.position + yawRotation * cameraOffset;
|
||||
cameraTransform.position = Vector3.Lerp(cameraTransform.position, desiredPosition, cameraSmooth * Time.deltaTime);
|
||||
cameraTransform.LookAt(transform.position + Vector3.up * 1.3f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23ff40a71fbf048f08376f5796c86de7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user