summaryrefslogtreecommitdiff
path: root/AlienSurvival/Assets/Test/Scripts/TestPeaceMakerGrenade.cs
blob: 38dc2f5f52954be263d52cadb40ae95e159b6c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestPeaceMakerGrenade : MonoBehaviour
{

	[SerializeField] private float m_GravityScale = 1f;

	[SerializeField] private GameObject m_ExplosionEffect;

	private TestFakeHeight m_Coords;

	private Vector3 GRAVITY = new Vector3(0, 0, -9.8f);

	private Vector3 m_Velocity; // x, y, fakeHeight


	/// <summary>
	/// 设置初始参数,都在fake空间下
	/// </summary>
	/// <param name="initPosition"></param>
	/// <param name="initDirection"></param>
	/// <param name="initSpeed"></param>
	public void Set(Vector3 initPosition, Vector3 initDirection, float initSpeed)
	{
		m_Coords = GetComponent<TestFakeHeight>();

		m_Coords.x = initPosition.x;
		m_Coords.y = initPosition.y;
		m_Coords.height = initPosition.z;

		m_Velocity = initDirection * initSpeed;
	}

	private void Update()
	{
		Vector3 move = m_Velocity * Time.deltaTime;

		if(m_Velocity.magnitude > 0 && m_Coords.height + move.z >= 0)
		{
			m_Coords.x += move.x;
			m_Coords.y += move.y;
			m_Coords.height += move.z;
			m_Velocity += GRAVITY * Time.deltaTime;

			transform.rotation *= Quaternion.Euler(0, 0, 500 * Time.deltaTime);
		}
		else
		{
			m_Velocity = Vector3.zero;

			this.gameObject.SetActive(false);
			Destroy(this.gameObject);
			PlayExplosion();
		}
	}

	private void PlayExplosion()
	{
		GameObject exp = Instantiate<GameObject>(m_ExplosionEffect);

		TestFakeHeight coord = exp.GetComponent<TestFakeHeight>();
		coord.x = m_Coords.x;
		coord.y = m_Coords.y;
		coord.height = m_Coords.height;

		exp.SetActive(true);
	}

}