Skip to content

Commit

Permalink
finished game
Browse files Browse the repository at this point in the history
  • Loading branch information
GidiRabi committed Nov 21, 2024
1 parent 7eae088 commit c43d40b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Assets/Images/Cactus.prefab

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions Assets/Scripts/CameraController.cs

This file was deleted.

2 changes: 0 additions & 2 deletions Assets/Scripts/CameraController.cs.meta

This file was deleted.

19 changes: 19 additions & 0 deletions Assets/Scripts/Mover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ public class Mover : MonoBehaviour

private Rigidbody2D rb;

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

private void Start()
{
rb = GetComponent<Rigidbody2D>();
Expand All @@ -23,4 +33,13 @@ 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;
}
}
35 changes: 34 additions & 1 deletion Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

public class PlayerController : MonoBehaviour
{
[SerializeField] private float speed = 5f; // Forward speed
[SerializeField] private float speed = 5f; // Initial forward speed
[SerializeField] private float speedIncrease = 1f; // Speed increase increment
[SerializeField] private float distanceThreshold = 100f; // Distance interval for speed increase
[SerializeField] private float explosionRadius = 2f; // Radius of explosion
[SerializeField] private int lives = 3; // Player lives
[SerializeField] private float winPositionX = 1000f; // X position to trigger "You Win"
Expand All @@ -12,24 +14,32 @@ public class PlayerController : MonoBehaviour

private Rigidbody2D rb;
private UIManager uiManager;
private Mover mover; // Reference to the Mover script
private bool isGameOver = false; // Flag to check if the game is over

private readonly float[] lanePositions = { -3.1f, 0f, 3.2f }; // Predefined lane positions
private int currentLane = 1; // Default starting lane is the middle lane

private float lastSpeedIncreasePosition = 0f; // Tracks the last position where speed increased
private SpriteRenderer spriteRenderer;

private void Start()
{
rb = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
uiManager = FindObjectOfType<UIManager>();
mover = FindObjectOfType<Mover>();

if (uiManager != null)
{
uiManager.SetLives(lives);
}

if (mover != null)
{
mover.MoveSpeed = speed; // Set the initial mover speed
}

SetPlayerPositionToLane();
}

Expand All @@ -40,6 +50,15 @@ private void Update()
// Automatic movement to the right
rb.linearVelocity = new Vector2(speed, rb.linearVelocity.y);

// Sync mover speed with player speed
if (mover != null)
{
mover.MoveSpeed = speed;
}

// Check for speed increase
CheckSpeedIncrease();

// Check for win condition
if (transform.position.x >= winPositionX)
{
Expand All @@ -64,6 +83,17 @@ private void Update()
}
}

private void CheckSpeedIncrease()
{
// Check if player has moved the required distance
if (transform.position.x - lastSpeedIncreasePosition >= distanceThreshold)
{
speed += speedIncrease; // Increase speed
lastSpeedIncreasePosition = transform.position.x; // Update last position
Debug.Log($"Speed increased to {speed}");
}
}

private void MoveLane(int direction)
{
int targetLane = currentLane + direction;
Expand Down Expand Up @@ -117,6 +147,7 @@ private void Explode()

if (lives <= 0)
{
mover.SetSpeed(0);
TriggerGameOver();
}
}
Expand Down Expand Up @@ -192,6 +223,8 @@ private void TriggerWin()
rb.linearVelocity = Vector2.zero;
rb.isKinematic = true;

mover.SetSpeed(0);

// Notify UIManager to display "You Win" text
if (uiManager != null)
{
Expand Down

0 comments on commit c43d40b

Please sign in to comment.