Skip to content

Commit

Permalink
Merge pull request #182 from 2024FALL-SWPP/bugfix/Anomaly22
Browse files Browse the repository at this point in the history
이상현상 22 바닥 타일 restoration 시간차 현상 해결
  • Loading branch information
seozzi authored Dec 3, 2024
2 parents 4ecded6 + 972504b commit 6468257
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
43 changes: 37 additions & 6 deletions 302/Assets/Scripts/SpecificAnomalyManager/Anomaly22Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Anomaly22Manager : MonoBehaviour
private GameObject floor; // 모든 타일들의 Parent
private GameManager gameManager;
private PlayerController playerController;
public float interval = 0.5f;
public float interval = 1f;
public float totalSeconds = 30f;
private bool isPlayerDead = false;

Expand Down Expand Up @@ -85,7 +85,7 @@ private IEnumerator AddBoxColliders()
{
foreach (var tile in floorTiles)
{
if (tile != floor.transform) // Skip the parent 'floor' object
if (tile != floor.transform && tile.name != "platform") // Skip the parent 'floor' object
{
BoxCollider tileCollider = tile.GetComponent<BoxCollider>();
if (tileCollider == null)
Expand Down Expand Up @@ -116,7 +116,7 @@ private IEnumerator CountSeconds()
void Update()
{
// 아래로 떨어졌는지 확인해서 Game Over 처리
if (playerController.transform.position.y < -10f && !isPlayerDead)
if (playerController.transform.position.y < -1f && !isPlayerDead && false)
{
playerController.Sleep();
isPlayerDead = true;
Expand Down Expand Up @@ -155,16 +155,47 @@ private IEnumerator TriggerRandomTileShakeAndFallWithInterval()

private IEnumerator RestoreAllTiles()
{
float duration = 1f; // Duration for the animation (1 second)
float elapsedTime = 0f;

// Store the original positions of all tiles
Dictionary<Transform, Vector3> originalPositions = new Dictionary<Transform, Vector3>();
foreach (var tile in floorTiles)
{
if (tile != floor.transform) // Skip the parent 'floor' object
{
originalPositions[tile] = tile.position;
}
}

// Animate the tiles back to y = 0 over the duration
while (elapsedTime < duration)
{
elapsedTime += Time.deltaTime;
float t = elapsedTime / duration; // Normalized time [0, 1]

foreach (var tile in originalPositions.Keys)
{
Vector3 startPos = originalPositions[tile];
Vector3 targetPos = new Vector3(startPos.x, 0f, startPos.z);
tile.position = Vector3.Lerp(startPos, targetPos, t);
}

yield return null; // Wait for the next frame
}

// Ensure all tiles are precisely at y = 0 at the end of the animation
foreach (var tile in originalPositions.Keys)
{
tile.position = new Vector3(tile.position.x, 0f, tile.position.z);

// Remove the Anomaly22_tile component if it exists
Anomaly22_tile tileScript = tile.GetComponent<Anomaly22_tile>();
if (tileScript != null)
{
tileScript.RestoreTile();
Destroy(tileScript);
}

yield return null;
}
}

}
44 changes: 13 additions & 31 deletions 302/Assets/Scripts/SpecificAnomalyManager/Anomaly22_tile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,65 @@ public class Anomaly22_tile : MonoBehaviour
{
private bool isFalling = false;
private bool isRestoring = false;
private Vector3 initialPosition;

// Duration for shaking, falling
public float shakeDuration = 2f; // Duration of shake
public float fallDistance = 15f; // Distance to fall in the Y axis
private float shakeDuration = 2f;
private float fallDistance = 15f;

// Audio properties
private AudioSource audioSource; // The AudioSource to play sounds on the tile
public AudioClip shakeSound; // The sound to play during shaking
[Header("Audio Settings")]
private AudioSource audioSource;
public AudioClip shakeSound;

void Awake()
{
// Ensure the AudioSource is properly initialized
audioSource = GetComponent<AudioSource>();
if (audioSource == null)
{
// If no AudioSource component, add one
audioSource = gameObject.AddComponent<AudioSource>();
}
}

// Public function to trigger the shake and fall process
public void TriggerShakeAndFall()
{
if (!isFalling && !isRestoring)
{

audioSource.PlayOneShot(shakeSound); // Play the sound once
StartCoroutine(ShakeAndFallRoutine());
audioSource.PlayOneShot(shakeSound);
StartCoroutine(ShakeAndFallRoutine());
}
}

// Coroutine to handle the shaking and falling process
private IEnumerator ShakeAndFallRoutine()
{
isFalling = true;

// Shake in Y-axis for the specified duration
float shakeTime = 0f;
Vector3 initialPosition = transform.position;
initialPosition = transform.position;

// shake 단계
while (shakeTime < shakeDuration)
{
float shakeAmount = Random.Range(-0.1f, 0.1f); // Adjust the shake amount as needed
float shakeAmount = Random.Range(-0.1f, 0.1f);
transform.position = new Vector3(initialPosition.x, initialPosition.y + shakeAmount, initialPosition.z);

shakeTime += Time.deltaTime;
yield return null;
}

// After shaking, reset position to initial (without shake)
// 떨어지는 단계
transform.position = initialPosition;

// Fall down by the specified distance on the Y-axis
float fallTime = 0f;
Vector3 fallStartPosition = transform.position;
Vector3 fallEndPosition = new Vector3(fallStartPosition.x, fallStartPosition.y - fallDistance, fallStartPosition.z);

while (fallTime < 1f) // Smooth fall over 1 second (adjust as needed)
while (fallTime < 1f)
{
transform.position = Vector3.Lerp(fallStartPosition, fallEndPosition, fallTime);
fallTime += Time.deltaTime;
yield return null;
}

// Ensure final position is exactly at the fall distance
transform.position = fallEndPosition;

isFalling = false;
}

// Public function to restore the tile (set Y position to 0)
public void RestoreTile()
{
// Set Y position to 0
transform.position = new Vector3(transform.position.x, 0f, transform.position.z);

// Destroy the Anomaly22_tile script after the tile is restored
Destroy(this); // This will remove the Anomaly22_tile script component from the GameObject
}
}

0 comments on commit 6468257

Please sign in to comment.