diff options
author | chai <215380520@qq.com> | 2023-05-04 11:25:52 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-04 11:25:52 +0800 |
commit | 848097c88bbcf24934a375dff39cf4defa2819dd (patch) | |
tree | 85f4cc4d09fbf2a3839752449d21cb3e0bec8fa1 /marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs | |
parent | ba1ad0efd8601dc4af023aca5a78609c55b4d67f (diff) |
*misc
Diffstat (limited to 'marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs')
-rw-r--r-- | marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs b/marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs new file mode 100644 index 0000000..29b182c --- /dev/null +++ b/marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs @@ -0,0 +1,155 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using static UnityEditor.PlayerSettings; + +namespace MH +{ + + public class SamuraiScript : UnitBase + { + [SerializeField] private WaypointScript m_Waypoint; + [SerializeField] private float m_Speed; + [SerializeField] private BladeScript m_Blade; + + 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; + + void Start() + { + m_Camera = Camera.main; + m_Moving = true; + m_Sprite = GetComponent<SpriteRenderer>(); + m_TimeSinceLastMove = float.MaxValue; + StartCoroutine(CoAttack(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 = 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<Animator>().speed = m_Moving ? 1 : 0; + + 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 + { + BladeScript blade = Instantiate(m_Blade); + blade.life = 0.5f; + blade.transform.position = transform.position + new Vector3(1 * fac, 1f, 0); + blade.SetFlip(fac == -1); + fac *= -1; + yield return new WaitForSeconds(interval); + } + } + } + + } + +} |