Mutually_Assured_Destruction/menu/start_game_menu.cs

185 lines
5.5 KiB
C#
Raw Normal View History

2023-06-10 12:04:03 -04:00
using Godot;
using System;
2023-06-10 14:38:07 -04:00
public partial class start_game_menu : Control {
2023-06-10 14:49:20 -04:00
enum _state { START, LAN, HOST, JOIN, QUIT };
2023-06-10 14:38:07 -04:00
enum _gameType { SINGLE, LOCAL, LAN };
2023-06-10 14:01:30 -04:00
private _state _currState = _state.START;
2023-06-10 12:04:03 -04:00
private const string _path = "/root/StartGameMenu/";
2023-06-10 12:13:57 -04:00
private Button _localMulti, _single, _LANMulti, _join, _host, _cancel, _quit, _yes, _no;
2023-06-10 15:08:42 -04:00
private LineEdit _ip;
2023-06-10 14:01:30 -04:00
private Label _error, _lip;
2023-06-10 12:04:03 -04:00
2023-06-11 11:59:19 -04:00
[Export]
private AudioStreamPlayer _sndBtn, _sndState;
2023-06-10 14:01:30 -04:00
private string Error { set { _error.Set("text", value); } }
private string IP_address {
set {
_ip.Set("text", value);
}
get => (string)_ip.Get("text");
2023-06-10 12:04:03 -04:00
}
2023-06-10 14:01:30 -04:00
private _state State { get => _currState; set {
if (value == _currState)
return;
IP_address = "";
2023-06-11 12:00:59 -04:00
//_sndState.Play();
2023-06-10 14:01:30 -04:00
switch (value) {
case _state.START:
Error = "";
_single.Set("visible", true);
2023-06-11 11:23:38 -04:00
//_localMulti.Set("visible", true);
//_LANMulti.Set("visible", true);
2023-06-10 14:01:30 -04:00
_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);
2023-06-11 11:23:38 -04:00
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
2023-06-10 14:01:30 -04:00
_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);
2023-06-11 11:23:38 -04:00
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
2023-06-10 14:01:30 -04:00
_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;
2023-06-10 14:38:07 -04:00
case _state.HOST:
Error = "Waiting for peer to join...";
_single.Set("visible", false);
2023-06-11 11:23:38 -04:00
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
2023-06-10 14:38:07 -04:00
_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;
2023-06-10 14:49:20 -04:00
case _state.JOIN:
Error = "Waiting for server...";
_single.Set("visible", false);
2023-06-11 11:23:38 -04:00
//_localMulti.Set("visible", false);
//_LANMulti.Set("visible", false);
2023-06-10 14:49:20 -04:00
_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;
2023-06-10 14:38:07 -04:00
default: return;
2023-06-10 14:01:30 -04:00
}
_currState = value;
}}
2023-06-10 12:04:03 -04:00
// Called when the node enters the scene tree for the first time.
2023-06-10 14:01:30 -04:00
public override void _Ready() {
2023-06-10 12:04:03 -04:00
_single = GetNode<Button>(_path + "Btn_Single");
2023-06-11 11:23:38 -04:00
//_localMulti = GetNode<Button>(_path + "Btn_LocalMulti");
//_LANMulti = GetNode<Button>(_path + "Btn_LANMulti");
2023-06-10 14:01:30 -04:00
_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");
2023-06-10 12:04:03 -04:00
_error = GetNode<Label>(_path + "lbl_err");
2023-06-10 15:08:42 -04:00
_ip = GetNode<LineEdit>(_path + "txt_IP");
2023-06-10 14:01:30 -04:00
_lip = GetNode<Label>(_path + "lbl_IP");
2023-06-11 12:02:08 -04:00
_single.Pressed += () => { _sndBtn.Play(); startGame(_gameType.SINGLE);};
//_localMulti.Pressed += () => { _sndBtn.Play(); startGame(_gameType.LOCAL);};
//_LANMulti.Pressed += () => { _sndBtn.Play(); State = _state.LAN;};
2023-06-11 11:59:19 -04:00
_host.Pressed += () => { _sndBtn.Play(); setupLANMultiGame();};
_join.Pressed += () => { _sndBtn.Play(); setupLANMultiGame(false);};
_quit.Pressed += () => { _sndBtn.Play(); State = _state.QUIT;};
_cancel.Pressed += () => { _sndBtn.Play(); State = _state.START;};
_no.Pressed += () => { _sndBtn.Play(); State = _state.START;};
2023-06-11 12:15:56 -04:00
_yes.Pressed += () => GetTree().Quit();
2023-06-10 14:01:30 -04:00
State = _state.START;
2023-06-10 12:04:03 -04:00
}
2023-06-10 14:49:20 -04:00
private void startGame(_gameType type, long id = 0) {
2023-06-10 14:38:07 -04:00
switch (type) {
case _gameType.SINGLE:
2023-06-11 12:15:56 -04:00
var scene = GD.Load<PackedScene>("res://levels/test_level.tscn");
var instance = scene.Instantiate();
GetNode<Node>("/root/").AddChild(instance);
this.QueueFree();
2023-06-10 14:38:07 -04:00
break;
case _gameType.LOCAL:
2023-06-10 14:38:47 -04:00
2023-06-10 14:38:07 -04:00
break;
case _gameType.LAN:
2023-06-10 15:08:42 -04:00
2023-06-10 14:38:07 -04:00
break;
default: return;
}
2023-06-10 14:01:30 -04:00
2023-06-10 12:04:03 -04:00
}
2023-06-10 14:38:07 -04:00
private void setupLANMultiGame(bool isHost = true) {
2023-06-10 14:25:42 -04:00
const string ERR = "Invalid IP address.";
if (isHost) {
2023-06-10 14:38:07 -04:00
ENetMultiplayerPeer peer = new ENetMultiplayerPeer();
2023-06-10 15:02:04 -04:00
peer.CreateServer(6666);
2023-06-10 14:25:42 -04:00
Multiplayer.MultiplayerPeer = peer;
2023-06-10 14:49:20 -04:00
peer.PeerConnected += (long id) => startGame(_gameType.LAN, id);
2023-06-10 14:38:07 -04:00
State = _state.HOST;
2023-06-10 14:25:42 -04:00
}
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;
}
}
2023-06-10 14:38:07 -04:00
ENetMultiplayerPeer peer = new ENetMultiplayerPeer();
2023-06-10 15:02:04 -04:00
peer.CreateClient(ip, 6666);
2023-06-10 14:25:42 -04:00
Multiplayer.MultiplayerPeer = peer;
2023-06-10 14:49:20 -04:00
State = _state.JOIN;
2023-06-10 14:25:42 -04:00
}
2023-06-10 12:04:03 -04:00
}
}