BGJ-2022.1/Assets/Scripts/Platforms/CollapsingPlatform.cs

84 lines
2.3 KiB
C#
Raw Normal View History

2022-02-25 13:32:01 +03:00
using System.Collections;
using Movement;
2022-02-23 13:47:46 +03:00
using UnityEngine;
namespace Platforms
{
public class CollapsingPlatform : MonoBehaviour
{
2022-02-25 13:32:01 +03:00
private const float TimeBeforeCollapse = 2.0f;
private const float TimeBeforeReset = 4.0f;
private CollisionChecker _collidingTriggerCheck;
2022-02-26 14:10:48 +03:00
public Sprite small;
public Sprite medium;
public Sprite large;
public Sprite smallCollapsing;
public Sprite mediumCollapsing;
public Sprite largeCollapsing;
2022-02-25 13:32:01 +03:00
private bool _onCollision;
2022-02-26 14:10:48 +03:00
[Range(1, 3)] public int size;
2022-02-25 13:32:01 +03:00
private void Awake()
{
_collidingTriggerCheck = GetComponentInChildren<CollisionChecker>();
}
private void Update()
{
if (!_collidingTriggerCheck.IsCollided || _onCollision) return;
_onCollision = true;
StartCoroutine(Collapse());
}
private void ChangeState(bool state)
{
GetComponent<SpriteRenderer>().enabled = state;
GetComponent<Collider2D>().enabled = state;
}
private IEnumerator Collapse()
{
2022-02-26 14:10:48 +03:00
switch (size)
{
case 1:
GetComponent<SpriteRenderer>().sprite = smallCollapsing;
break;
case 2:
GetComponent<SpriteRenderer>().sprite = mediumCollapsing;
break;
case 3:
GetComponent<SpriteRenderer>().sprite = largeCollapsing;
break;
}
2022-02-25 13:32:01 +03:00
yield return new WaitForSeconds(TimeBeforeCollapse);
ChangeState(false);
StartCoroutine(Reset());
}
2022-02-25 14:12:49 +03:00
// ReSharper disable once Unity.IncorrectMethodSignature
2022-02-25 13:32:01 +03:00
private IEnumerator Reset()
{
yield return new WaitForSeconds(TimeBeforeReset);
_onCollision = false;
2022-02-26 14:10:48 +03:00
switch (size)
{
case 1:
GetComponent<SpriteRenderer>().sprite = small;
break;
case 2:
GetComponent<SpriteRenderer>().sprite = medium;
break;
case 3:
GetComponent<SpriteRenderer>().sprite = large;
break;
}
2022-02-25 13:32:01 +03:00
ChangeState(true);
}
2022-02-23 13:47:46 +03:00
}
}