button functionality

main
Benjamin Kraft 2 years ago
parent 7a5924acba
commit 1ea09bddd0
  1. 20
      Assets/Scripts/Game/Player.cs
  2. 22
      Assets/Scripts/GameUI.cs

@ -100,6 +100,22 @@ namespace Game {
transform.Rotate(transform.forward, 180); transform.Rotate(transform.forward, 180);
Panel = GameUI.Singleton.PlayerPanel(Side); Panel = GameUI.Singleton.PlayerPanel(Side);
UpdatePanel(); 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) { private void OnCollisionEnter2D(Collision2D other) {
@ -115,8 +131,8 @@ namespace Game {
return; return;
var keyboard = Keyboard.current; var keyboard = Keyboard.current;
goingLeft = keyboard.aKey.isPressed; //goingLeft = keyboard.aKey.isPressed;
goingRight = keyboard.dKey.isPressed; //goingRight = keyboard.dKey.isPressed;
TryLinearMove(Time.fixedDeltaTime); TryLinearMove(Time.fixedDeltaTime);
} }

@ -48,8 +48,28 @@ public class GameUI : MonoBehaviour {
}; };
} }
void PreparePlayerPanels() { private void PreparePlayerPanels() {
float heightPercentage = Dimensions.Singleton.panelHeightPercentage; float heightPercentage = Dimensions.Singleton.panelHeightPercentage;
PlayerPanel(Side.Top).style.height = PlayerPanel(Side.Bottom).style.height = Length.Percent(heightPercentage); 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