diff options
20 files changed, 417 insertions, 61 deletions
diff --git a/marching/Assets/Bundle/prefabs/projectiles/blade.prefab b/marching/Assets/Bundle/prefabs/projectiles/blade.prefab index 1718d2e..113636e 100644 --- a/marching/Assets/Bundle/prefabs/projectiles/blade.prefab +++ b/marching/Assets/Bundle/prefabs/projectiles/blade.prefab @@ -11,7 +11,7 @@ GameObject: - component: {fileID: 2217681208478449289} - component: {fileID: 2217681208478449288} - component: {fileID: 2722328784031211685} - - component: {fileID: 8127628266338154912} + - component: {fileID: 1607546511} m_Layer: 7 m_Name: blade m_TagString: Untagged @@ -98,19 +98,18 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a4c0b0adacc6e3148aab9ce0f1c0cca1, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!58 &8127628266338154912 -CircleCollider2D: +--- !u!114 &1607546511 +MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2217681208478449291} m_Enabled: 1 - m_Density: 1 - m_Material: {fileID: 0} - m_IsTrigger: 1 - m_UsedByEffector: 0 - m_UsedByComposite: 0 - m_Offset: {x: 0, y: -0.02} - serializedVersion: 2 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: defd292dd15a961418d8ac5721b28712, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Type: 2 m_Radius: 1 + m_Offset: {x: -0.06, y: -0.06} diff --git a/marching/Assets/Scenes/SampleScene (1).unity b/marching/Assets/Scenes/SampleScene (1).unity index ec0e69c..5e675fe 100644 --- a/marching/Assets/Scenes/SampleScene (1).unity +++ b/marching/Assets/Scenes/SampleScene (1).unity @@ -13126,10 +13126,6 @@ PrefabInstance: propertyPath: m_Name value: blade objectReference: {fileID: 0} - - target: {fileID: 2217681208478449291, guid: 64b392d0acb4cb44c9907207b451bb4c, type: 3} - propertyPath: m_IsActive - value: 0 - objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 64b392d0acb4cb44c9907207b451bb4c, type: 3} --- !u!1001 &8393854536380925844 diff --git a/marching/Assets/Scripts/Physics/FastBoxCollider.cs b/marching/Assets/Scripts/Physics/FastBoxCollider.cs index 7b52caf..03fbb24 100644 --- a/marching/Assets/Scripts/Physics/FastBoxCollider.cs +++ b/marching/Assets/Scripts/Physics/FastBoxCollider.cs @@ -36,6 +36,20 @@ public class FastBoxCollider : MonoBehaviour, IQuadTreeObject } } + public Vector4 box + { + get + { + Vector2 c = center; + Vector4 b = new Vector4(); + b.x = c.x; + b.y = c.y; + b.z = size.x; + b.w = size.y; + return b; + } + } + public void Awake() { if (m_Type == ColliderType.Collider) diff --git a/marching/Assets/Scripts/Physics/FastCircleCollider.cs b/marching/Assets/Scripts/Physics/FastCircleCollider.cs index 5884a69..dde49f9 100644 --- a/marching/Assets/Scripts/Physics/FastCircleCollider.cs +++ b/marching/Assets/Scripts/Physics/FastCircleCollider.cs @@ -35,6 +35,19 @@ public class FastCircleCollider : MonoBehaviour, IQuadTreeObject } } + public Vector3 circle + { + get + { + Vector3 c = new Vector3(); + Vector2 ct = center; + c.x = ct.x; + c.y = ct.y; + c.z = radius; + return c; + } + } + public Vector2 offset => m_Offset; public void Awake() diff --git a/marching/Assets/Scripts/Physics/PhysicsManager.cs b/marching/Assets/Scripts/Physics/PhysicsManager.cs index 6daf610..c4e6017 100644 --- a/marching/Assets/Scripts/Physics/PhysicsManager.cs +++ b/marching/Assets/Scripts/Physics/PhysicsManager.cs @@ -11,8 +11,12 @@ public enum ColliderType Hitbox, } -public class PhysicsManager : Singleton<PhysicsManager> +/// <summary> +/// 四叉树空间划分,优化碰撞检测 +/// </summary> +public partial class PhysicsManager : Singleton<PhysicsManager> { + #region Quadtrees public Vector4 quadtreeCollisionRange { set { m_QuadtreeCollisionRange = value; } } private Vector4 m_QuadtreeCollisionRange; private Quadtree m_QuadtreeCollision; @@ -22,6 +26,9 @@ public class PhysicsManager : Singleton<PhysicsManager> private Vector4 m_QuadtreeHurtboxRange; private Quadtree m_QuadtreeHurtboxes; private List<IQuadTreeObject> m_QuadtreeObjHurtboxes = new List<IQuadTreeObject>(); + #endregion + public List<IQuadTreeObject> sharedRetriveResults => m_SharedRetriveResults; + private List<IQuadTreeObject> m_SharedRetriveResults = new List<IQuadTreeObject>(); public PhysicsManager() { @@ -99,9 +106,31 @@ public class PhysicsManager : Singleton<PhysicsManager> return m_QuadtreeCollision.Retrieve(ref returnObjs, bound); } - public static bool CircleVsCircle(Vector2 pos1, float r1, Vector2 pos2, float r2) + public bool RetriveHurtboxes(ref List<IQuadTreeObject> returnObjs, Vector4 bound) { - return (pos1 - pos2).magnitude <= r1+r2; + return m_QuadtreeHurtboxes.Retrieve(ref returnObjs, bound); + } + + public bool RetriveColliders(Vector4 bound) + { + m_SharedRetriveResults.Clear(); + return m_QuadtreeCollision.Retrieve(ref m_SharedRetriveResults, bound); + } + + public bool RetriveHurtboxes(Vector4 bound) + { + m_SharedRetriveResults.Clear(); + return m_QuadtreeHurtboxes.Retrieve(ref m_SharedRetriveResults, bound); + } + + public System.Func<Vector4, bool> GetRetriverByType(ColliderType type) + { + if (type == ColliderType.Collider) + return RetriveColliders; + else if (type == ColliderType.Hurtbox) + return RetriveHurtboxes; + else + return null; } }
\ No newline at end of file diff --git a/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs b/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs new file mode 100644 index 0000000..48af516 --- /dev/null +++ b/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs @@ -0,0 +1,71 @@ +using mh; +using System.Collections; +using System.Collections.Generic; +using Unity.VisualScripting.Antlr3.Runtime.Tree; +using UnityEngine; + +public partial class PhysicsManager : Singleton<PhysicsManager> +{ + private List<IQuadTreeObject> m_SharedCollideResults = new List<IQuadTreeObject>(); + + public Vector4 GetCircleBound(Vector3 circle) + { + float size = circle.z * 2; + return new Vector4(circle.x, circle.y, size, size); + } + + public Vector4 GetBoxBound(Vector4 box) + { + return box; + } + + public ref readonly List<IQuadTreeObject> CircleCast(ColliderType target, Vector3 circle) + { + m_SharedCollideResults.Clear(); + var retriver = GetRetriverByType(target); + if(retriver != null) + { + if (retriver(GetCircleBound(circle))) + { + for(int i = 0; i < m_SharedRetriveResults.Count; ++i) + { + var collider = m_SharedRetriveResults[i]; + if(collider != null) + { + if(collider is FastCircleCollider) + { + if(CircleVsCircle((collider as FastCircleCollider).circle, circle)) + { + m_SharedCollideResults.Add(collider); + } + } + else if(collider is FastBoxCollider) + { + if (BoxVsCircle((collider as FastBoxCollider).box, circle)) + { + m_SharedCollideResults.Add(collider); + } + } + } + } + } + } + return ref m_SharedCollideResults; + } + + public ref readonly List<IQuadTreeObject> BoxCast(ColliderType target, Vector4 box) + { + return ref m_SharedCollideResults; + } + + public ref readonly List<IQuadTreeObject> LineCast(ColliderType target, Vector4 line) + { + return ref m_SharedCollideResults; + } + + public ref readonly List<IQuadTreeObject> PointCast(ColliderType target, Vector2 point) + { + return ref m_SharedCollideResults; + } + +}
\ No newline at end of file diff --git a/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs.meta b/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs.meta new file mode 100644 index 0000000..d5f9202 --- /dev/null +++ b/marching/Assets/Scripts/Physics/PhysicsManager_Collide.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2d0c6870f58c8c6469810ba2ca63cbc7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs b/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs new file mode 100644 index 0000000..81625ac --- /dev/null +++ b/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs @@ -0,0 +1,80 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public partial class PhysicsManager : Singleton<PhysicsManager> +{ + + /// <summary> + /// circle x,y,radius + /// </summary> + /// <param name="pos1"></param> + /// <param name="r1"></param> + /// <param name="pos2"></param> + /// <param name="r2"></param> + /// <returns></returns> + public static bool CircleVsCircle(Vector2 pos1, float r1, Vector2 pos2, float r2) + { + return (pos1 - pos2).magnitude <= r1 + r2; + } + + public static bool CircleVsCircle(Vector3 c1, Vector3 c2) + { + return (c1.xy() - c2.xy()).magnitude <= c1.z + c2.z; + } + + /// <summary> + /// intersection是r2对于r1 + /// </summary> + /// <param name="b1"></param> + /// <param name="b2"></param> + /// <param name="intersection"></param> + /// <returns></returns> + public static bool BoxVsBox(Vector4 b1, Vector4 b2, out Vector2 intersection) + { + float b1w = b1.z / 2f, b1h = b1.w / 2f, b2w = b2.z / 2f, b2h = b2.w / 2f; + float distX = b2.x - b1.x; + float distY = b2.y - b1.y; + if(Mathf.Abs(distX) < b1w + b2w && Mathf.Abs(distY) < b1h +b2h) + { + intersection = new Vector2(); + intersection.x = Mathf.Sign(distX) * (b1w + b2w - Mathf.Abs(distX)); + intersection.y = Mathf.Sign(distY) * (b1h + b2h - Mathf.Abs(distY)); + return true; + } + intersection = Vector2.zero; + return false; + } + + public static bool BoxVsCircle(Vector4 box, Vector2 pos, float radius) + { + Vector4 boxScaled = box; + boxScaled.z = box.z + radius * 2; + boxScaled.w = box.w + radius * 2; + if (!IsPointInsideBox(boxScaled, pos)) + return false; + Vector2 v = MathUtils.Abs(pos - box.xy()); + Vector2 u = MathUtils.Max(v - box.zw(), 0); + return Vector2.Dot(u, u) < radius * radius; + } + + /// <summary> + /// box x,y,w,h circle x,y,raduis + /// </summary> + /// <param name="box"></param> + /// <param name="circle"></param> + /// <returns></returns> + public static bool BoxVsCircle(Vector4 box, Vector3 circle) + { + return BoxVsCircle(box, circle.xy(), circle.z); + } + + public static bool IsPointInsideBox(Vector4 box, Vector2 point) + { + return point.x >= box.x - box.z / 2f + && point.x <= box.x + box.z / 2f + && point.y >= box.y - box.w / 2f + && point.y <= box.y + box.w / 2f; + } + +} diff --git a/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs.meta b/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs.meta new file mode 100644 index 0000000..7be9116 --- /dev/null +++ b/marching/Assets/Scripts/Physics/PhysicsManager_CollisionDetection.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2577861427359c9459ea0fa471de4040 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/marching/Assets/Scripts/Projectiles/BladeScript.cs b/marching/Assets/Scripts/Projectiles/BladeScript.cs index 90cee79..929f574 100644 --- a/marching/Assets/Scripts/Projectiles/BladeScript.cs +++ b/marching/Assets/Scripts/Projectiles/BladeScript.cs @@ -11,6 +11,8 @@ public class BladeScript : ProjectileBase private static List<IQuadTreeObject> collisions = new List<IQuadTreeObject>(); + private FastCircleCollider collider; + private SpriteRenderer sr { get @@ -21,7 +23,9 @@ public class BladeScript : ProjectileBase public void Start() { + collider = GetComponent<FastCircleCollider>(); Invoke("DestroySelf", life); + Attack(); } void DestroySelf() @@ -41,38 +45,34 @@ public class BladeScript : ProjectileBase sr.flipX = flip; } - public void FixedUpdate() + public void Attack() { - Vector3 pos = transform.position; - if(PhysicsManager.Instance.RetriveColliders(ref collisions, new Vector4(pos.x, pos.y, 4, 4))) + Vector3 pos = transform.position; + if (PhysicsManager.Instance.RetriveHurtboxes(ref collisions, new Vector4(pos.x, pos.y, 2, 2))) { - for(int i =0; i < collisions.Count; i++) + for (int i = 0; i < collisions.Count; i++) { - + var col = collisions[i] as MonoBehaviour; + if (col != null) + { + if (col is FastBoxCollider) + { + var box = col as FastBoxCollider; + if (box != null) + { + if (PhysicsManager.BoxVsCircle(box.box, collider.circle)) + { + GameObject.Destroy(box.gameObject); + } + } + } + } } } } - private void OnTriggerEnter2D(Collider2D other) + public void FixedUpdate() { - Debug.Log(other.gameObject.name); - LayerMask layerMask = LayerMask.GetMask("Hurtbox"); - if ((layerMask & (1 << other.transform.gameObject.layer)) != 0) - { - //Debug.Log(other.GetType().Name); - Rigidbody2D rig = other.transform.parent.GetComponent<Rigidbody2D>(); - if(rig != null) - { - Debug.Log("force"); - UnitBase hero = UnitManager.hero; - Vector2 pos = transform.position; - Vector2 heroPos = hero.transform.position; - Vector2 dir = (heroPos - pos).normalized; - - rig.AddForce(-dir * 100); - - rig.GetComponent<SpiritScript>().Die(); - } - } } + } diff --git a/marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs b/marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs index 262c821..8de5aff 100644 --- a/marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs +++ b/marching/Assets/Scripts/Unit/Characters/Samurai/SamuraiScript.cs @@ -1,7 +1,9 @@ +using JetBrains.Annotations; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; +using UnityEngine.UIElements; namespace MH { @@ -39,6 +41,7 @@ namespace MH m_Sprite = GetComponent<SpriteRenderer>(); m_TimeSinceLastMove = float.MaxValue; StartCoroutine(CoAttack(1f)); + StartCoroutine(CoStrike(3f)); } protected override void Update() @@ -160,6 +163,37 @@ namespace MH } } + IEnumerator CoStrike(float interval) + { + int fac = 1; + while (true) + { + if (!m_Attacking) + { + yield return null; + } + else + { + Strike(); + yield return new WaitForSeconds(interval); + } + } + } + + private void Strike() + { + float radius = 2; + Vector3 pos = transform.position; + 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) + { + GameObject.Destroy((colliders[i] as MonoBehaviour).gameObject); + } + } + } + } } diff --git a/marching/Assets/Scripts/Unit/Enemies/SpiritScript.cs b/marching/Assets/Scripts/Unit/Enemies/SpiritScript.cs index cabed35..7ebae9c 100644 --- a/marching/Assets/Scripts/Unit/Enemies/SpiritScript.cs +++ b/marching/Assets/Scripts/Unit/Enemies/SpiritScript.cs @@ -96,6 +96,11 @@ public class SpiritScript : UnitBase { } + public void OnDestroy() + { + TestSpirits.spirits.Remove(this); + } + public void Die() { Item_Coin coin = Instantiate(coinPrefab) as Item_Coin; diff --git a/marching/Assets/Scripts/Utils/MathUtils.cs b/marching/Assets/Scripts/Utils/MathUtils.cs new file mode 100644 index 0000000..a25f5e5 --- /dev/null +++ b/marching/Assets/Scripts/Utils/MathUtils.cs @@ -0,0 +1,19 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public static class MathUtils +{ + + public static Vector2 Abs(Vector2 v) + { + return new Vector2(Mathf.Abs(v.x), Mathf.Abs(v.y)); + } + + public static Vector2 Max(Vector2 src, float v) + { + return new Vector2(Mathf.Max(src.x, v), Mathf.Max(src.y, v)); + } + + +}
\ No newline at end of file diff --git a/marching/Assets/Scripts/Utils/MathUtils.cs.meta b/marching/Assets/Scripts/Utils/MathUtils.cs.meta new file mode 100644 index 0000000..8e8b33c --- /dev/null +++ b/marching/Assets/Scripts/Utils/MathUtils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9b829e1e17fa796408f3a7b1a42ed1ca +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/marching/Assets/Scripts/Utils/Vector3Extension.cs b/marching/Assets/Scripts/Utils/Vector3Extension.cs new file mode 100644 index 0000000..5afe941 --- /dev/null +++ b/marching/Assets/Scripts/Utils/Vector3Extension.cs @@ -0,0 +1,26 @@ +using System.Collections; +using System.Collections.Generic; +using Unity.VisualScripting; +using UnityEditor; +using UnityEngine; + +public static class Vector3Extension +{ + + public static Vector2 xy(this Vector3 src) + { + Vector2 xy = new Vector2(); + xy.x = src.x; + xy.y = src.y; + return xy; + } + + public static Vector2 ToVector2(this Vector3 src) + { + Vector2 xy = new Vector2(); + xy.x = src.x; + xy.y = src.y; + return xy; + } + +} diff --git a/marching/Assets/Scripts/Utils/Vector3Extension.cs.meta b/marching/Assets/Scripts/Utils/Vector3Extension.cs.meta new file mode 100644 index 0000000..e563122 --- /dev/null +++ b/marching/Assets/Scripts/Utils/Vector3Extension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a3ec9d2ccbb46814eaa97fe04f1089e9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/marching/Assets/Scripts/Utils/Vector4Extension.cs b/marching/Assets/Scripts/Utils/Vector4Extension.cs new file mode 100644 index 0000000..e4b9b35 --- /dev/null +++ b/marching/Assets/Scripts/Utils/Vector4Extension.cs @@ -0,0 +1,34 @@ +using System.Collections; +using System.Collections.Generic; +using Unity.VisualScripting; +using UnityEditor; +using UnityEngine; + +public static class Vector4Extension +{ + + public static Vector2 xy(this Vector4 src) + { + Vector2 xy = new Vector2(); + xy.x = src.x; + xy.y = src.y; + return xy; + } + + public static Vector2 zw(this Vector4 src) + { + Vector2 zw = new Vector2(); + zw.x = src.z; + zw.y = src.w; + return zw; + } + + public static Vector2 ToVector2(this Vector4 src) + { + Vector2 xy = new Vector2(); + xy.x = src.x; + xy.y = src.y; + return xy; + } + +} diff --git a/marching/Assets/Scripts/Utils/Vector4Extension.cs.meta b/marching/Assets/Scripts/Utils/Vector4Extension.cs.meta new file mode 100644 index 0000000..b2d1dc0 --- /dev/null +++ b/marching/Assets/Scripts/Utils/Vector4Extension.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8a622b0bd09ab0242af55f6fb5660eda +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/marching/Packages/manifest.json b/marching/Packages/manifest.json index 3d05891..d5625d4 100644 --- a/marching/Packages/manifest.json +++ b/marching/Packages/manifest.json @@ -5,7 +5,6 @@ "com.unity.ide.rider": "3.0.15", "com.unity.ide.visualstudio": "2.0.16", "com.unity.ide.vscode": "1.2.5", - "com.unity.probuilder": "5.0.7", "com.unity.render-pipelines.universal": "12.1.7", "com.unity.test-framework": "1.1.31", "com.unity.textmeshpro": "3.0.6", diff --git a/marching/Packages/packages-lock.json b/marching/Packages/packages-lock.json index 59e56ad..f5810df 100644 --- a/marching/Packages/packages-lock.json +++ b/marching/Packages/packages-lock.json @@ -163,17 +163,6 @@ "dependencies": {}, "url": "https://packages.unity.com" }, - "com.unity.probuilder": { - "version": "5.0.7", - "depth": 0, - "source": "registry", - "dependencies": { - "com.unity.settings-manager": "1.0.3", - "com.unity.modules.physics": "1.0.0", - "com.unity.modules.imgui": "1.0.0" - }, - "url": "https://packages.unity.com" - }, "com.unity.render-pipelines.core": { "version": "12.1.7", "depth": 1, @@ -213,13 +202,6 @@ }, "url": "https://packages.unity.com" }, - "com.unity.settings-manager": { - "version": "1.0.3", - "depth": 1, - "source": "registry", - "dependencies": {}, - "url": "https://packages.unity.com" - }, "com.unity.shadergraph": { "version": "12.1.7", "depth": 1, |