summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/Test/TestB2.cs
blob: 532c254b4f5329fb57b75a73f7d7413690eafdc7 (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
72
73
74
75
76
77
78
79
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestB2 : MonoBehaviour
{
	[SerializeField] private TestBomb m_Bomb;
	[SerializeField] private float m_Speed;

	private Vector3 m_From;
	private Vector3 m_To;

	private Coroutine m_CoBomb;

	Vector3 direction
	{
		get
		{
			return (m_To - m_From).normalized;
		}
	}

	/// <summary>
	/// from to ÔÚ3d¿Õ¼ä
	/// </summary>
	/// <param name="from"></param>
	/// <param name="to"></param>
	public void Set(Vector3 from, Vector3 to, float speed, float lifeTime)
	{
		m_From = from;
		m_To = to;
		m_Speed = speed;

		transform.position = from;

		this.gameObject.SetActive(true);

		m_CoBomb = StartCoroutine(coBomb(0.1f));

		Invoke("DestroySelf", lifeTime);
	}

	void DestroySelf()
	{
		if (m_CoBomb != null)
		{
			StopCoroutine(m_CoBomb);
			m_CoBomb = null;
		}
		this.gameObject.SetActive(false);
		Destroy(this.gameObject);
	}

	private void Update()
	{
		Vector3 pos = transform.position;
		pos += direction * m_Speed * Time.deltaTime;

		transform.position = pos;
	}

	IEnumerator coBomb(float interval)
	{
		while (true)
		{
			Vector3 pos = transform.position;
			for(int i = 0; i < 1; ++i)
			{
				TestBomb grenade = Instantiate<TestBomb>(m_Bomb);
				Vector3 position = new Vector3(pos.x + Random.Range(-3, 3), pos.y + Random.Range(-5f, 5f), 7f);
				grenade.Set(position, new Vector3(0, 0, -1f), Random.Range(8f, 10f));
				grenade.gameObject.SetActive(true);
			}

			yield return new WaitForSeconds(interval);
		}
	}

}