Mutually_Assured_Destruction/menu/start_game_menu.cs
MarcEricMartel 7250ee17c8 MENNU
2023-06-11 11:23:38 -04:00

178 lines
5.0 KiB
C#

using Godot;
using System;
public partial class start_game_menu : Control {
enum _state { START, LAN, HOST, JOIN, QUIT };
enum _gameType { SINGLE, LOCAL, LAN };
private _state _currState = _state.START;
private const string _path = "/root/StartGameMenu/";
private Button _localMulti, _single, _LANMulti, _join, _host, _cancel, _quit, _yes, _no;
private LineEdit _ip;
private Label _error, _lip;
private string Error { set { _error.Set("text", value); } }
private string IP_address {
set {
_ip.Set("text", value);
}
get => (string)_ip.Get("text");
}
private _state State { get => _currState; set {
if (value == _currState)
return;
IP_address = "";
switch (value) {
case _state.START:
Error = "";
_single.Set("visible", true);
//_localMulti.Set("visible", true);
//_LANMulti.Set("visible", true);
_cancel.Set("visible", false);
_yes.Set("visible", false);
_no.Set("visible", false);
_join.Set("visible", false);
_host.Set("visible", false);
_quit.Set("visible", true);
_ip.Set("visible", false);
_lip.Set("visible", false);
break;
case _state.LAN:
Error = "";
_single.Set("visible", false);
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
_cancel.Set("visible", true);
_yes.Set("visible", false);
_no.Set("visible", false);
_join.Set("visible", true);
_host.Set("visible", true);
_quit.Set("visible", true);
_ip.Set("visible", true);
_lip.Set("visible", true);
break;
case _state.QUIT:
Error = "Are you sure you want to quit?";
_single.Set("visible", false);
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
_cancel.Set("visible", false);
_yes.Set("visible", true);
_no.Set("visible", true);
_join.Set("visible", false);
_host.Set("visible", false);
_quit.Set("visible", false);
_ip.Set("visible", false);
_lip.Set("visible", false);
break;
case _state.HOST:
Error = "Waiting for peer to join...";
_single.Set("visible", false);
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
_cancel.Set("visible", true);
_yes.Set("visible", false);
_no.Set("visible", false);
_join.Set("visible", false);
_host.Set("visible", false);
_quit.Set("visible", false);
_ip.Set("visible", false);
_lip.Set("visible", false);
break;
case _state.JOIN:
Error = "Waiting for server...";
_single.Set("visible", false);
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
_cancel.Set("visible", true);
_yes.Set("visible", false);
_no.Set("visible", false);
_join.Set("visible", false);
_host.Set("visible", false);
_quit.Set("visible", false);
_ip.Set("visible", false);
_lip.Set("visible", false);
break;
default: return;
}
_currState = value;
}}
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
_single = GetNode<Button>(_path + "Btn_Single");
//_localMulti = GetNode<Button>(_path + "Btn_LocalMulti");
//_LANMulti = GetNode<Button>(_path + "Btn_LANMulti");
_cancel = GetNode<Button>(_path + "Btn_Cancel");
_quit = GetNode<Button>(_path + "Btn_Quit");
_yes = GetNode<Button>(_path + "Btn_Yes");
_no = GetNode<Button>(_path + "Btn_No");
_host = GetNode<Button>(_path + "Btn_Host");
_join = GetNode<Button>(_path + "Btn_Join");
_error = GetNode<Label>(_path + "lbl_err");
_ip = GetNode<LineEdit>(_path + "txt_IP");
_lip = GetNode<Label>(_path + "lbl_IP");
_single.Pressed += () => startGame(_gameType.SINGLE);
//_localMulti.Pressed += () => startGame(_gameType.LOCAL);
//_LANMulti.Pressed += () => State = _state.LAN;
_host.Pressed += () => setupLANMultiGame();
_join.Pressed += () => setupLANMultiGame(false);
_quit.Pressed += () => State = _state.QUIT;
_cancel.Pressed += () => State = _state.START;
_no.Pressed += () => State = _state.START;
_yes.Pressed += () => GetTree().Quit();
State = _state.START;
}
private void startGame(_gameType type, long id = 0) {
switch (type) {
case _gameType.SINGLE:
break;
case _gameType.LOCAL:
break;
case _gameType.LAN:
break;
default: return;
}
}
private void setupLANMultiGame(bool isHost = true) {
const string ERR = "Invalid IP address.";
if (isHost) {
ENetMultiplayerPeer peer = new ENetMultiplayerPeer();
peer.CreateServer(6666);
Multiplayer.MultiplayerPeer = peer;
peer.PeerConnected += (long id) => startGame(_gameType.LAN, id);
State = _state.HOST;
}
else {
string ip = _ip.Get("text").ToString();
if (!ip.Contains('.')) {
Error = ERR;
return;
}
string[] ips = ip.Split('.');
if (ips.Length != 4) {
Error = ERR;
return;
}
for (int x =0; x < 4; ++x) {
int val = 0;
if (!int.TryParse(ips[0], out val) || val < 0 || val > 255) {
Error = ERR;
return;
}
}
ENetMultiplayerPeer peer = new ENetMultiplayerPeer();
peer.CreateClient(ip, 6666);
Multiplayer.MultiplayerPeer = peer;
State = _state.JOIN;
}
}
}