refactor
This commit is contained in:
@@ -1,162 +0,0 @@
|
|||||||
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<WaterSpring> _springs = new List<WaterSpring>();
|
|
||||||
[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, 0f);
|
|
||||||
waterSpline.SetCorner(index, false);
|
|
||||||
}
|
|
||||||
CreateSprings(waterSpline);
|
|
||||||
Splash(2, 0.1f);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateSprings(Spline waterSpline)
|
|
||||||
{
|
|
||||||
_springs = new List<WaterSpring>();
|
|
||||||
for(int idx=0;idx<_wavesCount+1; idx++)
|
|
||||||
{
|
|
||||||
int index = idx + 1;
|
|
||||||
|
|
||||||
Smoothen(waterSpline, index);
|
|
||||||
|
|
||||||
GameObject wavePoint = Instantiate(wavePointPref, _wavePoints.transform, false);
|
|
||||||
|
|
||||||
wavePoint.transform.position = waterSpline.GetPosition(index);
|
|
||||||
WaterSpring waterSpring=wavePoint.GetComponent<WaterSpring>();
|
|
||||||
waterSpring.Init(_spriteShapeController);
|
|
||||||
_springs.Add( waterSpring );
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Smoothen(Spline waterSpline, int index)
|
|
||||||
{
|
|
||||||
Vector3 position = waterSpline.GetPosition(index);
|
|
||||||
Vector3 positionPrev = position;
|
|
||||||
Vector3 positionNext = position;
|
|
||||||
if (index > 1)
|
|
||||||
{
|
|
||||||
positionPrev = waterSpline.GetPosition(index - 1);
|
|
||||||
}
|
|
||||||
if (index - 1 <= _wavesCount)
|
|
||||||
{
|
|
||||||
positionNext = waterSpline.GetPosition(index + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3 forward = gameObject.transform.forward;
|
|
||||||
|
|
||||||
float scale = Mathf.Min((positionNext - position).magnitude, (positionPrev - position).magnitude) * 0.33f;
|
|
||||||
|
|
||||||
Vector3 leftTangent = (positionPrev - position).normalized * scale;
|
|
||||||
Vector3 rightTangent = (positionNext - position).normalized * scale;
|
|
||||||
|
|
||||||
SplineUtility.CalculateTangents(position, positionPrev, positionNext, forward, scale, out rightTangent, out leftTangent);
|
|
||||||
|
|
||||||
waterSpline.SetLeftTangent(index, leftTangent);
|
|
||||||
waterSpline.SetRightTangent(index, rightTangent);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 <springsCount-1)
|
|
||||||
{
|
|
||||||
right_deltas[index] = _spread * (_springs[index].Height - _springs[index + 1].Height);
|
|
||||||
_springs[index + 1].Velocity += right_deltas[index];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Splash(int index,float speed)
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
spring.WavePointUpdate();
|
|
||||||
}
|
|
||||||
UpdateSprings();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 3ed3c1e5ea4ca4f41a13946462533c67
|
|
||||||
MonoImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
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()
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: bf31330d7df081c4c926958352c5fd98
|
|
||||||
MonoImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
||||||
Reference in New Issue
Block a user