using System.Collections; using System.Linq; using Unity.Netcode; using UnityEngine; namespace Game { public class Wormhole : NetworkBehaviour { public float duration = 10; private const float MaxSize = 10; private float size; private 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); Destroy(gameObject); } private void FixedUpdate() { 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); } } } }