diff options
Diffstat (limited to 'AlienSurvival/Assets/Scripts/Test')
66 files changed, 2386 insertions, 0 deletions
diff --git a/AlienSurvival/Assets/Scripts/Test/TestAstro.cs b/AlienSurvival/Assets/Scripts/Test/TestAstro.cs new file mode 100644 index 0000000..3b4df18 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestAstro.cs @@ -0,0 +1,77 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestAstro : MonoBehaviour +{ + [SerializeField] private Transform m_Camera; + + [SerializeField] private float m_Speed; + + [SerializeField] private SpriteRenderer m_Shadow; + + private SpriteRenderer m_SpriteRenderer; + private TestFakeHeight m_FakeHeight; + + + + void Start() + { + m_SpriteRenderer = GetComponent<SpriteRenderer>(); + m_FakeHeight = GetComponent<TestFakeHeight>(); + } + + void Update() + { + float x = Input.GetAxisRaw("Horizontal"); + float y = Input.GetAxisRaw("Vertical"); + + Vector3 targetZoom = Vector3.one; + + if(x != 0 || y != 0) + { + targetZoom = new Vector3(1f, 1f, 1); + + Vector2 direction = new Vector2(x, y).normalized; + + Vector3 position = transform.position; + position.x += direction.x * m_Speed * Time.deltaTime; + position.y += direction.y * m_Speed * Time.deltaTime; + + transform.position = position; + + if (x > 0) + { + m_SpriteRenderer.flipX = false; + m_Shadow.flipX = false; + } + else if (x < 0) + { + m_SpriteRenderer.flipX = true; + m_Shadow.flipX = true; + } + } + else + { + targetZoom = new Vector3(0.5f, 0.5f, 1); + } + + Vector3 zoom = m_Camera.localScale; + m_Camera.localScale = Vector3.Lerp(zoom, targetZoom, Time.deltaTime); + + CameraFollow(); + } + + void CameraFollow() + { + Vector3 pos = m_Camera.position; + pos.x = m_FakeHeight.x; + pos.y = m_FakeHeight.y; + + if(Vector3.Distance(pos, m_Camera.position) > 0.1f) + { + m_Camera.position = Vector3.Lerp(m_Camera.position, pos, 3 * Time.deltaTime); + } + + } +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestAstro.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestAstro.cs.meta new file mode 100644 index 0000000..a96a9b4 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestAstro.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: dc04e45eaa06e994a806119e4fd0701c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestB2.cs b/AlienSurvival/Assets/Scripts/Test/TestB2.cs new file mode 100644 index 0000000..532c254 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestB2.cs @@ -0,0 +1,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); + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestB2.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestB2.cs.meta new file mode 100644 index 0000000..1e507c7 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestB2.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a908894d8ef917543ad67959640ec30b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestBeamBullet.cs b/AlienSurvival/Assets/Scripts/Test/TestBeamBullet.cs new file mode 100644 index 0000000..17d0005 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBeamBullet.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestBeamBullet : MonoBehaviour +{ + public Vector3 direction; + public float speed; + + // Start is called before the first frame update + void Start() + { + Invoke("Dead", 3); + } + + void Dead() + { + if (this.gameObject) + { + GameObject.Destroy(this.gameObject); + } + } + + // Update is called once per frame + void Update() + { + Vector3 position = transform.position; + position += direction * speed * Time.deltaTime; + transform.position = position; + } + + private void OnTriggerEnter2D(Collider2D collision) + { + GameObject go = collision.gameObject; + + if (!go.CompareTag("enemy")) + { + return; + } + + GameObject.Destroy( collision.gameObject); + + //this.gameObject.SetActive(false); + + if (this.gameObject) + { + GameObject.Destroy(this.gameObject); + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestBeamBullet.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestBeamBullet.cs.meta new file mode 100644 index 0000000..b1551a7 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBeamBullet.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ca62ef044d3b4104eac135eea37e0c03 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestBeamGun.cs b/AlienSurvival/Assets/Scripts/Test/TestBeamGun.cs new file mode 100644 index 0000000..69225b6 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBeamGun.cs @@ -0,0 +1,79 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestBeamGun : MonoBehaviour +{ + public TestBeamBullet bullet; + + public Transform start; + + public float speed = 1; + + SpriteRenderer sprite; + + // Start is called before the first frame update + void Start() + { + sprite = GetComponent<SpriteRenderer>(); + + // StartCoroutine(coFire()); + } + + // Update is called once per frame + void Update() + { + + Vector3 screenPos = Input.mousePosition; + + Vector2 target = Camera.main.ScreenToWorldPoint(screenPos); + + Vector2 dir = Vector2.ClampMagnitude(target - (Vector2)transform.position, 1); + Vector3 move = dir * speed * Time.deltaTime; + + float x = move.x; + if (x > 0) + { + sprite.flipY = false; + } + else if (x < 0) + { + sprite.flipY = true; + } + + transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * Mathf.Atan2(dir.y, dir.x)); + + Shot(); + } + + void Shot() + { + if (Input.GetButtonDown("Fire1")) + { + //Debug.Log("Shoot"); + + TestBeamBullet b = TestBeamBullet.Instantiate(bullet); + b.gameObject.SetActive(true); + + b.transform.position = start.position; + + b.direction = transform.right; + } + } + + IEnumerator coFire() + { + while (true) + { + TestBeamBullet b = TestBeamBullet.Instantiate(bullet); + b.gameObject.SetActive(true); + + b.transform.position = start.position; + + b.direction = transform.right; + + yield return new WaitForSeconds(0.5f); + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestBeamGun.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestBeamGun.cs.meta new file mode 100644 index 0000000..cfeb8f2 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBeamGun.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 058a2b89691b2f94d912ff341719edd9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestBomb.cs b/AlienSurvival/Assets/Scripts/Test/TestBomb.cs new file mode 100644 index 0000000..77933f4 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBomb.cs @@ -0,0 +1,67 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestBomb : MonoBehaviour +{ + + [SerializeField] private float m_GravityScale = 1f; + + [SerializeField] private GameObject m_ExplosionEffect; + + private TopDownTransform m_Coords; + + private Vector3 GRAVITY = new Vector3(0, 0, -9.8f); + + private Vector3 m_Velocity; // x, y, fakeHeight + + /// <summary> + /// 设置初始参数,都在fake空间下 + /// </summary> + /// <param name="initPosition"></param> + /// <param name="initDirection"></param> + /// <param name="initSpeed"></param> + public void Set(Vector3 initPosition, Vector3 initDirection, float initSpeed) + { + m_Coords = GetComponent<TopDownTransform>(); + m_Coords.position = initPosition; + + m_Velocity = initDirection * initSpeed; + } + + private void Update() + { + Vector3 move = m_Velocity * Time.deltaTime; + + if (m_Velocity.magnitude > 0 && m_Coords.z + move.z >= 0) + { + m_Coords.x += move.x; + m_Coords.y += move.y; + m_Coords.z += move.z; + m_Velocity += GRAVITY * Time.deltaTime; + + //transform.rotation *= Quaternion.Euler(0, 0, 20 * m_Velocity.magnitude * Time.deltaTime); + } + else + { + m_Velocity = Vector3.zero; + + this.gameObject.SetActive(false); + Destroy(this.gameObject); + PlayExplosion(); + } + } + + private void PlayExplosion() + { + GameObject exp = Instantiate<GameObject>(m_ExplosionEffect); + + TopDownTransform coord = exp.GetComponent<TopDownTransform>(); + coord.position = m_Coords.position; + + exp.GetComponent<TopDownSorting>().Sorting(); + + exp.SetActive(true); + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestBomb.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestBomb.cs.meta new file mode 100644 index 0000000..06d61d2 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBomb.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c0bb267c6728269489ed4f713b032db5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestBrokenPiece.cs b/AlienSurvival/Assets/Scripts/Test/TestBrokenPiece.cs new file mode 100644 index 0000000..4399886 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBrokenPiece.cs @@ -0,0 +1,107 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestBrokenPiece : MonoBehaviour +{ + [SerializeField] private List<Sprite> m_Pieces; + + private TestFakeHeight m_Coord; + + private Vector3 m_Velocity; + + private Vector3 m_Gravity = new Vector3(0, 0, -9.8f); + + private float m_Damp = 0.5f; + + private SpriteRenderer m_SpriteRenderer; + + enum State + { + Avlie , + Dead , + } + + State m_State; + + private void Awake() + { + m_Coord = GetComponent<TestFakeHeight>(); + m_SpriteRenderer = GetComponent<SpriteRenderer>(); + } + + public void Set(Vector3 position, Vector3 dir, float speed, int index) + { + m_Coord = GetComponent<TestFakeHeight>(); + m_SpriteRenderer = GetComponent<SpriteRenderer>(); + + m_Coord.x = position.x; + m_Coord.y = position.y; + m_Coord.height = position.z; + + m_Velocity = dir * speed; + + m_SpriteRenderer.sprite = m_Pieces[index % m_Pieces.Count]; + + m_State = State.Avlie; + } + + private void Update() + { + if(m_State == State.Avlie) + { + m_Velocity += m_Gravity * Time.deltaTime; + + m_Coord.x += m_Velocity.x * Time.deltaTime; + m_Coord.y += m_Velocity.y * Time.deltaTime; + m_Coord.height += m_Velocity.z * Time.deltaTime; + + if (m_Coord.height < 0) + { + m_Coord.height = 0; + + // bounce + m_Velocity.x = m_Velocity.x * m_Damp; + m_Velocity.y = m_Velocity.y * m_Damp; + m_Velocity.z = -m_Velocity.z * m_Damp; + + if (m_Velocity.magnitude < 0.1f) + { + m_State = State.Dead; + StartCoroutine(coDead(Random.Range(1f, 3f))); + } + } + } + else if(m_State == State.Dead) + { + } + } + + IEnumerator coDead( float time) + { + Color c = m_SpriteRenderer.color; + + float t = 0; + while (true) + { + t += Time.deltaTime; + + c.a = (time - t) / time; + + m_SpriteRenderer.color = c; + + if(t > time) + { + break; + } + + yield return null; + } + + this.gameObject.SetActive(false); + Destroy(this.gameObject); + + yield break; + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestBrokenPiece.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestBrokenPiece.cs.meta new file mode 100644 index 0000000..73da026 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBrokenPiece.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c7182d8bf5ade5c47a2a98ae355119f3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestBucket.cs b/AlienSurvival/Assets/Scripts/Test/TestBucket.cs new file mode 100644 index 0000000..c3e28f4 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBucket.cs @@ -0,0 +1,35 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestBucket : MonoBehaviour +{ + [SerializeField] private TestBrokenPiece m_Piece; + + private TopDownTransform m_Coord; + + private void Awake() + { + m_Coord = GetComponent<TopDownTransform>(); + } + + public void Broken() + { + for(int i = 0; i < 10; ++i) + { + TestBrokenPiece piece = Instantiate<TestBrokenPiece>(m_Piece); + piece.Set(m_Coord.position + new Vector3(0,0,1f), + new Vector3(Random.Range(-1f,1f), Random.Range(-1f, 1f), Random.Range(0.5f, 2f)).normalized, Random.Range(1f, 3f) + , i); + } + + this.gameObject.SetActive(false); + Destroy(this.gameObject); + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestBucket.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestBucket.cs.meta new file mode 100644 index 0000000..3d9e92b --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestBucket.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b3861aac24d796348936aa3f51d27a97 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestCameraSortByY.cs b/AlienSurvival/Assets/Scripts/Test/TestCameraSortByY.cs new file mode 100644 index 0000000..585f2c3 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestCameraSortByY.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[RequireComponent(typeof(Camera))] +public class TestCameraSortByY : MonoBehaviour +{ + private Camera camera; + + // Start is called before the first frame update + void Start() + { + camera = GetComponent<Camera>(); + camera.transparencySortMode = TransparencySortMode.CustomAxis; + camera.transparencySortAxis = Vector3.up; + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestCameraSortByY.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestCameraSortByY.cs.meta new file mode 100644 index 0000000..210a02e --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestCameraSortByY.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b692e7619b71ada41b02f616b9a6134d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestCannon.cs b/AlienSurvival/Assets/Scripts/Test/TestCannon.cs new file mode 100644 index 0000000..22b168b --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestCannon.cs @@ -0,0 +1,44 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestCannon : MonoBehaviour +{ + public TestBeamBullet bullet; + + public Transform start; + + public float speed = 1; + + SpriteRenderer sprite; + + // Start is called before the first frame update + void Start() + { + sprite = GetComponent<SpriteRenderer>(); + + StartCoroutine(coFire(1f)); + } + + // Update is called once per frame + void Update() + { + + } + + IEnumerator coFire(float interval) + { + while (true) + { + TestBeamBullet b = TestBeamBullet.Instantiate(bullet); + b.gameObject.SetActive(true); + + b.transform.position = start.position; + + b.direction = -transform.right; + + yield return new WaitForSeconds(interval); + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestCannon.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestCannon.cs.meta new file mode 100644 index 0000000..52c902c --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestCannon.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 14e80cec1a94a5449b2081ac81bea639 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestCharacterMovement.cs b/AlienSurvival/Assets/Scripts/Test/TestCharacterMovement.cs new file mode 100644 index 0000000..8911ea9 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestCharacterMovement.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestCharacterMovement : MonoBehaviour +{ + public SpriteRenderer sprite; + + public float speed; + + // Start is called before the first frame update + void Start() + { + sprite = GetComponent<SpriteRenderer>(); + + } + + // Update is called once per frame + void Update() + { + float x = Input.GetAxis("Horizontal"); + float y = Input.GetAxis("Vertical"); + + Vector3 position = transform.position; + position.x += x * speed * Time.deltaTime; + position.y += y * speed * Time.deltaTime; + + transform.position = position; + + if (x > 0) + { + sprite.flipX = false; + } + else if(x < 0) + { + sprite.flipX = true; + } + + } +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestCharacterMovement.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestCharacterMovement.cs.meta new file mode 100644 index 0000000..606b4cc --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestCharacterMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a2fd7e5c6f466b7448001599a97c5b58 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestDestroySelf.cs b/AlienSurvival/Assets/Scripts/Test/TestDestroySelf.cs new file mode 100644 index 0000000..e0457cd --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestDestroySelf.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestDestroySelf : MonoBehaviour +{ + public float lifeTime; + + public AnimationCurve alphaCurve; + + private float time; + + private SpriteRenderer sr; + + void Start() + { + Invoke("DestroySelf", lifeTime); + time = 0; + sr = GetComponent<SpriteRenderer>(); + } + + void DestroySelf() + { + this.gameObject.SetActive(false); + Destroy(this.gameObject); + } + + void Update() + { + time += Time.deltaTime; + + if (sr) + { + Color c = sr.color; + c.a = alphaCurve.Evaluate(time / lifeTime); + sr.color = c; + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestDestroySelf.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestDestroySelf.cs.meta new file mode 100644 index 0000000..c1dc2b5 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestDestroySelf.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6bde42b3ff5cbc44ca4e036544c3cde9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestEnforceTransform0.cs b/AlienSurvival/Assets/Scripts/Test/TestEnforceTransform0.cs new file mode 100644 index 0000000..57b777d --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestEnforceTransform0.cs @@ -0,0 +1,11 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestEnforceTransform0 : MonoBehaviour +{ + void Update() + { +// transform.localToWorldMatrix = Matrix4x4.identity; + } +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestEnforceTransform0.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestEnforceTransform0.cs.meta new file mode 100644 index 0000000..3371029 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestEnforceTransform0.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 666db70fcce7c7e4a94cc321eb68e307 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestFakeCube.cs b/AlienSurvival/Assets/Scripts/Test/TestFakeCube.cs new file mode 100644 index 0000000..5d8f2c5 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestFakeCube.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestFakeCube : MonoBehaviour +{ + public float z; // fake height + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } + + + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestFakeCube.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestFakeCube.cs.meta new file mode 100644 index 0000000..83c3602 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestFakeCube.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 84033172d3ac67f4eb3eb8586b6ecb11 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestFakeHeight.cs b/AlienSurvival/Assets/Scripts/Test/TestFakeHeight.cs new file mode 100644 index 0000000..4cc13d1 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestFakeHeight.cs @@ -0,0 +1,132 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif + +//[ExecuteInEditMode] +public class TestFakeHeight : MonoBehaviour +{ + [SerializeField] private Transform m_Shadow; + [SerializeField] private Vector3 m_Coord; + + + public float height { + get + { + return m_Height; + } + set + { + m_Height = value; + } + } + [SerializeField] private float m_Height; // current fake height + + public float x + { + get + { + return transform.position.x; + } + + set + { + Vector3 pos = transform.position; + pos.x = value; + transform.position = pos; + } + } + + public float y + { + get + { + return transform.position.y - m_Height; + } + set + { + Vector3 pos = transform.position; + pos.y = value + m_Height; + transform.position = pos; + } + } + + public Vector3 position + { + get + { + Vector3 pos = new Vector3(x, y, height); + return pos; + } + set + { + height = value.z; // height先设置 + x = value.x; + y = value.y; + } + } + + public Vector2 positionOnGround + { + get + { + Vector2 pos = new Vector2(x, y); + return pos; + } + set + { + x = value.x; + y = value.y; + } + } + + private SpriteRenderer m_SpriteRenderer; + + private void OnEnable() + { + m_SpriteRenderer = GetComponent<SpriteRenderer>(); + } + + private void Start() + { + Project(); + } + + void Project() + { + Vector3 pos = transform.position; + pos.y = y + height; + transform.position = pos; + + if (m_Shadow != null) + { + m_Shadow.position = new Vector3(transform.position.x, transform.position.y - m_Height, transform.position.z); + } + + // 根据y设置sortOrder + m_SpriteRenderer.sortingOrder = (int)(-y * 100); + } + + void Update() + { + Project(); + } + +#if UNITY_EDITOR + + private void OnDrawGizmos() + { + // dash line + Vector3 start = transform.position; + Vector3 end = start - new Vector3(0, m_Height, 0); + + Handles.DrawDottedLine(start, end, 1f); + Handles.DrawWireCube(end, new Vector3(0.1f, 0.1f, 0f)); + + } + +#endif + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestFakeHeight.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestFakeHeight.cs.meta new file mode 100644 index 0000000..a91173e --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestFakeHeight.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 780072e6ea35d1f4abd930754c5e9c9f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestGrenade.cs b/AlienSurvival/Assets/Scripts/Test/TestGrenade.cs new file mode 100644 index 0000000..a1434d8 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestGrenade.cs @@ -0,0 +1,107 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestPseudoVec3 +{ + public float x; + public float y; + public float h; + public TestPseudoVec3(float x, float y, float h) + { + this.x = x; + this.y = y; + this.h = h; + } +} + +public class TestGrenade : MonoBehaviour +{ + public bool useGravity; + + public float gravity = -9.8f; + + public TestPseudoVec3 velocity; + public TestPseudoVec3 pseudoPos; + + public Transform shadow; + + private bool m_IsGround + { + get + { + return pseudoPos.h <= 0; + } + } + + // Start is called before the first frame update + void Start() + { + //Invoke("Dead", 3); + } + + void Dead() + { + if (this.gameObject) + { + GameObject.Destroy(this.gameObject); + } + } + + // Update is called once per frame + void Update() + { + if(useGravity && !m_IsGround) + { + velocity.h += gravity * Time.deltaTime; + } + + pseudoPos.x += velocity.x * Time.deltaTime; + pseudoPos.y += velocity.y * Time.deltaTime; + pseudoPos.h += velocity.h * Time.deltaTime; + + if(m_IsGround) + { + velocity.h = -velocity.h; + pseudoPos.h = 0; + } + + Vector3 position = transform.position; + + position.x = pseudoPos.x; + position.y = pseudoPos.y + pseudoPos.h; + + transform.position = position; + + if (shadow) + { + // shadow position + shadow.rotation = Quaternion.identity; + Vector3 shadowPos = Vector3.zero; + shadowPos.x = pseudoPos.x; + shadowPos.y = pseudoPos.y; + shadowPos.z = 0; + shadow.position = shadowPos; + } + } + + private void OnTriggerEnter2D(Collider2D collision) + { + //GameObject go = collision.gameObject; + + //if (!go.CompareTag("enemy")) + //{ + // return; + //} + + //GameObject.Destroy(collision.gameObject); + + ////this.gameObject.SetActive(false); + + //if (this.gameObject) + //{ + // GameObject.Destroy(this.gameObject); + //} + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestGrenade.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestGrenade.cs.meta new file mode 100644 index 0000000..f5d87e9 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestGrenade.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 76da92161ea22274996c5bc1d0bdea94 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestLauncher.cs b/AlienSurvival/Assets/Scripts/Test/TestLauncher.cs new file mode 100644 index 0000000..f053854 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestLauncher.cs @@ -0,0 +1,67 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestLauncher : MonoBehaviour +{ + public TestGrenade bullet; + + public Transform shadow; + + public Transform start; + + public float speed = 1; + + SpriteRenderer sprite; + + // Start is called before the first frame update + void Start() + { + sprite = GetComponent<SpriteRenderer>(); + } + + // Update is called once per frame + void Update() + { + + Vector3 screenPos = Input.mousePosition; + + Vector2 target = Camera.main.ScreenToWorldPoint(screenPos); + + Vector2 dir = Vector2.ClampMagnitude(target - (Vector2)transform.position, 1); + Vector3 move = dir * speed * Time.deltaTime; + + float x = move.x; + if (x > 0) + { + sprite.flipY = false; + } + else if (x < 0) + { + sprite.flipY = true; + } + + transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg * Mathf.Atan2(dir.y, dir.x)); + + Shot(); + } + + void Shot() + { + if (Input.GetButtonDown("Fire1")) + { + //Debug.Log("Shoot"); + + TestGrenade b = TestGrenade.Instantiate(bullet); + b.gameObject.SetActive(true); + + b.transform.position = start.position; + + b.pseudoPos = new TestPseudoVec3(start.position.x, start.position.y, 0.1f); + + float speed = 5; + b.velocity = new TestPseudoVec3(transform.right.x * speed, transform.right.y * speed, 3f); + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestLauncher.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestLauncher.cs.meta new file mode 100644 index 0000000..8688fc3 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestLauncher.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 45111da1fa00b2a4192afc01c5be0873 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestMathHelper.cs b/AlienSurvival/Assets/Scripts/Test/TestMathHelper.cs new file mode 100644 index 0000000..239d8ed --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestMathHelper.cs @@ -0,0 +1,43 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestMathHelper : MonoBehaviour +{ + + public static Vector2 Rotate(Vector2 v, float delta) + { + return new Vector2( + v.x * Mathf.Cos(delta) - v.y * Mathf.Sin(delta), + v.x * Mathf.Sin(delta) + v.y * Mathf.Cos(delta) + ); + } + + /// <summary> + /// 返回角度 + /// </summary> + /// <param name="vector2"></param> + /// <returns></returns> + public static float Angle(Vector2 vector2) + { + return 360 - (Mathf.Atan2(vector2.y, vector2.x) * Mathf.Rad2Deg * Mathf.Sign(vector2.y)); + } + + public static int Check(bool condition) + { + return condition ? 1 : 0; + } + + //public static float Angle(Vector2 vector2) + //{ + // if (vector2.x < 0) + // { + // return 360 - (Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg * -1); + // } + // else + // { + // return Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg; + // } + //} + +}
\ No newline at end of file diff --git a/AlienSurvival/Assets/Scripts/Test/TestMathHelper.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestMathHelper.cs.meta new file mode 100644 index 0000000..872d038 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestMathHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c40b8edb8d6570e419f024263e4cc43a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestMirror.cs b/AlienSurvival/Assets/Scripts/Test/TestMirror.cs new file mode 100644 index 0000000..832c785 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestMirror.cs @@ -0,0 +1,30 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestMirror : MonoBehaviour +{ + HashSet<GameObject> m_Mirrored = new HashSet<GameObject>(); + + private void OnTriggerEnter2D(Collider2D collision) + { + GameObject go = collision.gameObject; + if (go && go.CompareTag("bullet") && !m_Mirrored.Contains(go)) + { + //Debug.Log("mirror"); + + go.GetComponent<TestBeamBullet>().direction.x = -go.GetComponent<TestBeamBullet>().direction.x; + + m_Mirrored.Add(go); + } + } + + private void OnTriggerExit2D(Collider2D collision) + { + GameObject go = collision.gameObject; + if (go && go.CompareTag("bullet") && m_Mirrored.Contains(go)) + { + m_Mirrored.Remove(go); + } + } +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestMirror.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestMirror.cs.meta new file mode 100644 index 0000000..6fdba29 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestMirror.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bfd61ceb1fbec0944a66ace63dcd8e02 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestMoveToTarget.cs b/AlienSurvival/Assets/Scripts/Test/TestMoveToTarget.cs new file mode 100644 index 0000000..4b5f637 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestMoveToTarget.cs @@ -0,0 +1,45 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestMoveToTarget : MonoBehaviour +{ + public float speed = 1; + + Vector2 target; + + SpriteRenderer sprite; + + private void Start() + { + sprite = GetComponent<SpriteRenderer>(); + + } + + private void Update() + { + if (Input.GetButtonDown("Fire2")) + { + Vector3 screenPos = Input.mousePosition; + + target = Camera.main.ScreenToWorldPoint(screenPos); + //Debug.Log(target); + } + + Vector2 dir = Vector2.ClampMagnitude(target - (Vector2)transform.position, 1); + Vector3 move = dir * speed * Time.deltaTime; + transform.position = transform.position + move; + + float x = move.x; + if (x > 0) + { + sprite.flipX = false; + } + else if (x < 0) + { + sprite.flipX = true; + } + + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestMoveToTarget.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestMoveToTarget.cs.meta new file mode 100644 index 0000000..6f68458 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestMoveToTarget.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2283c114a7cfbd1429d3e2a0d5e6a957 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestPeaceMaker.cs b/AlienSurvival/Assets/Scripts/Test/TestPeaceMaker.cs new file mode 100644 index 0000000..11f76ba --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPeaceMaker.cs @@ -0,0 +1,314 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Events; + +public class TestPeaceMaker : MonoBehaviour +{ + + [SerializeField] private Transform m_Camera; + [SerializeField] private float m_Speed; + [SerializeField] private Transform m_Centre; + [SerializeField] private SpriteRenderer m_Arrow; + [SerializeField] private float m_ArrowRadius; + [SerializeField] private TestPeaceMakerBullet m_Bullet; + [SerializeField] private TestPeaceMakerGrenade m_Grenade; + [SerializeField] private Vector2 m_Zoom; + [SerializeField] private GameObject m_LaunchVfx; + [SerializeField] private Transform m_LaunchPoint; + [SerializeField] private TestB2 m_B2; + [SerializeField] private TestSpaceBeam m_SpaceBeam; + + private Vector3 zoomIn + { + get + { + Vector3 zin = new Vector3(m_Zoom.x, m_Zoom.x, 1); + return zin; + } + } + private Vector3 zoomOut + { + get + { + Vector3 zout = new Vector3(m_Zoom.y, m_Zoom.y, 1); + return zout; + } + } + + private TopDownShadowCaster m_Shadow; + + private SpriteRenderer m_SpriteRenderer; + private TopDownTransform m_Coord; + private Coroutine m_CoFire; + private Coroutine m_CoLaunchGrenade; + + private Vector3 m_PreMouse; + + private Vector2 m_AimDirection; + + private Vector3 m_TargetZoom; + + private enum ControlMode + { + Mouse, + Joystick, + } + private ControlMode m_ControlMode; + + void Start() + { + m_SpriteRenderer = GetComponent<SpriteRenderer>(); + m_Coord = GetComponent<TopDownTransform>(); + + m_ControlMode = ControlMode.Mouse; + m_AimDirection = Vector2.zero; + + m_TargetZoom = new Vector3(m_Zoom.x, m_Zoom.x, 1); + + m_Shadow = GetComponent<TopDownShadowCaster>(); + } + + void Update() + { + bool isMove = Move(); + + SetAim(); + + bool isFire = Fire(); + bool isGrenade = LaunchGrenade(); + bool isBeam = SpaceBeam(); + + CallB2(); + + CameraFollow(); + + CameraZoom(/*isMove ||*/ isFire || isGrenade /*|| isBeam*/); + } + + void CameraZoom(bool zout) + { + if(zout) + { + m_TargetZoom = zoomOut; + m_Camera.localScale = Vector3.Lerp(m_Camera.localScale, m_TargetZoom, 0.25f); + } + else + { + m_TargetZoom = zoomIn; + m_Camera.localScale = Vector3.Lerp(m_Camera.localScale, m_TargetZoom, /*Time.deltaTime * 2f*/0.25f); + } + } + + void CameraFollow() + { + Vector3 pos = m_Camera.position; + pos.x = m_Coord.x; + pos.y = m_Coord.y; + + if (Vector3.Distance(pos, m_Camera.position) > 0.1f) + { + m_Camera.position = Vector3.Lerp(m_Camera.position, pos, 3 * Time.deltaTime); + } + } + + bool Move() + { + float x = Input.GetAxisRaw("Horizontal"); + float y = Input.GetAxisRaw("Vertical"); + + bool isMove = x != 0 || y != 0; + + if (isMove) + { + Vector2 direction = new Vector2(x, y).normalized; + + Vector3 position = m_Coord.position; + position.x += direction.x * m_Speed * Time.deltaTime; + position.y += direction.y * m_Speed * Time.deltaTime; + + m_Coord.position = position; + + if (x > 0) + { + m_SpriteRenderer.flipX = false; + m_Shadow.Flip(false); + } + else if (x < 0) + { + m_SpriteRenderer.flipX = true; + m_Shadow.Flip(true); + } + } + + return isMove; + } + + void SetAim() + { + float xAxis = Input.GetAxisRaw("AimHorizontal"); + float yAxis = Input.GetAxisRaw("AimVertical"); + + bool bJoytick = xAxis != 0 || yAxis != 0; + + bool bMouse = (Input.mousePosition - m_PreMouse).magnitude > 0.01f; + m_PreMouse = Input.mousePosition; + + if(bJoytick) + { + m_ControlMode = ControlMode.Joystick; + } + if(bMouse) + { + m_ControlMode = ControlMode.Mouse; + } + + Vector2 centrePos = m_Centre.position; + Vector2 dir = Vector2.zero; + if (m_ControlMode == ControlMode.Mouse) + { + Vector2 camPos = GetCameraPosition(); + dir = (camPos - centrePos).normalized; + } + else + { + dir = new Vector2(xAxis, -yAxis).normalized; + } + + if(dir.magnitude > 0) + { + m_AimDirection = dir; + Vector2 arrowPos = centrePos + m_ArrowRadius * m_AimDirection; + m_Arrow.transform.position = new Vector3(arrowPos.x, arrowPos.y, m_Arrow.transform.position.z); + m_Arrow.transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(m_AimDirection.y, m_AimDirection.x) * Mathf.Rad2Deg); + } + } + + Vector2 GetCameraPosition() + { + Vector3 screenPos = Input.mousePosition; + Vector2 target = Camera.main.ScreenToWorldPoint(screenPos); + return target; + } + + bool Fire() + { + if (Input.GetButtonDown("Fire1") || (Input.GetAxis("GunTrigger") == 1)) + { + if (m_CoFire == null) + { + m_CoFire = StartCoroutine(coFire(0.1f)); + } + } + if (!Input.GetButton("Fire1") && (Input.GetAxis("GunTrigger") == 0)) + { + if(m_CoFire != null) + { + StopCoroutine(m_CoFire); + m_CoFire = null; + } + return false; + } + return true; + } + + bool LaunchGrenade() + { + if (Input.GetButtonDown("Fire2") || (Input.GetAxis("BombTrigger") == 1)) + { + if (m_CoLaunchGrenade == null) + { + m_CoLaunchGrenade = StartCoroutine(coLaunchGrenade(0.2f)); + } + } + if (!Input.GetButton("Fire2") && (Input.GetAxis("BombTrigger") == 0)) + { + if (m_CoLaunchGrenade != null) + { + StopCoroutine(m_CoLaunchGrenade); + m_CoLaunchGrenade = null; + } + return false; + } + return true; + } + + IEnumerator coFire(float interval) + { + while (true) + { + CreateBullet(m_AimDirection, 12f, 2f); + //CreateBullet(TestMathHelper.Rotate(m_AimDirection, 10 * Mathf.Deg2Rad), 12f, 2f); + //CreateBullet(TestMathHelper.Rotate(m_AimDirection, -10 * Mathf.Deg2Rad), 12f, 2f); + + yield return new WaitForSeconds(interval); + } + } + + TestPeaceMakerBullet CreateBullet(Vector2 dir, float speed, float lifeTime) + { + TestPeaceMakerBullet bullet = Instantiate<TestPeaceMakerBullet>(m_Bullet); + + bullet.transform.position = m_Arrow.transform.position + new Vector3(m_AimDirection.x, m_AimDirection.y, 0) * -0.3f; + + bullet.Set(dir, speed, lifeTime); + bullet.gameObject.SetActive(true); + + return bullet; + } + + + Vector3 GetRandomLaunchGrenadeDirection() + { + Vector3 dirOnPlane = Quaternion.Euler(0, 0, Random.Range(-15f, 15f)) * new Vector3(m_AimDirection.x, m_AimDirection.y, 0); + dirOnPlane = dirOnPlane.normalized; + Vector3 dir = new Vector3(dirOnPlane.x, dirOnPlane.y, Random.Range(0f, 1f)).normalized; + return dir.normalized; + } + + IEnumerator coLaunchGrenade(float interval) + { + while (true) + { + // grenade + TestPeaceMakerGrenade grenade = Instantiate<TestPeaceMakerGrenade>(m_Grenade); + Vector3 position = m_Coord.position + new Vector3(0, 0, 1.8f); + grenade.Set(position, GetRandomLaunchGrenadeDirection(), Random.Range(8f, 10f)); + grenade.gameObject.SetActive(true); + + // vfx + GameObject eff = Instantiate<GameObject>(m_LaunchVfx, this.transform); + eff.transform.position = m_LaunchPoint.transform.position; + eff.SetActive(true); + + yield return new WaitForSeconds(interval); + } + } + + void CallB2() + { + if(Input.GetButtonDown("Fire3")) + { + TestB2 b2 = Instantiate<TestB2>(m_B2); + + Vector3 pos3D = m_Coord.GetProjectedPosition(); + b2.Set(pos3D + new Vector3(-15, 0,0 ), pos3D + new Vector3(15, 0,0 ), 20f, 3f); + } + } + + bool SpaceBeam() + { + if (Input.GetButtonDown("SpaceBeam")) + { + TestSpaceBeam beam = Instantiate<TestSpaceBeam>(m_SpaceBeam); + + Vector3 pos3D = m_Coord.position; + beam.Set(pos3D + new Vector3(3, 0, 0)); + + TinyCountDown.Instance.Set("SpaceBeam", 0.1f); + return true; + } + return TinyCountDown.Instance.Get("SpaceBeam") > 0; + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestPeaceMaker.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestPeaceMaker.cs.meta new file mode 100644 index 0000000..e8f0316 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPeaceMaker.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e1b4df1c072214a4f947497f833281d9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerBullet.cs b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerBullet.cs new file mode 100644 index 0000000..8229753 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerBullet.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestPeaceMakerBullet : MonoBehaviour +{ + + private Vector2 m_Direction; + + private float m_Speed; + + private float m_LifeTime; + + public void Set(Vector2 dir, float speed, float lifeTime) + { + m_Direction = dir; + m_Speed = speed; + m_LifeTime = lifeTime; + transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg); + } + + void Update() + { + m_LifeTime -= Time.deltaTime; + if(m_LifeTime <= 0) + { + Destroy(this.gameObject); + return; + } + + Vector2 move = m_Direction * m_Speed * Time.deltaTime; + + Vector3 pos = transform.position; + pos.x += move.x; + pos.y += move.y; + + transform.position = pos; + } + + private void OnTriggerEnter2D(Collider2D collision) + { + TestBucket bucket = collision.gameObject.GetComponent<TestBucket>(); + + if (bucket) + { + bucket.Broken(); + } + + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerBullet.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerBullet.cs.meta new file mode 100644 index 0000000..c3bce5a --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerBullet.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cf74c7651403bc449b55858cefd8debb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerGrenade.cs b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerGrenade.cs new file mode 100644 index 0000000..021eda7 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerGrenade.cs @@ -0,0 +1,67 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestPeaceMakerGrenade : MonoBehaviour +{ + + [SerializeField] private float m_GravityScale = 1f; + + [SerializeField] private GameObject m_ExplosionEffect; + + private TopDownTransform m_Coords; + + private Vector3 GRAVITY = new Vector3(0, 0, -9.8f); + + private Vector3 m_Velocity; // x, y, fakeHeight + + /// <summary> + /// 设置初始参数,都在fake空间下 + /// </summary> + /// <param name="initPosition"></param> + /// <param name="initDirection"></param> + /// <param name="initSpeed"></param> + public void Set(Vector3 initPosition, Vector3 initDirection, float initSpeed) + { + m_Coords = GetComponent<TopDownTransform>(); + m_Coords.position = initPosition; + + m_Velocity = initDirection * initSpeed; + } + + private void Update() + { + Vector3 move = m_Velocity * Time.deltaTime; + + if(m_Velocity.magnitude > 0 && m_Coords.z + move.z >= 0) + { + m_Coords.x += move.x; + m_Coords.y += move.y; + m_Coords.z += move.z; + m_Velocity += GRAVITY * Time.deltaTime; + + transform.rotation *= Quaternion.Euler(0, 0, 100 * m_Velocity.magnitude * Time.deltaTime); + } + else + { + m_Velocity = Vector3.zero; + + this.gameObject.SetActive(false); + Destroy(this.gameObject); + PlayExplosion(); + } + } + + private void PlayExplosion() + { + GameObject exp = Instantiate<GameObject>(m_ExplosionEffect); + + TopDownTransform coord = exp.GetComponent<TopDownTransform>(); + coord.position = m_Coords.position; + + exp.GetComponent<TopDownSorting>().Sorting(); + + exp.SetActive(true); + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerGrenade.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerGrenade.cs.meta new file mode 100644 index 0000000..7b95df6 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPeaceMakerGrenade.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 40fc919f136c67e41a3608549bab3217 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestPhysicsHelper.cs b/AlienSurvival/Assets/Scripts/Test/TestPhysicsHelper.cs new file mode 100644 index 0000000..a1606f2 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPhysicsHelper.cs @@ -0,0 +1,13 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestPhysicsHelper : TestSingleton<TestPhysicsHelper> +{ + public TestPhysicsHelper() + { + } + + + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestPhysicsHelper.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestPhysicsHelper.cs.meta new file mode 100644 index 0000000..47ba3fc --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestPhysicsHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8031513d870e5e54e942664031efe406 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestRobot1.cs b/AlienSurvival/Assets/Scripts/Test/TestRobot1.cs new file mode 100644 index 0000000..367a661 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestRobot1.cs @@ -0,0 +1,33 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestRobot1 : MonoBehaviour +{ + public Transform shadow; + + public float x = 0; + public float y = -0.628f; + public float z; // fake height + + public AnimationCurve curve; + public AnimationCurve curveX; + public AnimationCurve curveY; + + private void Update() + { + x = curveX.Evaluate(Time.time % 1f); + y = curveY.Evaluate(Time.time % 1f); + z = curve.Evaluate(Time.time % 1f); + + Vector3 pos = transform.position; + pos.x = x; + pos.y = y + z; + + transform.position = pos; + + pos.y = y; + shadow.transform.position = pos; + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestRobot1.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestRobot1.cs.meta new file mode 100644 index 0000000..554affe --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestRobot1.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 04de5fad9d56a774498cac6d58b2f231 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestSceneViewShaderHelper.cs b/AlienSurvival/Assets/Scripts/Test/TestSceneViewShaderHelper.cs new file mode 100644 index 0000000..fca1bff --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSceneViewShaderHelper.cs @@ -0,0 +1,46 @@ +using System; +using UnityEngine; + +[ExecuteInEditMode] +public class TestSceneViewShaderHelper : MonoBehaviour +{ +#if UNITY_EDITOR + private int _isSceneViewID = Shader.PropertyToID("_IsSceneView"); + + public void OnEnable() + { + Camera.onPreRender += SetIfSceneViewCamera; + } + + public void OnDisable() + { + Camera.onPreRender -= SetIfSceneViewCamera; + } + + public void SetIfSceneViewCamera(Camera cam) + { + // Scene View camera is named "SceneCamera" + if (cam.gameObject.name == "SceneCamera") + { + Shader.EnableKeyword("SCENE_VIEW"); + Shader.SetGlobalFloat(_isSceneViewID, 1f); + } + // Inspector preview for materials, models, and prefabs is named "Preview Scene Camera" + // else if (cam.gameObject.name == "Preview Scene Camera") + // { + // Shader.EnableKeyword("SCENE_VIEW"); + // Shader.SetGlobalFloat(_isSceneViewID, 2f); + // } + // Otherwise this is a game view or other user camera + else + { + Shader.DisableKeyword("SCENE_VIEW"); + Shader.SetGlobalFloat(_isSceneViewID, 0f); + } + + // You can double check the camera names if something breaks in the future + // Debug.Log(cam); + } +#endif +} +
\ No newline at end of file diff --git a/AlienSurvival/Assets/Scripts/Test/TestSceneViewShaderHelper.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestSceneViewShaderHelper.cs.meta new file mode 100644 index 0000000..4b846fc --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSceneViewShaderHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8b08476505e1d804994534220f308b69 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestSeperator.cs b/AlienSurvival/Assets/Scripts/Test/TestSeperator.cs new file mode 100644 index 0000000..d1819c0 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSeperator.cs @@ -0,0 +1,41 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestSeperator : MonoBehaviour +{ + HashSet<GameObject> m_Seperated = new HashSet<GameObject>(); + + private void OnTriggerEnter2D(Collider2D collision) + { + GameObject go = collision.gameObject; + if (go && go.CompareTag("bullet") && !m_Seperated.Contains(go)) + { + //Debug.Log("seperate"); + + TestBeamBullet bullet1 = TestBeamBullet.Instantiate(go.GetComponent<TestBeamBullet>()); + bullet1.direction = Quaternion.Euler(0,0, 30) * bullet1.direction; + bullet1.enabled = true; + bullet1.gameObject.SetActive(true); + + TestBeamBullet bullet2 = TestBeamBullet.Instantiate(go.GetComponent<TestBeamBullet>()); + bullet2.direction = Quaternion.Euler(0, 0, -30) * bullet2.direction; + bullet2.enabled = true; + bullet2.gameObject.SetActive(true); + + m_Seperated.Add(go); + m_Seperated.Add(bullet1.gameObject); + m_Seperated.Add(bullet2.gameObject); + } + } + + private void OnTriggerExit2D(Collider2D collision) + { + GameObject go = collision.gameObject; + if (go.CompareTag("bullet") && m_Seperated.Contains(go)) + { + m_Seperated.Remove(go); + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestSeperator.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestSeperator.cs.meta new file mode 100644 index 0000000..58f5ccb --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSeperator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ec780be79a9ec774b80c4ba5593c3a39 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestSingleton.cs b/AlienSurvival/Assets/Scripts/Test/TestSingleton.cs new file mode 100644 index 0000000..d522653 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSingleton.cs @@ -0,0 +1,20 @@ +public class TestSingleton<T> where T : class, new() +{ + private static T _instance; + private static readonly object syslock = new object(); + + public static T getInstance() + { + if (_instance == null) + { + lock (syslock) + { + if (_instance == null) + { + _instance = new T(); + } + } + } + return _instance; + } +}
\ No newline at end of file diff --git a/AlienSurvival/Assets/Scripts/Test/TestSingleton.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestSingleton.cs.meta new file mode 100644 index 0000000..8035c63 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSingleton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0e4486fb2a577bb4fa367c939cd96141 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestSorting.cs b/AlienSurvival/Assets/Scripts/Test/TestSorting.cs new file mode 100644 index 0000000..7a2cf54 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSorting.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestSorting : MonoBehaviour +{ + //private void Update() + //{ + // Vector3 pos = transform.position; + // pos.z = pos.y - 5; + + // if(transform.position.z != pos.z) + // { + // transform.position = pos; + // } + //} + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestSorting.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestSorting.cs.meta new file mode 100644 index 0000000..56c5ed0 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSorting.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c6f36e1896ea64642b621e21a00a6488 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestSortingByDepth.cs b/AlienSurvival/Assets/Scripts/Test/TestSortingByDepth.cs new file mode 100644 index 0000000..6e346ec --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSortingByDepth.cs @@ -0,0 +1,9 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestSortingByDepth : MonoBehaviour +{ + + +}
\ No newline at end of file diff --git a/AlienSurvival/Assets/Scripts/Test/TestSortingByDepth.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestSortingByDepth.cs.meta new file mode 100644 index 0000000..934c164 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSortingByDepth.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 540605b09e537394f84b35d5bd77a505 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestSortingByY.cs b/AlienSurvival/Assets/Scripts/Test/TestSortingByY.cs new file mode 100644 index 0000000..59f44b7 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSortingByY.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestSortingByY : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestSortingByY.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestSortingByY.cs.meta new file mode 100644 index 0000000..5074c7f --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSortingByY.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9a346df7f7a231e46bfeb3dbc1a63116 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestSpaceBeam.cs b/AlienSurvival/Assets/Scripts/Test/TestSpaceBeam.cs new file mode 100644 index 0000000..b8ce07b --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSpaceBeam.cs @@ -0,0 +1,70 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// <summary> +/// 太空射线 +/// </summary> +public class TestSpaceBeam : MonoBehaviour +{ + [SerializeField] private GameObject m_Beam; + + TopDownTransform m_Coord; + + List<GameObject> m_Beams = new List<GameObject>(); + + float m_RotateSpeed = 5f; + + float m_Radius = 1f; + + int count = 8; + + float m_CurrentAngle = 0; + + private void Awake() + { + m_Coord = GetComponent<TopDownTransform>(); + } + + public void Set(Vector3 posTDS) + { + m_Coord.position = posTDS; + + Vector3 groundPos = m_Coord.positionOnGround; + + for(int i = 0; i < count; ++i) + { + float angle = Mathf.PI * 2f / (float)count * i; + Vector3 pos = /*groundPos +*/ new Vector3(m_Radius * Mathf.Cos(angle), m_Radius * Mathf.Sin(angle), 0f); + GameObject beam = Instantiate<GameObject>(m_Beam, this.transform); + beam.GetComponent<TopDownTransform>().localPosition = pos; + beam.SetActive(true); + m_Beams.Add(beam); + } + } + + void Update() + { + m_CurrentAngle += m_RotateSpeed * Time.deltaTime; + + RotateBeams(m_CurrentAngle); + } + + /// <summary> + /// 旋转 + /// </summary> + /// <param name="angle">弧度</param> + void RotateBeams(float angle) + { + Vector3 groundPos = m_Coord.positionOnGround; + + for (int i = 0; i < m_Beams.Count; ++i) + { + float rad = angle + Mathf.PI * 2f / (float)count * i; + Vector3 pos = /*groundPos + */new Vector3(m_Radius * Mathf.Cos(rad), m_Radius * Mathf.Sin(rad), 0f); + m_Beams[i].GetComponent<TopDownTransform>().localPosition = pos; + } + + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestSpaceBeam.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestSpaceBeam.cs.meta new file mode 100644 index 0000000..3e008d2 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestSpaceBeam.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 345857a5337aab0458bb084b716eccdc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestTopDown2DTransform.cs b/AlienSurvival/Assets/Scripts/Test/TestTopDown2DTransform.cs new file mode 100644 index 0000000..acfac68 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestTopDown2DTransform.cs @@ -0,0 +1,78 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class TestTopDown2DTransform : MonoBehaviour +{ + public bool useGravity = false; + + public Vector3 position // x, y, h + { + get + { + Vector3 topdownPos = transform.position; + topdownPos.y -= h; + topdownPos.z = h; + return topdownPos; + } + set + { + h = value.z; + Vector3 realPos = transform.position; + realPos.y = value.y + h; + transform.position = realPos; + } + } + + public float x + { + get + { + return position.x; + } + } + + public float y + { + get + { + return position.y; + } + } + + public float h = 0; + + public float z + { + get + { + return transform.position.z; + } + } + + public float depth + { + get + { + return this.z; + } + } + + private float vy = 0; + + private void Update() + { + if (useGravity) + { + Vector3 pos = position; + vy += -9.8f * Time.deltaTime; + pos.z = Mathf.Max(pos.z + vy * Time.deltaTime, 0f); + if(pos.z == 0) + { + vy = 0; + } + position = pos; + } + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestTopDown2DTransform.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestTopDown2DTransform.cs.meta new file mode 100644 index 0000000..b8279be --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestTopDown2DTransform.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 65a986ca41323a44c910b7d7660abf9c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AlienSurvival/Assets/Scripts/Test/TestWaspRobot.cs b/AlienSurvival/Assets/Scripts/Test/TestWaspRobot.cs new file mode 100644 index 0000000..3a73d21 --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestWaspRobot.cs @@ -0,0 +1,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]; + } + +} diff --git a/AlienSurvival/Assets/Scripts/Test/TestWaspRobot.cs.meta b/AlienSurvival/Assets/Scripts/Test/TestWaspRobot.cs.meta new file mode 100644 index 0000000..9bd17eb --- /dev/null +++ b/AlienSurvival/Assets/Scripts/Test/TestWaspRobot.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 37e797e79063cdf4883cf54509d9ddc3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |