feat: all new networking changes (except reservations), huge refactor and fmt
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class AddressUI : TextEdit
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
Text = Connection.WS_DEFAULT_ADDRESS;
|
||||
}
|
||||
}
|
||||
public partial class AddressUI : TextEdit {
|
||||
public override void _Ready() {
|
||||
Text = Connection.WS_DEFAULT_ADDRESS;
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,10 @@ public partial class AdminControls : HBoxContainer
|
||||
{
|
||||
Connection.Instance.OnBecomeAdmin += UpdateUI;
|
||||
Connection.Instance.OnTournamentEnd += UpdateUI;
|
||||
Connection.Instance.OnStartTournamentAck += UpdateUI;
|
||||
Connection.Instance.OnStartTournament += UpdateUI;
|
||||
Connection.Instance.OnCancelTournamentAck += UpdateUI;
|
||||
Connection.Instance.OnGetDataAcks += UpdateUI;
|
||||
Connection.Instance.OnSetDataAcks += UpdateUI;
|
||||
|
||||
StartTournament.Pressed += () => Connection.Instance.StartTournament();
|
||||
CancelTournament.Pressed += () => Connection.Instance.CancelTournament();
|
||||
@@ -23,7 +24,7 @@ public partial class AdminControls : HBoxContainer
|
||||
|
||||
Timeout.ValueChanged += value =>
|
||||
{
|
||||
Connection.Instance.SetTournamentWait((float)value);
|
||||
Connection.Instance.SetMoveWait((float)value);
|
||||
var time = Connection.Instance.CurrentWaitTimeout.ToString();
|
||||
if (time.Length > 3)
|
||||
{
|
||||
@@ -32,16 +33,17 @@ public partial class AdminControls : HBoxContainer
|
||||
Label.Text = "Wait To Move: " + time + "s ";
|
||||
};
|
||||
|
||||
BecomeAdmin.Pressed += ShowAuthPopup;
|
||||
BecomeAdmin.Pressed += showAuthPopup;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Connection.Instance.OnBecomeAdmin -= UpdateUI;
|
||||
Connection.Instance.OnTournamentEnd -= UpdateUI;
|
||||
Connection.Instance.OnStartTournamentAck -= UpdateUI;
|
||||
Connection.Instance.OnStartTournament -= UpdateUI;
|
||||
Connection.Instance.OnCancelTournamentAck -= UpdateUI;
|
||||
Connection.Instance.OnGetDataAcks -= UpdateUI;
|
||||
Connection.Instance.OnSetDataAcks -= UpdateUI;
|
||||
}
|
||||
|
||||
private void UpdateUI()
|
||||
@@ -66,7 +68,7 @@ public partial class AdminControls : HBoxContainer
|
||||
StartTournament.Hide();
|
||||
CancelTournament.Hide();
|
||||
}
|
||||
else if (Connection.Instance.IsAdmin && Connection.Instance.ActiveTournament)
|
||||
else if (Connection.Instance.IsAdmin && Connection.Instance.ActiveTournament != TournamentType.None)
|
||||
{
|
||||
StartTournament.Hide();
|
||||
CancelTournament.Show();
|
||||
@@ -87,7 +89,7 @@ public partial class AdminControls : HBoxContainer
|
||||
Label.Text = "Wait To Move: " + time + "s ";
|
||||
}
|
||||
|
||||
private void ShowAuthPopup()
|
||||
private void showAuthPopup()
|
||||
{
|
||||
var authWindow = new Window();
|
||||
authWindow.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class BackButton : TextureButton
|
||||
{
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
|
||||
public override void _Pressed()
|
||||
{
|
||||
TransitionToBracket();
|
||||
base._Pressed();
|
||||
}
|
||||
|
||||
private void TransitionToBracket()
|
||||
{
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
}
|
||||
public partial class BackButton : TextureButton {
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
|
||||
public override void _Pressed() {
|
||||
TransitionToBracket();
|
||||
base._Pressed();
|
||||
}
|
||||
|
||||
private void TransitionToBracket() {
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
}
|
||||
@@ -1,264 +1,249 @@
|
||||
using Godot;
|
||||
using System.Linq;
|
||||
|
||||
public partial class BoardScreen : Node2D
|
||||
{
|
||||
[Export] private AudioStream endingSfx;
|
||||
[Export] private Theme theme;
|
||||
|
||||
private const string RED_CHIP_PATH = "res://scenes/red_chip.tscn";
|
||||
private const string YELLOW_CHIP_PATH = "res://scenes/yellow_chip.tscn";
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
private const int CHIP_SCALE = 3;
|
||||
private const int CHIP_SIZE = 24;
|
||||
private const int CHIP_PADDING = 2;
|
||||
private const int CHIP_X_OFF = -(CHIP_SIZE + CHIP_PADDING) * 7 / 2 + (CHIP_SIZE + CHIP_PADDING) / 2;
|
||||
private const int Y_OFF = 300;
|
||||
private const int CARD_CENTER_X_DEFAULT = -536;
|
||||
private const double MOVE_TIMEOUT_BEFORE_PLACE = 1.0f;
|
||||
public partial class BoardScreen : Node2D {
|
||||
[Export] private AudioStream endingSfx;
|
||||
[Export] private Theme theme;
|
||||
|
||||
private double currentTimeout = 0.0f;
|
||||
private const string RED_CHIP_PATH = "res://scenes/red_chip.tscn";
|
||||
private const string YELLOW_CHIP_PATH = "res://scenes/yellow_chip.tscn";
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
private const int CHIP_SCALE = 3;
|
||||
private const int CHIP_SIZE = 24;
|
||||
private const int CHIP_PADDING = 2;
|
||||
private const int CHIP_X_OFF = -(CHIP_SIZE + CHIP_PADDING) * 7 / 2 + (CHIP_SIZE + CHIP_PADDING) / 2;
|
||||
private const int Y_OFF = 300;
|
||||
private const int CARD_CENTER_X_DEFAULT = -536;
|
||||
private const double MOVE_TIMEOUT_BEFORE_PLACE = 1.0f;
|
||||
|
||||
private PackedScene redChip;
|
||||
private PackedScene ylwChip;
|
||||
private double currentTimeout = 0.0f;
|
||||
|
||||
private Node2D player1Card;
|
||||
private Node2D player2Card;
|
||||
private MatchData matchData;
|
||||
|
||||
private RigidBody2D[,] chips = new RigidBody2D[6, 7]; // 6 rows 7 cols | 0, 0 is top left
|
||||
private PackedScene redChip;
|
||||
private PackedScene ylwChip;
|
||||
|
||||
private bool _lastMove = false;
|
||||
private float _lastMoveTimer = 2.5f;
|
||||
private string _winner = "";
|
||||
private Node2D player1Card;
|
||||
private Node2D player2Card;
|
||||
private MatchData matchData;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready() {
|
||||
// Node initialization
|
||||
player1Card = GetNode<Node2D>("Player1Card");
|
||||
player2Card = GetNode<Node2D>("Player2Card");
|
||||
private RigidBody2D[,] chips = new RigidBody2D[6, 7]; // 6 rows 7 cols | 0, 0 is top left
|
||||
|
||||
player1Card.GetNode<Label>("Name").Resized += () => setPlayerCardScale((player1Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player1Card);
|
||||
player2Card.GetNode<Label>("Name").Resized += () => setPlayerCardScale((player2Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player2Card);
|
||||
private bool _lastMove = false;
|
||||
private float _lastMoveTimer = 2.5f;
|
||||
private string _winner = "";
|
||||
|
||||
redChip = GD.Load<PackedScene>(RED_CHIP_PATH);
|
||||
ylwChip = GD.Load<PackedScene>(YELLOW_CHIP_PATH);
|
||||
|
||||
matchData = Connection.Instance.CurrentObservingMatch;
|
||||
player1Card.GetNode<Label>("Name").Text = matchData.player1;
|
||||
player2Card.GetNode<Label>("Name").Text = matchData.player2;
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready() {
|
||||
// Node initialization
|
||||
player1Card = GetNode<Node2D>("Player1Card");
|
||||
player2Card = GetNode<Node2D>("Player2Card");
|
||||
|
||||
if (Connection.Instance.PreviousMoves.Count == 0)
|
||||
{
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
else if (Connection.Instance.PreviousMoves.Last().Item1 == matchData.player1)
|
||||
{
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
Connection.Instance.OnObserveWin += OnObserveWin;
|
||||
Connection.Instance.OnObserveDraw += OnObserveDraw;
|
||||
Connection.Instance.OnObserveTerminated += OnObserveTerminated;
|
||||
Connection.Instance.OnObserveMove += ObserveMove;
|
||||
}
|
||||
player1Card.GetNode<Label>("Name").Resized += () =>
|
||||
setPlayerCardScale((player1Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player1Card);
|
||||
player2Card.GetNode<Label>("Name").Resized += () =>
|
||||
setPlayerCardScale((player2Card.GetNode<Label>("Name").GetRect().Size.X + 7) / 16, player2Card);
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (Connection.Instance.PreviousMoves.Count != 0 && currentTimeout <= 0.0f)
|
||||
{
|
||||
var move = Connection.Instance.PreviousMoves[0];
|
||||
Connection.Instance.PreviousMoves.RemoveAt(0);
|
||||
if (move.Item1 == matchData.player1)
|
||||
{
|
||||
spawnRed(move.Item2);
|
||||
}
|
||||
else
|
||||
{
|
||||
spawnYellow(move.Item2);
|
||||
}
|
||||
redChip = GD.Load<PackedScene>(RED_CHIP_PATH);
|
||||
ylwChip = GD.Load<PackedScene>(YELLOW_CHIP_PATH);
|
||||
|
||||
currentTimeout = MOVE_TIMEOUT_BEFORE_PLACE;
|
||||
}
|
||||
else if (currentTimeout >= 0.0f)
|
||||
{
|
||||
currentTimeout -= delta;
|
||||
}
|
||||
matchData = Connection.Instance.CurrentObservingMatch;
|
||||
player1Card.GetNode<Label>("Name").Text = matchData.player1;
|
||||
player2Card.GetNode<Label>("Name").Text = matchData.player2;
|
||||
|
||||
if (_lastMove)
|
||||
{
|
||||
_lastMoveTimer -= (float) delta;
|
||||
}
|
||||
if (Connection.Instance.PreviousMoves.Count == 0) {
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
else if (Connection.Instance.PreviousMoves.Last().Item1 == matchData.player1) {
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Show();
|
||||
}
|
||||
else {
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
if (_lastMoveTimer <= 0.0f)
|
||||
{
|
||||
if (_winner == "")
|
||||
{
|
||||
PopupMessage("Draw!");
|
||||
}
|
||||
else
|
||||
{
|
||||
PopupMessage(_winner + " wins!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Connection.Instance.OnObserveWin += OnObserveWin;
|
||||
Connection.Instance.OnObserveDraw += OnObserveDraw;
|
||||
Connection.Instance.OnObserveTerminated += OnObserveTerminated;
|
||||
Connection.Instance.OnObserveMove += OnObserveMove;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Connection.Instance.OnObserveWin -= OnObserveWin;
|
||||
Connection.Instance.OnObserveDraw -= OnObserveDraw;
|
||||
Connection.Instance.OnObserveTerminated -= OnObserveTerminated;
|
||||
Connection.Instance.OnObserveMove -= ObserveMove;
|
||||
}
|
||||
public override void _Process(double delta) {
|
||||
if (Connection.Instance.PreviousMoves.Count != 0 && currentTimeout <= 0.0f) {
|
||||
var move = Connection.Instance.PreviousMoves[0];
|
||||
Connection.Instance.PreviousMoves.RemoveAt(0);
|
||||
if (move.Item1 == matchData.player1) {
|
||||
spawnRed(move.Item2);
|
||||
}
|
||||
else {
|
||||
spawnYellow(move.Item2);
|
||||
}
|
||||
|
||||
private void OnObserveWin(string winner)
|
||||
{
|
||||
_lastMove = true;
|
||||
_winner = winner;
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
currentTimeout = MOVE_TIMEOUT_BEFORE_PLACE;
|
||||
}
|
||||
else if (currentTimeout >= 0.0f) {
|
||||
currentTimeout -= delta;
|
||||
}
|
||||
|
||||
private void OnObserveDraw()
|
||||
{
|
||||
_lastMove = true;
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
if (_lastMove) {
|
||||
_lastMoveTimer -= (float)delta;
|
||||
}
|
||||
|
||||
private void OnObserveTerminated()
|
||||
{
|
||||
PopupMessage("Match Terminated");
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
private void ObserveMove(string username, int column)
|
||||
{
|
||||
if (username == matchData.player1)
|
||||
{
|
||||
if (!_lastMove)
|
||||
player2Card.GetNode<Label>("Status").Show();
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_lastMove)
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
Connection.Instance.PreviousMoves.Add((username, column));
|
||||
}
|
||||
if (_lastMoveTimer <= 0.0f) {
|
||||
if (_winner == "") {
|
||||
showPopupMessage("Draw!");
|
||||
}
|
||||
else {
|
||||
showPopupMessage(_winner + " wins!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void PopupMessage(string message)
|
||||
{
|
||||
var popup = new Popup();
|
||||
popup.AlwaysOnTop = true;
|
||||
popup.Size = new Vector2I(200, 100);
|
||||
popup.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
var text = new Label();
|
||||
text.Text = message;
|
||||
var sfx = new AudioStreamPlayer();
|
||||
sfx.Stream = endingSfx;
|
||||
sfx.VolumeDb = -2;
|
||||
popup.AddChild(sfx);
|
||||
popup.AddChild(text);
|
||||
text.GrowHorizontal = Control.GrowDirection.Both;
|
||||
text.GrowVertical = Control.GrowDirection.Both;
|
||||
text.HorizontalAlignment = HorizontalAlignment.Center;
|
||||
text.VerticalAlignment = VerticalAlignment.Center;
|
||||
text.AnchorsPreset = (int) Control.LayoutPreset.FullRect;
|
||||
popup.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
|
||||
popup.PopupHide += () => popup.QueueFree();
|
||||
GetTree().Root.AddChild(popup);
|
||||
popup.PopupCentered();
|
||||
sfx.Play();
|
||||
popup.Show();
|
||||
TransitionToBracket();
|
||||
}
|
||||
|
||||
private void TransitionToBracket()
|
||||
{
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
public override void _ExitTree() {
|
||||
Connection.Instance.OnObserveWin -= OnObserveWin;
|
||||
Connection.Instance.OnObserveDraw -= OnObserveDraw;
|
||||
Connection.Instance.OnObserveTerminated -= OnObserveTerminated;
|
||||
Connection.Instance.OnObserveMove -= OnObserveMove;
|
||||
}
|
||||
|
||||
/*
|
||||
* Determines if the column can have a new chip placed
|
||||
* Will return -1 if invalid
|
||||
* or
|
||||
* Will return row of board in which chip can be placed
|
||||
*/
|
||||
public int canPlaceOnCol(int col) {
|
||||
if(col < 0 || col > 6) // Col out of range
|
||||
return -1;
|
||||
private void OnObserveWin(string winner) {
|
||||
_lastMove = true;
|
||||
_winner = winner;
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
return getNextAvailRow(col);
|
||||
}
|
||||
private void OnObserveDraw() {
|
||||
_lastMove = true;
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
public int getNextAvailRow(int col) {
|
||||
for(int i = chips.GetLength(0) - 1; i >= 0; i--) { // Start at bottom
|
||||
if(chips[i, col] == null)
|
||||
return i;
|
||||
}
|
||||
private void OnObserveTerminated() {
|
||||
showPopupMessage("Match Terminated");
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
private void OnObserveMove(string username, int column) {
|
||||
if (username == matchData.player1) {
|
||||
if (!_lastMove)
|
||||
player2Card.GetNode<Label>("Status").Show();
|
||||
player1Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
else {
|
||||
if (!_lastMove)
|
||||
player1Card.GetNode<Label>("Status").Show();
|
||||
player2Card.GetNode<Label>("Status").Hide();
|
||||
}
|
||||
|
||||
private void spawnRed(int col) {
|
||||
int row = canPlaceOnCol(col);
|
||||
if(row == -1) {
|
||||
GD.Print("Invalid Placement!");
|
||||
return;
|
||||
}
|
||||
Connection.Instance.PreviousMoves.Add((username, column));
|
||||
}
|
||||
|
||||
RigidBody2D newNode = redChip.Instantiate<RigidBody2D>();
|
||||
AddChild(newNode);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col), -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
private void showPopupMessage(string message) {
|
||||
var popup = new Popup();
|
||||
popup.AlwaysOnTop = true;
|
||||
popup.Size = new Vector2I(200, 100);
|
||||
popup.Theme = GD.Load<Theme>("res://assets/theme.tres");
|
||||
var text = new Label();
|
||||
text.Text = message;
|
||||
var sfx = new AudioStreamPlayer();
|
||||
sfx.Stream = endingSfx;
|
||||
sfx.VolumeDb = -2;
|
||||
popup.AddChild(sfx);
|
||||
popup.AddChild(text);
|
||||
text.GrowHorizontal = Control.GrowDirection.Both;
|
||||
text.GrowVertical = Control.GrowDirection.Both;
|
||||
text.HorizontalAlignment = HorizontalAlignment.Center;
|
||||
text.VerticalAlignment = VerticalAlignment.Center;
|
||||
text.AnchorsPreset = (int)Control.LayoutPreset.FullRect;
|
||||
popup.InitialPosition = Window.WindowInitialPosition.CenterMainWindowScreen;
|
||||
popup.PopupHide += () => popup.QueueFree();
|
||||
GetTree().Root.AddChild(popup);
|
||||
popup.PopupCentered();
|
||||
sfx.Play();
|
||||
popup.Show();
|
||||
transitionToBracket();
|
||||
}
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
private void transitionToBracket() {
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
|
||||
private void spawnYellow(int col) {
|
||||
int row = canPlaceOnCol(col);
|
||||
if(row == -1) {
|
||||
GD.Print("Invalid Placement!");
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* Determines if the column can have a new chip placed
|
||||
* Will return -1 if invalid
|
||||
* or
|
||||
* Will return row of board in which chip can be placed
|
||||
*/
|
||||
private int canPlaceOnCol(int col) {
|
||||
if (col < 0 || col > 6) // Col out of range
|
||||
return -1;
|
||||
|
||||
RigidBody2D newNode = ylwChip.Instantiate<RigidBody2D>();
|
||||
AddChild(newNode);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col), -(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
return getNextAvailRow(col);
|
||||
}
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
private int getNextAvailRow(int col) {
|
||||
for (int i = chips.GetLength(0) - 1; i >= 0; i--) {
|
||||
// Start at bottom
|
||||
if (chips[i, col] == null)
|
||||
return i;
|
||||
}
|
||||
|
||||
private void setPlayerCardScale(float x, Node2D playerCard) {
|
||||
Sprite2D cardCenter = playerCard.GetNode<Sprite2D>("Center");
|
||||
Sprite2D cardRight = playerCard.GetNode<Sprite2D>("Right");
|
||||
return -1;
|
||||
}
|
||||
|
||||
float offX = 16 * x / 2;
|
||||
cardCenter.Scale = new Vector2(x, 2);
|
||||
cardCenter.Position = new Vector2(CARD_CENTER_X_DEFAULT + offX, cardCenter.Position.Y);
|
||||
private void spawnRed(int col) {
|
||||
int row = canPlaceOnCol(col);
|
||||
if (row == -1) {
|
||||
GD.Print("Invalid Placement!");
|
||||
return;
|
||||
}
|
||||
|
||||
cardRight.Position = new Vector2(CARD_CENTER_X_DEFAULT + offX * 2 + 8, cardRight.Position.Y); // 8 is a magic number (im too lazy) for the size of the card edge multipled by 2
|
||||
}
|
||||
RigidBody2D newNode = redChip.Instantiate<RigidBody2D>();
|
||||
AddChild(newNode);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col),
|
||||
-(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
|
||||
private void spawnYellow(int col) {
|
||||
int row = canPlaceOnCol(col);
|
||||
if (row == -1) {
|
||||
GD.Print("Invalid Placement!");
|
||||
return;
|
||||
}
|
||||
|
||||
RigidBody2D newNode = ylwChip.Instantiate<RigidBody2D>();
|
||||
AddChild(newNode);
|
||||
newNode.Position = new Vector2(CHIP_SCALE * (CHIP_X_OFF + (CHIP_SIZE + CHIP_PADDING) * col),
|
||||
-(CHIP_SIZE + CHIP_PADDING) * 7);
|
||||
|
||||
chips[row, col] = newNode;
|
||||
}
|
||||
|
||||
private void setPlayerCardScale(float x, Node2D playerCard) {
|
||||
Sprite2D cardCenter = playerCard.GetNode<Sprite2D>("Center");
|
||||
Sprite2D cardRight = playerCard.GetNode<Sprite2D>("Right");
|
||||
|
||||
float offX = 16 * x / 2;
|
||||
cardCenter.Scale = new Vector2(x, 2);
|
||||
cardCenter.Position = new Vector2(CARD_CENTER_X_DEFAULT + offX, cardCenter.Position.Y);
|
||||
|
||||
cardRight.Position =
|
||||
new Vector2(CARD_CENTER_X_DEFAULT + offX * 2 + 8,
|
||||
cardRight.Position.Y); // 8 is a magic number (im too lazy) for the size of the card edge multipled by 2
|
||||
}
|
||||
}
|
||||
|
||||
enum Direction
|
||||
{
|
||||
UP,
|
||||
UP_RIGHT,
|
||||
RIGHT,
|
||||
DOWN_RIGHT,
|
||||
DOWN,
|
||||
DOWN_LEFT,
|
||||
LEFT,
|
||||
UP_LEFT
|
||||
}
|
||||
enum Direction {
|
||||
UP,
|
||||
UP_RIGHT,
|
||||
RIGHT,
|
||||
DOWN_RIGHT,
|
||||
DOWN,
|
||||
DOWN_LEFT,
|
||||
LEFT,
|
||||
UP_LEFT
|
||||
}
|
||||
@@ -1,109 +1,93 @@
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public partial class BracketScene : Control
|
||||
{
|
||||
[Export] public Tree Players;
|
||||
[Export] public Tree Matches;
|
||||
[Export] public Texture2D WatchButton;
|
||||
[Export] public Texture2D TerminateKickButton;
|
||||
private const string BOARD_SCENE_PATH = "res://scenes/game.tscn";
|
||||
|
||||
private List<PlayerData> _playerList;
|
||||
private List<MatchData> _matchList;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Players.SetColumnTitle(0, "Name");
|
||||
Players.SetColumnTitle(1, "Ready");
|
||||
Players.SetColumnTitle(2, "Playing");
|
||||
Players.HideRoot = true;
|
||||
Players.ButtonClicked += KickPlayer;
|
||||
|
||||
|
||||
Matches.SetColumnTitle(0, "Match");
|
||||
Matches.SetColumnTitle(1, "Red");
|
||||
Matches.SetColumnTitle(2, "Yellow");
|
||||
Matches.HideRoot = true;
|
||||
Matches.ButtonClicked += WatchGame;
|
||||
Matches.ButtonClicked += TerminateGame;
|
||||
|
||||
Connection.Instance.OnUpdatedPlayers += UpdatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches += UpdateMatches;
|
||||
Connection.Instance.OnWatchGameAck += TransitionToBoard;
|
||||
}
|
||||
public partial class BracketScene : Control {
|
||||
[Export] public Tree Players;
|
||||
[Export] public Tree Matches;
|
||||
[Export] public Texture2D WatchButton;
|
||||
[Export] public Texture2D TerminateKickButton;
|
||||
private const string BOARD_SCENE_PATH = "res://scenes/game.tscn";
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Connection.Instance.OnUpdatedPlayers -= UpdatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches -= UpdateMatches;
|
||||
Connection.Instance.OnWatchGameAck -= TransitionToBoard;
|
||||
}
|
||||
private List<PlayerData> playerList;
|
||||
private List<MatchData> matchList;
|
||||
|
||||
private void UpdatePlayers(List<PlayerData> playerList)
|
||||
{
|
||||
Players.Clear();
|
||||
_playerList = playerList;
|
||||
_playerList.Sort((a, b) => a.username.CompareTo(b.username));
|
||||
var root = Players.CreateItem();
|
||||
for (int i = 0; i < _playerList.Count; i++)
|
||||
{
|
||||
var item = Players.CreateItem(root);
|
||||
item.SetText(0, playerList[i].username);
|
||||
item.SetText(1, playerList[i].isReady ? "Yes" : "No");
|
||||
item.SetText(2, playerList[i].isPlaying ? "Yes" : "No");
|
||||
if (Connection.Instance.IsAdmin)
|
||||
{
|
||||
item.AddButton(0, TerminateKickButton, i, false, "Kick");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateMatches(List<MatchData> matchList)
|
||||
{
|
||||
Matches.Clear();
|
||||
_matchList = matchList;
|
||||
var root = Matches.CreateItem();
|
||||
for (int i = 0; i < matchList.Count; i++)
|
||||
{
|
||||
var item = Matches.CreateItem(root);
|
||||
item.SetText(0, matchList[i].matchId.ToString());
|
||||
item.SetText(1, matchList[i].player1);
|
||||
item.SetText(2, matchList[i].player2);
|
||||
item.AddButton(0, WatchButton, i, false, "Watch");
|
||||
if (Connection.Instance.IsAdmin)
|
||||
{
|
||||
item.AddButton(0, TerminateKickButton, 128 + i, false, "Terminate");
|
||||
}
|
||||
}
|
||||
}
|
||||
public override void _Ready() {
|
||||
Players.SetColumnTitle(0, "Name");
|
||||
Players.SetColumnTitle(1, "Ready");
|
||||
Players.SetColumnTitle(2, "Playing");
|
||||
Players.HideRoot = true;
|
||||
Players.ButtonClicked += kickPlayer;
|
||||
|
||||
private void WatchGame(TreeItem item, long column, long id, long mouseButtonIndex)
|
||||
{
|
||||
if (mouseButtonIndex == 1 && column == 0 && id < 128)
|
||||
{
|
||||
Connection.Instance.SendWatchGame(_matchList[(int) id].matchId);
|
||||
}
|
||||
}
|
||||
|
||||
private void TerminateGame(TreeItem item, long column, long id, long mouseButtonIndex)
|
||||
{
|
||||
if (mouseButtonIndex == 1 && column == 0 && id - 128 >= 0 && _matchList[(int) id - 128] != null)
|
||||
{
|
||||
Connection.Instance.TerminateGame(_matchList[(int) id - 128].matchId);
|
||||
}
|
||||
}
|
||||
|
||||
private void KickPlayer(TreeItem item, long column, long id, long mouseButtonIndex)
|
||||
{
|
||||
if (mouseButtonIndex == 1 && column == 0)
|
||||
{
|
||||
Connection.Instance.KickPlayer(_playerList[(int) id].username);
|
||||
}
|
||||
}
|
||||
|
||||
private void TransitionToBoard()
|
||||
{
|
||||
GetTree().ChangeSceneToFile(BOARD_SCENE_PATH);
|
||||
}
|
||||
}
|
||||
Matches.SetColumnTitle(0, "Match");
|
||||
Matches.SetColumnTitle(1, "Red");
|
||||
Matches.SetColumnTitle(2, "Yellow");
|
||||
Matches.HideRoot = true;
|
||||
Matches.ButtonClicked += watchGame;
|
||||
Matches.ButtonClicked += terminateGame;
|
||||
|
||||
Connection.Instance.OnUpdatedPlayers += updatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches += updateMatches;
|
||||
Connection.Instance.OnWatchGameAck += transitionToBoard;
|
||||
}
|
||||
|
||||
public override void _ExitTree() {
|
||||
Connection.Instance.OnUpdatedPlayers -= updatePlayers;
|
||||
Connection.Instance.OnUpdatedMatches -= updateMatches;
|
||||
Connection.Instance.OnWatchGameAck -= transitionToBoard;
|
||||
}
|
||||
|
||||
private void updatePlayers(List<PlayerData> newPlayerList) {
|
||||
Players.Clear();
|
||||
playerList = newPlayerList;
|
||||
playerList.Sort((a, b) => a.username.CompareTo(b.username));
|
||||
var root = Players.CreateItem();
|
||||
for (int i = 0; i < playerList.Count; i++) {
|
||||
var item = Players.CreateItem(root);
|
||||
item.SetText(0, newPlayerList[i].username);
|
||||
item.SetText(1, newPlayerList[i].isReady ? "Yes" : "No");
|
||||
item.SetText(2, newPlayerList[i].isPlaying ? "Yes" : "No");
|
||||
if (Connection.Instance.IsAdmin) {
|
||||
item.AddButton(0, TerminateKickButton, i, false, "Kick");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateMatches(List<MatchData> newMatchList) {
|
||||
Matches.Clear();
|
||||
matchList = newMatchList;
|
||||
var root = Matches.CreateItem();
|
||||
for (int i = 0; i < newMatchList.Count; i++) {
|
||||
var item = Matches.CreateItem(root);
|
||||
item.SetText(0, newMatchList[i].matchId.ToString());
|
||||
item.SetText(1, newMatchList[i].player1);
|
||||
item.SetText(2, newMatchList[i].player2);
|
||||
item.AddButton(0, WatchButton, i, false, "Watch");
|
||||
if (Connection.Instance.IsAdmin) {
|
||||
item.AddButton(0, TerminateKickButton, 128 + i, false, "Terminate");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void watchGame(TreeItem item, long column, long id, long mouseButtonIndex) {
|
||||
if (mouseButtonIndex == 1 && column == 0 && id < 128) {
|
||||
Connection.Instance.SendWatchGame(matchList[(int)id].matchId);
|
||||
}
|
||||
}
|
||||
|
||||
private void terminateGame(TreeItem item, long column, long id, long mouseButtonIndex) {
|
||||
if (mouseButtonIndex == 1 && column == 0 && id - 128 >= 0 && matchList[(int)id - 128] != null) {
|
||||
Connection.Instance.TerminateGame(matchList[(int)id - 128].matchId);
|
||||
}
|
||||
}
|
||||
|
||||
private void kickPlayer(TreeItem item, long column, long id, long mouseButtonIndex) {
|
||||
if (mouseButtonIndex == 1 && column == 0) {
|
||||
Connection.Instance.KickPlayer(playerList[(int)id].username);
|
||||
}
|
||||
}
|
||||
|
||||
private void transitionToBoard() {
|
||||
GetTree().ChangeSceneToFile(BOARD_SCENE_PATH);
|
||||
}
|
||||
}
|
||||
@@ -1,62 +1,50 @@
|
||||
using Godot;
|
||||
|
||||
public partial class ConnectButtonUI : Button
|
||||
{
|
||||
[Export] public TextEdit AddressField;
|
||||
[Export] public Label ErrorLabel;
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
public partial class ConnectButtonUI : Button {
|
||||
[Export] public TextEdit AddressField;
|
||||
[Export] public Label ErrorLabel;
|
||||
private const string BRACKET_SCENE_PATH = "res://scenes/bracket_view.tscn";
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Connection.Instance.OnWsConnectionSuccess += OnConnectionSuccess;
|
||||
Connection.Instance.OnWsConnectionFailed += OnConnectionFailed;
|
||||
public override void _Ready() {
|
||||
Connection.Instance.OnWsConnectionSuccess += OnConnectionSuccess;
|
||||
Connection.Instance.OnWsConnectionFailed += OnConnectionFailed;
|
||||
|
||||
if (Connection.Instance.LastUsedConnectionAddress.Length > 0)
|
||||
{
|
||||
AddressField.Text = Connection.Instance.LastUsedConnectionAddress;
|
||||
}
|
||||
if (Connection.Instance.LastUsedConnectionAddress.Length > 0) {
|
||||
AddressField.Text = Connection.Instance.LastUsedConnectionAddress;
|
||||
}
|
||||
|
||||
if (Connection.Instance.LastError.Length > 0)
|
||||
{
|
||||
ErrorLabel.Text = Connection.Instance.LastError;
|
||||
}
|
||||
if (Connection.Instance.LastError.Length > 0) {
|
||||
ErrorLabel.Text = Connection.Instance.LastError;
|
||||
}
|
||||
|
||||
AddressField.GuiInput += e =>
|
||||
{
|
||||
if (AddressField.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed())
|
||||
{
|
||||
if (inputEventKey.KeyLabel == Key.Enter)
|
||||
{
|
||||
Connection.Instance.Connect(AddressField.Text);
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
AddressField.GuiInput += e => {
|
||||
if (AddressField.HasFocus() && e is InputEventKey inputEventKey && inputEventKey.IsPressed()) {
|
||||
if (inputEventKey.KeyLabel == Key.Enter) {
|
||||
Connection.Instance.Connect(AddressField.Text);
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
|
||||
if (inputEventKey.KeyLabel == Key.Space)
|
||||
{
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
if (inputEventKey.KeyLabel == Key.Space) {
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
Connection.Instance.OnWsConnectionSuccess -= OnConnectionSuccess;
|
||||
Connection.Instance.OnWsConnectionFailed -= OnConnectionFailed;
|
||||
}
|
||||
public override void _ExitTree() {
|
||||
Connection.Instance.OnWsConnectionSuccess -= OnConnectionSuccess;
|
||||
Connection.Instance.OnWsConnectionFailed -= OnConnectionFailed;
|
||||
}
|
||||
|
||||
public override void _Pressed()
|
||||
{
|
||||
Connection.Instance.Connect(AddressField.Text);
|
||||
}
|
||||
public override void _Pressed() {
|
||||
Connection.Instance.Connect(AddressField.Text);
|
||||
}
|
||||
|
||||
private void OnConnectionSuccess()
|
||||
{
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
private void OnConnectionSuccess() {
|
||||
GetTree().ChangeSceneToFile(BRACKET_SCENE_PATH);
|
||||
}
|
||||
|
||||
private void OnConnectionFailed()
|
||||
{
|
||||
ErrorLabel.Text = "Couldn't connect to server! " + Connection.Instance.LastError;
|
||||
}
|
||||
}
|
||||
private void OnConnectionFailed() {
|
||||
ErrorLabel.Text = "Couldn't connect to server! " + Connection.Instance.LastError;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,11 @@
|
||||
public class MatchData
|
||||
{
|
||||
public int matchId { get; private set; }
|
||||
public string player1 { get; private set; }
|
||||
public string player2 { get; private set; }
|
||||
public class MatchData {
|
||||
public int matchId { get; private set; }
|
||||
public string player1 { get; private set; }
|
||||
public string player2 { get; private set; }
|
||||
|
||||
public MatchData(int matchId, string player1, string player2)
|
||||
{
|
||||
this.matchId = matchId;
|
||||
this.player1 = player1;
|
||||
this.player2 = player2;
|
||||
}
|
||||
}
|
||||
public MatchData(int matchId, string player1, string player2) {
|
||||
this.matchId = matchId;
|
||||
this.player1 = player1;
|
||||
this.player2 = player2;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
public class PlayerData
|
||||
{
|
||||
public string username { get; private set; }
|
||||
public bool isReady { get; private set; }
|
||||
public bool isPlaying { get; private set; }
|
||||
public class PlayerData {
|
||||
public string username { get; private set; }
|
||||
public bool isReady { get; private set; }
|
||||
public bool isPlaying { get; private set; }
|
||||
|
||||
public PlayerData(string username, bool isReady, bool isPlaying)
|
||||
{
|
||||
this.username = username;
|
||||
this.isReady = isReady;
|
||||
this.isPlaying = isPlaying;
|
||||
}
|
||||
public PlayerData(string username, bool isReady, bool isPlaying) {
|
||||
this.username = username;
|
||||
this.isReady = isReady;
|
||||
this.isPlaying = isPlaying;
|
||||
}
|
||||
}
|
||||
5
scripts/TournamentType.cs
Normal file
5
scripts/TournamentType.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
public enum TournamentType {
|
||||
None,
|
||||
RoundRobin
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user