added speed change and death
This commit is contained in:
1 parent
9e5f46b48c
commit
08e050ffd3
6 files changed
+1437
-5
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 725a8d40dde446e6843ad182b3474d9d
|
||||||
|
timeCreated: 1772914582
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
using Code.Gameplay.Player;
|
||||||
|
|
||||||
|
namespace Code.Gameplay.Management
|
||||||
|
{
|
||||||
|
public class GameSpeedManager : MonoBehaviour
|
||||||
|
{
|
||||||
|
[SerializeField] private PlayerController playerController;
|
||||||
|
[SerializeField] private float speedMultiplier = 1.2f;
|
||||||
|
[SerializeField] private float timeInterval = 10f;
|
||||||
|
|
||||||
|
private float _timer;
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
_timer += Time.deltaTime;
|
||||||
|
|
||||||
|
if (_timer >= timeInterval)
|
||||||
|
{
|
||||||
|
AccelerateGame();
|
||||||
|
_timer = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AccelerateGame()
|
||||||
|
{
|
||||||
|
if (playerController)
|
||||||
|
{
|
||||||
|
playerController.IncreaseSpeed(speedMultiplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7b5f84b26f43465ab2930bb64735922d
|
||||||
|
timeCreated: 1772914590
|
||||||
@@ -5,8 +5,9 @@ namespace Code.Gameplay.Player
|
|||||||
public class PlayerController : MonoBehaviour
|
public class PlayerController : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] private float forwardSpeed = 10f;
|
[SerializeField] private float forwardSpeed = 10f;
|
||||||
|
[SerializeField] private float maxSpeed = 40f;
|
||||||
[SerializeField] private float laneDistance = 5f;
|
[SerializeField] private float laneDistance = 5f;
|
||||||
[SerializeField] private float jumpForce = 8f;
|
[SerializeField] private float jumpForce = 11f;
|
||||||
[SerializeField] private float gravity = 20f;
|
[SerializeField] private float gravity = 20f;
|
||||||
|
|
||||||
private int _targetLane = 1;
|
private int _targetLane = 1;
|
||||||
@@ -14,6 +15,9 @@ namespace Code.Gameplay.Player
|
|||||||
private CharacterController _controller;
|
private CharacterController _controller;
|
||||||
private Vector3 _direction;
|
private Vector3 _direction;
|
||||||
|
|
||||||
|
public System.Action OnPlayerDeath;
|
||||||
|
private bool _isDead = false;
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
_controller = GetComponent<CharacterController>();
|
_controller = GetComponent<CharacterController>();
|
||||||
@@ -21,6 +25,8 @@ namespace Code.Gameplay.Player
|
|||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
|
if (_isDead) return;
|
||||||
|
|
||||||
if (UnityEngine.Input.GetKeyDown(KeyCode.RightArrow) || UnityEngine.Input.GetKeyDown(KeyCode.D))
|
if (UnityEngine.Input.GetKeyDown(KeyCode.RightArrow) || UnityEngine.Input.GetKeyDown(KeyCode.D))
|
||||||
{
|
{
|
||||||
_targetLane = Mathf.Min(_targetLane + 1, 2);
|
_targetLane = Mathf.Min(_targetLane + 1, 2);
|
||||||
@@ -66,11 +72,16 @@ namespace Code.Gameplay.Player
|
|||||||
var finalMove = new Vector3(moveX, _direction.y * Time.deltaTime, _direction.z * Time.deltaTime);
|
var finalMove = new Vector3(moveX, _direction.y * Time.deltaTime, _direction.z * Time.deltaTime);
|
||||||
|
|
||||||
_controller.Move(finalMove);
|
_controller.Move(finalMove);
|
||||||
|
CheckForDeath();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void IncreaseSpeed(float amount)
|
public void IncreaseSpeed(float amount)
|
||||||
{
|
{
|
||||||
forwardSpeed += amount;
|
if (forwardSpeed < maxSpeed)
|
||||||
|
{
|
||||||
|
var desirableSpeed = forwardSpeed + amount;
|
||||||
|
forwardSpeed = Mathf.Min(desirableSpeed, maxSpeed);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTriggerEnter(Collider foreign)
|
private void OnTriggerEnter(Collider foreign)
|
||||||
@@ -82,5 +93,23 @@ namespace Code.Gameplay.Player
|
|||||||
Destroy(foreign.gameObject);
|
Destroy(foreign.gameObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void CheckForDeath()
|
||||||
|
{
|
||||||
|
if (_isDead) return;
|
||||||
|
|
||||||
|
if (Time.timeSinceLevelLoad > 1f && _controller.velocity.z <= 0.1f)
|
||||||
|
{
|
||||||
|
Die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Die()
|
||||||
|
{
|
||||||
|
_isDead = true;
|
||||||
|
forwardSpeed = 0;
|
||||||
|
_direction = Vector3.zero;
|
||||||
|
OnPlayerDeath?.Invoke();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
using TMPro;
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.SceneManagement;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
using PrimeTween;
|
||||||
|
|
||||||
namespace Code.UI
|
namespace Code.UI
|
||||||
{
|
{
|
||||||
@@ -8,11 +11,32 @@ namespace Code.UI
|
|||||||
public static GameplayController Instance;
|
public static GameplayController Instance;
|
||||||
|
|
||||||
[SerializeField] private TMP_Text coinsText;
|
[SerializeField] private TMP_Text coinsText;
|
||||||
|
[SerializeField] private CanvasGroup deathPanelCanvasGroup;
|
||||||
|
[SerializeField] private RectTransform deathPanelWindow;
|
||||||
|
[SerializeField] private TMP_Text finalResultText;
|
||||||
|
[SerializeField] private TMP_InputField nameInputField;
|
||||||
|
[SerializeField] private Button restartButton;
|
||||||
|
[SerializeField] private Button exitButton;
|
||||||
|
|
||||||
private int _coins;
|
private int _coins;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake() => Instance = this;
|
||||||
|
|
||||||
|
private void Start()
|
||||||
{
|
{
|
||||||
Instance = this;
|
Time.timeScale = 1;
|
||||||
|
|
||||||
|
if (deathPanelCanvasGroup != null)
|
||||||
|
{
|
||||||
|
deathPanelCanvasGroup.gameObject.SetActive(false);
|
||||||
|
deathPanelCanvasGroup.alpha = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var player = GameObject.FindGameObjectWithTag("Player")?.GetComponent<Gameplay.Player.PlayerController>();
|
||||||
|
if (player != null) player.OnPlayerDeath += ShowDeathScreen;
|
||||||
|
|
||||||
|
restartButton.onClick.AddListener(RestartGame);
|
||||||
|
exitButton.onClick.AddListener(ExitGame);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddCoin()
|
public void AddCoin()
|
||||||
@@ -20,5 +44,51 @@ namespace Code.UI
|
|||||||
_coins++;
|
_coins++;
|
||||||
coinsText.text = $"{_coins} Coins";
|
coinsText.text = $"{_coins} Coins";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ShowDeathScreen()
|
||||||
|
{
|
||||||
|
deathPanelCanvasGroup.gameObject.SetActive(true);
|
||||||
|
|
||||||
|
Tween.Alpha(deathPanelCanvasGroup, startValue: 0f, endValue: 1f, duration: 0.5f, ease: Ease.OutSine, useUnscaledTime: true);
|
||||||
|
|
||||||
|
deathPanelWindow.localScale = Vector3.zero;
|
||||||
|
Tween.Scale(deathPanelWindow, endValue: 1f, duration: 0.6f, ease: Ease.OutBack, useUnscaledTime: true)
|
||||||
|
.OnComplete(() => {
|
||||||
|
Time.timeScale = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
finalResultText.text = $"You collected: {_coins} coins";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RestartGame()
|
||||||
|
{
|
||||||
|
SaveScore();
|
||||||
|
Time.timeScale = 1;
|
||||||
|
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ExitGame()
|
||||||
|
{
|
||||||
|
SaveScore();
|
||||||
|
Time.timeScale = 1;
|
||||||
|
SceneManager.LoadScene("MainMenu");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SaveScore()
|
||||||
|
{
|
||||||
|
var playerName = nameInputField.text;
|
||||||
|
if (string.IsNullOrEmpty(playerName))
|
||||||
|
playerName = "Player";
|
||||||
|
Debug.Log($"Saved: {playerName} - {_coins}");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
restartButton.onClick.RemoveAllListeners();
|
||||||
|
exitButton.onClick.RemoveAllListeners();
|
||||||
|
|
||||||
|
var player = GameObject.FindGameObjectWithTag("Player")?.GetComponent<Gameplay.Player.PlayerController>();
|
||||||
|
if (player != null) player.OnPlayerDeath -= ShowDeathScreen;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user