From 6c146a9cbda5c82da5b08548e2fc394e43470215 Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Wed, 12 Apr 2023 10:13:36 +0200 Subject: [PATCH] death zone handles balls, score text updated --- Assets/Scenes/Game.unity | 2 ++ Assets/Scripts/DeathGate.cs | 6 ---- Assets/Scripts/Game/AIPlayer.cs | 3 +- Assets/Scripts/Game/Ball.cs | 13 ++------ Assets/Scripts/Game/DeathGate.cs | 33 +++++++++++++++++++++ Assets/Scripts/{ => Game}/DeathGate.cs.meta | 0 Assets/Scripts/Game/GameManager.cs | 28 +++++++++++------ Assets/Scripts/Game/Player.cs | 18 +++++++++-- Assets/Scripts/Game/Settings.cs | 3 +- Assets/Scripts/GameUI.cs | 8 ++--- Assets/UI Toolkit/PlayerUI.uxml | 6 ++-- 11 files changed, 81 insertions(+), 39 deletions(-) delete mode 100644 Assets/Scripts/DeathGate.cs create mode 100644 Assets/Scripts/Game/DeathGate.cs rename Assets/Scripts/{ => Game}/DeathGate.cs.meta (100%) diff --git a/Assets/Scenes/Game.unity b/Assets/Scenes/Game.unity index 2538386..a1ae6d3 100644 --- a/Assets/Scenes/Game.unity +++ b/Assets/Scenes/Game.unity @@ -452,6 +452,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 2e929ccd8ac04a4fb91c00bda3b16bb5, type: 3} m_Name: m_EditorClassIdentifier: + side: 1 --- !u!1 &1462314553 GameObject: m_ObjectHideFlags: 0 @@ -648,6 +649,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 2e929ccd8ac04a4fb91c00bda3b16bb5, type: 3} m_Name: m_EditorClassIdentifier: + side: 0 --- !u!1 &2109945866 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/DeathGate.cs b/Assets/Scripts/DeathGate.cs deleted file mode 100644 index d6216c6..0000000 --- a/Assets/Scripts/DeathGate.cs +++ /dev/null @@ -1,6 +0,0 @@ -using UnityEngine; - -// Graphical bonus to make ball going through it look cool -public class DeathGate : MonoBehaviour { - -} diff --git a/Assets/Scripts/Game/AIPlayer.cs b/Assets/Scripts/Game/AIPlayer.cs index f58fd56..7f40b8f 100644 --- a/Assets/Scripts/Game/AIPlayer.cs +++ b/Assets/Scripts/Game/AIPlayer.cs @@ -144,8 +144,7 @@ namespace Game { } private float GetTargetPosition() { - var balls = GameManager.Singleton.Balls.Where(b => b.IsAlive); - var approaching = balls.Where(BallApproaches).ToList(); + var approaching = GameManager.Singleton.Balls.Where(BallApproaches).ToList(); while (approaching.Count > 0) { diff --git a/Assets/Scripts/Game/Ball.cs b/Assets/Scripts/Game/Ball.cs index ff6adec..84f7a4a 100644 --- a/Assets/Scripts/Game/Ball.cs +++ b/Assets/Scripts/Game/Ball.cs @@ -16,9 +16,8 @@ namespace Game { get => transform.localScale.x * Collider.radius; set => transform.localScale = new Vector3(1, 1, 1) * value * 2; } - - public bool IsAlive { get; private set; } = true; - private void OnEnable() { + + private void Awake() { Rb = GetComponent(); Collider = GetComponent(); } @@ -27,12 +26,6 @@ namespace Game { Rb.velocity = new Vector2(0, 25); Rb.position = new Vector2(Random.Range(-2, 2), 0); } - - private void FixedUpdate() { - //TODO remove this and use triggers for death zone - if (Rb.position.y > Dimensions.Singleton.top || Rb.position.y < Dimensions.Singleton.bottom) { - IsAlive = false; - } - } + } } diff --git a/Assets/Scripts/Game/DeathGate.cs b/Assets/Scripts/Game/DeathGate.cs new file mode 100644 index 0000000..ef39c62 --- /dev/null +++ b/Assets/Scripts/Game/DeathGate.cs @@ -0,0 +1,33 @@ +using System; +using UnityEngine; + +namespace Game { + public class DeathGate : MonoBehaviour { + + public Side side; + + private Player thisPlayer, otherPlayer; + + private void Start() { + Player p1 = GameManager.Singleton.Player1; + Player p2 = GameManager.Singleton.Player2; + if (p1.Side == side) { + thisPlayer = p1; + otherPlayer = p2; + } else { + thisPlayer = p2; + otherPlayer = p1; + } + } + + private void OnTriggerEnter2D(Collider2D other) { + otherPlayer.GainScore(); + + Ball ball = other.GetComponent(); + GameManager.Singleton.RemoveBall(ball); + + GameManager.Singleton.SpawnBall(); + } + + } +} diff --git a/Assets/Scripts/DeathGate.cs.meta b/Assets/Scripts/Game/DeathGate.cs.meta similarity index 100% rename from Assets/Scripts/DeathGate.cs.meta rename to Assets/Scripts/Game/DeathGate.cs.meta diff --git a/Assets/Scripts/Game/GameManager.cs b/Assets/Scripts/Game/GameManager.cs index 6e9a9ea..d2abe6b 100644 --- a/Assets/Scripts/Game/GameManager.cs +++ b/Assets/Scripts/Game/GameManager.cs @@ -24,6 +24,8 @@ namespace Game { public ModificationProperties[] modifications; public List Balls { get; } = new(); + public Player Player1 { get; private set; } + public Player Player2 { get; private set; } private static void Tests() { var v1 = new Vector2(1, 3); @@ -57,7 +59,7 @@ namespace Game { private void Awake() { Settings.Type = Type.Hybrid; - Settings.AIDifficulty = Difficulty.VeryHard; + Settings.AIDifficulty = Difficulty.VeryEasy; var ball = Instantiate(ballPrefab).GetComponent(); Balls.Add(ball); @@ -74,7 +76,7 @@ namespace Game { ((AIPlayer) p1).Difficulty = Settings.AIDifficulty; ((AIPlayer) p2).Difficulty = Settings.AIDifficulty; break; - case Type.Real: + case Type.RealOnline: p1 = p1Obj.AddComponent(); p2 = p2Obj.AddComponent(); ((RealPlayer) p1).isThisClient = true; @@ -91,6 +93,9 @@ namespace Game { p1.Side = Side.Bottom; p2.Side = Side.Top; + Player1 = p1; + Player2 = p2; + Tests(); } @@ -108,14 +113,19 @@ namespace Game { mod.Properties = properties; } - private void FixedUpdate() { - foreach (var ball in Balls.Where(b => !b.IsAlive)) - Destroy(ball.gameObject); + public void SpawnBall() { + Vector2 position = Vector2.zero; + Vector2 velocity = Vector2.up; + var ball = Instantiate(ballPrefab, position, Quaternion.identity).GetComponent(); + var rb = ball.Rb; + rb.velocity = velocity; - int count = Balls.RemoveAll(b => !b.IsAlive); - for (int _ = 0; _ < count; _++) { - Balls.Add(Instantiate(ballPrefab).GetComponent()); - } + Balls.Add(ball); + } + + public void RemoveBall(Ball ball) { + Balls.Remove(ball); + Destroy(ball.gameObject); } } diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index 1e0cc91..f756b2e 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 System.Transactions; using Unity.Netcode; +using UnityEditor; +using UnityEditor.UIElements; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.UIElements; @@ -14,14 +17,15 @@ namespace Game { public Side Side { get; set; } - private int score; - protected bool goingLeft, goingRight; public VisualElement Panel { get; private set; } public List Modifications { get; } = new(); + [SerializeField] + public int score = 0; + // Units per second protected float Speed => 15; @@ -37,6 +41,15 @@ namespace Game { protected float Y => transform.position.y; + public void GainScore() { + score++; + UpdatePanel(); + } + + private void UpdatePanel() { + Panel.Q("score").text = score.ToString(); + } + protected void ClampInsideBorders() { if (LeftEdge < -Border) transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World); @@ -61,6 +74,7 @@ namespace Game { if (Side == Side.Top) transform.Rotate(transform.forward, 180); Panel = GameUI.Singleton.PlayerPanel(Side); + UpdatePanel(); } private void OnCollisionEnter2D(Collision2D other) { diff --git a/Assets/Scripts/Game/Settings.cs b/Assets/Scripts/Game/Settings.cs index ac3b1c8..a4dbc8b 100644 --- a/Assets/Scripts/Game/Settings.cs +++ b/Assets/Scripts/Game/Settings.cs @@ -1,6 +1,7 @@ namespace Game { - public enum Type { Real, Hybrid, AI } + public enum Type { RealOnline, RealOffline, Hybrid, AI } public enum Difficulty { VeryEasy, Easy, Medium, Hard, VeryHard } + public struct Settings { public static Type Type; diff --git a/Assets/Scripts/GameUI.cs b/Assets/Scripts/GameUI.cs index ac21195..8029157 100644 --- a/Assets/Scripts/GameUI.cs +++ b/Assets/Scripts/GameUI.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using Game; +using UnityEditor.UIElements; using UnityEngine; using UnityEngine.Assertions; using UnityEngine.UIElements; @@ -26,7 +27,6 @@ public class GameUI : MonoBehaviour { }; } - // 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); @@ -49,11 +49,7 @@ public class GameUI : MonoBehaviour { } void PreparePlayerPanels() { - var players = PlayerPanels; - Assert.AreEqual(players.Count, 2); - float heightPercentage = Dimensions.Singleton.panelHeightPercentage; - foreach (var playerPanel in players) - playerPanel.style.height = Length.Percent(heightPercentage); + PlayerPanel(Side.Top).style.height = PlayerPanel(Side.Bottom).style.height = Length.Percent(heightPercentage); } } diff --git a/Assets/UI Toolkit/PlayerUI.uxml b/Assets/UI Toolkit/PlayerUI.uxml index 682e9f7..bae79fb 100644 --- a/Assets/UI Toolkit/PlayerUI.uxml +++ b/Assets/UI Toolkit/PlayerUI.uxml @@ -1,9 +1,9 @@