Files
Crash-Course/Assets/Scripts/Game/FallPlatform.cs

109 lines
3.6 KiB
C#
Raw Normal View History

using System.Collections;
2025-04-18 15:54:50 -04:00
using UnityEngine;
using Game;
using Music;
using Player;
2025-04-16 19:57:54 -04:00
namespace Game
{
2025-04-18 15:54:50 -04:00
/// <summary>
/// This class controls platforms that fall when touched by a player or another platform.
/// The platform will fall after a delay and then reset to its original position.
/// </summary>
public class FallPlatform : MonoBehaviour
{
/// <summary>
/// The time (in seconds) before the platform starts falling after being triggered.
/// </summary>
public float fallDelay = 2f;
2025-04-18 15:54:50 -04:00
/// <summary>
/// The time (in seconds) before the platform resets to its original position after falling.
/// </summary>
public float resetDelay = 4f;
2025-04-18 15:54:50 -04:00
/// <summary>
/// Indicates whether the platform is currently falling.
/// </summary>
private bool falling;
2025-04-18 15:54:50 -04:00
/// <summary>
/// Reference to the Rigidbody2D component of the platform's parent object.
/// </summary>
private Rigidbody2D rb;
/// <summary>
/// The original position of the platform's parent object.
/// </summary>
private Vector3 defposition;
/// <summary>
/// Initializes the platform's Rigidbody2D and stores its original position.
/// </summary>
private void Start()
{
2025-04-18 15:54:50 -04:00
defposition = transform.parent.position;
rb = transform.parent.GetComponent<Rigidbody2D>();
}
/// <summary>
/// Triggers the platform to fall when a player or another platform touches it.
/// </summary>
/// <param name="collision">The object that collided with the platform.</param>
private void OnTriggerEnter2D(Collider2D collision)
{
try
2025-04-07 18:56:43 -04:00
{
2025-04-18 15:54:50 -04:00
// Check if the collision is caused by a player or another falling platform
if (collision.transform.childCount != 0 && !falling &&
(collision.gameObject.CompareTag("Player") || collision.transform.GetChild(0).TryGetComponent(out FallPlatform _)))
{
StartCoroutine(FallAfterDelay());
}
}
catch (System.Exception e)
{
Debug.LogError("Error in FallPlatform: " + e.Message);
2025-04-07 18:56:43 -04:00
}
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Makes the platform fall after a delay and resets it after another delay.
/// </summary>
/// <returns>An IEnumerator for coroutine execution.</returns>
private IEnumerator FallAfterDelay()
2025-04-07 18:56:43 -04:00
{
2025-04-18 15:54:50 -04:00
falling = true;
// Wait for the fall delay before making the platform fall
yield return new WaitForSeconds(fallDelay);
rb.bodyType = RigidbodyType2D.Dynamic;
// Wait for the reset delay before resetting the platform
yield return new WaitForSeconds(resetDelay);
transform.parent.GetComponent<Animator>().SetTrigger("respawn");
// Wait briefly before resetting the platform
yield return new WaitForSeconds(0.5f);
Respawn();
}
2025-04-18 15:54:50 -04:00
/// <summary>
/// Resets the platform to its original position and state.
/// </summary>
private void Respawn()
{
falling = false;
2025-04-18 15:54:50 -04:00
// Set the platform back to a static state
rb.bodyType = RigidbodyType2D.Static;
// Reset the platform's position and rotation
transform.parent.position = defposition;
transform.parent.rotation = Quaternion.identity;
}
2025-03-07 11:29:09 -05:00
}
}
2025-04-18 15:54:50 -04:00