added bonus and side moving obstacle

This commit is contained in:
MrTorex committed 2026-03-08 10:29:09 +03:00
1 parent 08e050ffd3
commit e039461a83
17 files changed
+1171 -11

No files matched your search

+28 -7
View File
@@ -23,12 +23,17 @@ namespace Code.Gameplay.Level
[SerializeField] private GameObject[] highObstacles;
[SerializeField] private GameObject[] movingLowObstacles;
[SerializeField] private GameObject[] movingHighObstacles;
[SerializeField] private GameObject[] sideObstacles;
[Range(0, 100)] [SerializeField] private int obstacleChance = 40;
[SerializeField] private float laneDistance = 5f;
[Header("Coins Settings")]
[SerializeField] private GameObject coinPrefab;
[Range(0, 100)] [SerializeField] private int coinChance = 50;
[Header("Bonus Settings")]
[SerializeField] private GameObject bonusX2Prefab;
[Range(0, 100)] [SerializeField] private int bonusChance = 10;
private readonly List<GameObject> _activeTiles = new();
private readonly List<GameObject> _activeEnvironment = new();
@@ -74,7 +79,7 @@ namespace Code.Gameplay.Level
var lane = Random.Range(-1, 2);
var spawnPos = new Vector3(lane * laneDistance, 0, zPos);
var type = Random.Range(0, 4);
var type = Random.Range(0, 5);
var prefab = type switch
{
@@ -82,6 +87,7 @@ namespace Code.Gameplay.Level
1 => GetRandom(highObstacles),
2 => GetRandom(movingLowObstacles),
3 => GetRandom(movingHighObstacles),
4 => GetRandom(sideObstacles),
_ => null
};
@@ -99,13 +105,28 @@ namespace Code.Gameplay.Level
if (Random.Range(0, 100) > coinChance) return;
var coinLane = Random.Range(-1, 2);
if (coinLane == occupiedLane) coinLane = (coinLane + 1 > 1) ? -1 : coinLane + 1;
for (var i = 0; i < 2; i++)
if (coinLane == occupiedLane)
{
var spawnPos = new Vector3(coinLane * laneDistance, 1f, zPos + (i * 2f));
var coin = Instantiate(coinPrefab, spawnPos, Quaternion.identity);
coin.transform.SetParent(_activeTiles[^1].transform);
coinLane = (coinLane + 1 > 1) ? -1 : coinLane + 1;
if (coinLane == occupiedLane) coinLane = (coinLane + 1 > 1) ? -1 : coinLane + 1;
}
var shouldSpawnBonus = Random.Range(0, 100) < bonusChance;
if (shouldSpawnBonus && bonusX2Prefab)
{
var spawnPos = new Vector3(coinLane * laneDistance, 1f, zPos + tileLength / 2f);
var bonus = Instantiate(bonusX2Prefab, spawnPos, Quaternion.identity);
bonus.transform.SetParent(_activeTiles[^1].transform);
}
else if (coinPrefab)
{
for (var i = 0; i < 2; i++)
{
var spawnPos = new Vector3(coinLane * laneDistance, 1f, zPos + (i * 2f));
var coin = Instantiate(coinPrefab, spawnPos, Quaternion.identity);
coin.transform.SetParent(_activeTiles[^1].transform);
}
}
}
@@ -0,0 +1,39 @@
using UnityEngine;
namespace Code.Gameplay.Level
{
public class SideMovingObstacle : MonoBehaviour
{
public float speed = 10f;
[SerializeField] private float range = 7f;
private bool _movingRight;
private void Start()
{
_movingRight = transform.position.x < 0;
UpdateRotation();
}
private void Update()
{
var step = speed * Time.deltaTime;
transform.Translate(Vector3.forward * step);
if (_movingRight && transform.position.x >= range)
{
_movingRight = false;
UpdateRotation();
}
else if (!_movingRight && transform.position.x <= -range)
{
_movingRight = true;
UpdateRotation();
}
}
private void UpdateRotation()
{
transform.rotation = Quaternion.Euler(0, _movingRight ? 90 : -90, 0);
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: df5440b2c95b476d82146a905aa5aa50
timeCreated: 1772953789
@@ -1,3 +1,5 @@
using System;
using Code.UI;
using UnityEngine;
namespace Code.Gameplay.Player
@@ -15,8 +17,11 @@ namespace Code.Gameplay.Player
private CharacterController _controller;
private Vector3 _direction;
public System.Action OnPlayerDeath;
private bool _isDead = false;
public Action OnPlayerDeath;
private bool _isDead;
private bool _isDoubleCoins;
private float _bonusTimer;
private void Start()
{
@@ -73,6 +78,7 @@ namespace Code.Gameplay.Player
_controller.Move(finalMove);
CheckForDeath();
HandleBonusTimer();
}
public void IncreaseSpeed(float amount)
@@ -86,12 +92,42 @@ namespace Code.Gameplay.Player
private void OnTriggerEnter(Collider foreign)
{
if (foreign.CompareTag("Obstacle"))
{
Die();
}
if (foreign.CompareTag("Coin"))
{
UI.GameplayController.Instance.AddCoin();
var amount = _isDoubleCoins ? 2 : 1;
for(var i = 0; i < amount; i++) GameplayController.Instance.AddCoin();
Destroy(foreign.gameObject);
}
if (foreign.CompareTag("BonusX2"))
{
ActivateDoubleCoins(10f);
Destroy(foreign.gameObject);
}
}
private void ActivateDoubleCoins(float duration)
{
_isDoubleCoins = true;
_bonusTimer = duration;
}
private void HandleBonusTimer()
{
if (_bonusTimer > 0)
{
_bonusTimer -= Time.deltaTime;
if (_bonusTimer <= 0)
{
_isDoubleCoins = false;
}
}
}
private void CheckForDeath()