From 2bc4e754b71cfbfa99026fc69d3cc99eb0a9067a Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Tue, 25 Apr 2023 16:48:24 +0200 Subject: [PATCH] Refactoring --- Assets/Scripts/Dimensions.cs | 105 ----------------- Assets/Scripts/Game/AIPlayer.cs | 1 + Assets/Scripts/Game/BalanceValues.cs | 1 + Assets/Scripts/Game/Dimensions.cs | 107 ++++++++++++++++++ Assets/Scripts/{ => Game}/Dimensions.cs.meta | 0 Assets/Scripts/Game/GameManager.cs | 2 + Assets/Scripts/Game/GameUI.cs | 76 +++++++++++++ Assets/Scripts/{ => Game}/GameUI.cs.meta | 0 Assets/Scripts/GameUI.cs | 75 ------------ Assets/Scripts/Global.meta | 3 + .../{ => Global}/NetworkCommandLine.cs | 0 .../{ => Global}/NetworkCommandLine.cs.meta | 0 .../Scripts/{Game => Global}/RoomSettings.cs | 2 +- .../{Game => Global}/RoomSettings.cs.meta | 0 Assets/Scripts/Menu.meta | 3 + Assets/Scripts/Menu/MenuUI.cs | 66 +++++++++++ Assets/Scripts/{ => Menu}/MenuUI.cs.meta | 0 Assets/Scripts/Menu/RoomUI.cs | 5 + Assets/Scripts/Menu/RoomUI.cs.meta | 3 + Assets/Scripts/MenuUI.cs | 66 ----------- 20 files changed, 268 insertions(+), 247 deletions(-) delete mode 100644 Assets/Scripts/Dimensions.cs create mode 100644 Assets/Scripts/Game/Dimensions.cs rename Assets/Scripts/{ => Game}/Dimensions.cs.meta (100%) create mode 100644 Assets/Scripts/Game/GameUI.cs rename Assets/Scripts/{ => Game}/GameUI.cs.meta (100%) delete mode 100644 Assets/Scripts/GameUI.cs create mode 100644 Assets/Scripts/Global.meta rename Assets/Scripts/{ => Global}/NetworkCommandLine.cs (100%) rename Assets/Scripts/{ => Global}/NetworkCommandLine.cs.meta (100%) rename Assets/Scripts/{Game => Global}/RoomSettings.cs (97%) rename Assets/Scripts/{Game => Global}/RoomSettings.cs.meta (100%) create mode 100644 Assets/Scripts/Menu.meta create mode 100644 Assets/Scripts/Menu/MenuUI.cs rename Assets/Scripts/{ => Menu}/MenuUI.cs.meta (100%) create mode 100644 Assets/Scripts/Menu/RoomUI.cs create mode 100644 Assets/Scripts/Menu/RoomUI.cs.meta delete mode 100644 Assets/Scripts/MenuUI.cs diff --git a/Assets/Scripts/Dimensions.cs b/Assets/Scripts/Dimensions.cs deleted file mode 100644 index 9c976d3..0000000 --- a/Assets/Scripts/Dimensions.cs +++ /dev/null @@ -1,105 +0,0 @@ -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; - public SpriteRenderer playerPanelTop; - public SpriteRenderer playerPanelBottom; - public SpriteRenderer background; - - 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(); - SetDimensions(); - if (Application.isPlaying) - isUpdating = false; - } - - private void Update() { - if (!isUpdating) - return; - SetDimensions(); - } - - private void GetComponents() { - topL = topC.GetComponent(); - bottomL = bottomC.GetComponent(); - } - - private void SetDimensions() { - 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; - - var top = playGroundSize.y / 2; - var right = playGroundSize.x / 2; - - PanelHeightPixels = panelHeight / height * mainCamera.pixelHeight; - PlaySize = playGroundSize; - PlaySizeBoards = playGroundSize - new Vector2(0, emptySpaceHeight * 2); - - Vector2 v1 = new Vector3(-right, 0); - Vector2 v2 = new Vector3(right, 0); - Vector2 v3 = new Vector3(0, top); - Vector2 v4 = new Vector3(0, -top); - - topC.transform.position = new Vector2(0, top); - bottomC.transform.position = new Vector2(0, -top); - leftC.transform.position = new Vector2(-right, 0); - rightC.transform.position = new Vector2(right, 0); - topC.points = new[] {v1, v2}; - 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; - var offset = Vector3.up * lineWidth / 2; - - topL.positionCount = bottomL.positionCount = 2; - var v12 = new[] {(Vector3) v1 - offset, (Vector3) v2 - offset}; - topL.SetPositions(v12); - v12 = new[] {(Vector3) v1 + offset, (Vector3) v2 + offset}; - bottomL.SetPositions(v12); - - 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); - } -} diff --git a/Assets/Scripts/Game/AIPlayer.cs b/Assets/Scripts/Game/AIPlayer.cs index 99d77da..316f4cd 100644 --- a/Assets/Scripts/Game/AIPlayer.cs +++ b/Assets/Scripts/Game/AIPlayer.cs @@ -1,4 +1,5 @@ using System.Linq; +using Global; using UnityEngine; using UnityEngine.UIElements; diff --git a/Assets/Scripts/Game/BalanceValues.cs b/Assets/Scripts/Game/BalanceValues.cs index bae503f..dc20a6f 100644 --- a/Assets/Scripts/Game/BalanceValues.cs +++ b/Assets/Scripts/Game/BalanceValues.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.Remoting.Messaging; +using Global; using UnityEngine; namespace Game { diff --git a/Assets/Scripts/Game/Dimensions.cs b/Assets/Scripts/Game/Dimensions.cs new file mode 100644 index 0000000..f1f0d2d --- /dev/null +++ b/Assets/Scripts/Game/Dimensions.cs @@ -0,0 +1,107 @@ +using UnityEngine; +using UnityEngine.Serialization; + +namespace Game { + [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; + public SpriteRenderer playerPanelTop; + public SpriteRenderer playerPanelBottom; + public SpriteRenderer background; + + 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(); + SetDimensions(); + if (Application.isPlaying) + isUpdating = false; + } + + private void Update() { + if (!isUpdating) + return; + SetDimensions(); + } + + private void GetComponents() { + topL = topC.GetComponent(); + bottomL = bottomC.GetComponent(); + } + + private void SetDimensions() { + 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; + + var top = playGroundSize.y / 2; + var right = playGroundSize.x / 2; + + PanelHeightPixels = panelHeight / height * mainCamera.pixelHeight; + PlaySize = playGroundSize; + PlaySizeBoards = playGroundSize - new Vector2(0, emptySpaceHeight * 2); + + Vector2 v1 = new Vector3(-right, 0); + Vector2 v2 = new Vector3(right, 0); + Vector2 v3 = new Vector3(0, top); + Vector2 v4 = new Vector3(0, -top); + + topC.transform.position = new Vector2(0, top); + bottomC.transform.position = new Vector2(0, -top); + leftC.transform.position = new Vector2(-right, 0); + rightC.transform.position = new Vector2(right, 0); + topC.points = new[] {v1, v2}; + 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; + var offset = Vector3.up * lineWidth / 2; + + topL.positionCount = bottomL.positionCount = 2; + var v12 = new[] {(Vector3) v1 - offset, (Vector3) v2 - offset}; + topL.SetPositions(v12); + v12 = new[] {(Vector3) v1 + offset, (Vector3) v2 + offset}; + bottomL.SetPositions(v12); + + 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); + } + } +} diff --git a/Assets/Scripts/Dimensions.cs.meta b/Assets/Scripts/Game/Dimensions.cs.meta similarity index 100% rename from Assets/Scripts/Dimensions.cs.meta rename to Assets/Scripts/Game/Dimensions.cs.meta diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index 1a84152..541b86d 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -2,11 +2,13 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using Global; using Unity.Netcode; using Unity.VisualScripting; using UnityEngine; using Object = UnityEngine.Object; using Random = UnityEngine.Random; +using Type = Global.Type; namespace Game { public class GameManager : NetworkBehaviour { diff --git a/Assets/Scripts/Game/GameUI.cs b/Assets/Scripts/Game/GameUI.cs new file mode 100644 index 0000000..64cbfb9 --- /dev/null +++ b/Assets/Scripts/Game/GameUI.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using UnityEngine.UIElements; + +namespace Game { + public class GameUI : MonoBehaviour { + + 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(); + } + + public VisualElement PlayerPanel(Side side) { + return side switch { + Side.Top => PlayerPanels[0], + Side.Bottom => PlayerPanels[1], + _ => throw new ArgumentOutOfRangeException(nameof(side), side, null) + }; + } + + public static VisualElement AddModification(VisualElement playerPanel, ModificationProperties properties) { + var listElement = ModsList(playerPanel, properties.type); + + VisualElement newElement = new() { + style = { + backgroundImage = Background.FromSprite(properties.image) + } + }; + listElement.Add(newElement); + + return newElement; + } + + private static VisualElement ModsList(VisualElement player, ModType type) { + return type switch { + ModType.Buff => player.Children().First(e => e.ClassListContains("mods_list")), + ModType.Nerf => player.Children().Last(e => e.ClassListContains("mods_list")), + _ => throw new ArgumentOutOfRangeException(nameof(type), type, null) + }; + } + + private void PreparePlayerPanels() { + 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 Label GetGoButton(Side sideVertical, string sideHorizontal) { + return PlayerPanel(sideVertical).Q