From 4ad017d7eb6f26243b71a0ada9a7cfe6dc13a558 Mon Sep 17 00:00:00 2001 From: IF-ACT Date: Sun, 24 Jan 2021 14:21:27 +0800 Subject: [PATCH 1/4] save current work --- .../Runtime/BulletSystem/BulletParam.cs | 38 ++++++++++++ .../Runtime/BulletSystem/BulletParam.cs.meta | 3 + .../Runtime/BulletSystem/BulletSystemBase.cs | 1 + .../Runtime/BulletSystem/GameObjectBullet.cs | 18 ++++-- .../BulletSystem/GameObjectBulletSystem.cs | 27 +++++++-- .../Runtime/BulletSystem/IBulletController.cs | 9 +++ .../Modules/AccelerationModule.cs | 7 ++- .../BulletSystem/Modules/AroundAxisModule.cs | 7 +-- .../BulletSystem/Modules/DeflectionModule.cs | 6 +- .../BulletSystem/Modules/TracingModule.cs | 15 +++-- .../BulletSystem/ParticleBulletSystem.cs | 23 ++++++++ .../Runtime/Storm/Actions/Accelerate.cs | 57 ------------------ .../Runtime/Storm/Actions/Accelerate.cs.meta | 11 ---- .../Scripts/Runtime/Storm/Actions/Rotate.cs | 40 ------------- .../Runtime/Storm/Actions/Rotate.cs.meta | 11 ---- .../Runtime/Storm/Actions/RotateTowards.cs | 59 ------------------- .../Storm/Actions/RotateTowards.cs.meta | 11 ---- 17 files changed, 127 insertions(+), 216 deletions(-) create mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs create mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs.meta delete mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs delete mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs.meta delete mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs delete mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs.meta delete mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs delete mode 100644 Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs.meta diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs new file mode 100644 index 0000000..3be0592 --- /dev/null +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs @@ -0,0 +1,38 @@ +using UnityEngine; + +namespace CANStudio.BulletStorm.BulletSystem +{ + /// + /// All accessible parameters of a bullet in . + /// + public struct BulletParam + { + /// + /// Rotation is also the speed direction of a bullet. + /// + public Quaternion rotation; + + /// + /// Position in world space. + /// + public Vector3 position; + + /// + /// Speed of the bullet, negative values represents a velocity towards negative direction. + /// + public float speed; + + /// + /// Scaled time from emission. + /// + public readonly float lifetime; + + internal BulletParam(Quaternion rotation, Vector3 position, float speed, float lifetime) + { + this.rotation = rotation; + this.position = position; + this.speed = speed; + this.lifetime = lifetime; + } + } +} \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs.meta b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs.meta new file mode 100644 index 0000000..3dbe003 --- /dev/null +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletParam.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9902a3f56b214cbaa8f0de0793be6710 +timeCreated: 1610519578 \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs index 02ca228..6c6c408 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs @@ -56,6 +56,7 @@ public abstract class BulletSystemBase : MonoBehaviour, IBulletSystem, IBulletCo public virtual string Name => name; public abstract void ChangePosition(Func operation); public abstract void ChangeVelocity(Func operation); + public abstract void ChangeParam(Func operation); public abstract void Emit(BulletEmitParam emitParam, Transform emitter); public abstract void Destroy(); public virtual IBulletController GetController() => Instantiate(this); diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs index ebca915..0199cdc 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs @@ -7,9 +7,10 @@ namespace CANStudio.BulletStorm.BulletSystem [DisallowMultipleComponent] public class GameObjectBullet : MonoBehaviour { - public Vector3 velocity; + public float speed; public float lifetime; private bool enableLifetime; + public float StartTime { get; private set; } public void EnableLifeTime(float time) { @@ -26,19 +27,24 @@ internal void Init(Vector3 position, Vector3 velocity, Color color, Vector3 size { var t = transform; t.position = position; - this.velocity = velocity; + speed = velocity.magnitude; t.forward = velocity; enableLifetime = false; if (color != Color.clear) GetComponent().material.color = color; if (size != Vector3.zero) transform.localScale = size; } - + + private void Start() + { + StartTime = Time.time; + } + private void LateUpdate() { - transform.position += velocity * Time.deltaTime; + var t = transform; + t.position += speed * Time.deltaTime * t.forward; if (!enableLifetime) return; - lifetime -= Time.deltaTime; - if (lifetime <= 0) Destroy(this); + if (lifetime <= Time.time - StartTime) Destroy(this); } } } \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs index 05b4bbb..c926672 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using CANStudio.BulletStorm.Emission; +using CANStudio.BulletStorm.Util; using UnityEngine; #pragma warning disable 0649 @@ -13,7 +14,7 @@ namespace CANStudio.BulletStorm.BulletSystem /// A bullet system based on . It emits game objects as bullets, /// and keeps them moving. The bullet system will attach a /// component to every game objects it emits, from which you can get bullet attributes like - /// and . + /// and . /// /// This bullet system is much more inefficient than , but /// useful when you need more flexibility. For example, if you need an enemy bullet which is @@ -39,9 +40,8 @@ public override void ChangePosition(Func operation) ClearDestroyedBullets(); foreach (var gameObjectBullet in bullets) { - var bulletTransform = gameObjectBullet.transform; - bulletTransform.position = - operation(bulletTransform.position, gameObjectBullet.velocity); + var t = gameObjectBullet.transform; + t.position = operation(t.position, t.forward * gameObjectBullet.speed); } } @@ -50,13 +50,30 @@ public override void ChangeVelocity(Func operation) ClearDestroyedBullets(); foreach (var gameObjectBullet in bullets) { - gameObjectBullet.velocity = operation(gameObjectBullet.transform.position, gameObjectBullet.velocity); + var t = gameObjectBullet.transform; + var velocity = operation(t.position, t.forward * gameObjectBullet.speed); + t.forward = velocity.Normalized(); + gameObjectBullet.speed = velocity.magnitude; + } + } + + public override void ChangeParam(Func operation) + { + ClearDestroyedBullets(); + foreach (var gameObjectBullet in bullets) + { + var t = gameObjectBullet.transform; + var param = operation(new BulletParam(t.rotation, t.position, gameObjectBullet.speed, + Time.time - gameObjectBullet.StartTime)); + t.SetPositionAndRotation(param.position, param.rotation); + gameObjectBullet.speed = param.speed; } } public override void Emit(BulletEmitParam emitParam, Transform emitter) { var bulletComponent = Instantiate(bullet).AddComponent(); + bullets.Add(bulletComponent); var absEmitParam = emitParam.RelativeTo(emitter); bulletComponent.Init(absEmitParam.position, absEmitParam.velocity, absEmitParam.color, absEmitParam.size); if (enableLifetime) bulletComponent.EnableLifeTime(bulletLifeTime); diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs index 78a8b27..1e89e37 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs @@ -10,13 +10,22 @@ public interface IBulletController /// Changes all bullets' position in the controller. /// /// Vector3 ChangedPosition(Vector3 oldPosition, Vector3 oldVelocity) + [Obsolete("Use ChangeParam() instead")] void ChangePosition(Func operation); /// /// Changes all bullets' velocity in the controller. /// /// Vector3 ChangedVelocity(Vector3 oldPosition, Vector3 oldVelocity) + [Obsolete("Use ChangeParam() instead")] void ChangeVelocity(Func operation); + + /// + /// Changes all bullets' parameters in the controller. + /// Notice that the readonly value won't be changed by this function. + /// + /// + void ChangeParam(Func operation); /// /// Emits a new bullet from the controller. diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AccelerationModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AccelerationModule.cs index 0b89a34..18f2253 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AccelerationModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AccelerationModule.cs @@ -28,12 +28,13 @@ public void OnUpdate(IBulletController controller) var acc = acceleration; var min = minSpeed; var max = maxSpeed; - controller.ChangeVelocity((oldPosition, oldVelocity) => + controller.ChangeParam(param => { - var speed = oldVelocity.magnitude + acc * deltaTime; + var speed = param.speed + acc * deltaTime; if (acc < 0 && speed < min) speed = min; else if (acc > 0 && speed > max) speed = max; - return oldVelocity.SafeChangeMagnitude(speed); + param.speed = speed; + return param; }); } } diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs index 06e15de..094c06c 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs @@ -27,11 +27,10 @@ public void OnUpdate(IBulletController controller) } var ax = axis; var angle = anglePerSecond * Time.deltaTime; - controller.ChangeVelocity((oldPosition, oldVelocity) => + controller.ChangeParam(param => { - var forward = Vector3.Project(oldVelocity, ax); - var projection = Vector3.ProjectOnPlane(oldVelocity, ax); - return forward + Quaternion.AngleAxis(angle, ax) * projection; + param.rotation = Quaternion.AngleAxis(angle, ax) * param.rotation; + return param; }); } } diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs index d87f41c..ea29d34 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs @@ -56,7 +56,11 @@ public void OnUpdate(IBulletController controller) throw new ArgumentOutOfRangeException(); } - controller.ChangeVelocity((oldPosition, oldVelocity) => rotation * oldVelocity); + controller.ChangeParam(param => + { + param.rotation = rotation * param.rotation; + return param; + }); } [Serializable] diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs index b740b01..6e2b377 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs @@ -41,16 +41,15 @@ public void OnUpdate(IBulletController bullet) var rate = tracingRate; var enableCurve = enableRateCurve; var curve = tracingRateCurve; - bullet.ChangeVelocity((position, velocity) => + bullet.ChangeParam(param => { - var aimDirection = targetPosition - position; - var rateValue = enableCurve ? rate * curve.Evaluate(Vector3.Angle(aimDirection, velocity)) : rate; + var direction = param.rotation * Vector3.forward; + var aimDirection = targetPosition - param.position; + var rateValue = enableCurve ? rate * curve.Evaluate(Vector3.Angle(aimDirection, direction)) : rate; if (rateValue < 0) rateValue = 0; - return Vector3.RotateTowards( - velocity, - aimDirection, - rateValue * deltaTime * Mathf.Deg2Rad, - 0); + param.rotation = Quaternion.RotateTowards(param.rotation, + Quaternion.LookRotation(aimDirection, param.rotation * Vector3.up), rateValue * deltaTime); + return param; }); } } diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs index fa3ffcb..129062a 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs @@ -62,6 +62,29 @@ public override void ChangeVelocity(Func operation) }); } + public override void ChangeParam(Func operation) + { + UpdateParticles(); + var renderMode = psr.renderMode; + Parallel.For(0, particleCount, i => + { + var oldParam = new BulletParam(Quaternion.LookRotation(particles[i].velocity.Normalized()), + particles[i].position, + particles[i].velocity.magnitude, + particles[i].startLifetime - particles[i].remainingLifetime); + var newParam = operation(oldParam); + particles[i].velocity = (newParam.rotation * Vector3.forward).SafeChangeMagnitude(newParam.speed); + particles[i].position = newParam.position; + // also change rotation if particle is mesh + if (renderMode == ParticleSystemRenderMode.Mesh) + { + particles[i].rotation3D = + (Quaternion.Inverse(oldParam.rotation) * newParam.rotation * + Quaternion.Euler(particles[i].rotation3D)).eulerAngles; + } + }); + } + public override void ChangePosition(Func operation) { UpdateParticles(); diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs b/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs deleted file mode 100644 index 7a83826..0000000 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Collections; -using CANStudio.BulletStorm.BulletSystem; -using UnityEngine; - -namespace CANStudio.BulletStorm.Storm.Actions -{ - /// - /// Accelerates all bullets. - /// - [Serializable] - public class Accelerate : IStormAction - { - [SerializeField] private bool customDirection; - [SerializeField] private float acceleration; - [SerializeField] private Vector3 accelerationVector; - [SerializeField] private float duration; - - /// - /// Accelerate in bullet original direction. - /// - /// Speed change per second. - /// Total time for the accelerate action. - public Accelerate(float acceleration, float duration) - { - customDirection = false; - this.acceleration = acceleration; - this.duration = duration; - } - - /// - /// Accelerate in customized direction. - /// - /// Velocity change per second. - /// Total time for the accelerate action. - public Accelerate(Vector3 accelerationVector, float duration) - { - customDirection = true; - this.accelerationVector = accelerationVector; - this.duration = duration; - } - - public IEnumerator Execute(IBulletController controller, Transform emitter) - { - var startTime = Time.time; - while (Time.time - startTime < duration) - { - var deltaTime = Time.deltaTime; - controller.ChangeVelocity((position, velocity) => - velocity + (customDirection - ? accelerationVector * deltaTime - : velocity.normalized * (acceleration * deltaTime))); - yield return null; - } - } - } -} \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs.meta b/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs.meta deleted file mode 100644 index 529e3cf..0000000 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Accelerate.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 83b1b55a30f213e4d88bcef741c3dccf -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs b/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs deleted file mode 100644 index 6f92114..0000000 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections; -using CANStudio.BulletStorm.BulletSystem; -using UnityEngine; - -namespace CANStudio.BulletStorm.Storm.Actions -{ - /// - /// Rotates the velocity direction of bullets. - /// - [Serializable] - public class Rotate : IStormAction - { - [SerializeField] private Vector3 angularVelocity; - [SerializeField] private float duration; - - /// - /// Rotates the velocity direction. - /// - /// Rotates given angle per second. - /// Total time of the rotation action. - public Rotate(Vector3 angularVelocity, float duration) - { - this.angularVelocity = angularVelocity; - this.duration = duration; - } - - public IEnumerator Execute(IBulletController controller, Transform emitter) - { - var startTime = Time.time; - while (Time.time - startTime < duration) - { - var deltaTime = Time.deltaTime; - controller.ChangeVelocity((position, velocity) => - Quaternion.Euler(angularVelocity * deltaTime) * velocity); - yield return null; - } - } - } -} \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs.meta b/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs.meta deleted file mode 100644 index 919da1a..0000000 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/Rotate.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 884b64e21d74b6e468336f50b27550b8 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs b/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs deleted file mode 100644 index a65515a..0000000 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Collections; -using CANStudio.BulletStorm.BulletSystem; -using UnityEngine; - -namespace CANStudio.BulletStorm.Storm.Actions -{ - /// - /// Rotate bullet velocity towards a transform or position. - /// - [Serializable] - public class RotateTowards : IStormAction - { - [SerializeField] private bool useTransform; - [SerializeField] private Transform transform; - [SerializeField] private Vector3 position; - [SerializeField] private float rate; - [SerializeField] private float duration; - - /// - /// Rotates velocity to look at a transform. - /// - /// The target transform. - /// Max rotate radians per second in degree. - /// Total time of the rotation action. - public RotateTowards(Transform transform, float rate, float duration) - { - useTransform = true; - this.transform = transform; - this.rate = rate; - this.duration = duration; - } - - /// - /// Rotates velocity to look at a position. - /// - /// The target position. - /// Max rotate radians per second in degree. - /// Total time of the rotation action. - public RotateTowards(Vector3 position, float rate, float duration) - { - useTransform = false; - this.position = position; - this.rate = rate; - this.duration = duration; - } - - public IEnumerator Execute(IBulletController controller, Transform emitter) - { - var startTime = Time.time; - while (Time.time - startTime < duration) - { - controller.ChangeVelocity((pos, vel) => - Vector3.RotateTowards(vel, (useTransform ? transform.position : position) - pos, rate, 0)); - yield return null; - } - } - } -} \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs.meta b/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs.meta deleted file mode 100644 index 69791a3..0000000 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Storm/Actions/RotateTowards.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 12d07bdd34829ad4a9b63c64bb14b9f1 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: From c3993a073584af84cfe62ebcc255a9be3245330b Mon Sep 17 00:00:00 2001 From: ifact Date: Sat, 30 Jan 2021 10:56:19 +0800 Subject: [PATCH 2/4] fix: gameobjects doesn't destroy --- .../Runtime/BulletSystem/GameObjectBullet.cs | 7 +- .../BulletSystem/GameObjectBulletSystem.cs | 8 +- .../BulletSystem/Modules/TracingModule.cs | 7 +- .../BulletSystem/ParticleBulletSystem.cs | 2 +- .../Runtime/Emitters/AutoEmitterBase.cs | 2 +- Packages/manifest.json | 13 ++- Packages/packages-lock.json | 89 ++++++++++++++++++- ProjectSettings/PackageManagerSettings.asset | 4 +- 8 files changed, 106 insertions(+), 26 deletions(-) diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs index 0199cdc..dfbff6a 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBullet.cs @@ -18,10 +18,7 @@ public void EnableLifeTime(float time) lifetime = time; } - internal void Init(Vector3 position, Vector3 velocity) - { - Init(position, velocity, Color.clear, Vector3.zero); - } + internal void Init(Vector3 position, Vector3 velocity) => Init(position, velocity, Color.clear, Vector3.zero); internal void Init(Vector3 position, Vector3 velocity, Color color, Vector3 size) { @@ -44,7 +41,7 @@ private void LateUpdate() var t = transform; t.position += speed * Time.deltaTime * t.forward; if (!enableLifetime) return; - if (lifetime <= Time.time - StartTime) Destroy(this); + if (lifetime <= Time.time - StartTime) Destroy(gameObject); } } } \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs index c926672..56ef025 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/GameObjectBulletSystem.cs @@ -93,7 +93,7 @@ private IEnumerator WaitForDestroy() if (bullets is null || bullets.Count == 0) break; yield return null; } - Destroy(this); + Destroy(gameObject); } /// @@ -110,11 +110,5 @@ private void LateUpdate() { bulletsCleared = false; } - - private void OnDestroy() - { - ClearDestroyedBullets(); - foreach (var gameObjectBullet in bullets) Destroy(gameObjectBullet); - } } } \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs index 6e2b377..457bfeb 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs @@ -43,12 +43,11 @@ public void OnUpdate(IBulletController bullet) var curve = tracingRateCurve; bullet.ChangeParam(param => { - var direction = param.rotation * Vector3.forward; - var aimDirection = targetPosition - param.position; - var rateValue = enableCurve ? rate * curve.Evaluate(Vector3.Angle(aimDirection, direction)) : rate; + var aimRotation = Quaternion.LookRotation(targetPosition - param.position); + var rateValue = enableCurve ? rate * curve.Evaluate(Quaternion.Angle(param.rotation, aimRotation)) : rate; if (rateValue < 0) rateValue = 0; param.rotation = Quaternion.RotateTowards(param.rotation, - Quaternion.LookRotation(aimDirection, param.rotation * Vector3.up), rateValue * deltaTime); + aimRotation, rateValue * deltaTime); return param; }); } diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs index 129062a..d74cd2e 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/ParticleBulletSystem.cs @@ -145,7 +145,7 @@ private IEnumerator WaitForDestroy() if (ps.particleCount == 0) break; yield return null; } - Destroy(this); + Destroy(gameObject); } /// diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoEmitterBase.cs b/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoEmitterBase.cs index 2ea6a86..91a1f63 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoEmitterBase.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoEmitterBase.cs @@ -94,7 +94,7 @@ public void StartEmission() { coroutine = new ControllableCoroutine(this, StartEmitCoroutine(), () => { - if (destroyOnFinish) Destroy(this); + if (destroyOnFinish) Destroy(gameObject); }); coroutine.Start(); } diff --git a/Packages/manifest.json b/Packages/manifest.json index 6d1daf0..a9e45a8 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -1,11 +1,20 @@ { "dependencies": { + "com.unity.2d.sprite": "1.0.0", + "com.unity.2d.tilemap": "1.0.0", + "com.unity.ads": "3.5.2", + "com.unity.analytics": "3.3.5", + "com.unity.collab-proxy": "1.2.16", "com.unity.ide.rider": "2.0.7", "com.unity.ide.visualstudio": "2.0.2", - "com.unity.ide.vscode": "1.2.2", - "com.unity.test-framework": "1.1.18", + "com.unity.ide.vscode": "1.2.3", + "com.unity.multiplayer-hlapi": "1.0.6", + "com.unity.purchasing": "2.2.1", + "com.unity.test-framework": "1.1.20", + "com.unity.textmeshpro": "2.0.1", "com.unity.timeline": "1.4.3", "com.unity.ugui": "1.0.0", + "com.unity.xr.legacyinputhelpers": "2.1.7", "com.unity.modules.ai": "1.0.0", "com.unity.modules.androidjni": "1.0.0", "com.unity.modules.animation": "1.0.0", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index ea55a7f..7f20144 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -23,8 +23,45 @@ "dependencies": {}, "url": "https://package.openupm.com" }, - "com.unity.ext.nunit": { + "com.unity.2d.sprite": { + "version": "1.0.0", + "depth": 0, + "source": "builtin", + "dependencies": {} + }, + "com.unity.2d.tilemap": { "version": "1.0.0", + "depth": 0, + "source": "builtin", + "dependencies": {} + }, + "com.unity.ads": { + "version": "3.5.2", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.ugui": "1.0.0" + }, + "url": "https://packages.unity.cn" + }, + "com.unity.analytics": { + "version": "3.3.5", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.ugui": "1.0.0" + }, + "url": "https://packages.unity.cn" + }, + "com.unity.collab-proxy": { + "version": "1.2.16", + "depth": 0, + "source": "registry", + "dependencies": {}, + "url": "https://packages.unity.cn" + }, + "com.unity.ext.nunit": { + "version": "1.0.6", "depth": 1, "source": "registry", "dependencies": {}, @@ -47,23 +84,50 @@ "url": "https://packages.unity.cn" }, "com.unity.ide.vscode": { - "version": "1.2.2", + "version": "1.2.3", "depth": 0, "source": "registry", "dependencies": {}, "url": "https://packages.unity.cn" }, + "com.unity.multiplayer-hlapi": { + "version": "1.0.6", + "depth": 0, + "source": "registry", + "dependencies": { + "nuget.mono-cecil": "0.1.6-preview" + }, + "url": "https://packages.unity.cn" + }, + "com.unity.purchasing": { + "version": "2.2.1", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.ugui": "1.0.0" + }, + "url": "https://packages.unity.cn" + }, "com.unity.test-framework": { - "version": "1.1.18", + "version": "1.1.20", "depth": 0, "source": "registry", "dependencies": { - "com.unity.ext.nunit": "1.0.0", + "com.unity.ext.nunit": "1.0.6", "com.unity.modules.imgui": "1.0.0", "com.unity.modules.jsonserialize": "1.0.0" }, "url": "https://packages.unity.cn" }, + "com.unity.textmeshpro": { + "version": "2.0.1", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.ugui": "1.0.0" + }, + "url": "https://packages.unity.cn" + }, "com.unity.timeline": { "version": "1.4.3", "depth": 0, @@ -85,6 +149,23 @@ "com.unity.modules.imgui": "1.0.0" } }, + "com.unity.xr.legacyinputhelpers": { + "version": "2.1.7", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.modules.vr": "1.0.0", + "com.unity.modules.xr": "1.0.0" + }, + "url": "https://packages.unity.cn" + }, + "nuget.mono-cecil": { + "version": "0.1.6-preview", + "depth": 1, + "source": "registry", + "dependencies": {}, + "url": "https://packages.unity.cn" + }, "com.unity.modules.ai": { "version": "1.0.0", "depth": 0, diff --git a/ProjectSettings/PackageManagerSettings.asset b/ProjectSettings/PackageManagerSettings.asset index 6664885..ef729d7 100644 --- a/ProjectSettings/PackageManagerSettings.asset +++ b/ProjectSettings/PackageManagerSettings.asset @@ -9,9 +9,9 @@ MonoBehaviour: m_GameObject: {fileID: 0} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 0} + m_Script: {fileID: 13964, guid: 0000000000000000e000000000000000, type: 0} m_Name: - m_EditorClassIdentifier: UnityEditor:UnityEditor.PackageManager.UI:PackageManagerProjectSettings + m_EditorClassIdentifier: m_ScopedRegistriesSettingsExpanded: 1 oneTimeWarningShown: 0 m_Registries: From 7617b8f491a1e045b5eb71ab9fb46d56283df0ae Mon Sep 17 00:00:00 2001 From: ifact Date: Sat, 30 Jan 2021 12:32:48 +0800 Subject: [PATCH 3/4] improved tracing module --- .../Runtime/BulletSystem/Modules/TracingModule.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs index 457bfeb..b9ef28e 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs @@ -36,18 +36,19 @@ public void OnUpdate(IBulletController bullet) return; } - var deltaTime = Time.deltaTime; var targetPosition = target.AsTransform.position; - var rate = tracingRate; + var rate = tracingRate * Time.deltaTime; var enableCurve = enableRateCurve; var curve = tracingRateCurve; bullet.ChangeParam(param => { - var aimRotation = Quaternion.LookRotation(targetPosition - param.position); - var rateValue = enableCurve ? rate * curve.Evaluate(Quaternion.Angle(param.rotation, aimRotation)) : rate; - if (rateValue < 0) rateValue = 0; - param.rotation = Quaternion.RotateTowards(param.rotation, - aimRotation, rateValue * deltaTime); + var direction = param.rotation * Vector3.forward; + var aimDirection = targetPosition - param.position; + var axis = Vector3.Cross(direction, aimDirection); + var dAngle = enableCurve ? curve.Evaluate(Vector3.Angle(direction, aimDirection)) * rate : rate; + if (dAngle < 0) + dAngle = 0; + param.rotation = Quaternion.AngleAxis(dAngle, axis) * param.rotation; return param; }); } From 6b75ca3b269d624880ac0649809149dbc413b875 Mon Sep 17 00:00:00 2001 From: ifact Date: Sun, 31 Jan 2021 17:02:36 +0800 Subject: [PATCH 4/4] feat: add reference system for auto shape emitter --- .../Runtime/BulletSystem/BulletSystemBase.cs | 1 + .../Runtime/BulletSystem/IBulletController.cs | 2 + .../BulletSystem/Modules/AroundAxisModule.cs | 22 +++++-- .../BulletSystem/Modules/DeflectionModule.cs | 60 +------------------ .../Runtime/Emitters/AutoShapeEmitter.cs | 6 ++ .../Scripts/Runtime/Emitters/Emitter.cs | 17 ++++++ .../Scripts/Runtime/Util/Helpers.cs | 19 ------ 7 files changed, 45 insertions(+), 82 deletions(-) diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs index 6c6c408..afc4d62 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/BulletSystemBase.cs @@ -54,6 +54,7 @@ public abstract class BulletSystemBase : MonoBehaviour, IBulletSystem, IBulletCo private AroundAxisModule aroundAxis; public virtual string Name => name; + public Quaternion Rotation { get => transform.rotation; set => transform.rotation = value; } public abstract void ChangePosition(Func operation); public abstract void ChangeVelocity(Func operation); public abstract void ChangeParam(Func operation); diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs index 1e89e37..9edf49f 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/IBulletController.cs @@ -6,6 +6,8 @@ namespace CANStudio.BulletStorm.BulletSystem { public interface IBulletController { + Quaternion Rotation { get; set; } + /// /// Changes all bullets' position in the controller. /// diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs index 094c06c..4634d28 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/AroundAxisModule.cs @@ -1,6 +1,5 @@ using System; using CANStudio.BulletStorm.Util; -using NaughtyAttributes; using UnityEngine; #pragma warning disable 0649 @@ -10,11 +9,12 @@ namespace CANStudio.BulletStorm.BulletSystem.Modules [Serializable] public struct AroundAxisModule { - [InfoBox("This module is experimental, your configure may loss when updating to next version.", EInfoBoxType.Warning)] - [Tooltip("Rotates around this axis."), SerializeField] private Vector3 axis; + [Tooltip("If select 'self', use reference system set in emitter."), SerializeField] + private Space space; + [Tooltip("Per second rotation angle in degree."), SerializeField] private float anglePerSecond; @@ -25,11 +25,23 @@ public void OnUpdate(IBulletController controller) BulletStormLogger.LogErrorOnce($"{controller}: In Around axis module, axis can't be zero!"); return; } - var ax = axis; + + Vector3 axisInWorld; + switch (space) + { + case Space.World: + axisInWorld = axis; + break; + case Space.Self: + axisInWorld = controller.Rotation * axis; + break; + default: + throw new ArgumentOutOfRangeException(); + } var angle = anglePerSecond * Time.deltaTime; controller.ChangeParam(param => { - param.rotation = Quaternion.AngleAxis(angle, ax) * param.rotation; + param.rotation = Quaternion.AngleAxis(angle, axisInWorld) * param.rotation; return param; }); } diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs index ea29d34..6eaaa24 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/DeflectionModule.cs @@ -1,6 +1,5 @@ using System; using CANStudio.BulletStorm.Util; -using NaughtyAttributes; using UnityEngine; #pragma warning disable 0649 @@ -13,71 +12,16 @@ public struct DeflectionModule [Tooltip("Velocity deflection angle per second."), SerializeField] private Vector2 deflection; - [Tooltip("Specific the space that deflection calculated in."), SerializeField] - private Space space; - - [Tooltip("The target transform to be a space."), SerializeField, ShowIf(nameof(ShowTarget)), AllowNesting] - private TargetWrapper target; - - [Space, Space] // fix the AllowNesting attribute's default behavior - - [Tooltip("The euler to describe rotation of a space."), SerializeField, ShowIf(nameof(ShowEuler)), AllowNesting] - private Vector3 euler; - - private bool ShowEuler => space == Space.Fixed; - private bool ShowTarget => space == Space.Dynamic; - public void OnUpdate(IBulletController controller) { - Quaternion rotation; - var dEuler = new Vector3(-deflection.y * Time.deltaTime, deflection.x * Time.deltaTime); - - switch (space) - { - case Space.World: - rotation = Quaternion.Euler(dEuler); - break; - case Space.Fixed: - rotation = Helpers.Euler(dEuler, Quaternion.Euler(euler)); - break; - case Space.Dynamic: - if (!target.target.Check()) - { - BulletStormLogger.LogWarningOnce($"{controller}: Can't find space {target}, use world space by default."); - rotation = Quaternion.Euler(dEuler); - } - else - { - rotation = Helpers.Euler(dEuler, target.target.AsTransform.rotation); - } - break; - default: - throw new ArgumentOutOfRangeException(); - } - + var rotation = Quaternion.Euler(dEuler); + controller.ChangeParam(param => { param.rotation = rotation * param.rotation; return param; }); } - - [Serializable] - private enum Space - { - [Tooltip("Simulates in world space.")] - World, - [Tooltip("Take given euler as simulating space.")] - Fixed, - [Tooltip("Take given target as simulating space.")] - Dynamic - } - - [Serializable] - private struct TargetWrapper - { - public Target target; - } } } \ No newline at end of file diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoShapeEmitter.cs b/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoShapeEmitter.cs index 85686eb..2442cbb 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoShapeEmitter.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/AutoShapeEmitter.cs @@ -49,6 +49,9 @@ protected override IEnumerator StartEmitCoroutine() var repeatTimes = emission.repeat ? emission.repeatTimes : 1; + if (emission.newReferenceSystem) + SetReferenceSystem(bullet, Emitter.rotation); + for (var i = 0; i < repeatTimes; i++) { if (overriden is null) @@ -114,6 +117,9 @@ private struct ShapeConfig [Tooltip("Wait time in second after finish each emission."), MinValue(0), AllowNesting] public float wait; + [Tooltip("Record emitter's current rotation as reference system when this item begins.")] + public bool newReferenceSystem; + public IReadOnlyList OverridenShape { get diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/Emitter.cs b/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/Emitter.cs index 493861a..6ecbe09 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/Emitter.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/Emitters/Emitter.cs @@ -69,6 +69,23 @@ protected virtual void OnDestroy() { foreach (var copied in bulletSystems.Values) copied.Destroy(); } + + /// + /// Set reference system for a bullet system. After calling this, emitted bullets will take given rotation + /// their reference system. This function won't change reference system of already emitted bullets. + /// + /// + /// + protected void SetReferenceSystem(IBulletSystem bulletSystem, Quaternion rotation) + { + if (bulletSystems.TryGetValue(bulletSystem, out var controller)) + { + controller.Destroy(); + } + var newController = bulletSystem.GetController(); + newController.Rotation = rotation; + bulletSystems[bulletSystem] = newController; + } private IBulletController GetBulletController(IBulletSystem bullet) { diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/Util/Helpers.cs b/Packages/bullet-storm-unity/Scripts/Runtime/Util/Helpers.cs index 674d336..da1e234 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/Util/Helpers.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/Util/Helpers.cs @@ -73,24 +73,5 @@ public static Vector3 SafeChangeMagnitude(this in Vector3 vector3, float value) return maxExp < Accuracy ? vector3.Minimized() : calculated; } - - /// - /// Create quaternion from euler angles relative to given space. - /// - /// Euler angles - /// - /// - public static Quaternion Euler(Vector3 euler, Quaternion space) - { - var q = Quaternion.identity; - var rotationY = Quaternion.AngleAxis(euler.y, space * Vector3.up); - q *= rotationY; - space *= rotationY; - var rotationX = Quaternion.AngleAxis(euler.x, space * Vector3.right); - q *= rotationX; - space *= rotationX; - q *= Quaternion.AngleAxis(euler.z, space * Vector3.forward); - return q; - } } } \ No newline at end of file