Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
GidiRabi committed Nov 26, 2024
1 parent 2a6318f commit 5cecb6b
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 155 deletions.
78 changes: 39 additions & 39 deletions Assets/Scripts/LaneSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,56 @@

public class LaneSpawner : MonoBehaviour
{
[Tooltip("Items that can spawn (e.g., fruits, bombs, obstacles)")]
[SerializeField] private GameObject[] spawnableObjects;
[Tooltip("Items that can spawn (e.g., fruits, bombs, obstacles)")]
[SerializeField] private GameObject[] spawnableObjects;

[Tooltip("Maximum number of items to spawn within the range")]
[SerializeField] private int maxItems = 10;
[Tooltip("Maximum number of items to spawn within the range")]
[SerializeField] private int maxItems = 10;

[Tooltip("Maximum distance for the entire spawn range")]
[SerializeField] private float maxSpawnRange = 1000f;
[Tooltip("Maximum distance for the entire spawn range")]
[SerializeField] private float maxSpawnRange = 1000f;

[Tooltip("Maximum distance between consecutive items")]
[SerializeField] private float maxDistanceBetweenItems = 10f;
[Tooltip("Maximum distance between consecutive items")]
[SerializeField] private float maxDistanceBetweenItems = 10f;

[Tooltip("Parent object for spawned items (optional)")]
[SerializeField] private Transform parentObject;
[Tooltip("Parent object for spawned items (optional)")]
[SerializeField] private Transform parentObject;

private float spawnStartPositionX;
private float spawnStartPositionX;

private void Start()
{
spawnStartPositionX = transform.position.x;
private void Start()
{
spawnStartPositionX = transform.position.x;

// Spawn the items in the lane
SpawnItems();
}
// Spawn the items in the lane
SpawnItems();
}

private void SpawnItems()
{
float currentXPosition = spawnStartPositionX;
private void SpawnItems()
{
float currentXPosition = spawnStartPositionX;

for (int i = 0; i < maxItems; i++)
{
// Randomly pick an object to spawn
int randomIndex = Random.Range(0, spawnableObjects.Length);
GameObject selectedObject = spawnableObjects[randomIndex];
for (int i = 0; i < maxItems; i++)
{
// Randomly pick an object to spawn
int randomIndex = Random.Range(0, spawnableObjects.Length);
GameObject selectedObject = spawnableObjects[randomIndex];

// Calculate random distance between items
float randomDistance = Random.Range(5f, maxDistanceBetweenItems); // Adjust the minimum distance as needed
// Calculate random distance between items
float randomDistance = Random.Range(5f, maxDistanceBetweenItems); // Adjust the minimum distance as needed

// Update the current spawn position
currentXPosition += randomDistance;
// Update the current spawn position
currentXPosition += randomDistance;

// Ensure we don't exceed the max spawn range
if (currentXPosition - spawnStartPositionX > maxSpawnRange)
{
break; // Stop spawning if we exceed the range
}
// Ensure we don't exceed the max spawn range
if (currentXPosition - spawnStartPositionX > maxSpawnRange)
{
break; // Stop spawning if we exceed the range
}

// Spawn the object at the calculated position
Vector3 spawnPosition = new Vector3(currentXPosition, transform.position.y, 0);
Instantiate(selectedObject, spawnPosition, Quaternion.identity, parentObject);
}
}
// Spawn the object at the calculated position
Vector3 spawnPosition = new Vector3(currentXPosition, transform.position.y, 0);
Instantiate(selectedObject, spawnPosition, Quaternion.identity, parentObject);
}
}
}
70 changes: 35 additions & 35 deletions Assets/Scripts/Mover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@

public class Mover : MonoBehaviour
{
[Tooltip("Speed of movement to the right")]
[SerializeField] private float moveSpeed = 5f;
[Tooltip("Speed of movement to the right")]
[SerializeField] private float moveSpeed = 5f;

private Rigidbody2D rb;
private Rigidbody2D rb;

public float MoveSpeed
{
get => moveSpeed;
set
{
moveSpeed = value;
Debug.Log($"Mover speed updated to: {moveSpeed}");
}
}
public float MoveSpeed
{
get => moveSpeed;
set
{
moveSpeed = value;
Debug.Log($"Mover speed updated to: {moveSpeed}");
}
}

private void Start()
{
rb = GetComponent<Rigidbody2D>();
if (rb == null)
{
Debug.LogWarning("Rigidbody2D not found! Adding Rigidbody2D component dynamically.");
rb = gameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = 0; // Ensure no gravity for the player
}
}
private void Start()
{
rb = GetComponent<Rigidbody2D>();
if (rb == null)
{
Debug.LogWarning("Rigidbody2D not found! Adding Rigidbody2D component dynamically.");
rb = gameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = 0; // Ensure no gravity for the player
}
}

private void FixedUpdate()
{
// Move to the right at the specified speed
rb.linearVelocity = new Vector2(moveSpeed, rb.linearVelocity.y);
}
private void FixedUpdate()
{
// Move to the right at the specified speed
rb.linearVelocity = new Vector2(moveSpeed, rb.linearVelocity.y);
}

/// <summary>
/// Public method to change the movement speed.
/// </summary>
/// <param name="newSpeed">The new speed value.</param>
public void SetSpeed(float newSpeed)
{
MoveSpeed = newSpeed;
}
/// <summary>
/// Public method to change the movement speed.
/// </summary>
/// <param name="newSpeed">The new speed value.</param>
public void SetSpeed(float newSpeed)
{
MoveSpeed = newSpeed;
}
}
62 changes: 31 additions & 31 deletions Assets/Scripts/ObjectSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,40 @@

public class ObjectSpawner : MonoBehaviour
{
[SerializeField] private GameObject fruitPrefab;
[SerializeField] private GameObject bombPrefab;
[SerializeField] private GameObject obstaclePrefab;
[SerializeField] private GameObject fruitPrefab;
[SerializeField] private GameObject bombPrefab;
[SerializeField] private GameObject obstaclePrefab;

[SerializeField] private float spawnInterval = 1.5f;
[SerializeField] private float minY = -4f, maxY = 4f; // Spawning area
[SerializeField] private float spawnInterval = 1.5f;
[SerializeField] private float minY = -4f, maxY = 4f; // Spawning area

private void Start()
{
InvokeRepeating(nameof(SpawnObject), 0f, spawnInterval);
}
private void Start()
{
InvokeRepeating(nameof(SpawnObject), 0f, spawnInterval);
}

private void SpawnObject()
{
int randomType = Random.Range(0, 3); // 0 = Fruit, 1 = Bomb, 2 = Obstacle
GameObject prefabToSpawn = null;
private void SpawnObject()
{
int randomType = Random.Range(0, 3); // 0 = Fruit, 1 = Bomb, 2 = Obstacle
GameObject prefabToSpawn = null;

switch (randomType)
{
case 0:
prefabToSpawn = fruitPrefab;
break;
case 1:
prefabToSpawn = bombPrefab;
break;
case 2:
prefabToSpawn = obstaclePrefab;
break;
}
switch (randomType)
{
case 0:
prefabToSpawn = fruitPrefab;
break;
case 1:
prefabToSpawn = bombPrefab;
break;
case 2:
prefabToSpawn = obstaclePrefab;
break;
}

if (prefabToSpawn != null)
{
Vector3 spawnPosition = new Vector3(transform.position.x, Random.Range(minY, maxY), 0);
Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity);
}
}
if (prefabToSpawn != null)
{
Vector3 spawnPosition = new Vector3(transform.position.x, Random.Range(minY, maxY), 0);
Instantiate(prefabToSpawn, spawnPosition, Quaternion.identity);
}
}
}
100 changes: 50 additions & 50 deletions Assets/Scripts/UIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@

public class UIManager : MonoBehaviour
{
[Tooltip("Text field to display lives")]
[SerializeField] private TextMeshProUGUI livesText;
[Tooltip("Text field to display lives")]
[SerializeField] private TextMeshProUGUI livesText;

[Tooltip("Text field to display score")]
[SerializeField] private TextMeshProUGUI scoreText;
[Tooltip("Text field to display score")]
[SerializeField] private TextMeshProUGUI scoreText;

[Tooltip("Text field to display Game Over")]
[SerializeField] private TextMeshProUGUI gameOverText;
[Tooltip("Text field to display Game Over")]
[SerializeField] private TextMeshProUGUI gameOverText;

[Tooltip("Text field to display You Win")]
[SerializeField] private TextMeshProUGUI winText;
[Tooltip("Text field to display You Win")]
[SerializeField] private TextMeshProUGUI winText;

private int score = 0;
private int score = 0;

private void Start()
{
// Ensure these texts are hidden when the game starts
if (gameOverText != null)
{
gameOverText.gameObject.SetActive(false);
}
private void Start()
{
// Ensure these texts are hidden when the game starts
if (gameOverText != null)
{
gameOverText.gameObject.SetActive(false);
}

if (winText != null)
{
winText.gameObject.SetActive(false);
}
}
if (winText != null)
{
winText.gameObject.SetActive(false);
}
}

public void SetLives(int lives)
{
if (livesText != null)
{
livesText.text = $"Lives: {lives}";
}
}
public void SetLives(int lives)
{
if (livesText != null)
{
livesText.text = $"Lives: {lives}";
}
}

public void AddScore(int points)
{
score += points;
if (scoreText != null)
{
scoreText.text = $"Score: {score}";
}
}
public void AddScore(int points)
{
score += points;
if (scoreText != null)
{
scoreText.text = $"Score: {score}";
}
}

public void ShowGameOverText()
{
if (gameOverText != null)
{
gameOverText.gameObject.SetActive(true); // Display the Game Over text
}
}
public void ShowGameOverText()
{
if (gameOverText != null)
{
gameOverText.gameObject.SetActive(true); // Display the Game Over text
}
}

public void ShowWinText()
{
if (winText != null)
{
winText.gameObject.SetActive(true); // Display the You Win text
}
}
public void ShowWinText()
{
if (winText != null)
{
winText.gameObject.SetActive(true); // Display the You Win text
}
}
}

0 comments on commit 5cecb6b

Please sign in to comment.