From 0148e46c92473474587180e4c1348a87083df15b Mon Sep 17 00:00:00 2001 From: Benjamin Kraft Date: Mon, 17 Apr 2023 12:19:16 +0200 Subject: [PATCH] player panel style --- Assets/Scripts/Game/AIPlayer.cs | 39 +++++++++++++++++++++++---- Assets/Scripts/Game/Player.cs | 4 +-- Assets/Scripts/Game/RealPlayer.cs | 18 +++++++------ Assets/Scripts/GameUI.cs | 2 +- Assets/UI Toolkit/player_panel.uss | 10 +++---- ProjectSettings/ProjectSettings.asset | 6 ++--- 6 files changed, 55 insertions(+), 24 deletions(-) diff --git a/Assets/Scripts/Game/AIPlayer.cs b/Assets/Scripts/Game/AIPlayer.cs index 9294db0..46637e6 100644 --- a/Assets/Scripts/Game/AIPlayer.cs +++ b/Assets/Scripts/Game/AIPlayer.cs @@ -1,5 +1,6 @@ using System.Linq; using UnityEngine; +using UnityEngine.UIElements; namespace Game { @@ -10,6 +11,8 @@ namespace Game { private bool isApproaching; private float lastDirection; + private bool lastGoingLeft, lastGoingRight; + private Label leftButton, rightButton; public Difficulty Difficulty { get; set; } @@ -19,25 +22,51 @@ namespace Game { private float DistortAmount => 1 - 1 / ((float) Difficulty + 1); + protected new void Start() { + base.Start(); + leftButton = GameUI.Singleton.GetGoButton(Side, "left"); + rightButton = GameUI.Singleton.GetGoButton(Side, "right"); + } + private void FixedUpdate() { var dt = Time.fixedDeltaTime; var target = GetTargetPosition(); var h = Mathf.Max(Speed * dt, Width / 2); - goingLeft = target < X - h; - goingRight = target > X + h; - if (goingLeft || goingRight) { + goingLeftLinear = target < X - h; + goingRightLinear = target > X + h; + if (goingLeftLinear || goingRightLinear) { isApproaching = false; - lastDirection = goingLeft ? -1 : 1; + lastDirection = goingLeftLinear ? -1 : 1; TryLinearMove(dt); } else { if (!isApproaching) { isApproaching = true; currentSmoothV = Speed * lastDirection; } - ApproachPosition(target); } + bool goingLeft = isApproaching ? currentSmoothV < 0 : goingLeftLinear; + bool goingRight = isApproaching ? currentSmoothV > 0 : goingRightLinear; + switch (lastGoingLeft) { + case false when goingLeft: + + break; + case true when !goingLeft: + + break; + } + switch (lastGoingRight) { + case false when goingRight: + + break; + case true when !goingRight: + + break; + } + + lastGoingLeft = goingLeft; + lastGoingRight = goingRight; } // True if ball y velocity points towards player diff --git a/Assets/Scripts/Game/Player.cs b/Assets/Scripts/Game/Player.cs index b401a8a..0dcad0e 100644 --- a/Assets/Scripts/Game/Player.cs +++ b/Assets/Scripts/Game/Player.cs @@ -27,7 +27,7 @@ namespace Game { private const float BaseWidth = 6; private const float WidthMultiplier = 1.5f; - protected bool goingLeft, goingRight; + protected bool goingLeftLinear, goingRightLinear; public Side Side { get; set; } @@ -145,7 +145,7 @@ namespace Game { } protected void TryLinearMove(float h) { - var trans = new Vector2((goingLeft ? -1 : 0) + (goingRight ? 1 : 0), 0); + var trans = new Vector2((goingLeftLinear ? -1 : 0) + (goingRightLinear ? 1 : 0), 0); trans *= Speed * h; transform.Translate(trans, Space.World); ClampInsideBorders(); diff --git a/Assets/Scripts/Game/RealPlayer.cs b/Assets/Scripts/Game/RealPlayer.cs index b3b0966..adfffd5 100644 --- a/Assets/Scripts/Game/RealPlayer.cs +++ b/Assets/Scripts/Game/RealPlayer.cs @@ -7,21 +7,23 @@ namespace Game { private new void Start() { base.Start(); + if (!isThisClient) + return; GameUI.Singleton.buttonDown += (side, direction) => { if (!side.Equals(Side)) return; if (direction == "left") - goingLeft = true; + goingLeftLinear = true; else - goingRight = true; + goingRightLinear = true; }; GameUI.Singleton.buttonUp += (side, direction) => { if (!side.Equals(Side)) return; if (direction == "left") - goingLeft = false; + goingLeftLinear = false; else - goingRight = false; + goingRightLinear = false; }; } @@ -33,12 +35,12 @@ namespace Game { var keyboard = Keyboard.current; switch (Side) { case Side.Top: - goingLeft = keyboard.leftArrowKey.isPressed; - goingRight = keyboard.rightArrowKey.isPressed; + goingLeftLinear = keyboard.leftArrowKey.isPressed; + goingRightLinear = keyboard.rightArrowKey.isPressed; break; case Side.Bottom: - goingLeft = keyboard.aKey.isPressed; - goingRight = keyboard.dKey.isPressed; + goingLeftLinear = keyboard.aKey.isPressed; + goingRightLinear = keyboard.dKey.isPressed; break; } } diff --git a/Assets/Scripts/GameUI.cs b/Assets/Scripts/GameUI.cs index 3fe5adf..697bd3f 100644 --- a/Assets/Scripts/GameUI.cs +++ b/Assets/Scripts/GameUI.cs @@ -62,7 +62,7 @@ public class GameUI : MonoBehaviour { SetupGoButton(Side.Bottom, "right"); } - private Label GetGoButton(Side sideVertical, string sideHorizontal) { + public Label GetGoButton(Side sideVertical, string sideHorizontal) { return PlayerPanel(sideVertical).Q