Skip to content

Commit a9984dc

Browse files
committed
Nothing like some last minute changes
1 parent 3647401 commit a9984dc

File tree

5 files changed

+136
-3
lines changed

5 files changed

+136
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Platformer;
2+
3+
public partial class ControllerManager
4+
{
5+
private ControllerButton currentPressedButton;
6+
7+
public event Action<ControllerButton> ButtonPressed;
8+
9+
public ControllerButton CurrentPressedButton
10+
{
11+
get => this.currentPressedButton;
12+
private set
13+
{
14+
if (this.currentPressedButton != value)
15+
{
16+
this.currentPressedButton = value;
17+
18+
if (value != ControllerButton.None)
19+
{
20+
ButtonPressed?.Invoke(value);
21+
}
22+
}
23+
}
24+
}
25+
26+
public PointF DirectionalChange { get; set; }
27+
}
28+
29+
public enum ControllerButton
30+
{
31+
None,
32+
Start,
33+
Right,
34+
Left,
35+
Up,
36+
Down,
37+
Accept,
38+
NavigateForward,
39+
NavigateBackward
40+
}

games/Platformer/GameObjects/PinkMan.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class PinkMan : GameObject
1414
private CharacterState state;
1515
private float position = 0f;
1616
private float yPosition = 0f;
17+
private readonly ControllerManager controllerManager;
1718
private readonly PlayerStateManager playerStateManager;
1819
private float upwardsMovement;
1920
private readonly IImage jump;
@@ -72,10 +73,14 @@ private CharacterState State
7273
}
7374
}
7475

75-
public PinkMan(PlayerStateManager playerStateManager, SettingsService settingsService)
76+
public PinkMan(
77+
PlayerStateManager playerStateManager,
78+
SettingsService settingsService,
79+
ControllerManager controllerManager)
7680
{
7781
this.playerStateManager = playerStateManager;
7882
this.settingsService = settingsService;
83+
this.controllerManager = controllerManager;
7984

8085
state = CharacterState.Idle;
8186
idleSprite = new Sprite(
@@ -157,7 +162,18 @@ public override void Update(double millisecondsSinceLastUpdate)
157162
collision.Collide();
158163
}
159164

160-
State = this.playerStateManager.State;
165+
var actualState = this.playerStateManager.State;
166+
167+
if (controllerManager.CurrentPressedButton == ControllerButton.Left)
168+
{
169+
actualState = CharacterState.MovingLeft;
170+
}
171+
else if (controllerManager.CurrentPressedButton == ControllerButton.Right)
172+
{
173+
actualState = CharacterState.MovingRight;
174+
}
175+
176+
State = actualState;
161177

162178
switch (State)
163179
{

games/Platformer/MainPage.xaml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@ namespace Platformer;
55

66
public partial class MainPage : ContentPage
77
{
8+
private readonly ControllerManager controllerManager;
89
private readonly IGameSceneManager gameSceneManager;
910
private readonly PlayerStateManager playerStateManager;
1011
private readonly SettingsService settingsService;
1112

1213
public MainPage(
1314
IGameSceneManager gameSceneManager,
1415
PlayerStateManager playerStateManager,
15-
SettingsService settingsService)
16+
SettingsService settingsService,
17+
ControllerManager controllerManager)
1618
{
1719
InitializeComponent();
1820

1921
this.gameSceneManager = gameSceneManager;
2022
this.playerStateManager = playerStateManager;
2123
this.settingsService = settingsService;
24+
this.controllerManager = controllerManager;
25+
26+
#if MACCATALYST
27+
this.controllerManager.Initialise();
28+
#endif
2229

2330
gameSceneManager.StateChanged += OnGameSceneManagerStateChanged;
2431
gameSceneManager.LoadScene<FirstScene>(GameView);

games/Platformer/MauiProgram.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static MauiApp CreateMauiApp()
2929

3030
builder.Services.AddSingleton<PlayerStateManager>();
3131
builder.Services.AddSingleton<SettingsService>();
32+
builder.Services.AddSingleton<ControllerManager>();
3233

3334
return builder.Build();
3435
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Diagnostics;
2+
using Foundation;
3+
using GameController;
4+
using UIKit;
5+
6+
namespace Platformer;
7+
8+
public partial class ControllerManager
9+
{
10+
public ControllerManager()
11+
{
12+
GCController.Notifications.ObserveDidConnect(ConnectToController);
13+
}
14+
15+
private void ConnectToController(object sender, NSNotificationEventArgs e)
16+
{
17+
var controller = GCController.Controllers.FirstOrDefault();
18+
19+
if (controller is null)
20+
{
21+
return;
22+
}
23+
24+
if (controller.PhysicalInputProfile is not null)
25+
{
26+
controller.PhysicalInputProfile.ValueDidChangeHandler += Changed;
27+
}
28+
}
29+
30+
public async Task Initialise()
31+
{
32+
await GCController.StartWirelessControllerDiscoveryAsync();
33+
}
34+
35+
private void Changed(GCPhysicalInputProfile gamepad, GCControllerElement element)
36+
{
37+
Debug.WriteLine($"{element}");
38+
39+
if (element is GCControllerDirectionPad directionPad)
40+
{
41+
if (directionPad.XAxis.Value == 1)
42+
{
43+
CurrentPressedButton = ControllerButton.Right;
44+
}
45+
else if (directionPad.XAxis.Value == -1)
46+
{
47+
CurrentPressedButton = ControllerButton.Left;
48+
}
49+
else if (directionPad.YAxis.Value == 1)
50+
{
51+
CurrentPressedButton = ControllerButton.Up;
52+
}
53+
else if (directionPad.YAxis.Value == -1)
54+
{
55+
CurrentPressedButton = ControllerButton.Down;
56+
}
57+
else
58+
{
59+
CurrentPressedButton = ControllerButton.None;
60+
}
61+
62+
DirectionalChange = new(directionPad.XAxis.Value, -directionPad.YAxis.Value);
63+
}
64+
else
65+
{
66+
CurrentPressedButton = ControllerButton.None;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)