From 896ab23c09ec8ec90ac2f4026fe93c81872eca40 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Fri, 14 Apr 2023 21:56:59 +0200 Subject: [PATCH] better collectables and modifications --- Assets/Scripts/Game/Collectable.cs | 17 ++++++++++++ Assets/Scripts/Game/Collectable.cs.meta | 3 ++ Assets/Scripts/Game/Modification.cs | 37 ++----------------------- Assets/Scripts/Game/Player.cs | 32 +++++++++++++++++---- Assets/Scripts/GameUI.cs | 6 ++-- README.md | 1 - 6 files changed, 53 insertions(+), 43 deletions(-) create mode 100644 Assets/Scripts/Game/Collectable.cs create mode 100644 Assets/Scripts/Game/Collectable.cs.meta diff --git a/Assets/Scripts/Game/Collectable.cs b/Assets/Scripts/Game/Collectable.cs new file mode 100644 index 0000000..c837b85 --- /dev/null +++ b/Assets/Scripts/Game/Collectable.cs @@ -0,0 +1,17 @@ +using System; +using Unity.Netcode; +using UnityEngine; + +namespace Game { + public abstract class Collectable : NetworkBehaviour { + private void OnTriggerEnter2D(Collider2D other) { + Player player = other.GetComponent().LastContactPlayer; + if (player != null) { + Destroy(gameObject); + OnCollect(player); + } + } + + protected abstract void OnCollect(Player collector); + } +} diff --git a/Assets/Scripts/Game/Collectable.cs.meta b/Assets/Scripts/Game/Collectable.cs.meta new file mode 100644 index 0000000..19ada1d --- /dev/null +++ b/Assets/Scripts/Game/Collectable.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3b0f221ad4da44aabc6b7164ed4a11be +timeCreated: 1681499802 \ No newline at end of file diff --git a/Assets/Scripts/Game/Modification.cs b/Assets/Scripts/Game/Modification.cs index 651e119..1a2f513 100644 --- a/Assets/Scripts/Game/Modification.cs +++ b/Assets/Scripts/Game/Modification.cs @@ -10,33 +10,7 @@ namespace Game { public enum ModType { Nerf, Buff } public enum ModEffect { Speed, Border } - public class ActiveModification { - public ModificationProperties Properties { get; set; } - - public IEnumerator Process(VisualElement panel, List modifications) { - modifications.Add(this); - VisualElement element = GameUI.AddModification(panel, this); - - float doNothingTime = Properties.activeDuration * 0.7f; - float animateTime = Properties.activeDuration * 0.3f; - - yield return new WaitForSeconds(doNothingTime); - - float current = 0; - while (current < animateTime) { - current += Time.deltaTime; - element.style.opacity = new StyleFloat(1 - current / animateTime); - yield return null; - } - - element.RemoveFromHierarchy(); - modifications.Remove(this); - } - - - } - - public class Modification : NetworkBehaviour { + public class Modification : Collectable { public ModificationProperties Properties { get; set; } @@ -46,13 +20,8 @@ namespace Game { Destroy(gameObject); } - private void OnTriggerEnter2D(Collider2D other) { - Player p = other.GetComponent().LastContactPlayer; - if (p != null) { - Destroy(gameObject); - ActiveModification mod = new() {Properties = Properties}; - p.StartCoroutine(mod.Process(p.Panel, p.Modifications)); - } + protected override void OnCollect(Player collector) { + collector.StartCoroutine(collector.ProcessModification(Properties)); } } } diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index 8fee024..b18f7b7 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.Netcode; @@ -18,7 +19,7 @@ namespace Game { public VisualElement Panel { get; private set; } - public List Modifications { get; } = new(); + private List Modifications { get; } = new(); private int Score { get; set; } @@ -27,8 +28,8 @@ namespace Game { private const float SpeedMultiplier = 1.5f; protected float Speed { get { - return Modifications.Where(m => m.Properties.effect == ModEffect.Speed) - .Aggregate(BaseSpeed, (current, speedMod) => current * speedMod.Properties.type switch { + 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() @@ -41,8 +42,8 @@ namespace Game { private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.1f; protected float Border { get { - return Mathf.Min(Modifications.Where(m => m.Properties.effect == ModEffect.Border) - .Aggregate(baseBorder, (current, borderMod) => current + borderMod.Properties.type switch { + 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() @@ -70,6 +71,27 @@ namespace Game { Panel.Q("score").text = Score.ToString(); } + public IEnumerator ProcessModification(ModificationProperties properties) { + Modifications.Add(properties); + VisualElement element = GameUI.AddModification(Panel, properties); + + float doNothingTime = properties.activeDuration * 0.7f; + float animateTime = properties.activeDuration * 0.3f; + + yield return new WaitForSeconds(doNothingTime); + + float current = 0; + while (current < animateTime) { + current += Time.deltaTime; + element.style.opacity = new StyleFloat(1 - current / animateTime); + yield return null; + } + + element.RemoveFromHierarchy(); + Modifications.Remove(properties); + } + + protected void ClampInsideBorders() { if (LeftEdge < -Border) transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World); diff --git a/Assets/Scripts/GameUI.cs b/Assets/Scripts/GameUI.cs index 3463756..e09ec77 100644 --- a/Assets/Scripts/GameUI.cs +++ b/Assets/Scripts/GameUI.cs @@ -25,12 +25,12 @@ public class GameUI : MonoBehaviour { }; } - public static VisualElement AddModification(VisualElement playerPanel, ActiveModification modification) { - VisualElement listElement = ModsList(playerPanel, modification.Properties.type); + public static VisualElement AddModification(VisualElement playerPanel, ModificationProperties properties) { + VisualElement listElement = ModsList(playerPanel, properties.type); VisualElement newElement = new() { style = { - backgroundImage = Background.FromSprite(modification.Properties.image) + backgroundImage = Background.FromSprite(properties.image) } }; listElement.Add(newElement); diff --git a/README.md b/README.md index 9444864..189bbef 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,5 @@ - NewBall - Wormhole - Border visuals -- Ball visuals ### Menu