button functionality

main
Benjamin Kraft 1 year ago
parent 7a5924acba
commit 1ea09bddd0
  1. 22
      Assets/Scripts/Game/Player.cs
  2. 22
      Assets/Scripts/GameUI.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);
}

@ -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<Button>("btn_left");
btnLeft.clickable.activators.Clear();
btnLeft.RegisterCallback<MouseDownEvent>(_ => buttonDown(side, "left"));
btnLeft.RegisterCallback<MouseUpEvent>(_ => buttonUp(side, "left"));
Button btnRight = PlayerPanel(side).Q<Button>("btn_right");
btnRight.clickable.activators.Clear();
btnRight.RegisterCallback<MouseDownEvent>(_ => buttonDown(side, "right"));
btnRight.RegisterCallback<MouseUpEvent>(_ => buttonUp(side, "right"));
}
}

Loading…
Cancel
Save