diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 6fc6168..54495e2 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -1027,7 +1027,7 @@ MonoBehaviour: - {fileID: 11400000, guid: 73dc3bbc93cba37b1b989a9497b680e7, type: 2} - {fileID: 11400000, guid: 3237b9257f669690ea1a6f828451509e, type: 2} - {fileID: 11400000, guid: c6de04aab82eeff81894387c19ac1310, type: 2} - wormholePrefab: {fileID: 0} + wormholePrefab: {fileID: 7486271855853268360, guid: bf48a6813e1f5a2f7a63fc5c413808cb, type: 3} --- !u!4 &2109945868 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index bbbf93d..3c6c798 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -41,6 +41,8 @@ namespace Game { while (Application.isPlaying) { yield return new WaitForSeconds(1); SpawnModification(); + yield return new WaitForSeconds(2); + SpawnWormhole(); } } @@ -87,7 +89,10 @@ namespace Game { Player2 = p2; } - private void SpawnWormhole() { } + private void SpawnWormhole() { + var pos = new Vector2(Random.Range(0, Dimensions.Singleton.PlaySize.x) - Dimensions.Singleton.PlaySize.x / 2, Random.Range(-5, 5)); + Instantiate(wormholePrefab, pos, Quaternion.identity); + } private void SpawnModification() { var pos = new Vector2(Random.Range(0, Dimensions.Singleton.PlaySize.x) - Dimensions.Singleton.PlaySize.x / 2, Random.Range(-2, 2)); diff --git a/Assets/Scripts/Game/Wormhole.cs b/Assets/Scripts/Game/Wormhole.cs index 80e8a25..bc7b58e 100644 --- a/Assets/Scripts/Game/Wormhole.cs +++ b/Assets/Scripts/Game/Wormhole.cs @@ -1,17 +1,72 @@ +using System.Collections; +using System.Linq; using Unity.Netcode; using UnityEngine; namespace Game { public class Wormhole : NetworkBehaviour { - public float Size { get; set; } + public float duration = 10; - private void Start() { - transform.localScale = new Vector3(Size, Size, Size); + private const float MaxSize = 10; + + 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() { - 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); + } } } }