From 1ea09bddd05bdf63c82018417a3b6ba47688878e Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Wed, 12 Apr 2023 23:27:56 +0200 Subject: [PATCH] button functionality --- Assets/Scripts/Game/Player.cs | 22 +++++++++++++++++++--- Assets/Scripts/GameUI.cs | 22 +++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index fa74bbc..3c3bd77 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -18,7 +18,7 @@ namespace Game { public class Player : NetworkBehaviour { public Side Side { get; set; } - + protected bool goingLeft, goingRight; public VisualElement Panel { get; private set; } @@ -100,6 +100,22 @@ namespace Game { transform.Rotate(transform.forward, 180); Panel = GameUI.Singleton.PlayerPanel(Side); UpdatePanel(); + GameUI.Singleton.buttonDown += (side, direction) => { + if (!side.Equals(Side)) + return; + if (direction == "left") + goingLeft = true; + else + goingRight = true; + }; + GameUI.Singleton.buttonUp += (side, direction) => { + if (!side.Equals(Side)) + return; + if (direction == "left") + goingLeft = false; + else + goingRight = false; + }; } private void OnCollisionEnter2D(Collision2D other) { @@ -115,8 +131,8 @@ namespace Game { return; var keyboard = Keyboard.current; - goingLeft = keyboard.aKey.isPressed; - goingRight = keyboard.dKey.isPressed; + //goingLeft = keyboard.aKey.isPressed; + //goingRight = keyboard.dKey.isPressed; TryLinearMove(Time.fixedDeltaTime); } diff --git a/Assets/Scripts/GameUI.cs b/Assets/Scripts/GameUI.cs index 8029157..72944eb 100644 --- a/Assets/Scripts/GameUI.cs +++ b/Assets/Scripts/GameUI.cs @@ -48,8 +48,28 @@ public class GameUI : MonoBehaviour { }; } - void PreparePlayerPanels() { + private void PreparePlayerPanels() { float heightPercentage = Dimensions.Singleton.panelHeightPercentage; PlayerPanel(Side.Top).style.height = PlayerPanel(Side.Bottom).style.height = Length.Percent(heightPercentage); + + SetupButtons(Side.Top); + SetupButtons(Side.Bottom); + } + + public delegate void ButtonDown(Side side, string direction); + public delegate void ButtonUp(Side side, string direction); + public ButtonDown buttonDown; + public ButtonUp buttonUp; + + private void SetupButtons(Side side) { + Button btnLeft = PlayerPanel(side).Q