Refactoring

main
Benjamin Kraft 1 year ago
parent 741bb7fbc0
commit 2bc4e754b7
  1. 105
      Assets/Scripts/Dimensions.cs
  2. 1
      Assets/Scripts/Game/AIPlayer.cs
  3. 1
      Assets/Scripts/Game/BalanceValues.cs
  4. 107
      Assets/Scripts/Game/Dimensions.cs
  5. 0
      Assets/Scripts/Game/Dimensions.cs.meta
  6. 2
      Assets/Scripts/Game/GameManager.cs
  7. 76
      Assets/Scripts/Game/GameUI.cs
  8. 0
      Assets/Scripts/Game/GameUI.cs.meta
  9. 75
      Assets/Scripts/GameUI.cs
  10. 3
      Assets/Scripts/Global.meta
  11. 0
      Assets/Scripts/Global/NetworkCommandLine.cs
  12. 0
      Assets/Scripts/Global/NetworkCommandLine.cs.meta
  13. 2
      Assets/Scripts/Global/RoomSettings.cs
  14. 0
      Assets/Scripts/Global/RoomSettings.cs.meta
  15. 3
      Assets/Scripts/Menu.meta
  16. 66
      Assets/Scripts/Menu/MenuUI.cs
  17. 0
      Assets/Scripts/Menu/MenuUI.cs.meta
  18. 5
      Assets/Scripts/Menu/RoomUI.cs
  19. 3
      Assets/Scripts/Menu/RoomUI.cs.meta
  20. 66
      Assets/Scripts/MenuUI.cs

@ -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<LineRenderer>();
bottomL = bottomC.GetComponent<LineRenderer>();
}
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);
}
}

@ -1,4 +1,5 @@
using System.Linq; using System.Linq;
using Global;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;

@ -1,5 +1,6 @@
using System; using System;
using System.Runtime.Remoting.Messaging; using System.Runtime.Remoting.Messaging;
using Global;
using UnityEngine; using UnityEngine;
namespace Game { namespace Game {

@ -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<LineRenderer>();
bottomL = bottomC.GetComponent<LineRenderer>();
}
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);
}
}
}

@ -2,11 +2,13 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Global;
using Unity.Netcode; using Unity.Netcode;
using Unity.VisualScripting; using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using Object = UnityEngine.Object; using Object = UnityEngine.Object;
using Random = UnityEngine.Random; using Random = UnityEngine.Random;
using Type = Global.Type;
namespace Game { namespace Game {
public class GameManager : NetworkBehaviour { public class GameManager : NetworkBehaviour {

@ -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<VisualElement> PlayerPanels => document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")).ToList();
private void Awake() {
Singleton = this;
document = GetComponent<UIDocument>();
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<Label>("go_" + sideHorizontal);
}
private void SetupGoButton(Side sideVertical, string sideHorizontal) {
var element = GetGoButton(sideVertical, sideHorizontal);
element.RegisterCallback<PointerDownEvent>(_ => buttonDown(sideVertical, sideHorizontal));
element.RegisterCallback<PointerUpEvent>(_ => buttonUp(sideVertical, sideHorizontal));
element.RegisterCallback<PointerLeaveEvent>(_ => buttonUp(sideVertical, sideHorizontal));
}
}
}

@ -1,75 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Game;
using UnityEngine;
using UnityEngine.UIElements;
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<VisualElement> PlayerPanels => document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")).ToList();
private void Awake() {
Singleton = this;
document = GetComponent<UIDocument>();
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<Label>("go_" + sideHorizontal);
}
private void SetupGoButton(Side sideVertical, string sideHorizontal) {
var element = GetGoButton(sideVertical, sideHorizontal);
element.RegisterCallback<PointerDownEvent>(_ => buttonDown(sideVertical, sideHorizontal));
element.RegisterCallback<PointerUpEvent>(_ => buttonUp(sideVertical, sideHorizontal));
element.RegisterCallback<PointerLeaveEvent>(_ => buttonUp(sideVertical, sideHorizontal));
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 93c5f73e1f8b4c8e8bf3fd5441f57118
timeCreated: 1682433867

@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Game { namespace Global {
public enum Type { RealOnline, RealOffline, Hybrid, AI, RealOnlineRanked } public enum Type { RealOnline, RealOffline, Hybrid, AI, RealOnlineRanked }
public enum Difficulty { VeryEasy, Easy, Medium, Hard, VeryHard } public enum Difficulty { VeryEasy, Easy, Medium, Hard, VeryHard }
[Serializable] [Serializable]

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c8417b25b37d4910b8ba034e65d026fc
timeCreated: 1682433665

@ -0,0 +1,66 @@
using Game;
using Global;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
namespace Menu {
public class MenuUI : MonoBehaviour {
private VisualElement mainMenu, playMenu, settingsMenu;
private VisualElement root;
private void OnEnable() {
root = GetComponent<UIDocument>().rootVisualElement;
mainMenu = root.Q("main_menu");
playMenu = root.Q("play_menu");
settingsMenu = root.Q("settings_menu");
mainMenu.Q("btn_play").RegisterCallback<ClickEvent>(PlayPressed);
mainMenu.Q("btn_settings").RegisterCallback<ClickEvent>(SettingsPressed);
playMenu.Q("btn_find").RegisterCallback<ClickEvent>(FindPressed);
playMenu.Q("btn_host").RegisterCallback<ClickEvent>(HostPressed);
playMenu.Q("btn_join").RegisterCallback<ClickEvent>(JoinPressed);
playMenu.Q("btn_bot").RegisterCallback<ClickEvent>(BotPressed);
playMenu.Q("btn_bots").RegisterCallback<ClickEvent>(BotsPressed);
}
private void PlayPressed(ClickEvent evt) {
mainMenu.style.display = DisplayStyle.None;
playMenu.style.display = DisplayStyle.Flex;
RoomSettings.SpawnRates[Spawnable.Modification] = SpawnRate.Many;
RoomSettings.SpawnRates[Spawnable.NewBallPermanent] = SpawnRate.Many;
RoomSettings.SpawnRates[Spawnable.NewBallTemporary] = SpawnRate.Few;
RoomSettings.SpawnRates[Spawnable.Wormhole] = SpawnRate.Many;
}
private void SettingsPressed(ClickEvent evt) {
mainMenu.style.display = DisplayStyle.None;
settingsMenu.style.display = DisplayStyle.Flex;
}
private void FindPressed(ClickEvent evt) { }
private void HostPressed(ClickEvent evt) { }
private void JoinPressed(ClickEvent evt) { }
private void BotPressed(ClickEvent evt) {
RoomSettings.Type = Type.Hybrid;
RoomSettings.AIDifficulty = Difficulty.VeryHard;
SceneManager.LoadScene("Game");
}
private void BotsPressed(ClickEvent evt) {
RoomSettings.Type = Type.AI;
RoomSettings.AIDifficulty = Difficulty.VeryHard;
SceneManager.LoadScene("Game");
}
}
}

@ -0,0 +1,5 @@
namespace Menu {
public class RoomUI {
}
}

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1019f607711d4756a89c2d56e4edfc9b
timeCreated: 1682433830

@ -1,66 +0,0 @@
using Game;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
public class MenuUI : MonoBehaviour {
private VisualElement mainMenu, playMenu, settingsMenu;
private VisualElement root;
private void OnEnable() {
root = GetComponent<UIDocument>().rootVisualElement;
mainMenu = root.Q("main_menu");
playMenu = root.Q("play_menu");
settingsMenu = root.Q("settings_menu");
mainMenu.Q("btn_play").RegisterCallback<ClickEvent>(PlayPressed);
mainMenu.Q("btn_settings").RegisterCallback<ClickEvent>(SettingsPressed);
playMenu.Q("btn_find").RegisterCallback<ClickEvent>(FindPressed);
playMenu.Q("btn_host").RegisterCallback<ClickEvent>(HostPressed);
playMenu.Q("btn_join").RegisterCallback<ClickEvent>(JoinPressed);
playMenu.Q("btn_bot").RegisterCallback<ClickEvent>(BotPressed);
playMenu.Q("btn_bots").RegisterCallback<ClickEvent>(BotsPressed);
// PlayPressed(null);
// BotPressed(null);
}
private void PlayPressed(ClickEvent evt) {
mainMenu.style.display = DisplayStyle.None;
playMenu.style.display = DisplayStyle.Flex;
RoomSettings.SpawnRates[Spawnable.Modification] = SpawnRate.Many;
RoomSettings.SpawnRates[Spawnable.NewBallPermanent] = SpawnRate.Many;
RoomSettings.SpawnRates[Spawnable.NewBallTemporary] = SpawnRate.Few;
RoomSettings.SpawnRates[Spawnable.Wormhole] = SpawnRate.Many;
}
private void SettingsPressed(ClickEvent evt) {
mainMenu.style.display = DisplayStyle.None;
settingsMenu.style.display = DisplayStyle.Flex;
}
private void FindPressed(ClickEvent evt) { }
private void HostPressed(ClickEvent evt) { }
private void JoinPressed(ClickEvent evt) { }
private void BotPressed(ClickEvent evt) {
RoomSettings.Type = Type.Hybrid;
RoomSettings.AIDifficulty = Difficulty.VeryHard;
SceneManager.LoadScene("Game");
}
private void BotsPressed(ClickEvent evt) {
RoomSettings.Type = Type.AI;
RoomSettings.AIDifficulty = Difficulty.VeryHard;
SceneManager.LoadScene("Game");
}
}
Loading…
Cancel
Save