diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index 119bd43..622d2d6 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -1,4 +1,3 @@ -using System; using UnityEngine; public class Player : MonoBehaviour @@ -102,7 +101,6 @@ public class Player : MonoBehaviour return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, ladderLayer); } - private void FixedUpdate() { diff --git a/Assets/Scripts/WaterShapeController.cs b/Assets/Scripts/WaterShapeController.cs new file mode 100644 index 0000000..8ff82b9 --- /dev/null +++ b/Assets/Scripts/WaterShapeController.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.U2D; + +public class WaterShapeController : MonoBehaviour +{ + [SerializeField] + private GameObject wavePointPref; + [SerializeField] + private float _springStiffness = 0.1f; + [SerializeField] + private float _dampening = 0.3f; + [SerializeField] + private List _springs = new List(); + [SerializeField] + private SpriteShapeController _spriteShapeController; + [SerializeField] + private int _wavesCount = 6; + [SerializeField] + private GameObject _wavePoints; + private int _cornersCount = 2; + + private float _spread = 0.006f; + + private void SetWaves() + { + Spline waterSpline = _spriteShapeController.spline; + var waterPointsCount = waterSpline.GetPointCount(); + + for (int count = _cornersCount; count < waterPointsCount - _cornersCount; count++) + { + waterSpline.RemovePointAt(_cornersCount); + } + + Vector3 waterTopLeftCorner = waterSpline.GetPosition(1); + Vector3 waterTopRightCorner = waterSpline.GetPosition(2); + float waterWidth = waterTopRightCorner.x - waterTopLeftCorner.x; + float spacingPerWawe = waterWidth / (_wavesCount + 1); + + for (int count = _wavesCount; count > 0; count--) + { + int index = _cornersCount; + float xPosition=waterTopLeftCorner.x+(spacingPerWawe * count); + Vector3 wavePoint=new Vector3(xPosition,waterTopLeftCorner.y,waterTopLeftCorner.z); + waterSpline.InsertPointAt(index, wavePoint ); + waterSpline.SetHeight(index, 0.1f); + waterSpline.SetCorner(index, false); + } + CreateSprings(waterSpline); + Splash(2, 1); + } + + private void CreateSprings(Spline waterSpline) + { + _springs = new List(); + for(int idx=0;idx<_wavesCount+1; idx++) + { + int index = idx + 1; + GameObject wavePoint = Instantiate(wavePointPref, _wavePoints.transform, false); + wavePoint.transform.localPosition = waterSpline.GetPosition(index); + WaterSpring waterSpring=wavePoint.GetComponent(); + waterSpring.Init(_spriteShapeController); + _springs.Add( waterSpring ); + + } + } + + private void UpdateSprings() + { + int springsCount = _springs.Count; + float[] left_deltas = new float[springsCount]; + float[] right_deltas = new float[springsCount]; + for (int index = 0; index < springsCount; index++) + { + if (index > 0) + { + left_deltas[index] = _spread * (_springs[index].Height - _springs[index - 1].Height); + _springs[index - 1].Velocity += left_deltas[index]; + } + if (index =0 && index<_springs.Count) { + _springs[index].Velocity += speed; + } + } + + // On Enable for example purposes + void OnEnable() + { + StartCoroutine(CreateWaves()); + foreach (WaterSpring waterSpringComponent in _springs) + { + waterSpringComponent.Init(_spriteShapeController); + } + } + IEnumerator CreateWaves() + { + foreach (Transform child in _wavePoints.transform) + { + StartCoroutine(Destroy(child.gameObject)); + } + yield return null; + SetWaves(); + yield return null; + } + IEnumerator Destroy(GameObject go) + { + yield return null; + DestroyImmediate(go); + } + + private void FixedUpdate() + { + foreach (WaterSpring spring in _springs) + { + spring.WaveSpringUpdate(_springStiffness, _dampening); + } + UpdateSprings(); + } +} diff --git a/Assets/Scripts/WaterShapeController.cs.meta b/Assets/Scripts/WaterShapeController.cs.meta new file mode 100644 index 0000000..c667e97 --- /dev/null +++ b/Assets/Scripts/WaterShapeController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3ed3c1e5ea4ca4f41a13946462533c67 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/WaterSpring.cs b/Assets/Scripts/WaterSpring.cs new file mode 100644 index 0000000..066743c --- /dev/null +++ b/Assets/Scripts/WaterSpring.cs @@ -0,0 +1,49 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.U2D; + +public class WaterSpring : MonoBehaviour +{ + public float Velocity { get; set; } = 0f; + private float _force = 0f; + public float Height { get; set; } = 0f; + private float _targetHeight = 0f; + private int _waveIndex; + private SpriteShapeController _spriteShapeController; + + public void Init(SpriteShapeController spriteShapeController) + { + _spriteShapeController = spriteShapeController; + var index=transform.GetSiblingIndex(); + _waveIndex = index + 1; + + Velocity = 0; + Height = transform.localPosition.y; + Height = transform.localPosition.y; + } + + public void WavePointUpdate(int level) + { + if (_spriteShapeController != null) + { + Spline waterSpline = _spriteShapeController.spline; + Vector3 wavePosition = waterSpline.GetPosition(_waveIndex); + waterSpline.SetPosition(_waveIndex, new Vector3(wavePosition.x, transform.localPosition.y, wavePosition.z)); + } + } + + public void WaveSpringUpdate(float springStiffness, float dampening) + { + + Height = transform.localPosition.y; + var maximumExtension = Height - _targetHeight; + + var loss = -dampening * Velocity; + _force = -springStiffness * maximumExtension+loss; + Velocity +=_force; + + var y=transform.localPosition.y; + transform.localPosition = new Vector3(transform.position.x, y+Velocity, transform.position.z); + } +} diff --git a/Assets/Scripts/WaterSpring.cs.meta b/Assets/Scripts/WaterSpring.cs.meta new file mode 100644 index 0000000..cef6e92 --- /dev/null +++ b/Assets/Scripts/WaterSpring.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bf31330d7df081c4c926958352c5fd98 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: