Skip to content

Commit

Permalink
Added additional checks for animations.
Browse files Browse the repository at this point in the history
Improved PlayerJoinView
Added TaskHelper
  • Loading branch information
vchelaru committed Mar 16, 2024
1 parent 5e322a2 commit 92a13e8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Utilities\SpriteSelectionOptions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\StringBuilderExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\StringFunctions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities\TaskHelper.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="$(MSBuildThisFileDirectory)Gui\Behaviors\" />
Expand Down
26 changes: 26 additions & 0 deletions Engines/FlatRedBallXNA/FlatRedBall/Utilities/TaskHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace FlatRedBall.Utilities
{
public static class TaskHelper
{
public static async Task InSequence(params Func<Task>[] tasks)
{
foreach(var task in tasks)
{
await task();
}
}

public static async Task InSequence(params Task[] tasks)
{
foreach(var task in tasks)
{
await task;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,19 @@ public ReadOnlyCollection<PlayerJoinViewItem> PlayerJoinViewItems

public bool IsSubscribedToGamepadEvents { get; private set; }

public JoinStyle JoinStyle { get; set; } = JoinStyle.AnyConnectedInput;
JoinStyle joinStyle = JoinStyle.AnyConnectedInput;
public JoinStyle JoinStyle
{
get => joinStyle;
set
{
joinStyle = value;
for (int i = 0; i < PlayerJoinViewItems.Count; i++)
{
UpdateConnectedJoinedStateForIndex(i, true);
}
}
}

#endregion

Expand Down Expand Up @@ -197,35 +209,64 @@ private void UpdateConnectedJoinedStateForIndex(int index, bool force)
{
var item = PlayerJoinViewItemsInternal[index];

var isConnected = index <= InputManager.Xbox360GamePads.Length &&
InputManager.Xbox360GamePads[index].IsConnected;
var isConnected = false;

if(isConnected)
if(JoinStyle == JoinStyle.ExplicitlyAssignedInput)
{
var gamepad = InputManager.Xbox360GamePads[index];
item.InputDevice = gamepad;
isConnected = item.InputDevice != null;

if(item.InputDevice is Xbox360GamePad gamepad)
{
isConnected = gamepad.IsConnected;
#if !UWP && !XNA4_OLD && !FNA
item.ControllerDisplayName = gamepad.Capabilities.DisplayName;

item.ControllerDisplayName = gamepad.Capabilities.DisplayName;
#endif
item.ConnectedJoinedState = ConnectedJoinedState.Connected;
item.GamepadLayout = gamepad.GamepadLayout;
}
else if(item.IsUsingKeyboardAsBackup)
{
item.ConnectedJoinedState = ConnectedJoinedState.Connected;
item.InputDevice = InputManager.Keyboard;
item.ControllerDisplayName = KeyboardName; // should it be something else? Localized? Need to think about this...
item.GamepadLayout = gamepad.GamepadLayout;

}
else if(item.InputDevice is FlatRedBall.Input.Keyboard)
{
isConnected = true;
item.ControllerDisplayName = KeyboardName;
item.GamepadLayout = GamepadLayout.Keyboard;
}

item.GamepadLayout = GamepadLayout.Keyboard;
}
else
{
item.ConnectedJoinedState = ConnectedJoinedState.NotConnected;
item.InputDevice = null;
item.GamepadLayout = GamepadLayout.Unknown;
isConnected = index <= InputManager.Xbox360GamePads.Length &&
InputManager.Xbox360GamePads[index].IsConnected;

if(isConnected)
{
var gamepad = InputManager.Xbox360GamePads[index];
item.InputDevice = gamepad;
#if !UWP && !XNA4_OLD && !FNA
item.ControllerDisplayName = gamepad.Capabilities.DisplayName;
#endif
item.ConnectedJoinedState = ConnectedJoinedState.Connected;
item.GamepadLayout = gamepad.GamepadLayout;
}
else if(item.IsUsingKeyboardAsBackup)
{
item.ConnectedJoinedState = ConnectedJoinedState.Connected;
item.InputDevice = InputManager.Keyboard;
item.ControllerDisplayName = KeyboardName; // should it be something else? Localized? Need to think about this...

item.GamepadLayout = GamepadLayout.Keyboard;
}
else
{
item.ConnectedJoinedState = ConnectedJoinedState.NotConnected;
item.InputDevice = null;
item.GamepadLayout = GamepadLayout.Unknown;
}
}




if (force)
{
item.ForceUpdateState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,17 @@ protected override void UpdateState()
{
if(InputDevice != null)
{
ControllerDisplayName = PlayerJoinView.KeyboardName;
if(InputDevice is Keyboard)
{
ControllerDisplayName = PlayerJoinView.KeyboardName;
}
else if(InputDevice is Xbox360GamePad gamepad)
{
#if !UWP && !XNA4_OLD && !FNA

ControllerDisplayName = gamepad.Capabilities.DisplayName;
#endif
}
}
else if(this.ConnectedJoinedState == ConnectedJoinedState.NotConnected && IsUsingKeyboardAsBackup)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ private static void GeneratePlayAnimationsAsync(ICodeBlock classBodyBlock)
foreachBlock.Line("sprite.TimeIntoAnimation = 0;");
foreachBlock.Line("sprite.CurrentFrameIndex = 0;");
foreachBlock.Line("sprite.UpdateToCurrentAnimationFrame();");

foreachBlock.If("sprite.CurrentChain == null")
.Line("throw new System.InvalidOperationException($\"Could not find the animation {animation}\");");

foreachBlock.Line("// subtract second difference to prevent it from looping once if it happens to fall mid-frame");
foreachBlock.Line("// Due to frame order, we need to delay one frame less, and multiply by 1.1 to fix possible accuracy issues");
foreachBlock.Line("await FlatRedBall.TimeManager.DelaySeconds(sprite.CurrentChain.TotalLength - FlatRedBall.TimeManager.SecondDifference * 1.1f);");
Expand Down

0 comments on commit 92a13e8

Please sign in to comment.