parent
8e8f1345ed
commit
43a50d5374
3 changed files with 66 additions and 6 deletions
@ -1,17 +1,72 @@ |
|||||||
|
using System.Collections; |
||||||
|
using System.Linq; |
||||||
using Unity.Netcode; |
using Unity.Netcode; |
||||||
using UnityEngine; |
using UnityEngine; |
||||||
|
|
||||||
namespace Game { |
namespace Game { |
||||||
public class Wormhole : NetworkBehaviour { |
public class Wormhole : NetworkBehaviour { |
||||||
|
|
||||||
public float Size { get; set; } |
public float duration = 10; |
||||||
|
|
||||||
private void Start() { |
private const float MaxSize = 10; |
||||||
transform.localScale = new Vector3(Size, Size, Size); |
|
||||||
|
private float size; |
||||||
|
public float Size { |
||||||
|
get => size; |
||||||
|
set { |
||||||
|
size = value; |
||||||
|
transform.localScale = new Vector3(size, size, 1); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private float Radius => Size / 2; |
||||||
|
|
||||||
|
private static float CubicEaseInOut(float t, float b, float c, float d) { |
||||||
|
if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; |
||||||
|
return c / 2 * ((t -= 2) * t * t + 2) + b; |
||||||
|
} |
||||||
|
|
||||||
|
private static float CircleEaseInOut(float t, float b, float c, float d) { |
||||||
|
if ((t /= d / 2) < 1) return -c / 2 * (Mathf.Sqrt(1 - t * t) - 1) + b; |
||||||
|
return c / 2 * (Mathf.Sqrt(1 - (t -= 2) * t) + 1) + b; |
||||||
|
} |
||||||
|
|
||||||
|
private IEnumerator Grow(float growTime) { |
||||||
|
float currentTime = 0; |
||||||
|
while (currentTime <= growTime) { |
||||||
|
Size = CircleEaseInOut(currentTime, 0, MaxSize, growTime); |
||||||
|
yield return new WaitForFixedUpdate(); |
||||||
|
currentTime += Time.fixedDeltaTime; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private IEnumerator Shrink(float shrinkTime) { |
||||||
|
float currentTime = 0; |
||||||
|
while (currentTime <= shrinkTime) { |
||||||
|
Size = CubicEaseInOut(currentTime, MaxSize, -MaxSize, shrinkTime); |
||||||
|
yield return new WaitForFixedUpdate(); |
||||||
|
currentTime += Time.fixedDeltaTime; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private IEnumerator Start() { |
||||||
|
Size = 0; |
||||||
|
yield return Grow(duration * 0.2f); |
||||||
|
yield return new WaitForSeconds(duration * 0.7f); |
||||||
|
yield return Shrink(duration * 0.1f); |
||||||
} |
} |
||||||
|
|
||||||
private void FixedUpdate() { |
private void FixedUpdate() { |
||||||
foreach (var ball in GameManager.Singleton.Balls) { } |
Vector2 thisPos = new Vector2(transform.position.x, transform.position.y); |
||||||
|
foreach (var ball in GameManager.Singleton.Balls) { |
||||||
|
Vector2 diff = thisPos - ball.Rb.position; |
||||||
|
if (diff.sqrMagnitude > Radius * Radius) |
||||||
|
continue; |
||||||
|
float dist = diff.magnitude; |
||||||
|
Vector2 force = diff.normalized / dist * (Size * 5); |
||||||
|
force = Vector2.ClampMagnitude(force, 15); |
||||||
|
ball.Rb.AddForce(force); |
||||||
|
} |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue