-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b707b0a
commit 5f860bd
Showing
19 changed files
with
681 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
VoxelThing.Client/src/Gui/Screens/MultiplayerConnectionScreen.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using VoxelThing.Client.Gui.Controls; | ||
using VoxelThing.Client.Rendering; | ||
using VoxelThing.Game.Networking; | ||
|
||
namespace VoxelThing.Client.Gui.Screens; | ||
|
||
public class MultiplayerConnectionScreen : Screen | ||
{ | ||
private readonly TextBox nameBox; | ||
private Task<bool>? connectionTask; | ||
|
||
public MultiplayerConnectionScreen(Game game) : base(game) | ||
{ | ||
AddControl(new Label(this) | ||
{ | ||
Text = "Enter IP", | ||
Position = (0, -40), | ||
AlignPosition = (0.5f, 0.5f) | ||
}); | ||
|
||
var ipBox = AddControl(new TextBox(this) | ||
{ | ||
Text = "127.0.0.1", | ||
Position = (-100, -30), | ||
Size = (200, 20), | ||
AlignPosition = (0.5f, 0.5f) | ||
}); | ||
|
||
nameBox = AddControl(new TextBox(this) | ||
{ | ||
Text = "BlueStaggo", | ||
Position = (-100, -10), | ||
Size = (200, 20), | ||
AlignPosition = (0.5f, 0.5f) | ||
}); | ||
|
||
var backButton = AddControl(new Label(this) | ||
{ | ||
Text = "Back", | ||
Position = (-105, 20), | ||
Size = (100, 20), | ||
AlignPosition = (0.5f, 0.5f), | ||
HasBackground = true | ||
}); | ||
backButton.OnClick += (_, _) => Game.CurrentScreen = Parent; | ||
|
||
var playButton = AddControl(new Label(this) | ||
{ | ||
Text = "Play", | ||
Position = (5, 20), | ||
Size = (100, 20), | ||
AlignPosition = (0.5f, 0.5f), | ||
HasBackground = true | ||
}); | ||
playButton.OnClick += (_, _) => | ||
{ | ||
if (connectionTask is null || connectionTask.IsCompleted) | ||
connectionTask = Game.ConnectToServer(ipBox.Text); | ||
}; | ||
} | ||
|
||
public override void Tick() | ||
{ | ||
if (connectionTask?.IsCompletedSuccessfully ?? false) | ||
{ | ||
Game.Connection?.SendPacket(new CUpdateDisplayName(nameBox.Text)); | ||
Game.CurrentScreen = new MultiplayerTestScreen(Game, nameBox.Text); | ||
} | ||
} | ||
|
||
public override void Draw() | ||
{ | ||
base.Draw(); | ||
if (connectionTask is not null) | ||
{ | ||
string displayString = "§cffff00Waiting..."; | ||
if (connectionTask.IsCompletedSuccessfully) | ||
displayString = "§c00ff00Success!"; | ||
else if (connectionTask.IsFaulted) | ||
{ | ||
displayString = "§cff0000Failure!"; | ||
} | ||
|
||
MainRenderer renderer = Game.MainRenderer; | ||
ScreenDimensions dimensions = renderer.ScreenDimensions; | ||
renderer.Fonts.Outlined.Print( | ||
displayString, | ||
dimensions.IntWidth / 2, dimensions.IntHeight / 2 + 50, | ||
align: 0.5f | ||
); | ||
} | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
VoxelThing.Client/src/Gui/Screens/MultiplayerTestScreen.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using OpenTK.Mathematics; | ||
using OpenTK.Windowing.GraphicsLibraryFramework; | ||
using VoxelThing.Client.Gui.Controls; | ||
using VoxelThing.Game.Networking; | ||
|
||
namespace VoxelThing.Client.Gui.Screens; | ||
|
||
public class MultiplayerTestScreen : Screen | ||
{ | ||
private readonly List<string> messages = []; | ||
|
||
public MultiplayerTestScreen(Game game, string name) : base(game) | ||
{ | ||
AddControl(new Label(this) | ||
{ | ||
Text = $"You are {name}", | ||
Font = game.MainRenderer.Fonts.Outlined, | ||
Position = (2, 2), | ||
AlignText = (0.0f, 0.0f) | ||
}); | ||
|
||
var messageBox = AddControl(new TextBox(this) | ||
{ | ||
Position = (0, -20), | ||
Size = (-100, 20), | ||
AlignPosition = (0.0f, 1.0f), | ||
AlignSize = (1.0f, 0.0f) | ||
}); | ||
messageBox.OnKeyPressed += (_, args) => | ||
{ | ||
if (args.Key != Keys.Enter) | ||
return; | ||
Game.Connection?.SendPacket(new CSendMessagePacket(messageBox.Text)); | ||
messageBox.Text = string.Empty; | ||
}; | ||
|
||
var sendButton = AddControl(new Label(this) | ||
{ | ||
Text = "Send", | ||
Position = (-100, -20), | ||
Size = (100, 20), | ||
AlignPosition = (1.0f, 1.0f), | ||
HasBackground = true | ||
}); | ||
sendButton.OnClick += (_, _) => | ||
{ | ||
Game.Connection?.SendPacket(new CSendMessagePacket(messageBox.Text)); | ||
messageBox.Text = string.Empty; | ||
}; | ||
|
||
var exitButton = AddControl(new IconButton(this) | ||
{ | ||
Icon = Icons.Delete, | ||
Position = (-20, 0), | ||
Size = (20, 20), | ||
AlignPosition = (1.0f, 0.0f), | ||
}); | ||
exitButton.OnClick += (_, _) => | ||
{ | ||
Game.DisconnectFromServer(); | ||
Game.CurrentScreen = null; | ||
}; | ||
} | ||
|
||
public override void Tick() | ||
{ | ||
if (Game.Connection is null) | ||
Game.CurrentScreen = null; | ||
} | ||
|
||
public override void Draw() | ||
{ | ||
ScreenDimensions dimensions = Game.MainRenderer.ScreenDimensions; | ||
Game.MainRenderer.Draw2D.DrawQuad(new() | ||
{ | ||
Color = (Vector4)Color4.Black, | ||
Size = dimensions.IntSize | ||
}); | ||
for (int i = 0; i < messages.Count; i++) | ||
{ | ||
int y = dimensions.IntHeight - 30 - i * 10; | ||
Game.MainRenderer.Fonts.Normal.Print(messages[i], 2, y); | ||
} | ||
base.Draw(); | ||
} | ||
|
||
public void HandlePacket(IPacket packet) | ||
{ | ||
if (packet is SSendMessagePacket sendMessage) | ||
{ | ||
string fullMessage = $"{sendMessage.Author}: {sendMessage.Message}"; | ||
string fullMessageColored = $"{sendMessage.Author}: §cffffff{sendMessage.Message}"; | ||
Console.WriteLine(fullMessage); | ||
messages.Insert(0, fullMessageColored); | ||
if (messages.Count > 50) | ||
messages.RemoveAt(50); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.