diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index 5b3e1f2..83f4455 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -113,7 +113,7 @@ Transform: m_GameObject: {fileID: 5402279313309450415} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 4, y: 1, z: 1} + m_LocalScale: {x: 5, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: - {fileID: 4352057116839657264} diff --git a/Assets/Scripts/Dimensions.cs b/Assets/Scripts/Dimensions.cs index 0f00aeb..9c976d3 100644 --- a/Assets/Scripts/Dimensions.cs +++ b/Assets/Scripts/Dimensions.cs @@ -1,23 +1,24 @@ -using System; using UnityEngine; using UnityEngine.Serialization; [ExecuteInEditMode] public class Dimensions : MonoBehaviour { + public static Dimensions Singleton; + public bool isUpdating; [FormerlySerializedAs("panelHeightPixels")] [Tooltip("Player panels height")] public float panelHeightPixelsMinimum; - + [Tooltip("Size in Unity units")] public Vector2 playGroundSize; - + [Tooltip("Height of the empty space between board and death zone")] public float emptySpaceHeight; - - + + public Camera mainCamera; public EdgeCollider2D topC, bottomC, leftC, rightC; public SpriteRenderer playGround; @@ -25,7 +26,14 @@ public class Dimensions : MonoBehaviour { public SpriteRenderer playerPanelBottom; public SpriteRenderer background; - public static Dimensions Singleton; + private LineRenderer topL, bottomL; + + public Vector2 PlaySize { get; private set; } + + public Vector2 PlaySizeBoards { get; private set; } + + public float PanelHeightPixels { get; private set; } + private void Awake() { Singleton = this; GetComponents(); @@ -34,29 +42,28 @@ public class Dimensions : MonoBehaviour { isUpdating = false; } + private void Update() { + if (!isUpdating) + return; + SetDimensions(); + } + private void GetComponents() { topL = topC.GetComponent(); bottomL = bottomC.GetComponent(); } - - public Vector2 PlaySize { get; private set; } - public Vector2 PlaySizeBoards { get; private set; } - - public float PanelHeightPixels { get; private set; } - - private LineRenderer topL, bottomL; private void SetDimensions() { - float heightByPanels = 1 / (1 - 2 * panelHeightPixelsMinimum / mainCamera.pixelHeight) * playGroundSize.y; - float heightByPlayground = playGroundSize.x / mainCamera.aspect; - float height = Mathf.Max(heightByPanels, heightByPlayground); - float width = mainCamera.aspect * height; - float panelHeight = (height - playGroundSize.y) / 2; - + var heightByPanels = 1 / (1 - 2 * panelHeightPixelsMinimum / mainCamera.pixelHeight) * playGroundSize.y; + var heightByPlayground = playGroundSize.x / mainCamera.aspect; + var height = Mathf.Max(heightByPanels, heightByPlayground); + var width = mainCamera.aspect * height; + var panelHeight = (height - playGroundSize.y) / 2; + mainCamera.orthographicSize = height / 2; - - float top = playGroundSize.y / 2; - float right = playGroundSize.x / 2; + + var top = playGroundSize.y / 2; + var right = playGroundSize.x / 2; PanelHeightPixels = panelHeight / height * mainCamera.pixelHeight; PlaySize = playGroundSize; @@ -75,13 +82,13 @@ public class Dimensions : MonoBehaviour { bottomC.points = new[] {v1, v2}; leftC.points = new[] {v3, v4}; rightC.points = new[] {v3, v4}; - + playGround.transform.rotation = Quaternion.Euler(0, 0, 90); playGround.size = new Vector2(playGroundSize.y, playGroundSize.x); - + const float lineWidth = 0.5f; - Vector3 offset = Vector3.up * lineWidth / 2; - + var offset = Vector3.up * lineWidth / 2; + topL.positionCount = bottomL.positionCount = 2; var v12 = new[] {(Vector3) v1 - offset, (Vector3) v2 - offset}; topL.SetPositions(v12); @@ -90,15 +97,9 @@ public class Dimensions : MonoBehaviour { playerPanelTop.transform.position = new Vector2(0, (height + PlaySize.y) / 4); playerPanelBottom.transform.position = new Vector2(0, -(height + PlaySize.y) / 4); - + playerPanelTop.size = playerPanelBottom.size = new Vector2(width, panelHeight); background.size = new Vector2(width, playGroundSize.y); } - - private void Update() { - if (!isUpdating) - return; - SetDimensions(); - } } diff --git a/Assets/Scripts/Game/AIPlayer.cs b/Assets/Scripts/Game/AIPlayer.cs index b8f9e72..b4a7baf 100644 --- a/Assets/Scripts/Game/AIPlayer.cs +++ b/Assets/Scripts/Game/AIPlayer.cs @@ -1,15 +1,45 @@ -using System; using System.Linq; using UnityEngine; namespace Game { public class AIPlayer : Player { - + private const float SmoothTime = 0.2f; + + private float currentSmoothV; + + private bool isApproaching; + private float lastDirection; + public Difficulty Difficulty { get; set; } - + private float FutureSeconds => (float) Difficulty * 0.5f; + private float IdlePosition => Difficulty >= Difficulty.Medium ? 0 : X; + + private float DistortAmount => 1 - 1 / ((float) Difficulty + 1); + + private void FixedUpdate() { + var dt = Time.fixedDeltaTime; + var target = GetTargetPosition(); + var h = Mathf.Max(Speed * dt, Width / 2); + goingLeft = target < X - h; + goingRight = target > X + h; + if (goingLeft || goingRight) { + isApproaching = false; + lastDirection = goingLeft ? -1 : 1; + TryLinearMove(dt); + } else { + if (!isApproaching) { + isApproaching = true; + currentSmoothV = Speed * lastDirection; + } + + ApproachPosition(target); + } + + } + // True if ball y velocity points towards player private bool BallApproaches(Ball ball) { var ballVy = ball.Rb.velocity.y; @@ -26,8 +56,8 @@ namespace Game { point = Vector2.zero; rs = Vector2.zero; - Vector2 m1 = e1 - o1; - Vector2 m2 = e2 - o2; + var m1 = e1 - o1; + var m2 = e2 - o2; /* * @@ -62,12 +92,10 @@ namespace Game { return false; } - private float IdlePosition => Difficulty >= Difficulty.Medium ? 0 : X; - // TODO Also must include fact that players have a height, maybe check the incoming angle // angle: 0 -> perpendicular, from -90 to 90 private bool IsPositionReachableInTime(float futurePosition, float seconds, float radius, float angle) { - float requiredDistance = Mathf.Abs(futurePosition - X) - Width / 2 - radius; + var requiredDistance = Mathf.Abs(futurePosition - X) - Width / 2 - radius; if (requiredDistance < 0) return true; if (Mathf.Abs(futurePosition) > Border) @@ -77,33 +105,33 @@ namespace Game { private float Simulate(Vector2 origin, Vector2 velocity, float radius, float secondsLeft, float secondsUsed, out bool ignore) { ignore = false; - Vector2 r = new Vector2(radius, radius); - Vector2 validAreaOrigin = -Dimensions.Singleton.PlaySizeBoards / 2 + r; - Vector2 validAreaSize = Dimensions.Singleton.PlaySizeBoards - r * 2; - Rect area = new Rect(validAreaOrigin, validAreaSize); + var r = new Vector2(radius, radius); + var validAreaOrigin = -Dimensions.Singleton.PlaySizeBoards / 2 + r; + var validAreaSize = Dimensions.Singleton.PlaySizeBoards - r * 2; + var area = new Rect(validAreaOrigin, validAreaSize); // Try to follow this line from origin -> end - Vector2 end = origin + velocity * secondsLeft; - + var end = origin + velocity * secondsLeft; + // Line ends in playground if (area.Contains(end)) return end.x; - float playerY = Side == Side.Bottom ? area.yMin : area.yMax; - Vector2 playerLeft = new Vector2(area.xMin, playerY); - Vector2 playerRight = new Vector2(area.xMax, playerY); + var playerY = Side == Side.Bottom ? area.yMin : area.yMax; + var playerLeft = new Vector2(area.xMin, playerY); + var playerRight = new Vector2(area.xMax, playerY); // Horizontal line (player line) -> stop simulating - if (Intersect(origin, end, playerLeft, playerRight, out Vector2 point, out Vector2 rs)) { + if (Intersect(origin, end, playerLeft, playerRight, out var point, out var rs)) { secondsUsed += secondsLeft * rs.x; if (!IsPositionReachableInTime(point.x, secondsUsed, radius, Vector2.Angle(velocity, transform.up))) ignore = true; return point.x; } - bool borderHit = false; - Vector2 borderHitPoint = Vector2.zero; - Vector2 borderRs = Vector2.zero; + var borderHit = false; + var borderHitPoint = Vector2.zero; + var borderRs = Vector2.zero; // Left vertical border if (Intersect(origin, end, new Vector2(area.xMin, area.yMin), new Vector2(area.xMin, area.yMax), out point, out rs)) { @@ -121,7 +149,7 @@ namespace Game { // Any border -> invert x velocity and simulate again from there if (borderHit) { - float secondsUsedHere = borderRs.x * secondsLeft; + var secondsUsedHere = borderRs.x * secondsLeft; secondsLeft -= secondsUsedHere; secondsUsed += secondsUsedHere; velocity = new Vector2(-velocity.x, velocity.y); @@ -134,24 +162,22 @@ namespace Game { return 0; } - private float DistortAmount => 1 - 1 / ((float) Difficulty + 1); - private float Distort(float target) { - float max = Width / 3; - float distortionOffset = DistortAmount * max; + var max = Width / 3; + var distortionOffset = DistortAmount * max; distortionOffset *= target > X ? -1 : 1; return target + distortionOffset; } private float GetTargetPosition() { var approaching = GameManager.Singleton.Balls.Where(BallApproaches).ToList(); - + while (approaching.Count > 0) { - + // Nearest by Y-Distance - Ball ball = approaching.Aggregate(approaching[0], (prev, current) => YDistanceToBall(current) < YDistanceToBall(prev) ? current : prev); + var ball = approaching.Aggregate(approaching[0], (prev, current) => YDistanceToBall(current) < YDistanceToBall(prev) ? current : prev); - float target = Simulate(ball.Rb.position, ball.Rb.velocity, ball.Radius, FutureSeconds, 0, out bool ignore); + var target = Simulate(ball.Rb.position, ball.Rb.velocity, ball.Radius, FutureSeconds, 0, out var ignore); if (!ignore) return Distort(target); @@ -162,35 +188,10 @@ namespace Game { return IdlePosition; } - private float currentSmoothV; - private const float SmoothTime = 0.2f; private void ApproachPosition(float pos) { - float result = Mathf.SmoothDamp(X, pos, ref currentSmoothV, SmoothTime, Speed); + var result = Mathf.SmoothDamp(X, pos, ref currentSmoothV, SmoothTime, Speed); transform.position = new Vector2(result, Y); ClampInsideBorders(); } - - private bool isApproaching; - private float lastDirection; - private void FixedUpdate() { - float dt = Time.fixedDeltaTime; - float target = GetTargetPosition(); - float h = Mathf.Max(Speed * dt, Width / 2); - goingLeft = target < X - h; - goingRight = target > X + h; - if (goingLeft || goingRight) { - isApproaching = false; - lastDirection = goingLeft ? -1 : 1; - TryLinearMove(dt); - } - else { - if (!isApproaching) { - isApproaching = true; - currentSmoothV = Speed * lastDirection; - } - ApproachPosition(target); - } - - } } -} \ No newline at end of file +} diff --git a/Assets/Scripts/Game/Ball.cs b/Assets/Scripts/Game/Ball.cs index 099a317..6222420 100644 --- a/Assets/Scripts/Game/Ball.cs +++ b/Assets/Scripts/Game/Ball.cs @@ -1,26 +1,29 @@ -using System; -using System.Collections; using Unity.Netcode; using UnityEngine; -using UnityEngine.Serialization; -using Random = UnityEngine.Random; namespace Game { public class Ball : NetworkBehaviour { - + + private const float SpeedGain = 1.025f; + public Rigidbody2D Rb { get; private set; } + public Player LastContactPlayer { get; private set; } + public bool IsPermanent { get; set; } private CircleCollider2D Collider { get; set; } - private const float SpeedGain = 1.025f; - public float Radius { get => transform.localScale.x * Collider.radius; set => transform.localScale = new Vector3(1, 1, 1) * value * 2; } + private void Awake() { + Rb = GetComponent(); + Collider = GetComponent(); + } + private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.TryGetComponent(out Player player)) PlayerContact(player); @@ -30,11 +33,5 @@ namespace Game { LastContactPlayer = player; Rb.velocity *= SpeedGain; } - - private void Awake() { - Rb = GetComponent(); - Collider = GetComponent(); - } - } } diff --git a/Assets/Scripts/Game/Collectable.cs b/Assets/Scripts/Game/Collectable.cs index c837b85..ffc758a 100644 --- a/Assets/Scripts/Game/Collectable.cs +++ b/Assets/Scripts/Game/Collectable.cs @@ -1,11 +1,10 @@ -using System; using Unity.Netcode; using UnityEngine; namespace Game { public abstract class Collectable : NetworkBehaviour { private void OnTriggerEnter2D(Collider2D other) { - Player player = other.GetComponent().LastContactPlayer; + var player = other.GetComponent().LastContactPlayer; if (player != null) { Destroy(gameObject); OnCollect(player); diff --git a/Assets/Scripts/Game/DeathGate.cs b/Assets/Scripts/Game/DeathGate.cs index 4b3eb1c..c855551 100644 --- a/Assets/Scripts/Game/DeathGate.cs +++ b/Assets/Scripts/Game/DeathGate.cs @@ -1,4 +1,3 @@ -using System; using UnityEngine; namespace Game { @@ -9,8 +8,8 @@ namespace Game { private Player thisPlayer, otherPlayer; private void Start() { - Player p1 = GameManager.Singleton.Player1; - Player p2 = GameManager.Singleton.Player2; + var p1 = GameManager.Singleton.Player1; + var p2 = GameManager.Singleton.Player2; if (p1.Side == side) { thisPlayer = p1; otherPlayer = p2; @@ -21,16 +20,15 @@ namespace Game { } private void OnTriggerEnter2D(Collider2D ballCollider2D) { - Ball ball = ballCollider2D.GetComponent(); - + var ball = ballCollider2D.GetComponent(); + if (ball.LastContactPlayer != null) otherPlayer.GainScore(); - + GameManager.Singleton.RemoveBall(ball); if (ball.IsPermanent) GameManager.Singleton.SpawnBall(thisPlayer, true); } - } } diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index c7e2d81..bbbf93d 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -1,44 +1,56 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Linq; using Unity.Netcode; using Unity.VisualScripting; using UnityEngine; -using UnityEngine.Assertions; using Object = UnityEngine.Object; using Random = UnityEngine.Random; namespace Game { public class GameManager : NetworkBehaviour { - public static GameManager Singleton { get; private set; } - - private void OnEnable() { - Singleton = this; - - // Move this to settings - Application.targetFrameRate = 144; - QualitySettings.vSyncCount = 0; - } - public Object ballPrefab; public Object playerPrefab; public Object modificationPrefab; public ModificationProperties[] modifications; public Object wormholePrefab; + public static GameManager Singleton { get; private set; } + public List Balls { get; } = new(); + public Player Player1 { get; private set; } + public Player Player2 { get; private set; } + private void Awake() { + SetupPlayers(); + SpawnBall(Player1, true); + } + + private void OnEnable() { + Singleton = this; + + // Move this to settings + Application.targetFrameRate = 144; + QualitySettings.vSyncCount = 0; + } + + private IEnumerator Start() { + while (Application.isPlaying) { + yield return new WaitForSeconds(1); + SpawnModification(); + } + } + private void SetupPlayers() { Settings.Type = Type.Hybrid; Settings.AIDifficulty = Difficulty.Hard; var p1Obj = Instantiate(playerPrefab); var p2Obj = Instantiate(playerPrefab); - + Player p1, p2; switch (Settings.Type) { case Type.AI: @@ -67,6 +79,7 @@ namespace Game { default: throw new ArgumentOutOfRangeException(); } + p1.Side = Side.Bottom; p2.Side = Side.Top; @@ -74,38 +87,31 @@ namespace Game { Player2 = p2; } - private void Awake() { - SetupPlayers(); - SpawnBall(Player1, true); - } - - private void SpawnWormhole() { - - } + private void SpawnWormhole() { } private void SpawnModification() { - Vector2 pos = new Vector2(Random.Range(0, Dimensions.Singleton.PlaySize.x) - Dimensions.Singleton.PlaySize.x / 2, Random.Range(-2, 2)); - ModificationProperties properties = modifications[Random.Range(0, modifications.Length)]; + var pos = new Vector2(Random.Range(0, Dimensions.Singleton.PlaySize.x) - Dimensions.Singleton.PlaySize.x / 2, Random.Range(-2, 2)); + var properties = modifications[Random.Range(0, modifications.Length)]; var mod = Instantiate(modificationPrefab, pos, Quaternion.identity).GetComponent(); mod.Properties = properties; } public void SpawnBall(Player towards, bool isPermanent) { const float startSpeed = 15; - Vector2 position = new Vector2(0, -towards.transform.position.y * 0.5f); + var position = new Vector2(0, -towards.transform.position.y * 0.5f); var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent(); ball.Rb.velocity = RandomDirectionTowards(towards) * startSpeed; ball.IsPermanent = isPermanent; ball.Radius = 0.5f; - + Balls.Add(ball); } - + private static Vector2 RandomDirectionTowards(Player player) { const float maxAngle = 45; - float radians = Random.Range(-maxAngle, maxAngle) * Mathf.PI / 180; - float x = Mathf.Sin(radians); - float y = Mathf.Cos(radians) * player.Side switch { + var radians = Random.Range(-maxAngle, maxAngle) * Mathf.PI / 180; + var x = Mathf.Sin(radians); + var y = Mathf.Cos(radians) * player.Side switch { Side.Top => 1, Side.Bottom => -1, _ => throw new ArgumentOutOfRangeException() @@ -117,6 +123,5 @@ namespace Game { Balls.Remove(ball); Destroy(ball.gameObject); } - } } diff --git a/Assets/Scripts/Game/Modification.cs b/Assets/Scripts/Game/Modification.cs index 3606c7b..6234331 100644 --- a/Assets/Scripts/Game/Modification.cs +++ b/Assets/Scripts/Game/Modification.cs @@ -1,9 +1,5 @@ -using System; using System.Collections; -using System.Collections.Generic; -using Unity.Netcode; using UnityEngine; -using UnityEngine.UIElements; namespace Game { diff --git a/Assets/Scripts/Game/ModificationProperties.cs b/Assets/Scripts/Game/ModificationProperties.cs index 46c99ae..cceb825 100644 --- a/Assets/Scripts/Game/ModificationProperties.cs +++ b/Assets/Scripts/Game/ModificationProperties.cs @@ -1,11 +1,10 @@ using UnityEngine; -using UnityEngine.Serialization; namespace Game { - + public enum ModType { Nerf, Buff } - public enum ModEffect { Speed, Border } - + public enum ModEffect { Speed, Border, Width } + [CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/ModificationProperties", order = 0)] public class ModificationProperties : ScriptableObject { public ModType type; @@ -15,5 +14,3 @@ namespace Game { public Sprite image; } } - - diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index 7893a0a..f6f73a2 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -4,64 +4,90 @@ using System.Collections.Generic; using System.Linq; using Unity.Netcode; using UnityEngine; -using UnityEngine.InputSystem; using UnityEngine.UIElements; namespace Game { - - public enum Side {Top, Bottom} - - public class Player : NetworkBehaviour { - public Side Side { get; set; } + public enum Side { Top, Bottom } + + public class Player : NetworkBehaviour { + private const float BaseSpeed = 15; + private const float SpeedMultiplier = 1.5f; + + // Unit distance from zero + private readonly float baseBorder = Dimensions.Singleton.PlaySize.x / 2 * 0.85f; + private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.15f; + + private const float BaseWidth = 6; + private const float WidthMultiplier = 1.5f; + protected bool goingLeft, goingRight; + public Side Side { get; set; } + private VisualElement Panel { get; set; } private List Modifications { get; } = new(); private int Score { get; set; } - // Units per second - private const float BaseSpeed = 15; - private const float SpeedMultiplier = 1.5f; + private float GetModified(ModEffect effect, float baseValue, float modValue, Func combineFunc, Func invertFunc) { + return Modifications.Where(m => m.effect == effect) + .Aggregate(baseValue, + (current, mod) => combineFunc(current, mod.type switch { + ModType.Buff => modValue, + ModType.Nerf => invertFunc(modValue), + _ => throw new ArgumentOutOfRangeException() + }) + ); + } + protected float Speed { get { - return Modifications.Where(m => m.effect == ModEffect.Speed) - .Aggregate(BaseSpeed, (current, speedMod) => current * speedMod.type switch { - ModType.Buff => SpeedMultiplier, - ModType.Nerf => 1 / SpeedMultiplier, - _ => throw new ArgumentOutOfRangeException() - }); + float value = GetModified(ModEffect.Speed, BaseSpeed, SpeedMultiplier, (f, f1) => f * f1, f => 1 / f); + return value; } } - - // Unit distance from zero - private readonly float baseBorder = Dimensions.Singleton.PlaySize.x / 2 * 0.85f; - private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.15f; protected float Border { get { - return Mathf.Min(Modifications.Where(m => m.effect == ModEffect.Border) - .Aggregate(baseBorder, (current, borderMod) => current + borderMod.type switch { - ModType.Buff => borderSummand, - ModType.Nerf => -borderSummand, - _ => throw new ArgumentOutOfRangeException() - }), - Dimensions.Singleton.PlaySize.x / 2 - ); + float value = GetModified(ModEffect.Border, baseBorder, borderSummand, (f, f1) => f + f1, f => -f); + return Mathf.Clamp(value, Width / 2, Dimensions.Singleton.PlaySize.x / 2); } - } + } + + protected float Width { + get { + float value = GetModified(ModEffect.Width, BaseWidth, WidthMultiplier, (f, f1) => f * f1, f => 1 / f); + return Mathf.Clamp(value, 0.1f, Dimensions.Singleton.PlaySize.x); + } + } - protected float Width => transform.localScale.x; - private float LeftEdge => X - Width / 2; + private float RightEdge => X + Width / 2; protected float X => transform.position.x; protected float Y => transform.position.y; + private void Update() { + transform.localScale = new Vector3(Width, 1, 1); + } + + protected void Start() { + var y = Side switch { + Side.Bottom => -Dimensions.Singleton.PlaySizeBoards.y / 2, + Side.Top => Dimensions.Singleton.PlaySizeBoards.y / 2, + _ => throw new ArgumentOutOfRangeException() + }; + transform.position = new Vector2(0, y); + if (Side == Side.Top) + transform.Rotate(transform.forward, 180); + Panel = GameUI.Singleton.PlayerPanel(Side); + UpdatePanel(); + } + public void GainScore() { Score++; UpdatePanel(); @@ -73,10 +99,10 @@ namespace Game { public IEnumerator ProcessModification(ModificationProperties properties) { Modifications.Add(properties); - VisualElement element = GameUI.AddModification(Panel, properties); + var element = GameUI.AddModification(Panel, properties); - float doNothingTime = properties.activeDuration * 0.7f; - float animateTime = properties.activeDuration * 0.3f; + var doNothingTime = properties.activeDuration * 0.7f; + var animateTime = properties.activeDuration * 0.3f; yield return new WaitForSeconds(doNothingTime); @@ -90,33 +116,20 @@ namespace Game { element.RemoveFromHierarchy(); Modifications.Remove(properties); } - + protected void ClampInsideBorders() { if (LeftEdge < -Border) transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World); - if (RightEdge > Border) + if (RightEdge > Border) transform.Translate(Vector2.left * (RightEdge - Border), Space.World); } protected void TryLinearMove(float h) { - Vector2 trans = new Vector2((goingLeft ? -1 : 0) + (goingRight ? 1 : 0), 0); + var trans = new Vector2((goingLeft ? -1 : 0) + (goingRight ? 1 : 0), 0); trans *= Speed * h; transform.Translate(trans, Space.World); ClampInsideBorders(); } - - protected void Start() { - float y = Side switch { - Side.Bottom => -Dimensions.Singleton.PlaySizeBoards.y / 2, - Side.Top => Dimensions.Singleton.PlaySizeBoards.y / 2, - _ => throw new ArgumentOutOfRangeException() - }; - transform.position = new Vector2(0, y); - if (Side == Side.Top) - transform.Rotate(transform.forward, 180); - Panel = GameUI.Singleton.PlayerPanel(Side); - UpdatePanel(); - } } } diff --git a/Assets/Scripts/Game/RealPlayer.cs b/Assets/Scripts/Game/RealPlayer.cs index 74fae93..b3b0966 100644 --- a/Assets/Scripts/Game/RealPlayer.cs +++ b/Assets/Scripts/Game/RealPlayer.cs @@ -24,7 +24,7 @@ namespace Game { goingRight = false; }; } - + private void FixedUpdate() { if (!isThisClient) return; @@ -33,13 +33,13 @@ namespace Game { var keyboard = Keyboard.current; switch (Side) { case Side.Top: - goingLeft = keyboard.aKey.isPressed; - goingRight = keyboard.dKey.isPressed; - break; - case Side.Bottom: goingLeft = keyboard.leftArrowKey.isPressed; goingRight = keyboard.rightArrowKey.isPressed; break; + case Side.Bottom: + goingLeft = keyboard.aKey.isPressed; + goingRight = keyboard.dKey.isPressed; + break; } } diff --git a/Assets/Scripts/Game/Settings.cs b/Assets/Scripts/Game/Settings.cs index a4dbc8b..e9e1a80 100644 --- a/Assets/Scripts/Game/Settings.cs +++ b/Assets/Scripts/Game/Settings.cs @@ -1,10 +1,10 @@ namespace Game { public enum Type { RealOnline, RealOffline, Hybrid, AI } public enum Difficulty { VeryEasy, Easy, Medium, Hard, VeryHard } - + public struct Settings { public static Type Type; public static Difficulty AIDifficulty; - }; + } } diff --git a/Assets/Scripts/Game/Wormhole.cs b/Assets/Scripts/Game/Wormhole.cs index a9e3c43..80e8a25 100644 --- a/Assets/Scripts/Game/Wormhole.cs +++ b/Assets/Scripts/Game/Wormhole.cs @@ -1,21 +1,17 @@ -using System; using Unity.Netcode; using UnityEngine; namespace Game { public class Wormhole : NetworkBehaviour { - + public float Size { get; set; } - + private void Start() { transform.localScale = new Vector3(Size, Size, Size); } private void FixedUpdate() { - foreach (var ball in GameManager.Singleton.Balls) { - - } + foreach (var ball in GameManager.Singleton.Balls) { } } - } } diff --git a/Assets/Scripts/GameUI.cs b/Assets/Scripts/GameUI.cs index e09ec77..3fe5adf 100644 --- a/Assets/Scripts/GameUI.cs +++ b/Assets/Scripts/GameUI.cs @@ -7,15 +7,21 @@ using UnityEngine.UIElements; public class GameUI : MonoBehaviour { - private UIDocument document; + public delegate void ButtonDown(Side verticalSide, string horizontalSide); + public delegate void ButtonUp(Side side, string direction); public static GameUI Singleton; + public ButtonDown buttonDown; + public ButtonUp buttonUp; + + private UIDocument document; + + private List PlayerPanels => document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")).ToList(); + private void Awake() { Singleton = this; document = GetComponent(); PreparePlayerPanels(); } - - private List PlayerPanels => document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")).ToList(); public VisualElement PlayerPanel(Side side) { return side switch { @@ -24,10 +30,10 @@ public class GameUI : MonoBehaviour { _ => throw new ArgumentOutOfRangeException(nameof(side), side, null) }; } - + public static VisualElement AddModification(VisualElement playerPanel, ModificationProperties properties) { - VisualElement listElement = ModsList(playerPanel, properties.type); - + var listElement = ModsList(playerPanel, properties.type); + VisualElement newElement = new() { style = { backgroundImage = Background.FromSprite(properties.image) @@ -47,24 +53,19 @@ public class GameUI : MonoBehaviour { } private void PreparePlayerPanels() { - float pixelHeight = Dimensions.Singleton.PanelHeightPixels; + var pixelHeight = Dimensions.Singleton.PanelHeightPixels; PlayerPanel(Side.Top).style.height = PlayerPanel(Side.Bottom).style.height = new Length(pixelHeight, LengthUnit.Pixel); - + SetupGoButton(Side.Top, "left"); SetupGoButton(Side.Top, "right"); SetupGoButton(Side.Bottom, "left"); SetupGoButton(Side.Bottom, "right"); } - - public delegate void ButtonDown(Side verticalSide, string horizontalSide); - public delegate void ButtonUp(Side side, string direction); - public ButtonDown buttonDown; - public ButtonUp buttonUp; private Label GetGoButton(Side sideVertical, string sideHorizontal) { return PlayerPanel(sideVertical).Q