diff --git a/Assets/Scripts/Game/Ball.cs b/Assets/Scripts/Game/Ball.cs index 699d7af..ff6adec 100644 --- a/Assets/Scripts/Game/Ball.cs +++ b/Assets/Scripts/Game/Ball.cs @@ -1,6 +1,7 @@ using System; using Unity.Netcode; using UnityEngine; +using UnityEngine.Serialization; using Random = UnityEngine.Random; namespace Game { @@ -9,6 +10,8 @@ namespace Game { private CircleCollider2D Collider { get; set; } + public Player LastContactPlayer { get; set; } + public float Radius { get => transform.localScale.x * Collider.radius; set => transform.localScale = new Vector3(1, 1, 1) * value * 2; diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index 2db028b..fda55aa 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -52,7 +52,7 @@ namespace Game { Assert.AreApproximatelyEqual(rs.y, 0.5f); } - private void Start() { + private void Awake() { Settings.Type = Type.Hybrid; Settings.AIDifficulty = Difficulty.VeryHard; diff --git a/Assets/Scripts/Game/Modification.cs b/Assets/Scripts/Game/Modification.cs index becf739..b04fd20 100644 --- a/Assets/Scripts/Game/Modification.cs +++ b/Assets/Scripts/Game/Modification.cs @@ -1,15 +1,55 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using Unity.Netcode; using UnityEngine; +using UnityEngine.UIElements; namespace Game { - public class Modification : MonoBehaviour { - + + public enum ModType { Nerf, Buff } + public enum ModEffect { Speed, Border } + + public struct ModificationProperties { + public ModType Type { get; set; } + public ModEffect Effect { get; set; } + + public float PickupDuration { get; set; } + + public float ActiveDuration { get; set; } } - public class SpeedModification : Modification { - + public class ActiveModification { + public ModificationProperties Properties { get; set; } + + public IEnumerator Process(VisualElement panel, List modifications) { + modifications.Add(this); + VisualElement element = GameUI.AddModification(panel, this); + + yield return new WaitForSeconds(Properties.ActiveDuration); + + element.RemoveFromHierarchy(); + modifications.Remove(this); + } } + + public class Modification : NetworkBehaviour { - public class BorderModification : Modification { - + public ModificationProperties Properties { get; set; } + + private IEnumerator Start() { + yield return new WaitForSeconds(Properties.PickupDuration); + Destroy(gameObject); + } + + private void OnTriggerEnter2D(Collider2D other) { + Player p = other.GetComponent().LastContactPlayer; + if (p != null) { + Destroy(gameObject); + ActiveModification mod = new() {Properties = Properties}; + StartCoroutine(mod.Process(p.Panel, p.Modifications)); + } + } } + } diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index 4324700..1e0cc91 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -1,7 +1,10 @@ using System; +using System.Collections; +using System.Collections.Generic; using Unity.Netcode; using UnityEngine; using UnityEngine.InputSystem; +using UnityEngine.UIElements; namespace Game { @@ -15,14 +18,15 @@ namespace Game { protected bool goingLeft, goingRight; + public VisualElement Panel { get; private set; } + + public List Modifications { get; } = new(); + // Units per second protected float Speed => 15; // Unit distance from zero protected float Border => Dimensions.Singleton.Width / 2; - - private SpeedModification speedModification; - private BorderModification borderModification; protected float Width => transform.localScale.x; @@ -56,6 +60,11 @@ namespace Game { transform.position = new Vector2(0, y); if (Side == Side.Top) transform.Rotate(transform.forward, 180); + Panel = GameUI.Singleton.PlayerPanel(Side); + } + + private void OnCollisionEnter2D(Collision2D other) { + other.gameObject.GetComponent().LastContactPlayer = this; } } diff --git a/Assets/Scripts/GameUI.cs b/Assets/Scripts/GameUI.cs index ffe78c7..c61e4db 100644 --- a/Assets/Scripts/GameUI.cs +++ b/Assets/Scripts/GameUI.cs @@ -1,21 +1,51 @@ using System; +using System.Collections.Generic; using System.Linq; +using Game; using UnityEngine; +using UnityEngine.Assertions; using UnityEngine.UIElements; public class GameUI : MonoBehaviour { private UIDocument document; - private void OnEnable() { + public static GameUI Singleton; + 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 { + Side.Top => PlayerPanels[0], + Side.Bottom => PlayerPanels[1], + _ => throw new ArgumentOutOfRangeException(nameof(side), side, null) + }; + } + + // TODO create correct visual element, attach it to panel with background-image and return it + public static VisualElement AddModification(VisualElement playerPanel, ActiveModification modification) { + VisualElement listElement = ModsList(playerPanel, modification.Properties.Type); + + return listElement; + } + + 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) + }; + } void PreparePlayerPanels() { - var players = document.rootVisualElement.Children().Where(e => e.ClassListContains("player_panel")); + var players = PlayerPanels; + Assert.AreEqual(players.Count, 2); float heightPercentage = Dimensions.Singleton.panelHeightPercentage; - foreach (var playerPanel in players) playerPanel.style.height = Length.Percent(heightPercentage); } diff --git a/Assets/UI Toolkit/player_panel.uss b/Assets/UI Toolkit/player_panel.uss index 1d9b733..08f9af2 100644 --- a/Assets/UI Toolkit/player_panel.uss +++ b/Assets/UI Toolkit/player_panel.uss @@ -19,7 +19,7 @@ Button { margin-bottom: 5px; } -.modification { +.mods_list > VisualElement { width: 100%; flex-grow: 1; background-image: url('project://database/Packages/com.unity.2d.sprite/Editor/ObjectMenuCreation/DefaultAssets/Textures/HexagonFlat-Top.png?fileID=2747690628134850419&guid=16037dbd798344f57a526d53d48f5ead&type=3#Hexagon Flat-Top');