summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/Test/TestBrokenPiece.cs
diff options
context:
space:
mode:
Diffstat (limited to 'SurvivalTest/Assets/Scripts/Test/TestBrokenPiece.cs')
-rw-r--r--SurvivalTest/Assets/Scripts/Test/TestBrokenPiece.cs107
1 files changed, 107 insertions, 0 deletions
diff --git a/SurvivalTest/Assets/Scripts/Test/TestBrokenPiece.cs b/SurvivalTest/Assets/Scripts/Test/TestBrokenPiece.cs
new file mode 100644
index 0000000..4399886
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/Test/TestBrokenPiece.cs
@@ -0,0 +1,107 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class TestBrokenPiece : MonoBehaviour
+{
+ [SerializeField] private List<Sprite> m_Pieces;
+
+ private TestFakeHeight m_Coord;
+
+ private Vector3 m_Velocity;
+
+ private Vector3 m_Gravity = new Vector3(0, 0, -9.8f);
+
+ private float m_Damp = 0.5f;
+
+ private SpriteRenderer m_SpriteRenderer;
+
+ enum State
+ {
+ Avlie ,
+ Dead ,
+ }
+
+ State m_State;
+
+ private void Awake()
+ {
+ m_Coord = GetComponent<TestFakeHeight>();
+ m_SpriteRenderer = GetComponent<SpriteRenderer>();
+ }
+
+ public void Set(Vector3 position, Vector3 dir, float speed, int index)
+ {
+ m_Coord = GetComponent<TestFakeHeight>();
+ m_SpriteRenderer = GetComponent<SpriteRenderer>();
+
+ m_Coord.x = position.x;
+ m_Coord.y = position.y;
+ m_Coord.height = position.z;
+
+ m_Velocity = dir * speed;
+
+ m_SpriteRenderer.sprite = m_Pieces[index % m_Pieces.Count];
+
+ m_State = State.Avlie;
+ }
+
+ private void Update()
+ {
+ if(m_State == State.Avlie)
+ {
+ m_Velocity += m_Gravity * Time.deltaTime;
+
+ m_Coord.x += m_Velocity.x * Time.deltaTime;
+ m_Coord.y += m_Velocity.y * Time.deltaTime;
+ m_Coord.height += m_Velocity.z * Time.deltaTime;
+
+ if (m_Coord.height < 0)
+ {
+ m_Coord.height = 0;
+
+ // bounce
+ m_Velocity.x = m_Velocity.x * m_Damp;
+ m_Velocity.y = m_Velocity.y * m_Damp;
+ m_Velocity.z = -m_Velocity.z * m_Damp;
+
+ if (m_Velocity.magnitude < 0.1f)
+ {
+ m_State = State.Dead;
+ StartCoroutine(coDead(Random.Range(1f, 3f)));
+ }
+ }
+ }
+ else if(m_State == State.Dead)
+ {
+ }
+ }
+
+ IEnumerator coDead( float time)
+ {
+ Color c = m_SpriteRenderer.color;
+
+ float t = 0;
+ while (true)
+ {
+ t += Time.deltaTime;
+
+ c.a = (time - t) / time;
+
+ m_SpriteRenderer.color = c;
+
+ if(t > time)
+ {
+ break;
+ }
+
+ yield return null;
+ }
+
+ this.gameObject.SetActive(false);
+ Destroy(this.gameObject);
+
+ yield break;
+ }
+
+}