blob: 3a73d21bfab03a172d7b34cc3c30d0af1989af9b (
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestWaspRobot : MonoBehaviour
{
[SerializeField] private TopDownTransform m_Follow;
[SerializeField] private float m_MoveSpeed;
private SpriteRenderer m_SpriteRenderer;
private TopDownTransform m_Coords;
// wasp»·ÈÆ
[SerializeField] private float m_Radius;
[SerializeField] private List<Sprite> m_Sprites;
private bool m_Following;
private float m_Angle;
private float m_OrbitSpeed = 3f;
enum State
{
None,
Move,
Orbit,
}
private State m_State = State.None;
private Coroutine m_Coroutine;
private void Start()
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_Coords = GetComponent<TopDownTransform>();
m_Following = false;
m_Angle = 0;
ChangeState(State.Move);
}
private void ChangeState(State state)
{
if(m_State != state)
{
//Debug.Log(m_State.ToString() + "->" + state.ToString());
if(m_Coroutine != null)
{
StopCoroutine(m_Coroutine);
}
m_State = state;
m_Coroutine = StartCoroutine("co" + m_State.ToString());
}
}
void Update()
{
float distance = (m_Follow.positionOnGround - m_Coords.positionOnGround).magnitude;
m_Following = distance > m_Radius + 0.1f;
if(m_Following)
{
ChangeState(State.Move);
}
else
{
ChangeState(State.Orbit);
}
}
IEnumerator coMove()
{
while (true)
{
if (m_Following)
{
Vector2 dir = (m_Follow.positionOnGround - m_Coords.positionOnGround).normalized;
Vector2 posOnGround = m_Coords.positionOnGround;
posOnGround.x += m_MoveSpeed * Time.deltaTime * dir.x;
posOnGround.y += m_MoveSpeed * Time.deltaTime * dir.y;
m_Coords.positionOnGround = posOnGround;
float rad = GetAngleToTarget();
int index = GetSpriteIndex(rad);
SetSprite(index);
}
yield return null;
}
}
IEnumerator coOrbit()
{
m_Angle = GetAngleToTarget();
while (true)
{
m_Angle += Time.deltaTime * m_OrbitSpeed;
Vector2 centre = m_Follow.positionOnGround;
centre += new Vector2(Mathf.Cos(m_Angle), Mathf.Sin(m_Angle)).normalized * m_Radius;
m_Coords.positionOnGround = Vector2.Lerp(m_Coords.positionOnGround, centre, 0.25f);
int index = GetSpriteIndex(m_Angle);
SetSprite(index);
yield return null;
}
}
float GetAngleToTarget()
{
Vector2 posOnGround = m_Coords.positionOnGround;
Vector2 target = m_Follow.positionOnGround;
Vector2 dir = (posOnGround - target).normalized;
return Mathf.Atan2(dir.y, dir.x) + 2 * Mathf.PI * TestMathHelper.Check(dir.y < 0);
}
// angle rad
int GetSpriteIndex(float rad)
{
int index = ((int)Mathf.Floor(((rad * Mathf.Rad2Deg) % 360) / 30)) % 12;
index = Mathf.Clamp(index, 0, m_Sprites.Count - 1);
return index;
}
void SetSprite(int index)
{
m_SpriteRenderer.sprite = m_Sprites[index];
}
}
|