using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CarController11 : MonoBehaviour
{
public float speed = 0; // Adjust this according to your needs
public float acceleration = 435f; // Adjust acceleration for smooth movement
public float deceleration = 0.6f; // Adjust deceleration for smooth stopping
public Rigidbody2D rb; // Reference to the Rigidbody component
public string scene = "ResultScene11";
public int stars = 0;
public int level = 11;
public GameObject flag;
public GameObject car;
public float x = 1.0f;
public float extensionAmount = -0.3f;
bool isMoving = false;
Vector2 startPos;
void Start()
{
rb = GetComponent<Rigidbody2D>(); // Get the Rigidbody component
rb.gravityScale = 13f;
// Remove existing Rigidbody2D component
ReplaceCollider();
flag = GameObject.Find("flag");
car = GameObject.Find("car");
Application.targetFrameRate = 60;
}
void ReplaceCollider()
{
// Remove BoxCollider if it exists
BoxCollider2D boxCollider = GetComponent<BoxCollider2D>();
if (boxCollider != null)
Destroy(boxCollider);
// Add CapsuleCollider
CapsuleCollider2D capsuleCollider = gameObject.AddComponent<CapsuleCollider2D>();
capsuleCollider.size = new Vector2(2.2f, 0.9f); // Default size
capsuleCollider.offset = new Vector2(0, 0.2f); // Offset
capsuleCollider.direction = CapsuleDirection2D.Horizontal; // Horizontal direction
// Calculate the scale of the object
Vector3 scale = transform.lossyScale;
// Scale the collider horizontally based on the maximum scale component
float maxSize = Mathf.Max(scale.x, scale.y, scale.z);
capsuleCollider.size = new Vector2(capsuleCollider.size.x * maxSize, capsuleCollider.size.y);
}
void Update()
{
if (Input.GetMouseButtonDown(0) && !isMoving)
{
startPos = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0) && !isMoving)
{
Vector2 endPos = Input.mousePosition;
float swipeLength = endPos.x - startPos.x;
// Check if the swipe is in the forward direction (to the right)
if (swipeLength > 30)
{
// Increase speed based on swipe length and acceleration
Debug.Log(swipeLength);
speed = (swipeLength * 55) / PlayerPrefs.GetInt("Speed");
Debug.Log(speed);
GetComponent<AudioSource>().Play();
isMoving = true;
}
else
{
// Handle the case when swipe length is not greater than 30
// You may add any necessary code here
}
}
// Move the car forward when it's supposed to be moving
if (isMoving)
{
// Calculate the movement amount based on speed and player preference for speed
float movement = speed;
// Set the velocity of the Rigidbody to move the car
rb.velocity = new Vector2(movement, 0);
// Apply a deceleration factor to gradually slow down
speed *= deceleration;
// Ensure speed doesn't go negative (prevent moving backward)
if (speed < 0.01f)
{
speed = 0;
StartCoroutine(DelayedExecution());
}
}
}
IEnumerator DelayedExecution()
{
yield return new WaitForSeconds(x);
float length = flag.transform.position.x - car.transform.position.x;
isMoving = false; // Reset the flag when the car stops moving
Debug.Log(length);
// Determine stars based on distance
if (length <= 2)
{
stars = 5;
}
else if (length > 2 && length <= 4)
{
stars = 4;
}
else if (length > 4 && length <= 6)
{
stars = 3;
}
else if (length > 6 && length <= 14)
{
stars = 2;
}
else if (length > 14)
{
stars = 1;
}
// Log the determined number of stars
Debug.Log(stars);
// Save level and stars
SaveLevel(level);
SaveStars(stars);
PlayerPrefs.SetInt("Stars", stars);
SceneManager.LoadScene(scene);
}
void SaveLevel(int level)
{
PlayerPrefs.SetInt("Level", level);
PlayerPrefs.Save(); // Save the PlayerPrefs to disk
}
// Method to save the stars count
void SaveStars(int starsCount)
{
PlayerPrefs.SetInt("Stars", starsCount);
PlayerPrefs.Save(); // Save the PlayerPrefs to disk
}
}