You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.0 KiB

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