//Sets up Level selection for multiplayer public void SetMp(Lobby.MultiplayerRole role) { this.mpRole = role; if (role == Lobby.MultiplayerRole.P2) { //Set buttons to P2 style foreach (LevelData l in this.levels) { l.button.TextureHover = this.p2ButtonHover; l.button.TexturePressed = this.p2ButtonPressed; l.button.TextureDisabled = this.p2ButtonPressed; } } else if (role != Lobby.MultiplayerRole.P1) //All none players { this.nodeButtonPlay.Visible = false; } }
//Called remoted by a player when they have picked a map public void MpSelected(Lobby.MultiplayerRole role, int index) { if (role == Lobby.MultiplayerRole.P1) { this.mpP1Selection = index; } else if (role == Lobby.MultiplayerRole.P2) { this.mpP2Selection = index; } //else {wtf?} //Both players have chosen, pass choices through to clients, if host if (this.mpP1Selection != -1 && this.mpP2Selection != -1 && Lobby.IsHost) { //Randomly generate a winner Lobby.MultiplayerRole win = Command.Random(0, 1) == 0 ? Lobby.MultiplayerRole.P1 : Lobby.MultiplayerRole.P2; Rpc(nameof(MpChosen), new object[] { win, this.mpP1Selection, this.mpP2Selection }); } }
//Called by the host once both p1 and p2 have chosen maps, plays the correct animation public void MpChosen(Lobby.MultiplayerRole winner, int p1, int p2) { //Set mp selection correctly, if they chose the same it doesn't matter who wins this.mpSelection = winner == Lobby.MultiplayerRole.P1 ? p1 : p2; //Hide the confirm buttons this.nodeButtonPlay.Visible = false; //Make all the buttons f**k off foreach (LevelData l in this.levels) { l.button.TextureDisabled = null; l.button.Disabled = true; } //Same level chosen if (p1 == p2) { this.nodeButtonAnim.GetAnimation("same_chosen").TrackSetPath(0, this.levels[p1].button.GetPath() + ":texture_disabled"); this.nodeButtonAnim.Play("same_chosen"); } else { //Setup buttons this.levels[p1].button.TextureDisabled = this.p1ButtonPressed; this.levels[p2].button.TextureDisabled = this.p2ButtonPressed; String anim = this.mpSelection == p1 ? "diff_chosen_p1" : "diff_chosen_p2"; //Set tracks to point to the correct position this.nodeButtonAnim.GetAnimation(anim).TrackSetPath(0, this.levels[p1].button.GetPath() + ":visible"); this.nodeButtonAnim.GetAnimation(anim).TrackSetPath(1, this.levels[p2].button.GetPath() + ":visible"); this.nodeButtonAnim.Play(anim); } }
//Sets a player selection to be confirmed public void Confirm(Lobby.MultiplayerRole p, int selection) { if (p == Lobby.MultiplayerRole.P1) { this.p1.nodeConfirmed.Visible = true; this.p1.selection = selection; this.p1.mpConfirmed = true; } else if (p == Lobby.MultiplayerRole.P2) { this.p2.nodeConfirmed.Visible = true; this.p2.selection = selection; this.p2.mpConfirmed = true; } else { } //Cry I guess? //Both are selected, start the game if (this.p1.mpConfirmed && this.p2.mpConfirmed) { this.callback(this, this.chars[this.p1.selection].character, this.chars[this.p2.selection].character); } }
public override void _Ready() { this.mpRole = Lobby.MultiplayerRole.OFFLINE; this.level = null; this.mpP1Selection = -1; this.mpP2Selection = -1; this.mpSelection = -1; //Gets nodes and sets up connections this.nodeButtonPanel = this.GetNode <Control>("pa_buttons"); this.nodeButtonPlay = this.GetNode <TextureButton>("bt_play"); //Animations are only used in mulitplayer this.nodeButtonAnim = this.GetNode <AnimationPlayer>("an_buttons"); this.nodeButtonAnim.Connect("animation_finished", this, nameof(_OnAnimFinished)); this.nodeLevelSprite = this.GetNode <Sprite>("sp_level"); this.nodeLevelViewport = this.GetNode <Viewport>("vp_level"); this.nodeLevelCamera = this.GetNode <Camera2D>("vp_level/camera"); this.nodeLevelSlider = this.GetNode <HSlider>("sl_level"); this.nodeLevelText = this.GetNode <RichTextLabel>("tx_level"); this.nodeButtonPlay.Connect("pressed", this, nameof(_OnPlayPressed)); this.nodeLevelSlider.Connect("value_changed", this, nameof(_OnSliderChanged)); //Checks that number of buttons and levels are the same Godot.Collections.Array buttonPanelChildren = this.nodeButtonPanel.GetChildren(); if (this.levelData.Length != buttonPanelChildren.Count) { GD.Print("ERROR: LEVEL SELECTION: Unequal number of buttons and maps, aborting"); return; } //Init levels array this.levels = new LevelData[this.levelData.Length]; //Temp looping values int i = 0; LevelData level; //Iterate through level data provided foreach (LevelSelectionLevelData data in this.levelData) { //Create new resource for level level = new LevelData(); level.resource = data; //Instance level level.level = level.resource.packed.Instance() as Level; if (level.level == null) { GD.Print("Error: Processing level from pack"); continue; } //Remove hud and camera track level.level.RemoveChild(level.level.GetNode("hud")); level.level.RemoveChild(level.level.GetNode("camera_track")); //Gets all children of the button panel level.button = buttonPanelChildren[i] as TextureButton; //Call _OnButtonPress with this buttons index on press level.button.Connect("pressed", this, nameof(this._OnButtonPressed), new Godot.Collections.Array(new int[] { i })); this.levels[i] = level; i++; //Something has gone very wrong if there's more than 6 buttons if (i == 6) { GD.Print("ERROR: LEVEL SELECTION: More than 6 maps"); break; } } //Sends only to server RpcConfig(nameof(this.MpSelected), MultiplayerAPI.RPCMode.Remotesync); //Only server can call this RpcConfig(nameof(this.MpChosen), MultiplayerAPI.RPCMode.Remotesync); this.GrabFocus(); }