blob: 6e877b325de00e0bd48db1941f9b2b19c4c8eaa7 (
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
using JetBrains.Annotations;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
using WK.Rendering;
namespace MH
{
public class UnitManager
{
public static UnitBase hero;
}
public class SamuraiScript : UnitBase
{
[SerializeField] private WaypointScript m_Waypoint;
[SerializeField] private float m_Speed;
[SerializeField] private BladeScript m_Blade;
[SerializeField] private GameObject m_GroundBreak;
private SpriteRenderer m_Sprite;
private bool m_Moving;
private Camera m_Camera;
private float m_TimeSinceLastMove;
private const float kKeepMovingThreshold = 0.4f;
private bool m_IsKeepMoving = false;
private Coroutine m_CoWaypoint;
private bool m_Attacking = false;
private bool m_IsKeepAttacking = false;
private void Awake()
{
UnitManager.hero = this;
}
void Start()
{
m_Camera = Camera.main;
m_Moving = true;
m_Sprite = GetComponent<SpriteRenderer>();
m_TimeSinceLastMove = float.MaxValue;
StartCoroutine(CoAttack(1f));
StartCoroutine(CoStrike(1f));
}
protected override void Update()
{
base.Update();
Move();
LookAt();
}
private void LookAt()
{
Vector3 pos = transform.position;
Vector3 camPos = m_Camera.transform.position;
camPos.x = pos.x;
camPos.y = pos.y;
m_Camera.transform.position = camPos;
}
private void Move()
{
if(Input.GetMouseButtonDown(0))
{
float dt = Time.time - m_TimeSinceLastMove;
if(dt > 0 && dt < kKeepMovingThreshold)
{
m_IsKeepMoving = true;
}
else
{
m_IsKeepMoving = false;
}
}
m_Attacking = true;//Input.GetMouseButton(1);
if (Input.GetMouseButton(0) || m_IsKeepMoving)
{
m_Moving = true;
Vector3 mousePos = Input.mousePosition;
Vector3 mousePos3D = m_Camera.ScreenToWorldPoint(mousePos);
mousePos3D.z = 0;
Vector3 pos = transform.position;
pos.z = 0;
Vector3 toward = mousePos3D - pos;
toward.z = 0;
if (toward.magnitude < 0.1f)
{
return;
}
Vector3 dir = (mousePos3D - pos).normalized;
dir.z = 0;
//pos.x += Time.deltaTime;
transform.position += dir * Time.deltaTime * m_Speed;
m_Sprite.flipX = dir.x <= 0;
if(m_CoWaypoint == null)
{
m_CoWaypoint = StartCoroutine(CoShowWaypoint(0.05f));
}
}
else
{
if(m_CoWaypoint != null)
{
StopCoroutine(m_CoWaypoint);
m_CoWaypoint = null;
}
m_Moving = false;
}
GetComponent<SpriteAnimationController>().playing = m_Moving ? true : false;
if(Input.GetMouseButtonUp(0))
{
m_TimeSinceLastMove = Time.time;
}
}
IEnumerator CoShowWaypoint(float dt)
{
while (true)
{
WaypointScript waypoint = Instantiate(m_Waypoint) as WaypointScript;
Vector3 mousePos = Input.mousePosition;
Vector3 mousePos3D = m_Camera.ScreenToWorldPoint(mousePos);
mousePos3D.z = 0;
waypoint.transform.position = mousePos3D;
waypoint.life = 1;
yield return new WaitForSeconds(dt);
}
}
IEnumerator CoAttack(float interval)
{
int fac = 1;
while (true)
{
if (!m_Attacking)
{
yield return null;
}
else
{
Vector3 mousePos = Input.mousePosition;
Vector3 mousePos3D = m_Camera.ScreenToWorldPoint(mousePos);
Vector2 dir = (mousePos3D.xy() - transform.position.xy()).normalized;
BladeScript blade = Instantiate(m_Blade);
blade.life = 5f;
blade.transform.position = transform.position + new Vector3(1 * fac, 1f, 0);
blade.SetFlip(fac == -1);
blade.dir = dir;
fac *= -1;
yield return new WaitForSeconds(interval);
}
}
}
IEnumerator CoStrike(float interval)
{
int fac = 1;
while (true)
{
if (!m_Attacking)
{
yield return null;
}
else
{
yield return new WaitForSeconds(interval);
yield return new WaitForFixedUpdate();
Strike();
}
}
}
private void Strike()
{
Vector3 pos = transform.position;
float radius = 2;
var go = Instantiate(m_GroundBreak);
go.transform.position = pos;
go.gameObject.SetActive(true);
go.transform.localScale *= radius / 1.7f;
var colliders = PhysicsManager.Instance.CircleCast(ColliderType.Hurtbox, new Vector3(pos.x, pos.y, radius));
if (colliders.Count != 0)
{
for(int i = 0; i < colliders.Count; ++i)
{
go = (colliders[i] as MonoBehaviour).gameObject;
if(go != this.gameObject)
{
GameObject.Destroy(go);
}
}
}
}
}
}
|