slimecing

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit d334726ef90e57b83d69b137c0d38b0c98caed67
parent 508ab651aea7aaca1a70a065dadd70ccd710adcf
Author: PlutoTank <qwolkensperg@gmail.com>
Date:   Thu, 23 Apr 2020 16:18:57 -0400

added moving platforms

Diffstat:
AAssets/Scripts/Slimecing/Environment.meta | 8++++++++
AAssets/Scripts/Slimecing/Environment/MoverSimulationManager.cs | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AAssets/Scripts/Slimecing/Environment/MoverSimulationManager.cs.meta | 11+++++++++++
AAssets/Scripts/Slimecing/Environment/Moving.meta | 8++++++++
AAssets/Scripts/Slimecing/Environment/Moving/MovingController.cs | 20++++++++++++++++++++
AAssets/Scripts/Slimecing/Environment/Moving/MovingController.cs.meta | 11+++++++++++
AAssets/Scripts/Slimecing/Environment/Moving/MovingPlatformLogic.meta | 8++++++++
AAssets/Scripts/Slimecing/Environment/Moving/MovingPlatformLogic/BasicMovingPlatform.cs | 34++++++++++++++++++++++++++++++++++
AAssets/Scripts/Slimecing/Environment/Moving/RigidbodyMover.cs | 93+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AAssets/Scripts/Slimecing/Environment/Moving/RigidbodyMoverState.cs | 21+++++++++++++++++++++
10 files changed, 303 insertions(+), 0 deletions(-)

diff --git a/Assets/Scripts/Slimecing/Environment.meta b/Assets/Scripts/Slimecing/Environment.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 74b93f453121722459e1aeb1489e1817 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Slimecing/Environment/MoverSimulationManager.cs b/Assets/Scripts/Slimecing/Environment/MoverSimulationManager.cs @@ -0,0 +1,88 @@ +using System; +using Boo.Lang; +using Slimecing.Environment.Moving; +using UnityEngine; + +namespace Slimecing.Environment +{ + [DefaultExecutionOrder(-100)] + public class MoverSimulationManager : MonoBehaviour + { + private static MoverSimulationManager _instance; + + private static List<RigidbodyMover> _rigidbodyMovers = new List<RigidbodyMover>(); + + public static void CheckAlive() + { + if (_instance != null) return; + + GameObject holdingGameObject = new GameObject("MoverSimManager"); + _instance = holdingGameObject.AddComponent<MoverSimulationManager>(); + + holdingGameObject.hideFlags = HideFlags.NotEditable; + _instance.hideFlags = HideFlags.NotEditable; + } + private void Awake() + { + _instance = this; + } + + private void OnDisable() + { + Destroy(gameObject); + } + + public static void RegisterMover(RigidbodyMover mover) + { + _rigidbodyMovers.Add(mover); + } + + public static void UnregisterMover(RigidbodyMover mover) + { + _rigidbodyMovers.Remove(mover); + } + + private void FixedUpdate() + { + float deltaTime = Time.deltaTime; + PreSimulationTick(); + SimulateTick(deltaTime); + PostSimulationTick(); + } + + private void PreSimulationTick() + { + foreach (var mover in _rigidbodyMovers) + { + var moverTransform = mover.transform; + mover.beginningTickPosition = moverTransform.position; + mover.beginningTickRotation = moverTransform.rotation; + } + } + + private void SimulateTick(float deltaTime) + { + foreach (var mover in _rigidbodyMovers) + { + mover.UpdateMoverVelocity(deltaTime); + + mover.transform.SetPositionAndRotation(mover.currentPosition, mover.currentRotation); + mover.moverRigidbody.position = mover.currentPosition; + mover.moverRigidbody.rotation = mover.currentRotation; + } + } + + private void PostSimulationTick() + { + Physics.SyncTransforms(); + + foreach (var mover in _rigidbodyMovers) + { + mover.moverRigidbody.position = mover.beginningTickPosition; + mover.moverRigidbody.rotation = mover.beginningTickRotation; + mover.moverRigidbody.MovePosition(mover.currentPosition); + mover.moverRigidbody.MoveRotation(mover.currentRotation); + } + } + } +}+ \ No newline at end of file diff --git a/Assets/Scripts/Slimecing/Environment/MoverSimulationManager.cs.meta b/Assets/Scripts/Slimecing/Environment/MoverSimulationManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 572b8a59d1792524282bb6b6cbc9f0cd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Slimecing/Environment/Moving.meta b/Assets/Scripts/Slimecing/Environment/Moving.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2c72fee411e2698488900db516a500cb +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Slimecing/Environment/Moving/MovingController.cs b/Assets/Scripts/Slimecing/Environment/Moving/MovingController.cs @@ -0,0 +1,20 @@ +using UnityEngine; + +namespace Slimecing.Environment.Moving +{ + public abstract class MovingController : MonoBehaviour + { + //public Vector3 originalPosition { get; set; } + + //public Quaternion originalRotation { get; set; } + + public RigidbodyMover mover { get; private set; } + + public void InitializeMover(RigidbodyMover rbMover) + { + mover = rbMover; + } + + public abstract void TickMover(out Vector3 targetPosition, out Quaternion targetRotation, float deltaTime); + } +} diff --git a/Assets/Scripts/Slimecing/Environment/Moving/MovingController.cs.meta b/Assets/Scripts/Slimecing/Environment/Moving/MovingController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 575c3080b3cb1f14e881e0ed076d13b7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Slimecing/Environment/Moving/MovingPlatformLogic.meta b/Assets/Scripts/Slimecing/Environment/Moving/MovingPlatformLogic.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9ea0c4c82a49b8d419aae23df3daf898 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Slimecing/Environment/Moving/MovingPlatformLogic/BasicMovingPlatform.cs b/Assets/Scripts/Slimecing/Environment/Moving/MovingPlatformLogic/BasicMovingPlatform.cs @@ -0,0 +1,34 @@ +using System; +using UnityEngine; + +namespace Slimecing.Environment.Moving.MovingPlatformLogic +{ + public class BasicMovingPlatform : MovingController + { + [SerializeField] private Vector3 translationAxis = Vector3.right; + [SerializeField] private float translationPeriod = 10; + [SerializeField] private float translationSpeed = 1; + [SerializeField] private Vector3 rotationAxis = Vector3.up; + [SerializeField] private float rotSpeed = 10; + [SerializeField] private Vector3 oscillationAxis = Vector3.zero; + [SerializeField] private float oscillationPeriod = 10; + [SerializeField] private float oscillationSpeed = 10; + + private Vector3 _originalPosition; + private Quaternion _originalRotation; + + private void Start() + { + _originalPosition = mover.moverRigidbody.position; + _originalRotation = mover.moverRigidbody.rotation; + } + + public override void TickMover(out Vector3 targetPosition, out Quaternion targetRotation, float deltaTime) + { + targetPosition = _originalPosition + + translationAxis.normalized * (Mathf.Sin(Time.time * translationSpeed) * translationPeriod); + Quaternion targetRotForOsc = Quaternion.Euler(oscillationAxis.normalized * (Mathf.Sin(Time.time * oscillationSpeed) * oscillationPeriod)) * _originalRotation; + targetRotation = Quaternion.Euler(rotationAxis * (rotSpeed * Time.time)) * targetRotForOsc; + } + } +} diff --git a/Assets/Scripts/Slimecing/Environment/Moving/RigidbodyMover.cs b/Assets/Scripts/Slimecing/Environment/Moving/RigidbodyMover.cs @@ -0,0 +1,93 @@ +using System; +using UnityEngine; + +namespace Slimecing.Environment.Moving +{ + [RequireComponent(typeof(Rigidbody))] + public class RigidbodyMover : MonoBehaviour + { + [SerializeField] private Rigidbody objectRigidbody; + + [SerializeField] private MovingController moveController; + + public Rigidbody moverRigidbody + { + get => objectRigidbody; + set => objectRigidbody = value; + } + + public Vector3 beginningTickPosition { get; set; } + public Quaternion beginningTickRotation { get; set; } + + public Transform objectTransform { get; private set; } + + public Vector3 beginningSimulationPosition { get; private set; } + public Quaternion beginningSimulationRotation { get; private set; } + + private Vector3 _internalCurrentPosition; + + public Vector3 currentPosition + { + get => _internalCurrentPosition; + private set => _internalCurrentPosition = value; + } + + private Quaternion _internalCurrentRotation; + public Quaternion currentRotation + { + get => _internalCurrentRotation; + private set => _internalCurrentRotation = value; + } + + private void OnEnable() + { + MoverSimulationManager.CheckAlive(); + MoverSimulationManager.RegisterMover(this); + } + + private void OnDisable() + { + MoverSimulationManager.UnregisterMover(this); + } + + public void ValidateRigidbody() + { + objectRigidbody.centerOfMass = Vector3.zero; + objectRigidbody.useGravity = false; + objectRigidbody.drag = 0f; + objectRigidbody.angularDrag = 0f; + objectRigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete; + objectRigidbody.isKinematic = true; + objectRigidbody.constraints = RigidbodyConstraints.None; + objectRigidbody.interpolation = RigidbodyInterpolation.Interpolate; + } + + private void Awake() + { + objectTransform = transform; + ValidateRigidbody(); + + moveController.InitializeMover(this); + + currentPosition = objectRigidbody.position; + currentRotation = objectRigidbody.rotation; + beginningSimulationPosition = objectRigidbody.position; + beginningSimulationRotation = objectRigidbody.rotation; + } + + public void UpdateMoverVelocity(float deltaTime) + { + beginningSimulationPosition = currentPosition; + beginningSimulationRotation = currentRotation; + + moveController.TickMover(out _internalCurrentPosition, out _internalCurrentRotation, deltaTime); + + if (!(deltaTime > 0f)) return; + + objectRigidbody.velocity = (currentPosition - beginningSimulationPosition) / deltaTime; + + Quaternion rotationCurrentToTarget = currentRotation * Quaternion.Inverse(beginningSimulationRotation); + objectRigidbody.angularVelocity = Mathf.Deg2Rad * rotationCurrentToTarget.eulerAngles / deltaTime; + } + } +} diff --git a/Assets/Scripts/Slimecing/Environment/Moving/RigidbodyMoverState.cs b/Assets/Scripts/Slimecing/Environment/Moving/RigidbodyMoverState.cs @@ -0,0 +1,21 @@ +using UnityEngine; + +namespace Slimecing.Environment.Moving +{ + [System.Serializable] + public struct RigidbodyMoverState + { + public Vector3 position { get; set; } + public Quaternion rotation { get; set; } + public Vector3 velocity { get; set; } + public Vector3 angularVelocity { get; set; } + + public RigidbodyMoverState(Vector3 position, Quaternion rotation, Vector3 velocity, Vector3 angularVelocity) + { + this.position = position; + this.rotation = rotation; + this.velocity = velocity; + this.angularVelocity = angularVelocity; + } + } +}