Skip to content

Commit

Permalink
Added support for min players
Browse files Browse the repository at this point in the history
Added ConfirmJoinedInput
  • Loading branch information
vchelaru committed Feb 28, 2024
1 parent a423adf commit 71facf5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ public int MaxPlayers
}
}

public int MinPlayers { get; set; } = 1;

List<InputDeviceSelectionItem> InputDeviceSelectionItemsInternal = new List<InputDeviceSelectionItem>();

public event EventHandler ConfirmedJoinedInput;

#endregion

#region Initialize
Expand Down Expand Up @@ -195,7 +199,10 @@ bool DidUnjoin(IInputDevice inputDevice) =>

foreach(var inputDevice in AllConnectedInputDevices)
{
if(inputDevice.DefaultJoinInput.WasJustPressed)
// We use the primary action (such as A or Space) to join,
// then the Join input (such as Enter or Start) as the "hard"
// proceed input.
if(inputDevice.DefaultPrimaryActionInput.WasJustPressed)
{
HandleJoin(inputDevice);
}
Expand All @@ -205,31 +212,49 @@ bool DidUnjoin(IInputDevice inputDevice) =>
}
}

int joinedInputDeviceCount = 0;
for(int i = 0; i < JoinedInputDevices.Length; i++)
{
if (JoinedInputDevices[i] != null )
{
joinedInputDeviceCount++;
}
}

if(joinedInputDeviceCount >= MinPlayers)
{
for(int i = 0; i < JoinedInputDevices.Length; i++)
{
if (JoinedInputDevices[i] != null && JoinedInputDevices[i].DefaultJoinInput.WasJustPressed)
{
ConfirmedJoinedInput?.Invoke(this, null);
break;
}
}
}

base.Activity();
}

private void HandleJoin(IInputDevice inputDevice)
{
var isInputDeviceAlreadyShownInItem = false;
foreach(var item in InputDeviceSelectionItemsInternal)
int? firstEmpty = null;
for(int i = 0; i < JoinedInputDevices.Length; i++)
{
if(item.InputDevice == inputDevice)
if (JoinedInputDevices[i] == inputDevice)
{
// user just pressed join, no biggie
isInputDeviceAlreadyShownInItem = true;
return;
}
else if(firstEmpty == null && JoinedInputDevices[i] == null)
{
firstEmpty = i;
}
}

if(!isInputDeviceAlreadyShownInItem)
// If we got here, it's not already joined:
if(firstEmpty != null)
{
// find the first empty
var firstEmpty = InputDeviceSelectionItemsInternal.Find(item => item.InputDevice == null);

if(firstEmpty != null)
{
firstEmpty.InputDevice = inputDevice;
}
JoinedInputDevices[firstEmpty.Value] = inputDevice;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,6 @@
<Name>JoinedCategoryState</Name>
<SetsValue>false</SetsValue>
</Variable>
<Variable>
<Type>float</Type>
<Name>MinSizeContainer.Height</Name>
<Value xsi:type="xsd:float">44</Value>
<SetsValue>true</SetsValue>
</Variable>
<Variable>
<Type>float</Type>
<Name>MinSizeContainer.X</Name>
<Value xsi:type="xsd:float">0</Value>
<SetsValue>true</SetsValue>
</Variable>
<Variable>
<Type>float</Type>
<Name>MinSizeContainer.Y</Name>
<Value xsi:type="xsd:float">40</Value>
<SetsValue>true</SetsValue>
</Variable>
<Variable>
<Type>float</Type>
<Name>RemoveDeviceButtonInstance.Height</Name>
Expand Down Expand Up @@ -321,11 +303,6 @@
<BaseType>Text</BaseType>
<DefinedByBase>false</DefinedByBase>
</Instance>
<Instance>
<Name>MinSizeContainer</Name>
<BaseType>Container</BaseType>
<DefinedByBase>false</DefinedByBase>
</Instance>
<Instance>
<Name>RemoveDeviceButtonInstance</Name>
<BaseType>Controls/ButtonClose</BaseType>
Expand Down

0 comments on commit 71facf5

Please sign in to comment.