summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/Weapons/Boomerang/Boomerang.cs
blob: 5d2a5bb1f692019a52c3d1bd6429a65b973e3a82 (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 回旋镖
/// </summary>
public class Boomerang : MonoBehaviour
{
	public BoomerangAfterImage m_AfterImage;

	private Vector2 m_Direction;

	// 当前速度
	private float m_Speed;

	// 最大飞行距离
	private float m_MaxDist;

	private float m_RotateSpeed = 500f;

	Coroutine m_CoAfterImage;

	public void Set(Vector2 dir, float speed, float maxDist)
	{
		m_Direction = dir;
		m_Speed = speed;
		m_MaxDist = maxDist;

		m_CoAfterImage = StartCoroutine(CoAfterImage(0.05f));
	}

	private void Update()
	{
		Vector2 move = m_Direction * m_Speed * Time.deltaTime;

		m_MaxDist -= move.magnitude;
		if(m_MaxDist <= 0)
		{
			Destroy(this.gameObject);

			if (m_CoAfterImage != null)
			{
				StopCoroutine(m_CoAfterImage);
			}

			return;
		}

		Vector3 pos = transform.position;
		pos += new Vector3(move.x, move.y, 0);

		transform.position = pos;

		transform.rotation *= Quaternion.Euler(0,0,m_RotateSpeed * Time.deltaTime);
	}

	IEnumerator CoAfterImage(float interval)
	{
		while (true)
		{
			BoomerangAfterImage afterImage = BoomerangAfterImage.Instantiate(m_AfterImage);
			afterImage.Set(this);
			afterImage.transform.position = this.transform.position;
			afterImage.transform.localScale=  new Vector3(0.5f, 0.5f, 1f);
			afterImage.transform.rotation = this.transform.rotation;
			afterImage.gameObject.SetActive(true);

			yield return new WaitForSeconds(interval);
		}
	}

}